#!/usr/bin/env bash # Health check for the deployed torly relay. # Runs from torly-monitor.timer every five minutes and writes a JSON status. set -uo pipefail DATA=${TORLY_DATA_DIR:-/home/mleku/.local/share/torly} HTTP=${TORLY_HTTP:-http://127.0.0.1:3336} STATUS=$DATA/monitor-status.json FAILURES=$DATA/monitor-failures TORLY_UNIT=${TORLY_UNIT:-torly.service} mkdir -p "$DATA" health=false if curl -fsS --max-time 5 "$HTTP/health" 2>/dev/null | jq -e '.status == "ok"' >/dev/null 2>&1; then health=true fi nip11=false if curl -fsS --max-time 5 -H 'Accept: application/nostr+json' "$HTTP/" 2>/dev/null | jq -e '.name and (.supported_nips | length > 0) and .pubkey' >/dev/null 2>&1; then nip11=true fi torly_active=false systemctl is-active --quiet "$TORLY_UNIT" && torly_active=true healthy=false if [ "$health" = true ] && [ "$nip11" = true ] && [ "$torly_active" = true ]; then healthy=true fi # Restart a genuinely dead service after three consecutive unhealthy runs. if [ "$health" = true ]; then rm -f "$FAILURES" else n=0 [ -f "$FAILURES" ] && n=$(cat "$FAILURES" 2>/dev/null || echo 0) n=$((n + 1)) echo "$n" >"$FAILURES" if [ "$n" -ge 3 ]; then echo "torly-monitor: health failed $n times, restarting $TORLY_UNIT" >&2 systemctl restart "$TORLY_UNIT" 2>/dev/null || true rm -f "$FAILURES" fi fi jq -n \ --arg updated_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ --argjson healthy "$healthy" \ --argjson health "$health" \ --argjson nip11 "$nip11" \ --argjson torly_active "$torly_active" \ '{updated_at: $updated_at, healthy: $healthy, health: $health, nip11: $nip11, torly_active: $torly_active}' >"$STATUS" if [ "$healthy" = true ]; then echo "torly-monitor: ok" exit 0 fi echo "torly-monitor: unhealthy health=$health nip11=$nip11 torly=$torly_active" >&2 exit 1