The agent ran as (capability-less) root only so needrestart could read Ubuntu's root-only /boot images. Switch the unit to DynamicUser=yes and make needrestart optional. To cover the case needrestart was catching (running kernel's package upgraded in place, same uname -r), the agent now reports the installed package version of the running kernel and the server compares it with the build in uname -v: "Debian 6.12.57-1" vs 6.12.57-1 on Debian, "#85-Ubuntu" / "#33~24.04.1-Ubuntu" vs the upload part of 6.8.0-85.85 / 6.14.0-33.33~24.04.1 on Ubuntu. Unrecognised builds fall back to needrestart and the newest-installed comparison. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
120 lines
4.5 KiB
Python
120 lines
4.5 KiB
Python
"""Turn a host's latest report into a dashboard status.
|
|
|
|
Levels, worst first:
|
|
stale - no report recently (or ever); nothing else can be trusted
|
|
reboot - a newer kernel (or other reboot-requiring update) is installed but not running
|
|
update - updates are waiting to be installed, or the package lists are too old to tell
|
|
ok - running the newest installed kernel, nothing pending
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from dataclasses import dataclass, field
|
|
from typing import Any
|
|
|
|
from .config import Config
|
|
|
|
LEVELS = ("stale", "reboot", "update", "ok")
|
|
_RANK = {level: i for i, level in enumerate(LEVELS)}
|
|
|
|
# needrestart's NEEDRESTART-KSTA values.
|
|
KSTA_CURRENT = 1
|
|
KSTA_ABI_UPGRADE = 2
|
|
KSTA_VERSION_UPGRADE = 3
|
|
|
|
|
|
@dataclass
|
|
class Status:
|
|
level: str = "ok"
|
|
reasons: list[str] = field(default_factory=list)
|
|
|
|
def raise_to(self, level: str, reason: str) -> None:
|
|
if _RANK[level] < _RANK[self.level]:
|
|
self.level = level
|
|
self.reasons.append(reason)
|
|
|
|
@property
|
|
def rank(self) -> int:
|
|
return _RANK[self.level]
|
|
|
|
|
|
# Debian `uname -v`: "#1 SMP PREEMPT_DYNAMIC Debian 6.12.57-1 (2025-11-05)"
|
|
_DEBIAN_BUILD = re.compile(r"\bDebian (\S+)")
|
|
# Ubuntu `uname -v`: "#85-Ubuntu SMP ..." (GA) or "#33~24.04.1-Ubuntu SMP ..." (HWE)
|
|
_UBUNTU_BUILD = re.compile(r"^#(\S+)-Ubuntu\b")
|
|
# Ubuntu package version: "6.8.0-85.85" or "6.14.0-33.33~24.04.1"; upload part after "-ABI."
|
|
_UBUNTU_PKG = re.compile(r"^\d+\.\d+\.\d+-\d+\.(\S+)$")
|
|
|
|
|
|
def running_build_outdated(report: dict[str, Any]) -> tuple[str, str] | None:
|
|
"""(running build, installed package version) if the running kernel's package was
|
|
upgraded in place since boot (same `uname -r`, new build), else None.
|
|
|
|
None also means "can't tell", e.g. an unrecognised build string."""
|
|
build = report.get("kernel_build") or ""
|
|
pkg = report.get("kernel_running_pkg_version")
|
|
if not pkg:
|
|
return None
|
|
if m := _DEBIAN_BUILD.search(build):
|
|
running = m.group(1)
|
|
return (running, pkg) if running != pkg else None
|
|
if (m := _UBUNTU_BUILD.match(build)) and (p := _UBUNTU_PKG.match(pkg)):
|
|
return (m.group(1), p.group(1)) if m.group(1) != p.group(1) else None
|
|
return None
|
|
|
|
|
|
def kernel_reboot_pending(report: dict[str, Any]) -> str | None:
|
|
"""Why the running kernel is out of date, or None if it isn't (as far as we can tell)."""
|
|
running = report.get("kernel_running") or "?"
|
|
if outdated := running_build_outdated(report):
|
|
built, installed = outdated
|
|
return f"running {running} build {built}, package upgraded in place to {installed}"
|
|
|
|
ksta = report.get("needrestart_ksta")
|
|
if ksta == KSTA_VERSION_UPGRADE:
|
|
expected = report.get("needrestart_kexp") or report.get("kernel_newest_installed") or "?"
|
|
return f"running {running}, {expected} installed"
|
|
if ksta == KSTA_ABI_UPGRADE:
|
|
return f"running {running}, an ABI-compatible update of it is installed"
|
|
if ksta == KSTA_CURRENT:
|
|
return None
|
|
|
|
# No usable needrestart result: compare against the newest installed image.
|
|
newest = report.get("kernel_newest_installed")
|
|
if newest and newest != running:
|
|
return f"running {running}, {newest} installed"
|
|
return None
|
|
|
|
|
|
def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
|
|
cfg: Config) -> Status:
|
|
status = Status()
|
|
if received_at is None or report is None:
|
|
status.raise_to("stale", "never reported")
|
|
return status
|
|
|
|
age_h = (now - received_at) / 3600
|
|
if age_h > cfg.stale_after_hours:
|
|
status.raise_to("stale", f"last report {age_h:.0f}h ago")
|
|
|
|
if why := kernel_reboot_pending(report):
|
|
status.raise_to("reboot", f"kernel: {why}")
|
|
if report.get("reboot_required"):
|
|
pkgs = report.get("reboot_required_pkgs") or []
|
|
detail = f" ({', '.join(pkgs[:5])}{', …' if len(pkgs) > 5 else ''})" if pkgs else ""
|
|
status.raise_to("reboot", f"/run/reboot-required{detail}")
|
|
|
|
if kernels := report.get("kernel_upgradable"):
|
|
status.raise_to("update", f"kernel update available: {', '.join(kernels)}")
|
|
if sec := report.get("security_count"):
|
|
status.raise_to("update", f"{sec} security update{'s' if sec != 1 else ''} pending")
|
|
|
|
apt_age = report.get("apt_lists_age_seconds")
|
|
if apt_age is None:
|
|
status.raise_to("update", "package list age unknown")
|
|
elif apt_age / 3600 > cfg.apt_stale_after_hours:
|
|
status.raise_to("update", f"package lists {apt_age / 86400:.0f}d old; apt update not running?")
|
|
|
|
return status
|