Flag hosts where unattended-upgrades has stopped succeeding

On a host with unattended-upgrades enabled, no successful run in
unattended_upgrades_stale_after_days (default 3, 0 disables) puts it in
update. The reason says which way it broke: still running but failing
("last succeeded 5d ago and is failing (last ran 6h ago); check
/var/log/unattended-upgrades/"), not running at all ("is
apt-daily-upgrade.timer enabled?"), or never succeeded. A host where it
hasn't completed its first run yet gets a note, and packages it keeps back
are noted. Hosts with it disabled or not installed, and agents before
0.8.0, are judged as before. The host page shows the state, both stamp
ages and the kept-back packages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-31 20:18:41 -04:00
1 parent df097c9da3
commit bfac723850
7 files changed
+143

No files matched your search

+5
View File
@@ -37,6 +37,9 @@ class Config:
# (or Ubuntu standard support ends without ESM). 0 disables the warning; a host
# past the date is in update either way.
os_eol_warn_days: int = 90
# A host with unattended-upgrades enabled that hasn't had a successful run in this
# long is in update. It normally runs daily. 0 disables the check.
unattended_upgrades_stale_after_days: float = 3.0
# Where `scheduled-backup` (kernelwatch-backup.timer) writes dated copies of the
# database, and how many days of them it keeps; 0 keeps them all. It must stay
@@ -100,6 +103,8 @@ def load(path: str | None = None) -> Config:
raise SystemExit(f"{path}: ntfy_timeout_seconds must be positive and finite")
if cfg.report_min_interval_seconds < 0:
raise SystemExit(f"{path}: report_min_interval_seconds must be zero or positive")
if not math.isfinite(cfg.unattended_upgrades_stale_after_days) or cfg.unattended_upgrades_stale_after_days < 0:
raise SystemExit(f"{path}: unattended_upgrades_stale_after_days must be zero or positive and finite")
if cfg.os_eol_warn_days < 0:
raise SystemExit(f"{path}: os_eol_warn_days must be zero or positive")
if cfg.boot_min_free_mb < 0:
+46
View File
@@ -157,6 +157,40 @@ def boot_space_low(report: dict[str, Any], cfg: Config) -> str | None:
return f"/boot has {free_kb // 1024} MB free{used}; the next kernel may not fit"
UU_LOG = "/var/log/unattended-upgrades/"
def _age(seconds: int) -> str:
return f"{seconds // 86400}d" if seconds >= 86400 else f"{seconds // 3600}h"
def unattended_upgrades_problem(report: dict[str, Any], cfg: Config) -> tuple[bool, str] | None:
"""(is it a problem, why) about unattended-upgrades on a host that has it enabled,
or None. Hosts without it, and agents before 0.8.0, get nothing.
The success stamp is apt.systemd.daily's (touched only when unattended-upgrade
exits 0); the run stamp is unattended-upgrade's own (written even when the run
then fails). Ages are as of the report."""
if report.get("unattended_upgrades") != "enabled" or not cfg.unattended_upgrades_stale_after_days:
return None
success = report.get("unattended_upgrades_success_age_seconds")
run = report.get("unattended_upgrades_run_age_seconds")
if success is None:
if run is None:
# Freshly installed: the first daily run hasn't happened yet.
return False, "unattended-upgrades is enabled but hasn't completed a run yet"
return True, (f"unattended-upgrades has never succeeded (last ran {_age(run)} ago); "
f"check {UU_LOG}")
if success <= cfg.unattended_upgrades_stale_after_days * 86400:
return None
# A successful run writes both stamps within moments of each other.
if run is not None and run < success - 3600:
return True, (f"unattended-upgrades last succeeded {_age(success)} ago and is failing "
f"(last ran {_age(run)} ago); check {UU_LOG}")
return True, (f"unattended-upgrades hasn't run in {_age(success)}; "
"is apt-daily-upgrade.timer enabled?")
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 []
@@ -229,6 +263,18 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
if low := boot_space_low(report, cfg):
status.raise_to("update", low)
if found := unattended_upgrades_problem(report, cfg):
problem, why = found
if problem:
status.raise_to("update", why)
else:
status.note(why)
# Packages from its allowed origins (by default security) it won't install: held,
# blacklisted or stuck on a conffile prompt. They stay pending until someone acts.
if report.get("unattended_upgrades") == "enabled" and (kept := report.get("unattended_upgrades_kept_back")):
more = f", +{len(kept) - 5} more" if len(kept) > 5 else ""
status.note(f"unattended-upgrades is keeping back {', '.join(kept[:5])}{more}")
today = datetime.fromtimestamp(now, timezone.utc).date()
if found := support.check(report, today, cfg.os_eol_warn_days):
problem, why = found
+13
View File
@@ -84,6 +84,17 @@ def utc(ts: float | None) -> str:
return time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime(ts)) if ts else ""
def unattended_upgrades_summary(report: dict[str, Any]) -> str | None:
state = report.get("unattended_upgrades")
if state != "enabled":
return state # "disabled", "not-installed", or None from agents before 0.8.0
last = {"success": report.get("unattended_upgrades_success_age_seconds"),
"run": report.get("unattended_upgrades_run_age_seconds")}
return "enabled, " + ", ".join(
f"last {what} " + ("never" if age is None else f"{duration(age)} before the report")
for what, age in last.items())
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"):
@@ -512,6 +523,8 @@ class Handler(BaseHTTPRequestHandler):
field("/boot free", None if r.get("boot_free_kb") is None else
f"{r['boot_free_kb'] // 1024} MB of {(r.get('boot_size_kb') or 0) // 1024} MB"),
field("Unfinished dpkg packages", r.get("dpkg_unfinished_packages"), code=True),
field("unattended-upgrades", unattended_upgrades_summary(r)),
field("Kept back by unattended-upgrades", r.get("unattended_upgrades_kept_back"), code=True),
field("Package lists age", duration(r.get("apt_lists_age_seconds"))
if r.get("apt_lists_age_seconds") is not None else None),
field("Uptime", duration(r.get("uptime_seconds")) if r.get("uptime_seconds") is not None else None),