Report when kernelwatch's own helper units keep failing
kernelwatch-apt-update.service failed hourly on most hosts for weeks without anything showing it. The agent only Wants= its helper units, so the report goes out regardless, with stale lists or (for needrestart) results that quietly turn "unknown". Each helper (apt-update, needrestart) now runs kernelwatch-unit-result as its ExecStopPost=, which systemd runs after failed runs too. It writes "RESULT EXIT_CODE EXIT_STATUS FAILURES" to /run/kernelwatch/<name>.result, FAILURES counting failed runs in a row. The agent (0.10.0) reports results from the last two hours as helper_units. On the server, one or two failures are a note (an admin's apt can hold the lock now and then); three in a row put the host in update, naming the unit and its exit status and pointing at journalctl. The host page gets a "kernelwatch helpers" row. The Livepatch unit isn't tracked: it fails by design where the snap is installed but Livepatch isn't enabled, and Livepatch has its own check. The apt-update unit now has RuntimeDirectory=kernelwatch (preserved, like the others) for its result file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ad310a4948
commit
f88261d2f5
18 files changed
+303
-8
No files matched your search
@@ -4,4 +4,4 @@ __version__ = "0.4.0"
|
||||
|
||||
# Version of agent/kernelwatch-agent shipped alongside this server; older agents
|
||||
# are noted on the dashboard. tests/test_status.py checks the two stay in sync.
|
||||
AGENT_VERSION = "0.9.0"
|
||||
AGENT_VERSION = "0.10.0"
|
||||
@@ -54,6 +54,7 @@ FIELDS: dict[str, str] = {
|
||||
"apt_lists_age_seconds": "int",
|
||||
"security_repo_suite": "str",
|
||||
"security_repo_age_seconds": "int",
|
||||
"helper_units": "strlist",
|
||||
"uptime_seconds": "int",
|
||||
}
|
||||
|
||||
|
||||
@@ -218,6 +218,46 @@ def security_repo_problem(report: dict[str, Any], cfg: Config, today: date) -> s
|
||||
f"run apt update and look for errors about {suite}")
|
||||
|
||||
|
||||
# kernelwatch's helper units (apt-update, needrestart) run before every report, so
|
||||
# about hourly. This many failures in a row put the host in update; fewer are a
|
||||
# note, since one can fail now and then (apt's lock held by an admin's apt run).
|
||||
HELPER_FAILURES_BEFORE_PROBLEM = 3
|
||||
|
||||
|
||||
def helper_units(report: dict[str, Any]) -> list[tuple[str, str, str, str, int]]:
|
||||
"""(name, result, exit code, exit status, failures in a row) for each helper unit
|
||||
the agent reported. Agents before 0.10.0 report none."""
|
||||
out = []
|
||||
for entry in report.get("helper_units") or []:
|
||||
parts = entry.split()
|
||||
if len(parts) == 5 and parts[4].isdigit():
|
||||
out.append((*parts[:4], int(parts[4])))
|
||||
return out
|
||||
|
||||
|
||||
def helper_failure(result: str, code: str, status: str) -> str:
|
||||
"""How a helper unit's run failed, in words."""
|
||||
if code == "exited":
|
||||
return f"exit status {status}"
|
||||
if code in ("killed", "dumped"):
|
||||
return f"{result}, signal {status}"
|
||||
return result
|
||||
|
||||
|
||||
def helper_unit_problems(report: dict[str, Any]) -> list[tuple[bool, str]]:
|
||||
"""(is it a problem, why) for each kernelwatch helper unit whose last run failed."""
|
||||
found = []
|
||||
for name, result, code, status, failures in helper_units(report):
|
||||
if result == "success" or not failures:
|
||||
continue
|
||||
unit = f"kernelwatch-{name}.service"
|
||||
runs = "run" if failures == 1 else f"{failures} runs"
|
||||
found.append((failures >= HELPER_FAILURES_BEFORE_PROBLEM,
|
||||
f"{unit} failed its last {runs} ({helper_failure(result, code, status)}); "
|
||||
f"see journalctl -u {unit}"))
|
||||
return found
|
||||
|
||||
|
||||
def _kernel_only_reboot_file(report: dict[str, Any]) -> bool:
|
||||
"""/run/reboot-required exists only because of kernel packages."""
|
||||
pkgs = report.get("reboot_required_pkgs") or []
|
||||
@@ -324,6 +364,13 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
|
||||
status.raise_to("update", f"package lists {apt_age / 86400:.0f}d old; apt update not running?")
|
||||
if why := security_repo_problem(report, cfg, datetime.fromtimestamp(now, timezone.utc).date()):
|
||||
status.raise_to("update", why)
|
||||
# A failing helper leaves its data stale or missing, which the checks above may
|
||||
# read as fine (needrestart's results just go "unknown").
|
||||
for problem, why in helper_unit_problems(report):
|
||||
if problem:
|
||||
status.raise_to("update", why)
|
||||
else:
|
||||
status.note(why)
|
||||
|
||||
if host and (reported := hostname_mismatch(host, report)):
|
||||
status.note(f"registered as {host} but reports itself as {reported}; "
|
||||
|
||||
@@ -22,7 +22,7 @@ from urllib.parse import parse_qs, quote, unquote, urlsplit
|
||||
from . import __version__, auth, db, support
|
||||
from .config import Config
|
||||
from .report import MAX_BODY_BYTES, ReportError, sanitize
|
||||
from .status import LEVELS, Status, evaluate
|
||||
from .status import LEVELS, Status, evaluate, helper_failure, helper_units
|
||||
|
||||
SESSION_COOKIE = "kw_session"
|
||||
HOST_NAME_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$")
|
||||
@@ -105,6 +105,20 @@ def security_repo_summary(report: dict[str, Any]) -> str | None:
|
||||
return f"{suite}, published {duration(age)} before the report"
|
||||
|
||||
|
||||
def helper_units_summary(report: dict[str, Any]) -> str | None:
|
||||
units = helper_units(report)
|
||||
if not units:
|
||||
return None # agents before 0.10.0, or nothing ran in the last two hours
|
||||
parts = []
|
||||
for name, result, code, st, failures in units:
|
||||
if result == "success" or not failures:
|
||||
parts.append(f"{name}: ok")
|
||||
else:
|
||||
row = "" if failures == 1 else f", {failures} runs in a row"
|
||||
parts.append(f"{name}: failed ({helper_failure(result, code, st)}{row})")
|
||||
return ", ".join(parts)
|
||||
|
||||
|
||||
def phased_summary(report: dict[str, Any]) -> tuple[str, str]:
|
||||
"""(short cell text, full description) for updates deferred by Ubuntu's phasing."""
|
||||
if report.get("os_id") not in (None, "ubuntu"):
|
||||
@@ -538,6 +552,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
field("Package lists age", duration(r.get("apt_lists_age_seconds"))
|
||||
if r.get("apt_lists_age_seconds") is not None else None),
|
||||
field("Security repository", security_repo_summary(r)),
|
||||
field("kernelwatch helpers", helper_units_summary(r)),
|
||||
field("Uptime", duration(r.get("uptime_seconds")) if r.get("uptime_seconds") is not None else None),
|
||||
field("Agent version", r.get("agent_version")),
|
||||
])
|
||||
|
||||
Reference in new issue
Block a user