Notify when a host has needed a reboot or updates for too long

The alert timer now evaluates each host with the dashboard's status logic
and sends one ntfy message once a host has been in reboot or update for
status_alert_after_hours, measured from when it entered that status.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Sonnet 5.5 committed 2026-10-29 20:32:24 -04:00
1 parent d6480ef1b9
commit ce231bd471
4 files changed
+196 -4

No files matched your search

+51 -3
View File
@@ -12,6 +12,7 @@ from urllib.request import HTTPRedirectHandler, Request, build_opener
from . import db
from .config import Config
from .status import LEVELS, evaluate
class DeliveryError(Exception):
@@ -24,13 +25,14 @@ class _NoRedirect(HTTPRedirectHandler):
return None
def publish(cfg: Config, title: str, message: str, *, stale: bool = False) -> None:
def publish(cfg: Config, title: str, message: str, *, stale: bool = False,
tag: str | None = None) -> None:
if not cfg.ntfy_url:
raise DeliveryError("ntfy_url is not configured")
headers = {
"Content-Type": "text/plain; charset=utf-8",
"Title": title,
"Tags": "warning" if stale else "white_check_mark",
"Tags": tag or ("warning" if stale else "white_check_mark"),
"Priority": "high" if stale else "default",
}
if cfg.ntfy_token:
@@ -71,7 +73,10 @@ def check(cfg: Config) -> int:
return 0
conn = db.connect(cfg.db_path)
try:
return _check_hosts(cfg, conn)
failures = _check_hosts(cfg, conn)
if cfg.status_alert_after_hours > 0:
failures += _check_status(cfg, conn)
return failures
finally:
conn.close()
@@ -110,3 +115,46 @@ def _check_hosts(cfg: Config, conn: sqlite3.Connection) -> int:
db.clear_stale_alert(conn, name)
print(f"kernelwatch: {'stale' if stale else 'recovery'} notification sent for {name}")
return failures
def _check_status(cfg: Config, conn: sqlite3.Connection) -> int:
"""Notify once when a host has needed a reboot or updates for the grace period.
`since` is when the host entered its current level, so it measures how long the
need has gone unmet rather than how recently the host reported. Returning to ok
forgets the host; reaching a worse level (update -> reboot) notifies again after
another grace period. Stale hosts are left alone: the stale alert covers them
and their data can't be trusted."""
saved = db.status_alerts(conn)
grace = cfg.status_alert_after_hours * 3600
failures = 0
for h in db.latest_reports(conn):
name, now = h["host"], time.time()
status = evaluate(h["received_at"], h["report"], now, cfg)
if status.level == "stale":
continue
if status.level == "ok":
if name in saved:
db.clear_status_alert(conn, name)
continue
level, since, notified = saved.get(name, (status.level, int(now), None))
if level != status.level:
level, since = status.level, int(now)
worse = notified is None or LEVELS.index(level) < LEVELS.index(notified)
if now - since < grace or not worse:
db.save_status_alert(conn, name, level, since, notified)
continue
needs = "a reboot" if level == "reboot" else "updates"
title = f"kernelwatch: {name} needs {needs}"
message = (f"{name} has needed {needs} since {_utc(since)}.\n"
+ "\n".join(f"- {r}" for r in status.reasons))
try:
publish(cfg, title, message, tag="arrows_counterclockwise" if level == "reboot" else "package")
except DeliveryError as exc:
print(f"kernelwatch: notification failed for {name}: {exc}", file=sys.stderr)
failures += 1
db.save_status_alert(conn, name, level, since, notified)
continue
db.save_status_alert(conn, name, level, since, level)
print(f"kernelwatch: {level} notification sent for {name}")
return failures