#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
URL="${GNEXUS_BOOK_HEALTH_URL:-http://127.0.0.1:8000/health}"
notify() {
if [ -n "${SYNAPSE_URL:-}" ] && [ -n "${SYNAPSE_API_KEY:-}" ]; then
"$SCRIPT_DIR/notify-synapse.sh" \
"Gnexus Book health check failed" \
"health.failed" \
"$1" \
"high" >/dev/null 2>&1 || true
fi
}
notify_disk() {
if [ -n "${SYNAPSE_URL:-}" ] && [ -n "${SYNAPSE_API_KEY:-}" ]; then
"$SCRIPT_DIR/notify-synapse.sh" \
"Gnexus Book backup disk space" \
"$1" \
"$2" \
"$3" >/dev/null 2>&1 || true
fi
}
STATUS=0
check_disk() {
local path="${GNEXUS_BOOK_DISK_PATH:-}"
local used warn max message
[ -n "$path" ] || return 0
if [ ! -d "$path" ]; then
message="Backup path unavailable: $path on $(hostname)"
echo "$message" >&2
notify "$message"
return 1
fi
used="$(df -P "$path" | awk 'NR==2 {gsub(/%/, "", $5); print $5}')"
warn="${GNEXUS_BOOK_DISK_WARN_PERCENT:-90}"
max="${GNEXUS_BOOK_DISK_MAX_PERCENT:-98}"
if [ -n "$used" ] && [ "$used" -ge "$max" ]; then
message="Backup disk critical: $path ${used}% used on $(hostname)"
echo "$message" >&2
notify_disk "disk.critical" "$message" "high"
return 1
fi
if [ -n "$used" ] && [ "$used" -ge "$warn" ]; then
message="Backup disk warning: $path ${used}% used on $(hostname)"
echo "$message" >&2
notify_disk "disk.warning" "$message" "normal"
echo "disk_warning=${used}%"
return 0
fi
echo "disk_ok=${used}%"
return 0
}
if ! check_disk; then
STATUS=1
fi
if ! RESPONSE="$(curl -fsS --max-time 5 "$URL")"; then
MESSAGE="Health request failed: $URL on $(hostname)"
echo "$MESSAGE" >&2
notify "$MESSAGE"
STATUS=1
elif ! python3 -c '
import json
import sys
ok = {"ok", "healthy", "pass", "passing", "up"}
data = json.load(sys.stdin)
status = str(data.get("status", "")).lower()
if status not in ok:
sys.exit(1)
db = (data.get("checks") or {}).get("db")
if db and str(db.get("status", "")).lower() not in ok:
sys.exit(1)
' <<<"$RESPONSE"; then
MESSAGE="Health response is not ok: $RESPONSE"
echo "$MESSAGE" >&2
notify "$MESSAGE"
STATUS=1
fi
if [ "$STATUS" -eq 0 ]; then
echo "ok"
else
exit 1
fi