Add ntfy alerts for hosts that need a reboot or updates for too long #10
No files matched your search
@@ -41,6 +41,8 @@ class Config:
|
|||||||
ntfy_url: str = ""
|
ntfy_url: str = ""
|
||||||
ntfy_token: str = ""
|
ntfy_token: str = ""
|
||||||
ntfy_timeout_seconds: float = 10.0
|
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:
|
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")
|
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:
|
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")
|
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):
|
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")
|
raise SystemExit(f"{path}: ntfy settings must not contain control characters")
|
||||||
if cfg.ntfy_url:
|
if cfg.ntfy_url:
|
||||||
|
|||||||
@@ -34,6 +34,12 @@ CREATE TABLE IF NOT EXISTS stale_alerts (
|
|||||||
host TEXT PRIMARY KEY REFERENCES hosts(name) ON DELETE CASCADE,
|
host TEXT PRIMARY KEY REFERENCES hosts(name) ON DELETE CASCADE,
|
||||||
last_seen INTEGER
|
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,))
|
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 -------------------------------------------------------
|
# --- users & sessions -------------------------------------------------------
|
||||||
|
|
||||||
def set_user(conn: sqlite3.Connection, name: str, pw_hash: str) -> None:
|
def set_user(conn: sqlite3.Connection, name: str, pw_hash: str) -> None:
|
||||||
|
|||||||
Reference in new issue
Block a user