#!/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 # 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" send_alert() { 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" } # Log-only mode: replaced by prose contract pm2-self-heal.prose.md # Only sends DM on actual failure (status != online), not just high restarts # 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 ALERTS="${ALERTS}⚠️ abiba-telegram was **$TEL_STATUS** → restarted to online\n" else ALERTS="${ALERTS}🚨 abiba-telegram **failed restart** (was $TEL_STATUS, still $TEL_STATUS2)\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" # Only DM when there's something to report if [ -n "$ALERTS" ]; then send_alert "**PM2 Self-Heal — $(date '+%H:%M UTC')**" "$ALERTS" fi