From 85c2101f17e6b4d8b8542875df8d2a0e919c3bb5 Mon Sep 17 00:00:00 2001 From: Chris Hager Date: Thu, 13 Jun 2024 17:26:43 +0200 Subject: [PATCH] healthcheck --- scripts/website-healthcheck.sh | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 scripts/website-healthcheck.sh diff --git a/scripts/website-healthcheck.sh b/scripts/website-healthcheck.sh new file mode 100755 index 0000000..97b91ad --- /dev/null +++ b/scripts/website-healthcheck.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# +# Check health of relayscan.io and send notifications if state changes +# +set -o errexit +set -o nounset +set -o pipefail + +url="https://www.relayscan.io/healthz" +# url="localhost:9060/healthz" +check_fn="/tmp/relayscan-error.txt" + +# load environment variables $PUSHOVER_APP_TOKEN and $PUSHOVER_APP_KEY +source "$(dirname "$0")/../.env.prod" + +function send_notification() { + curl -s \ + --form-string "token=$PUSHOVER_APP_TOKEN" \ + --form-string "user=$PUSHOVER_APP_KEY" \ + --form-string "message=$1" \ + https://api.pushover.net/1/messages.json +} + +function error() { + # prevent sending multiple notifications + if [ -f $check_fn ]; then + return + fi + + echo "relayscan.io is unhealthy" + send_notification "relayscan.io is unhealthy" + curl -vvvv $url > $check_fn 2>&1 +} + +function reset() { + # Don't run if no error + if [ ! -f $check_fn ]; then + return + fi + + rm $check_fn + echo "relayscan.io is healthy again" + send_notification "relayscan.io is healthy again" +} + +# allow errors, to catch bad curl exit code +set +e +cmd="curl -s $url" +echo $cmd +$cmd +if [ $? -eq 0 ]; then + reset +else + echo "curl error" + error +fi