Update kuma-push.sh
This commit is contained in:
parent
45546df89a
commit
1048076564
1 changed files with 22 additions and 26 deletions
48
kuma-push.sh
48
kuma-push.sh
|
@ -1,54 +1,50 @@
|
|||
#!/usr/bin/env bash
|
||||
###############################################################################
|
||||
# Push local status checks to Uptime Kuma
|
||||
# Requires: bash ≥4, curl, yq (mikefarah/go-yq), optional docker & systemd
|
||||
# Uptime Kuma push script using JSON inventory + jq
|
||||
###############################################################################
|
||||
set -Eeuo pipefail
|
||||
|
||||
CFG="/opt/kuma-checks.yaml"
|
||||
CFG="/opt/kuma-checks.json"
|
||||
BASE="https://status.jordanwages.com/api/push"
|
||||
CURL_OPTS=(--silent --show-error --max-time 4)
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Small random delay so many VMs don't hammer Kuma at the same second
|
||||
sleep $(( RANDOM % 5 )) # 0-4 s
|
||||
# --------------------------------------------------------------------------- #
|
||||
# 0-4 s start-up jitter so many VMs don’t hit Kuma simultaneously
|
||||
sleep $(( RANDOM % 5 ))
|
||||
|
||||
push() { # id status msg ping
|
||||
# --------------------------------------------------------------------------- #
|
||||
push() { # $1 token $2 status $3 msg $4 ping-ms
|
||||
curl "${CURL_OPTS[@]}" \
|
||||
"${BASE}/${1}?status=${2}&msg=${3// /%20}&ping=${4}" \
|
||||
|| true # don't let a failed push abort the script
|
||||
|| true # don’t abort script if push itself fails
|
||||
}
|
||||
|
||||
check_http() { # url
|
||||
local start code
|
||||
start=$(date +%s%3N)
|
||||
check_http() { # arg: URL
|
||||
local t0=$(date +%s%3N)
|
||||
local code
|
||||
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 ))"
|
||||
echo "$([[ $code =~ ^2|3 ]] && echo up || echo down) $(( $(date +%s%3N) - t0 ))"
|
||||
}
|
||||
|
||||
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"
|
||||
check_mount() { mountpoint -q "$1" 2>/dev/null && echo "up 0" || echo "down 0"; }
|
||||
check_native() { echo "up 0"; }
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# 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")
|
||||
# Loop over checks defined in the JSON inventory
|
||||
jq -c '.[]' "$CFG" | while IFS= read -r item; do
|
||||
name=$(jq -r '.name' <<<"$item")
|
||||
type=$(jq -r '.type' <<<"$item")
|
||||
id=$(jq -r '.push' <<<"$item")
|
||||
tgt=$(jq -r '.target // empty' <<<"$item") # may be absent for "native"
|
||||
|
||||
case $type in
|
||||
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 ;;
|
||||
*) stat=down; ping=0 ;;
|
||||
esac
|
||||
|
||||
push "$id" "$stat" "$name" "$ping"
|
||||
done < <(yq eval '.checks[] | @json' -o=json -I0 "$CFG" | tr '\n' '\0')
|
||||
# --------------------------------------------------------------------------- #
|
||||
done
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue