Files
felhom.eu/documentation/audits/rehearsal-2026-10-20/tools/memwatch.py
T

17 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""memwatch.py — every 30 s, the box's own per-container memory/CPU (/api/metrics/containers/summary) + system memory
(/api/system/info), one line each, until killed. Run under sec (dashboard password). Diagnoses R-1192 from outside."""
import json, os, sys, time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))); import ctl
while True:
t = time.strftime("%H:%M:%SZ", time.gmtime())
try:
c, d = ctl.call("GET", "/api/system/info", timeout=25); s = (d.get("data") or {}).get("system") or {}
c2, d2 = ctl.call("GET", "/api/metrics/containers/summary", timeout=25)
top = sorted(d2.get("data") or [], key=lambda x: -x.get("mem_usage_mb", 0))[:8]
print(t, f"used={s.get('used_mem_mb')}MB avail={s.get('avail_mem_mb')}MB load1={s.get('load_avg_1')} cpu={round(s.get('cpu_percent') or 0)}%",
" | ".join(f"{x['name']}:{round(x['mem_usage_mb'])}/{x['mem_limit_mb']}MB {round(x['cpu_percent'])}%" for x in top), flush=True)
except Exception as e: # noqa: BLE001
print(t, "NO ANSWER", type(e).__name__, flush=True)
time.sleep(30)