#!/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
}
if ! RESPONSE="$(curl -fsS --max-time 5 "$URL")"; then
MESSAGE="Health request failed: $URL on $(hostname)"
echo "$MESSAGE" >&2
notify "$MESSAGE"
exit 1
fi
if ! 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"
exit 1
fi
echo "ok"