Alert when Livepatch is broken or has stopped reporting
The alert check now also notifies, once per problem, when a host's Livepatch has been unhealthy for livepatch_alert_after_hours (default 6), or when a host that used to report Livepatch no longer does (a dead daemon leaves the agent with nothing to send). A livepatch_alerts table records which hosts have reported Livepatch, when the current problem began and whether it was notified, so hosts without Livepatch never alert and restarts do not repeat messages. Stale hosts are skipped. livepatch_problem() is now the single test for unhealthy, shared with the cover rule for pending reboots, and names the check state when that is the problem. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
203c729e14
commit
650806bfce
7 files changed
+346
-6
No files matched your search
@@ -13,7 +13,7 @@ from urllib.request import HTTPRedirectHandler, Request, build_opener
|
||||
|
||||
from . import db
|
||||
from .config import Config
|
||||
from .status import LEVELS, evaluate
|
||||
from .status import LEVELS, evaluate, livepatch_problem
|
||||
|
||||
|
||||
class DeliveryError(Exception):
|
||||
@@ -99,6 +99,8 @@ def check(cfg: Config) -> int:
|
||||
failures = _check_hosts(cfg, conn)
|
||||
if cfg.status_alert_after_hours > 0:
|
||||
failures += _check_status(cfg, conn)
|
||||
if cfg.livepatch_alert_after_hours > 0:
|
||||
failures += _check_livepatch(cfg, conn)
|
||||
return failures
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -183,6 +185,57 @@ def _check_status(cfg: Config, conn: sqlite3.Connection) -> int:
|
||||
return failures
|
||||
|
||||
|
||||
def _check_livepatch(cfg: Config, conn: sqlite3.Connection) -> int:
|
||||
"""Notify once per episode when Livepatch has been broken for the grace period.
|
||||
|
||||
Two cases: Livepatch reports an unhealthy state, or a host that used to report
|
||||
Livepatch no longer does (its daemon died, or `canonical-livepatch status` fails,
|
||||
so the agent has nothing to send). A host is only "used to" once it has reported
|
||||
Livepatch, so hosts without it never alert. Getting healthy again forgets the
|
||||
problem, so a later one alerts again. Stale hosts are skipped: their data can't
|
||||
be trusted and the stale alert covers them."""
|
||||
saved = db.livepatch_alerts(conn)
|
||||
grace = cfg.livepatch_alert_after_hours * 3600
|
||||
failures = 0
|
||||
for h in db.latest_reports(conn):
|
||||
name, report, now = h["host"], h["report"], time.time()
|
||||
if evaluate(h["received_at"], report, now, cfg).level == "stale":
|
||||
continue
|
||||
state = report.get("livepatch_state")
|
||||
if state is None and name not in saved:
|
||||
continue
|
||||
problem = (livepatch_problem(report) if state is not None
|
||||
else "livepatch status is no longer being reported")
|
||||
since, notified = saved.get(name, (None, False))
|
||||
if problem is None:
|
||||
current = (None, False)
|
||||
else:
|
||||
since = int(now) if since is None else since
|
||||
current = (since, notified)
|
||||
if not notified and now - since >= grace:
|
||||
title = f"kernelwatch: {name} Livepatch problem"
|
||||
if state is None:
|
||||
detail = ("It reported Livepatch before and no longer does, so the Livepatch "
|
||||
"daemon may have stopped or been disabled.")
|
||||
else:
|
||||
detail = (f"State: {state}, check: {report.get('livepatch_check_state') or '?'}, "
|
||||
f"supported: {report.get('livepatch_supported') or '?'}.")
|
||||
message = (f"{name}: {problem} since {_utc(since)}.\n{detail}\n"
|
||||
"The running kernel may be missing live patches. On the host, check "
|
||||
"`canonical-livepatch status` and `systemctl status kernelwatch-livepatch`.")
|
||||
try:
|
||||
publish(cfg, title, message, stale=True, tag="shield")
|
||||
except DeliveryError as exc:
|
||||
print(f"kernelwatch: notification failed for {name}: {exc}", file=sys.stderr)
|
||||
failures += 1
|
||||
else:
|
||||
current = (since, True)
|
||||
print(f"kernelwatch: livepatch notification sent for {name}")
|
||||
if saved.get(name) != current:
|
||||
db.save_livepatch_alert(conn, name, *current)
|
||||
return failures
|
||||
|
||||
|
||||
def notify_backup_failure(cfg: Config) -> int:
|
||||
"""Tell the ntfy topic the nightly backup failed. Returns 1 if that couldn't be sent.
|
||||
|
||||
|
||||
Reference in new issue
Block a user