From d6480ef1b9c9120abdd3aa8ff795e78bf54152fd Mon Sep 17 00:00:00 2001 From: patrickbeane Date: Thu, 29 Oct 2026 20:30:51 -0400 Subject: [PATCH] Add status_alerts table and status_alert_after_hours setting Co-Authored-By: Claude Sonnet 5.5 --- server/kernelwatch/config.py | 4 ++++ server/kernelwatch/db.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/server/kernelwatch/config.py b/server/kernelwatch/config.py index 9089473..840faab 100644 --- a/server/kernelwatch/config.py +++ b/server/kernelwatch/config.py @@ -41,6 +41,8 @@ class Config: ntfy_url: str = "" ntfy_token: str = "" ntfy_timeout_seconds: float = 10.0 + # Notify when a host has needed a reboot or updates for this long; 0 disables. + status_alert_after_hours: float = 24.0 def load(path: str | None = None) -> Config: @@ -72,6 +74,8 @@ def load(path: str | None = None) -> Config: raise SystemExit(f"{path}: stale_after_hours must be positive and finite") if not math.isfinite(cfg.ntfy_timeout_seconds) or cfg.ntfy_timeout_seconds <= 0: raise SystemExit(f"{path}: ntfy_timeout_seconds must be positive and finite") + if not math.isfinite(cfg.status_alert_after_hours) or cfg.status_alert_after_hours < 0: + raise SystemExit(f"{path}: status_alert_after_hours must be zero or positive and finite") if any(ord(c) < 32 or ord(c) == 127 for c in cfg.ntfy_url + cfg.ntfy_token): raise SystemExit(f"{path}: ntfy settings must not contain control characters") if cfg.ntfy_url: diff --git a/server/kernelwatch/db.py b/server/kernelwatch/db.py index cad7915..5251d6d 100644 --- a/server/kernelwatch/db.py +++ b/server/kernelwatch/db.py @@ -34,6 +34,12 @@ CREATE TABLE IF NOT EXISTS stale_alerts ( host TEXT PRIMARY KEY REFERENCES hosts(name) ON DELETE CASCADE, last_seen INTEGER ); +CREATE TABLE IF NOT EXISTS status_alerts ( + host TEXT PRIMARY KEY REFERENCES hosts(name) ON DELETE CASCADE, + level TEXT NOT NULL, + since INTEGER NOT NULL, + notified_level TEXT +); """ @@ -164,6 +170,30 @@ def clear_stale_alert(conn: sqlite3.Connection, host: str) -> None: conn.execute("DELETE FROM stale_alerts WHERE host = ?", (host,)) +# --- pending reboot/update notifications ------------------------------------ + +def status_alerts(conn: sqlite3.Connection) -> dict[str, tuple[str, int, str | None]]: + """host -> (level, when it entered that level, level last notified for).""" + return {r["host"]: (r["level"], r["since"], r["notified_level"]) + for r in conn.execute("SELECT * FROM status_alerts")} + + +def save_status_alert(conn: sqlite3.Connection, host: str, level: str, since: int, + notified_level: str | None) -> None: + # The host may have been removed while a notification was in flight. + conn.execute( + "INSERT INTO status_alerts (host, level, since, notified_level) " + "SELECT name, ?, ?, ? FROM hosts WHERE name = ? " + "ON CONFLICT(host) DO UPDATE SET level = excluded.level, since = excluded.since, " + "notified_level = excluded.notified_level", + (level, since, notified_level, host), + ) + + +def clear_status_alert(conn: sqlite3.Connection, host: str) -> None: + conn.execute("DELETE FROM status_alerts WHERE host = ?", (host,)) + + # --- users & sessions ------------------------------------------------------- def set_user(conn: sqlite3.Connection, name: str, pw_hash: str) -> None: