diff --git a/README.md b/README.md index 29ea98d..79e9e9b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Each host gets one status. When several apply, the worst one wins: |----------|---------| | `stale` | No report in `stale_after_hours` (default 3h), or never. Other data can't be trusted. | | `reboot` | A newer kernel is installed but not running, or `/run/reboot-required` exists. | -| `update` | A kernel or security update is available but not installed, services are running outdated libraries and need a restart, or apt's package lists are too old for "no updates" to mean anything. | +| `update` | A kernel or security update is available but not installed, services are running outdated libraries and need a restart, or apt's package lists are too old, or apt itself fails, so "no updates" can't be trusted. | | `ok` | Running the newest installed kernel, with no security or kernel updates pending. | Each row lists the reasons behind its status, and clicking a host shows its full diff --git a/agent/kernelwatch-agent b/agent/kernelwatch-agent index bd5fcef..3c04cdd 100755 --- a/agent/kernelwatch-agent +++ b/agent/kernelwatch-agent @@ -6,7 +6,7 @@ # rules can change without redeploying agents. set -euo pipefail -AGENT_VERSION=0.4.1 +AGENT_VERSION=0.5.0 # Prefix for every file the agent reads. Only the test suite sets this. ROOT=${KERNELWATCH_ROOT:-} @@ -193,13 +193,30 @@ json_str_or_null() { # with /proc/1/root. Unprivileged we can't read /proc/1/root, so ischroot # answers "chroot" and apt would offer every phased update. We're not in one: # say so, and apt applies phasing from /etc/machine-id like it does for root. +# +# apt's stderr goes to the file named by $1. A failed simulation prints nothing, +# which would otherwise read as "no updates", so the caller reports the failure. apt_simulation() { LC_ALL=C apt-get -s -q \ -o Dir::Bin::ischroot=/bin/false \ -o Debug::NoLocking=true \ -o Dir::Cache::pkgcache= \ -o Dir::Cache::srcpkgcache= \ - dist-upgrade 2>/dev/null || true + dist-upgrade 2>"$1" +} + +# Why apt failed, from its stderr on stdin: the first "E:" line, else the first +# line, else the exit status in $1. +apt_error() { + local line first= + while IFS= read -r line; do + if [[ $line == 'E: '* ]]; then + printf '%s' "${line#E: }" + return 0 + fi + [[ -z $first && -n $line ]] && first=$line + done + printf '%s' "${first:-apt-get exited with status $1}" } # Packages Ubuntu is holding back from this host for now (phased updates). apt @@ -233,7 +250,7 @@ uptime_seconds() { } build_report() { - local running newest kernels sim inst reboot_file=0 reboot_pkgs= + local running newest kernels sim inst sim_err rc apt_ok=1 apt_err= reboot_file=0 reboot_pkgs= running=$(uname -r) kernels=$(installed_kernels) @@ -241,7 +258,15 @@ build_report() { read_needrestart read_livepatch - sim=$(apt_simulation) + sim_err=$(mktemp) + if sim=$(apt_simulation "$sim_err"); then + : + else + rc=$? + apt_ok=0 sim= + apt_err=$(apt_error "$rc" <"$sim_err") + fi + rm -f "$sim_err" inst=$(grep '^Inst ' <<<"$sim" || true) if [[ -e $ROOT/run/reboot-required ]]; then @@ -270,10 +295,16 @@ build_report() { fi printf '"reboot_required":%s,' "$(json_bool "$reboot_file")" printf '"reboot_required_pkgs":%s,' "$(json_str_array <<<"$reboot_pkgs")" - printf '"upgradable_count":%s,' "$(json_int "$(grep -c . <<<"$inst" || true)")" - printf '"security_count":%s,' "$(json_int "$(grep -ci 'security' <<<"$inst" || true)")" - printf '"phased_packages":%s,' "$(phased_packages <<<"$sim" | json_str_array)" - printf '"kernel_upgradable":%s,' "$(awk '$2 ~ /^linux-image-/ { print $2 }' <<<"$inst" | json_str_array)" + if ((apt_ok)); then + printf '"upgradable_count":%s,' "$(json_int "$(grep -c . <<<"$inst" || true)")" + printf '"security_count":%s,' "$(json_int "$(grep -ci 'security' <<<"$inst" || true)")" + printf '"phased_packages":%s,' "$(phased_packages <<<"$sim" | json_str_array)" + printf '"kernel_upgradable":%s,' "$(awk '$2 ~ /^linux-image-/ { print $2 }' <<<"$inst" | json_str_array)" + else + printf '"upgradable_count":null,"security_count":null,"phased_packages":null,"kernel_upgradable":null,' + fi + printf '"apt_simulation_ok":%s,' "$(json_bool "$apt_ok")" + printf '"apt_simulation_error":%s,' "$(json_str_or_null "$apt_err")" printf '"livepatch_state":%s,' "$(json_str_or_null "$LP_STATE")" printf '"livepatch_check_state":%s,' "$(json_str_or_null "$LP_CHECK")" printf '"livepatch_supported":%s,' "$(json_str_or_null "$LP_SUPPORTED")" diff --git a/docs/how-it-works.md b/docs/how-it-works.md index 6b87b34..43d5d8f 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -71,7 +71,11 @@ How the agent works it out: - **Available updates**: a simulated `apt-get dist-upgrade`. It is read-only, takes no locks and writes no cache files. `dist-upgrade` is used so that new-ABI kernel packages pulled in by the meta-package show up. Lines whose origin contains - `-security` count as security updates. + `-security` count as security updates. If the simulation fails (unmet + dependencies, a broken sources entry, a damaged package cache), apt prints no + updates at all, so the agent reports the counts as unknown along with apt's + error, and the host goes to `update` with "apt can't work out pending updates". + Agents before 0.5.0 can't tell a failure from "no updates". - **Fresh package lists**: the simulation only knows what apt's package lists say, and the agent can't refresh them itself. Ubuntu refreshes them about once a day. Debian doesn't by default unless `unattended-upgrades` is set up, so a diff --git a/server/kernelwatch/__init__.py b/server/kernelwatch/__init__.py index 9a79dd6..bbf6e83 100644 --- a/server/kernelwatch/__init__.py +++ b/server/kernelwatch/__init__.py @@ -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.4.1" +AGENT_VERSION = "0.5.0" diff --git a/server/kernelwatch/report.py b/server/kernelwatch/report.py index e42acfe..5de34b8 100644 --- a/server/kernelwatch/report.py +++ b/server/kernelwatch/report.py @@ -40,6 +40,8 @@ FIELDS: dict[str, str] = { "livepatch_supported": "str", "livepatch_upgrade_required": "str", "livepatch_fix_count": "int", + "apt_simulation_ok": "bool", + "apt_simulation_error": "str", "apt_lists_age_seconds": "int", "uptime_seconds": "int", } diff --git a/server/kernelwatch/status.py b/server/kernelwatch/status.py index 14441fc..e8cac3e 100644 --- a/server/kernelwatch/status.py +++ b/server/kernelwatch/status.py @@ -198,6 +198,11 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float, if livepatch_note and not covered: status.note(livepatch_note) + # An agent whose apt simulation failed sends no update counts, so "no security + # updates" would be a guess. Agents before 0.5.0 don't send the field. + if report.get("apt_simulation_ok") is False: + why = report.get("apt_simulation_error") or "no error message" + status.raise_to("update", f"apt can't work out pending updates: {why}") if kernels := report.get("kernel_upgradable"): status.raise_to("update", f"kernel update available: {', '.join(kernels)}") if sec := report.get("security_count"): diff --git a/server/kernelwatch/web.py b/server/kernelwatch/web.py index 730d29a..0992493 100644 --- a/server/kernelwatch/web.py +++ b/server/kernelwatch/web.py @@ -502,6 +502,8 @@ class Handler(BaseHTTPRequestHandler): field("Kernel packages upgradable", r.get("kernel_upgradable"), code=True), field("Updates pending", r.get("upgradable_count")), field("Security updates", r.get("security_count")), + field("apt simulation", None if r.get("apt_simulation_ok") is None else + "ok" if r["apt_simulation_ok"] else f"failed: {r.get('apt_simulation_error') or '?'}"), field("Deferred by phasing", r.get("phased_packages"), code=True), field("Reboot-required packages", r.get("reboot_required_pkgs"), code=True), field("Services needing restart", services_summary(r)[1]), @@ -518,7 +520,7 @@ class Handler(BaseHTTPRequestHandler): f'
{e(h['report'].get('kernel_running') or '–')}