release: neutralized public connector catalog (R1-R5 transform + A3 neutralization)
This commit is contained in:
+140
-38
@@ -1,43 +1,145 @@
|
||||
#!/bin/bash
|
||||
# Watchdog for PMDB SSE Connector (R7-3)
|
||||
# Delegation Watchdog — prüft auf hängengebliebene oder verschwundene Subagenten.
|
||||
# Wird via cronjob alle 5 Minuten aufgerufen.
|
||||
# Leere Ausgabe = still (kein Alarm). Nicht-leere Ausgabe = Alarm an team/team.
|
||||
#
|
||||
# Single source of truth (per profile):
|
||||
# profile <- $1 (argument, NOT hardcoded)
|
||||
# heartbeat path <- /tmp/pmdb-sse-hb-${PROFILE}
|
||||
# service name <- pmdb-sse-connector@${PROFILE} (systemd user template)
|
||||
#
|
||||
# R7-3 Argument-Bindung: PROFILE wird als erstes Argument übergeben
|
||||
# (crontab: .../watchdog.sh ${PROFILE}). Kein Hardcoding — ein Watchdog je
|
||||
# Profil, der Aufrufer bestimmt das Profil.
|
||||
PROFILE="$1"
|
||||
if [ -z "$PROFILE" ]; then
|
||||
echo "Usage: $0 <profile>" >&2
|
||||
exit 1
|
||||
# Das Timeout ist PRO AUFTRAG in delegation_tracker.json hinterlegt — nicht pro Profil.
|
||||
# team schätzt bei jeder Delegation: "Dieser Review braucht 10 Min, jene Implementierung 20."
|
||||
# Fehlt der Tracker-Eintrag (z.B. extern gestarteter Prozess), Fallback: 15 min Default.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
TRACKER="${HOME}/.hermes/delegation_tracker.json"
|
||||
DEFAULT_TIMEOUT=900 # 15 min Fallback wenn kein Tracker-Eintrag existiert
|
||||
now_epoch=$(date +%s)
|
||||
|
||||
# ── Aktive hermes chat --profile Prozesse finden ──────────────────────
|
||||
pids=$(pgrep -f "hermes.*chat.*--profile" 2>/dev/null || true)
|
||||
|
||||
alerts=""
|
||||
|
||||
# ── 1. Hängengebliebene Prozesse (läuft länger als Timeout) ───────────
|
||||
for pid in $pids; do
|
||||
elapsed=$(ps -p "$pid" -o etimes= 2>/dev/null | tr -d ' ' || true)
|
||||
if [ -z "$elapsed" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
cmd=$(ps -p "$pid" -o args= 2>/dev/null || true)
|
||||
profile=$(echo "$cmd" | grep -oP '(?<=--profile )\S+' | head -1 || true)
|
||||
[ -z "$profile" ] && profile="unknown"
|
||||
|
||||
# -- SICHERHEITS-CHECK: Broker-Kinder erkennen --
|
||||
# Prüfe PPID-Kette auf 'connector.py --profile'
|
||||
is_broker_child=$(python3 -c "
|
||||
import subprocess, os
|
||||
def get_ppid_chain(pid):
|
||||
chain = []
|
||||
try:
|
||||
while pid > 1:
|
||||
with open(f'/proc/{pid}/status') as f:
|
||||
for line in f:
|
||||
if line.startswith('PPid:'):
|
||||
pid = int(line.split()[1])
|
||||
chain.append(pid)
|
||||
break
|
||||
# Get cmdline for this parent
|
||||
with open(f'/proc/{pid}/cmdline') as f:
|
||||
if 'connector.py --profile' in f.read().replace('\x00', ' '):
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
print(get_ppid_chain($pid))
|
||||
" 2>/dev/null || true)
|
||||
|
||||
# Timeout aus Tracker-Datei holen (pro Auftrag)
|
||||
threshold=""
|
||||
task_desc=""
|
||||
if [ -f "$TRACKER" ]; then
|
||||
tracker_info=$(python3 -c "
|
||||
import json, sys
|
||||
try:
|
||||
with open('$TRACKER') as f:
|
||||
entries = json.load(f)
|
||||
for e in entries:
|
||||
if e.get('pid') == $pid and e.get('status') == 'running':
|
||||
print(e.get('timeout_sec', 900))
|
||||
print('|||')
|
||||
print(e.get('task', ''))
|
||||
sys.exit(0)
|
||||
except: sys.exit(1)
|
||||
" 2>/dev/null || true)
|
||||
if [ -n "$tracker_info" ]; then
|
||||
threshold=$(echo "$tracker_info" | head -1)
|
||||
task_desc=$(echo "$tracker_info" | tail -1 | sed 's/^|||$//')
|
||||
fi
|
||||
fi
|
||||
|
||||
# -- ENFORCEMENT --
|
||||
if [ "$is_broker_child" = "True" ]; then
|
||||
# Broker-Kinder niemals in Timeout-Prüfung, nur melden wenn kein Tracker
|
||||
if [ -z "$threshold" ]; then
|
||||
alerts="${alerts}ℹ️ Broker-Kind (PID ${pid}, ${profile}) läuft ohne Tracker-Eintrag (Sicherheits-Ignore)\n"
|
||||
fi
|
||||
continue
|
||||
fi
|
||||
|
||||
if [ -z "$threshold" ]; then
|
||||
# Treffer OHNE Tracker-Eintrag nur melden, NIEMALS in Timeout-Prüfung
|
||||
alerts="${alerts}ℹ️ Prozess (PID ${pid}, ${profile}) läuft ohne Tracker-Eintrag (Sicherheits-Ignore)\n"
|
||||
continue
|
||||
fi
|
||||
|
||||
if [ "$elapsed" -gt "$threshold" ]; then
|
||||
minutes=$((elapsed / 60))
|
||||
threshold_min=$((threshold / 60))
|
||||
alerts="${alerts}⚠️ ${profile} läuft seit ${minutes} min — Timeout war ${threshold_min} min (PID ${pid})\n Aufgabe: ${task_desc}\n → Nachhaken oder neu delegieren?\n"
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
# ── 2. Verschwundene Prozesse (Absturz / Gateway-Neustart) ────────────
|
||||
if [ -f "$TRACKER" ]; then
|
||||
vanished=$(python3 -c "
|
||||
import json, os, sys, time
|
||||
try:
|
||||
with open('$TRACKER') as f:
|
||||
entries = json.load(f)
|
||||
now = int(time.time())
|
||||
for e in entries:
|
||||
if e.get('status') != 'running':
|
||||
continue
|
||||
pid = e.get('pid', 0)
|
||||
if pid <= 0:
|
||||
continue
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
except ProcessLookupError:
|
||||
profile = e.get('agent', 'unknown')
|
||||
task = e.get('task', 'unbekannt')
|
||||
timeout_sec = e.get('timeout_sec', 600)
|
||||
age_min = (now - e.get('start_epoch', now)) // 60
|
||||
expected_min = timeout_sec // 60
|
||||
print(f'🚨 {profile} ist verschwunden ohne Abschluss-Benachrichtigung')
|
||||
print(f' Aufgabe: {task}')
|
||||
print(f' War seit {age_min} min aktiv (erwartet: {expected_min} min, PID {pid})')
|
||||
print(f' Wahrscheinlich: Absturz oder Gateway-Neustart. Neu delegieren?')
|
||||
print()
|
||||
except PermissionError:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
" 2>/dev/null || true)
|
||||
if [ -n "$vanished" ]; then
|
||||
alerts="${alerts}${vanished}"
|
||||
fi
|
||||
fi
|
||||
|
||||
HB_FILE="/tmp/pmdb-sse-hb-${PROFILE}"
|
||||
|
||||
if [ ! -f "$HB_FILE" ]; then
|
||||
echo "Heartbeat file missing for ${PROFILE}, restarting service..."
|
||||
systemctl --user restart "pmdb-sse-connector@${PROFILE}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
LAST_HB=$(cat "$HB_FILE")
|
||||
NOW=$(date +%s)
|
||||
|
||||
# R7-3 Vor-Punkt-Arithmetik: %% entfernt das LÄNGSTE Suffix ab dem ERSTEN
|
||||
# Punkt — ein Wert wie "1791785554.546477811791785565.70794762" (mehrere
|
||||
# aneinandergehängte Timestamps, P-NEW-F702-HBSHARE) wird korrekt auf die
|
||||
# Ganzzahl VOR dem ersten Punkt gekürzt (1791785554). Ein einzelnes % würde
|
||||
# nur bis zum LETZTEN Punkt kürzen und einen Float-Rest übrig lassen, an dem
|
||||
# die bash-Arithmetik mit "Ungültiger arithmetischer Operator" crasht.
|
||||
DIFF=$((NOW - ${LAST_HB%%.*}))
|
||||
|
||||
if [ "$DIFF" -gt 300 ]; then
|
||||
echo "Heartbeat outdated (${DIFF}s) for ${PROFILE}, restarting service..."
|
||||
# R7-3 Profil-Suffix-Restart: das Template-Instance-Unit (nicht das
|
||||
# generische pmdb-sse-connector) neustarten, damit nach dem Restart das
|
||||
# richtige Profil-HB-File gelesen wird.
|
||||
systemctl --user restart "pmdb-sse-connector@${PROFILE}"
|
||||
# ── Alarm ausgeben (oder still bleiben) ────────────────────────────────
|
||||
if [ -n "$alerts" ]; then
|
||||
echo -e "🔍 Delegation-Watchdog — Aufträge benötigen Aufmerksamkeit:\n"
|
||||
echo -e "$alerts"
|
||||
echo "team: Bitte prüfen — Prozess am Leben? Abgebrochen? Nachhaken oder neu delegieren."
|
||||
fi
|
||||
# Keine Alerts → leere Ausgabe → still (keine Nachricht)
|
||||
Reference in New Issue
Block a user