proxmox-server-setup/kuma-push.sh
2025-08-01 01:41:26 -05:00

54 lines
2.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
###############################################################################
# Push local status checks to Uptime Kuma
# Requires: bash ≥4, curl, yq (mikefarah/go-yq), optional docker & systemd
###############################################################################
set -Eeuo pipefail
CFG="/opt/kuma-checks.yaml"
BASE="https://status.jordanwages.com/api/push"
CURL_OPTS=(--silent --show-error --max-time 4)
# --------------------------------------------------------------------------- #
# Small random delay so many VMs dont hammer Kuma at the same second
sleep $(( RANDOM % 5 )) # 0-4 s
# --------------------------------------------------------------------------- #
push() { # id status msg ping
curl "${CURL_OPTS[@]}" \
"${BASE}/${1}?status=${2}&msg=${3// /%20}&ping=${4}" \
|| true # dont let a failed push abort the script
}
check_http() { # url
local start code
start=$(date +%s%3N)
code=$(curl "${CURL_OPTS[@]}" -o /dev/null -w '%{http_code}' "$1" || echo 000)
echo "$([[ $code =~ ^2|3 ]] && echo up || echo down) $(( $(date +%s%3N)-start ))"
}
check_service() { systemctl is-active --quiet "$1" && echo "up 0" || echo "down 0"; }
check_docker() { docker inspect -f '{{.State.Running}}' "$1" &>/dev/null && echo "up 0" || echo "down 0"; }
check_mount() { mountpoint -q "$1" && echo "up 0" || echo "down 0"; }
check_native() { echo "up 0"; } # simply reports “alive”
# --------------------------------------------------------------------------- #
# Iterate over YAML entries (null-delimited to survive spaces)
while IFS= read -r -d '' line; do
name=$(yq eval '.name' <<<"$line")
type=$(yq eval '.type' <<<"$line")
tgt=$(yq eval '.target' <<<"$line")
id=$(yq eval '.push' <<<"$line")
case $type in
http) read -r stat ping < <(check_http "$tgt") ;;
service) read -r stat ping < <(check_service "$tgt") ;;
docker) read -r stat ping < <(check_docker "$tgt") ;;
mount) read -r stat ping < <(check_mount "$tgt") ;;
native) read -r stat ping < <(check_native) ;;
*) stat=down ping=0 ;;
esac
push "$id" "$stat" "$name" "$ping"
done < <(yq eval '.checks[] | @json' -o=json -I0 "$CFG" | tr '\n' '\0')
# --------------------------------------------------------------------------- #