Files
felhom.eu/documentation/audits/release-2026-10-30/p1-r1344/healthlog.py
T

51 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""healthlog.py <until-ISO-UTC> — R-1344 spike (Part 2, 2026-10-29): every 30 s, read each test box controller's Docker
health record (`docker inspect -f '{{json .State.Health}}' felhom-controller`, the probe Docker runs every 30 s:
`curl -f localhost:8080/api/health`, timeout 5 s) and append every NEW probe (keyed by its start time) to a CSV:
box, start, end, duration_ms, exit_code, status, failing_streak, container_started. Read-only; nothing on a box changes."""
import csv, json, os, subprocess, sys, time
from datetime import datetime, timezone
UNTIL = datetime.fromisoformat(sys.argv[1].replace("Z", "+00:00"))
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "health.csv")
BOXES = {"tester-1": ["ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=10", "root@192.168.0.154"],
"demo-hp": ["ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=10", "hp"]}
seen = {b: set() for b in BOXES}
new = not os.path.exists(OUT)
f = open(OUT, "a", newline="")
w = csv.writer(f)
if new:
w.writerow(["box", "start", "end", "duration_ms", "exit_code", "status", "failing_streak", "container_started", "read_at"])
def ts(s):
# Docker: 2026-10-29T11:11:37.145382734Z (nanoseconds) → microseconds for fromisoformat
s = s.rstrip("Z")
if "." in s:
a, b = s.split(".", 1)
s = a + "." + (b + "000000")[:6]
return datetime.fromisoformat(s).replace(tzinfo=timezone.utc)
while datetime.now(timezone.utc) < UNTIL:
for box, ssh in BOXES.items():
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
try:
r = subprocess.run(ssh + ["pct exec 9201 -- docker inspect -f '{{json .State.Health}}|{{.State.StartedAt}}' felhom-controller"],
capture_output=True, text=True, timeout=40)
line = [l for l in r.stdout.splitlines() if l.startswith("{")]
if not line:
w.writerow([box, "", "", "", "", "READ-FAILED", "", "", now]); f.flush(); continue
health, started = line[0].rsplit("|", 1)
h = json.loads(health)
for e in h.get("Log") or []:
if e["Start"] in seen[box]:
continue
seen[box].add(e["Start"])
d = (ts(e["End"]) - ts(e["Start"])).total_seconds() * 1000
w.writerow([box, e["Start"][:23], e["End"][:23], round(d), e["ExitCode"], h.get("Status"), h.get("FailingStreak"), started[:19], now])
f.flush()
except Exception as ex: # a read that fails is a row, never a silent gap
w.writerow([box, "", "", "", "", "READ-ERROR " + type(ex).__name__, "", "", now]); f.flush()
time.sleep(30)