From bec79c55e1f07506a365f46c0ac7cb8cfe2e1c3a Mon Sep 17 00:00:00 2001 From: wagesj45 Date: Fri, 1 Aug 2025 03:11:43 -0500 Subject: [PATCH] Update kuma-push.sh --- kuma-push.sh | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/kuma-push.sh b/kuma-push.sh index 24dbb61..a224c99 100644 --- a/kuma-push.sh +++ b/kuma-push.sh @@ -1,41 +1,46 @@ #!/usr/bin/env bash ############################################################################### -# Uptime Kuma push script using JSON inventory + jq +# Uptime Kuma push script – Bash + jq + wget (no curl) ############################################################################### set -Eeuo pipefail CFG="/opt/kuma-checks.json" BASE="https://status.jordanwages.com/api/push" -CURL_OPTS=(--silent --show-error --max-time 4) +WGET_OPTS=(--quiet --tries=1 --timeout=4 --output-document=/dev/null) -# 0-4 s start-up jitter so many VMs don’t hit Kuma simultaneously +# 0-4 s jitter so many VMs don’t hit Kuma at the exact same second sleep $(( RANDOM % 5 )) # --------------------------------------------------------------------------- # 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 abort script if push itself fails + local url="${BASE}/${1}?status=${2}&msg=${3// /%20}&ping=${4}" + wget "${WGET_OPTS[@]}" "$url" || true # Kuma will alert if pushes stop } 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) - t0 ))" + local start elapsed + start=$(date +%s%3N) + if wget "${WGET_OPTS[@]}" "$1"; then + elapsed=$(( $(date +%s%3N) - start )) + echo "up $elapsed" + else + elapsed=$(( $(date +%s%3N) - start )) + echo "down $elapsed" + fi } + 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" 2>/dev/null && echo "up 0" || echo "down 0"; } check_native() { echo "up 0"; } # --------------------------------------------------------------------------- # -# Loop over checks defined in the JSON inventory +# Iterate through 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" + name=$(jq -r '.name' <<<"$item") + type=$(jq -r '.type' <<<"$item") + id=$(jq -r '.push' <<<"$item") + tgt=$(jq -r '.target // empty' <<<"$item") # target may be absent case "$type" in http) read -r stat ping < <(check_http "$tgt") ;; @@ -43,7 +48,7 @@ jq -c '.[]' "$CFG" | while IFS= read -r item; do 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"