torly-monitor.sh raw
1 #!/usr/bin/env bash
2 # Health check for the deployed torly relay.
3 # Runs from torly-monitor.timer every five minutes and writes a JSON status.
4 set -uo pipefail
5
6 DATA=${TORLY_DATA_DIR:-/home/mleku/.local/share/torly}
7 HTTP=${TORLY_HTTP:-http://127.0.0.1:3336}
8 STATUS=$DATA/monitor-status.json
9 FAILURES=$DATA/monitor-failures
10 TORLY_UNIT=${TORLY_UNIT:-torly.service}
11
12 mkdir -p "$DATA"
13
14 health=false
15 if curl -fsS --max-time 5 "$HTTP/health" 2>/dev/null |
16 jq -e '.status == "ok"' >/dev/null 2>&1; then
17 health=true
18 fi
19
20 nip11=false
21 if curl -fsS --max-time 5 -H 'Accept: application/nostr+json' "$HTTP/" 2>/dev/null |
22 jq -e '.name and (.supported_nips | length > 0) and .pubkey' >/dev/null 2>&1; then
23 nip11=true
24 fi
25
26 torly_active=false
27 systemctl is-active --quiet "$TORLY_UNIT" && torly_active=true
28
29 healthy=false
30 if [ "$health" = true ] && [ "$nip11" = true ] && [ "$torly_active" = true ]; then
31 healthy=true
32 fi
33
34 # Restart a genuinely dead service after three consecutive unhealthy runs.
35 if [ "$health" = true ]; then
36 rm -f "$FAILURES"
37 else
38 n=0
39 [ -f "$FAILURES" ] && n=$(cat "$FAILURES" 2>/dev/null || echo 0)
40 n=$((n + 1))
41 echo "$n" >"$FAILURES"
42 if [ "$n" -ge 3 ]; then
43 echo "torly-monitor: health failed $n times, restarting $TORLY_UNIT" >&2
44 systemctl restart "$TORLY_UNIT" 2>/dev/null || true
45 rm -f "$FAILURES"
46 fi
47 fi
48
49 jq -n \
50 --arg updated_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
51 --argjson healthy "$healthy" \
52 --argjson health "$health" \
53 --argjson nip11 "$nip11" \
54 --argjson torly_active "$torly_active" \
55 '{updated_at: $updated_at, healthy: $healthy, health: $health, nip11: $nip11,
56 torly_active: $torly_active}' >"$STATUS"
57
58 if [ "$healthy" = true ]; then
59 echo "torly-monitor: ok"
60 exit 0
61 fi
62 echo "torly-monitor: unhealthy health=$health nip11=$nip11 torly=$torly_active" >&2
63 exit 1
64