Files
prose-contracts/scripts/pm2-self-heal.sh
T
root dd985f0e49 pm2-self-heal: migrate alerts from Zulip to Telegram
- abiba-zulip PM2 process decommissioned (2026-07-04)
- Replace Zulip stream alerts (agent-hub > alerts-pm2) with Telegram DM
- Remove stale abiba-zulip monitoring block (process no longer exists)
- Retain abiba-telegram auto-restart (critical — sole communication bridge)
- Token sourced from extension .env, not hardcoded
2026-07-04 22:07:35 +00:00

60 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# pm2-self-heal — hourly PM2 process check
# Part of the pm2-self-heal prose contract
# Alerts via Telegram (abiba-zulip decommissioned 2026-07-04)
# Field positions (awk -F'│'): $7=pid $8=uptime $9=restarts $10=status
TELEGRAM_BOT_TOKEN="$(grep TELEGRAM_BOT_TOKEN /root/.pi/agent/extensions/telegram/.env 2>/dev/null | cut -d= -f2 || echo '')"
TELEGRAM_CHAT_ID="5822977936"
notify_tg() {
local subject="$1"
local body="$2"
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 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)
STATUS=$(pm2 status --no-color 2>/dev/null)
ALERTS=""
# Check abiba-telegram (safe to auto-restart)
TEL_LINE=$(echo "$STATUS" | grep "abiba-telegram")
TEL_STATUS=$(echo "$TEL_LINE" | awk -F'│' '{print $10}' | xargs)
TEL_RESTARTS=$(echo "$TEL_LINE" | awk -F'│' '{print $9}' | xargs)
if [ "$TEL_STATUS" != "online" ]; then
pm2 restart abiba-telegram > /dev/null 2>&1
sleep 3
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
msg="⚠️ abiba-telegram was **$TEL_STATUS** → restarted to online"
ALERTS="${ALERTS}${msg}\n"
else
msg="🚨 abiba-telegram **failed restart** (was $TEL_STATUS, still $TEL_STATUS2)"
ALERTS="${ALERTS}${msg}\n"
fi
fi
# Log check
{
echo "[$(date '+%Y-%m-%d %H:%M:%S')] tel=$TEL_STATUS alerts=${ALERTS:+yes}"
[ -n "$ALERTS" ] && echo "$ALERTS"
} >> "$LOG"
# Only Telegram DM when there's something to report
if [ -n "$ALERTS" ]; then
notify_tg "PM2 Self-Heal — $(date '+%H:%M UTC')" "$ALERTS"
fi