diff --git a/healthcheck b/healthcheck new file mode 100755 index 0000000..c2a91b8 --- /dev/null +++ b/healthcheck @@ -0,0 +1,95 @@ +#!/usr/bin/env bash + +set -u + +CHECK_SPACE_APIKEY="" +HEALTCHECKSIO_UPTIMEKUMA_UUID="" + +UPTIMEKUMA_URL="https://uptime-kuma.golem.linux.it" +PUSH_PATH="/api/push/" + +UK_PING_TIMEOUT=2 +UK_PING_RETRY=3 +UK_STATUS_UP="up" +UK_STATUS_DOWN="down" + +HC_PING_TIMEOUT=10 +HC_PING_RETRY=5 +HC_FAIL_PATH="/fail" + +ROOTFS_USED_THRESHOLD=75 + +# uptime-kuma ########################################################### + +get-push-url() { + local apikey="$1" + echo "${UPTIMEKUMA_URL}${PUSH_PATH}${apikey}" +} + +ping-url() { + local url="${1}" + echo "Ping to ${url}: " + curl -m "$UK_PING_TIMEOUT" --retry "$UK_PING_RETRY" "${url}" + echo +} + +ping-status-msg() { + local apikey="${1}" + local status="${2}" + local msg="${3}" + local url + url=$(get-push-url "${apikey}")"?status=${status}&msg=${msg}" + ping-url "${url}" +} + +urlencode() { + echo "${1}" | jq -s -R -r @uri +} + +# healthchecks.io ####################################################### + +hc-ping() { + local url="$1" + echo "Ping to ${url}: " + curl -m $HC_PING_TIMEOUT --retry $HC_PING_RETRY "${url}" + echo +} + +# checks ################################################################ + +check-site-hc() { + local hcurl="$1" + local site="$2" + local hcpath="" + echo "Checking ${site}:" + # shellcheck disable=SC2015 + curl -m 3 -fSs -o /dev/null "${site}" + if test $? -ne 0; then + hcpath=$HC_FAIL_PATH + fi + hc-ping "${hcurl}${hcpath}" +} + +check-space() { + local apikey=$CHECK_SPACE_APIKEY + local status=$UK_STATUS_UP + local msg + + local rootfs_used + rootfs_used="$(df --output=pcent / | tail -n1 | sed 's/\%//')" + if test "$rootfs_used" -ge $ROOTFS_USED_THRESHOLD; then + status=$UK_STATUS_DOWN + msg="WARN / : ${rootfs_used}%" + else + msg="OK / : ${rootfs_used}%" + fi + + echo "$msg" + msg=$(urlencode "$msg") + ping-status-msg "$apikey" "$status" "$msg" +} + +######################################################################### + +check-space +check-site-hc "https://hc-ping.com/${HEALTCHECKSIO_UPTIMEKUMA_UUID}" uptime-kuma.golem.linux.it