diff --git a/scripts/pm2-self-heal.sh b/scripts/pm2-self-heal.sh index 9c7da2b..45d454d 100755 --- a/scripts/pm2-self-heal.sh +++ b/scripts/pm2-self-heal.sh @@ -1,29 +1,26 @@ #!/bin/bash # pm2-self-heal — hourly PM2 process check # Part of the pm2-self-heal prose contract -# Only sends Zulip DM when action is taken or escalation needed +# Alerts via Telegram (abiba-zulip decommissioned 2026-07-04) # Field positions (awk -F'│'): $7=pid $8=uptime $9=restarts $10=status -ZULIP_BOT="abiba-bot@chat.sysloggh.net" -ZULIP_KEY="cKTDMZAPW08dk3zl05sStzO7HRztzyn8" -ZULIP_SITE="https://chat.sysloggh.net" -OWNER_ID=9 -LOG="/tmp/pm2-self-heal.log" +TELEGRAM_BOT_TOKEN="$(grep TELEGRAM_BOT_TOKEN /root/.pi/agent/extensions/telegram/.env 2>/dev/null | cut -d= -f2 || echo '')" +TELEGRAM_CHAT_ID="5822977936" -send_alert() { +notify_tg() { local subject="$1" local body="$2" - curl -s -X POST "$ZULIP_SITE/api/v1/messages" \ - -u "$ZULIP_BOT:$ZULIP_KEY" \ - -d "type=stream" \ - -d "to=agent-hub" \ - -d "subject=alerts-pm2" \ - -d "content=$subject\n$body" > /dev/null 2>&1 - echo "[$(date '+%Y-%m-%d %H:%M:%S')] Alert sent to #agent-hub > alerts-pm2" >> "$LOG" + local msg="${subject}\n${body}" + curl -sf -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ + -d "chat_id=${TELEGRAM_CHAT_ID}" \ + -d "text=${msg}" \ + -d "parse_mode=HTML" > /dev/null 2>&1 || true +} + ALERTS="${ALERTS}$msg" } # Log-only mode: replaced by prose contract pm2-self-heal.prose.md -# Only sends DM on actual failure (status != online), not just high restarts +# Only alerts Telegram on actual failure (status != online) # This prevents the process from receiving alerts about itself # Parse PM2 status (--no-color to avoid ANSI escape codes in awk columns) @@ -42,28 +39,21 @@ if [ "$TEL_STATUS" != "online" ]; then TEL_LINE2=$(pm2 status --no-color 2>/dev/null | grep "abiba-telegram") TEL_STATUS2=$(echo "$TEL_LINE2" | awk -F'│' '{print $10}' | xargs) if [ "$TEL_STATUS2" = "online" ]; then - ALERTS="${ALERTS}⚠️ abiba-telegram was **$TEL_STATUS** → restarted to online\n" + msg="⚠️ abiba-telegram was **$TEL_STATUS** → restarted to online" + ALERTS="${ALERTS}${msg}\n" else - ALERTS="${ALERTS}🚨 abiba-telegram **failed restart** (was $TEL_STATUS, still $TEL_STATUS2)\n" + msg="🚨 abiba-telegram **failed restart** (was $TEL_STATUS, still $TEL_STATUS2)" + ALERTS="${ALERTS}${msg}\n" fi fi -# Check abiba-zulip (read-only — never restart) -ZUL_LINE=$(echo "$STATUS" | grep "abiba-zulip") -ZUL_STATUS=$(echo "$ZUL_LINE" | awk -F'│' '{print $10}' | xargs) -ZUL_RESTARTS=$(echo "$ZUL_LINE" | awk -F'│' '{print $9}' | xargs) - -if [ "$ZUL_STATUS" != "online" ]; then - ALERTS="${ALERTS}🚨 **abiba-zulip is $ZUL_STATUS** — needs investigation!\n" - ALERTS="${ALERTS} NOT auto-restarting (runs this contract)\n" -elif [ "$ZUL_RESTARTS" -gt 30 ] 2>/dev/null; then - ALERTS="${ALERTS}⚠️ abiba-zulip has **$ZUL_RESTARTS restarts** — may need attention\n" -fi - # Log check -echo "[$(date '+%Y-%m-%d %H:%M:%S')] tel=$TEL_STATUS zul=$ZUL_STATUS alerts=${ALERTS:+yes}" >> "$LOG" +{ + echo "[$(date '+%Y-%m-%d %H:%M:%S')] tel=$TEL_STATUS alerts=${ALERTS:+yes}" + [ -n "$ALERTS" ] && echo "$ALERTS" +} >> "$LOG" -# Only DM when there's something to report +# Only Telegram DM when there's something to report if [ -n "$ALERTS" ]; then - send_alert "**PM2 Self-Heal — $(date '+%H:%M UTC')**" "$ALERTS" + notify_tg "PM2 Self-Heal — $(date '+%H:%M UTC')" "$ALERTS" fi