my-homelab-configs/scripts/doctor-rpi-os.py

30 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""Turn a marked read-only host OS snapshot into report rows."""
import sys
def emit(status, check, detail):
print("\t".join((status, check, detail.replace("\t", " ").replace("\n", " "))))
try:
lines = sys.stdin.read().splitlines()
start = lines.index("__JEANNIE_HOST_OS_V1__") + 1
data = dict(line.split("=", 1) for line in lines[start:] if "=" in line)
memory_total, memory_available = map(int, data["memory_kib"].split())
disk = int(data["root_percent"])
if memory_total <= 0 or not 0 <= memory_available <= memory_total or not 0 <= disk <= 100:
raise ValueError("invalid memory or disk values")
threshold = int(sys.argv[1])
emit("ok", "Kernel", data["kernel"])
emit("ok", "Uptime and load", data["uptime_load"])
emit("warn" if memory_available * 100 < memory_total * 10 else "ok",
"Memory available", f"{memory_available // 1024} MiB available of {memory_total // 1024} MiB")
emit("warn" if disk >= threshold else "ok", "Root filesystem usage", f"{disk}% used (threshold {threshold}%)")
state = data["systemd"]
emit("ok" if state == "running" else "warn", "Systemd state", state)
except (ValueError, KeyError, IndexError) as exc:
print(f"Cannot interpret host OS snapshot: {exc}", file=sys.stderr)
sys.exit(1)