Flag hosts without room on disk for their pending upgrades
CI / tests (Python 3.10) (pull_request) Successful in 43s
CI / tests (Python 3.11) (pull_request) Successful in 44s
CI / tests (Python 3.12) (pull_request) Successful in 43s
CI / tests (Python 3.13) (pull_request) Successful in 42s
CI / lint (pull_request) Successful in 3s
CI / tests (Python 3.10) (pull_request) Successful in 43s
CI / tests (Python 3.11) (pull_request) Successful in 44s
CI / tests (Python 3.12) (pull_request) Successful in 43s
CI / tests (Python 3.13) (pull_request) Successful in 42s
CI / lint (pull_request) Successful in 3s
A filesystem that fills up while apt downloads or dpkg unpacks leaves the upgrade half-done and apt broken, the same failure as a full /boot but on / or /var. Nothing caught it until the host's updates stopped. The agent (0.11.0) now works out what the pending upgrades need. A simulation doesn't print apt's "Need to get" or "additional disk space" lines, so it sums the package records from apt-cache show for each version the simulation would install: Size for each .deb not already in /var/cache/apt/archives, and the growth in Installed-Size over the installed versions (from dpkg-query). Removals aren't subtracted, so it errs high. It also reports free space and the mount point of the filesystems holding /var/cache/apt/archives and /usr. The server puts a host in update when the upgrades would leave less than upgrade_min_free_mb (default 100, 0 disables) on either filesystem. The download and the install both count when they're on the same one. With nothing pending there is nothing to check, so this doesn't turn into general disk monitoring. The host page gets a "Pending upgrades size" row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b5be19f85d
commit
15b1f5be4f
13 files changed
+363
-7
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.10.0"
|
||||
AGENT_VERSION = "0.11.0"
|
||||
@@ -33,6 +33,10 @@ class Config:
|
||||
# Less free space than this on /boot's filesystem puts a host in update: the next
|
||||
# kernel (image plus initramfs, often over 100 MB) may not fit. 0 disables.
|
||||
boot_min_free_mb: int = 200
|
||||
# Pending upgrades that would leave less free space than this on the filesystem
|
||||
# apt downloads to, or the one /usr is on, put a host in update: dpkg would run
|
||||
# out of room part-way. 0 disables.
|
||||
upgrade_min_free_mb: int = 100
|
||||
# Note a host this many days before its OS release stops getting security updates
|
||||
# (or Ubuntu standard support ends without ESM). 0 disables the warning; a host
|
||||
# past the date is in update either way.
|
||||
@@ -116,6 +120,8 @@ def load(path: str | None = None) -> Config:
|
||||
raise SystemExit(f"{path}: os_eol_warn_days must be zero or positive")
|
||||
if cfg.boot_min_free_mb < 0:
|
||||
raise SystemExit(f"{path}: boot_min_free_mb must be zero or positive")
|
||||
if cfg.upgrade_min_free_mb < 0:
|
||||
raise SystemExit(f"{path}: upgrade_min_free_mb must be zero or positive")
|
||||
if cfg.backup_keep_days < 0:
|
||||
raise SystemExit(f"{path}: backup_keep_days must be zero or positive")
|
||||
if not cfg.backup_dir.startswith("/") or any(ord(c) < 32 or ord(c) == 127 for c in cfg.backup_dir):
|
||||
|
||||
@@ -47,6 +47,12 @@ FIELDS: dict[str, str] = {
|
||||
"apt_simulation_error": "str",
|
||||
"boot_size_kb": "int",
|
||||
"boot_free_kb": "int",
|
||||
"upgrade_download_kb": "int",
|
||||
"upgrade_install_kb": "int",
|
||||
"apt_archives_free_kb": "int",
|
||||
"apt_archives_mount": "str",
|
||||
"usr_free_kb": "int",
|
||||
"usr_mount": "str",
|
||||
"dpkg_unfinished_packages": "strlist",
|
||||
"held_packages": "strlist",
|
||||
"ubuntu_pro_attached": "bool",
|
||||
|
||||
@@ -159,6 +159,36 @@ 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"
|
||||
|
||||
|
||||
def _mb(kb: int) -> int:
|
||||
"""KiB to MB, rounded up so a small need never reads as 0 MB."""
|
||||
return -(-kb // 1024)
|
||||
|
||||
|
||||
def upgrade_space_low(report: dict[str, Any], cfg: Config) -> list[str]:
|
||||
"""Filesystems the pending upgrades would leave with under upgrade_min_free_mb
|
||||
free. Downloads land on the filesystem holding /var/cache/apt/archives, installed
|
||||
files on /usr's; when that's the same filesystem, both count against it. Nothing
|
||||
pending means nothing to check, however full the disk: general disk usage is out
|
||||
of scope. Agents before 0.11.0 don't report it; unknown counts as fine."""
|
||||
download, install = report.get("upgrade_download_kb"), report.get("upgrade_install_kb")
|
||||
if not cfg.upgrade_min_free_mb or download is None or install is None:
|
||||
return []
|
||||
needs: dict[str, list[int]] = {} # mount -> [download KiB, install KiB, free KiB]
|
||||
for prefix, slot, amount in (("apt_archives", 0, download), ("usr", 1, max(install, 0))):
|
||||
mount, free = report.get(f"{prefix}_mount"), report.get(f"{prefix}_free_kb")
|
||||
if mount and free is not None:
|
||||
needs.setdefault(mount, [0, 0, free])[slot] += amount
|
||||
reasons = []
|
||||
for mount, (dl, inst, free) in needs.items():
|
||||
if not dl + inst or free - dl - inst >= cfg.upgrade_min_free_mb * 1024:
|
||||
continue
|
||||
parts = [f"{_mb(dl)} MB download" if dl else "", f"{_mb(inst)} MB installed" if inst else ""]
|
||||
reasons.append(f"pending upgrades need {_mb(dl + inst)} MB on {mount} "
|
||||
f"({' + '.join(p for p in parts if p)}), {free // 1024} MB free; "
|
||||
f"they may fail part-way")
|
||||
return reasons
|
||||
|
||||
|
||||
UU_LOG = "/var/log/unattended-upgrades/"
|
||||
|
||||
|
||||
@@ -351,6 +381,8 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
|
||||
f"{', '.join(other[:5])}{more}")
|
||||
if low := boot_space_low(report, cfg):
|
||||
status.raise_to("update", low)
|
||||
for low in upgrade_space_low(report, cfg):
|
||||
status.raise_to("update", low)
|
||||
|
||||
if found := unattended_upgrades_problem(report, cfg):
|
||||
problem, why = found
|
||||
|
||||
@@ -105,6 +105,22 @@ def security_repo_summary(report: dict[str, Any]) -> str | None:
|
||||
return f"{suite}, published {duration(age)} before the report"
|
||||
|
||||
|
||||
def upgrade_space_summary(r: dict[str, Any]) -> str | None:
|
||||
"""Download and install size of the pending upgrades, and free space where they go."""
|
||||
download, install = r.get("upgrade_download_kb"), r.get("upgrade_install_kb")
|
||||
if download is None or install is None:
|
||||
return None # agents before 0.11.0, or apt couldn't say
|
||||
sign = "+" if install >= 0 else "-"
|
||||
text = f"{-(-download // 1024)} MB to download, {sign}{-(-abs(install) // 1024)} MB installed"
|
||||
free: dict[str, int] = {}
|
||||
for prefix in ("apt_archives", "usr"):
|
||||
mount, kb = r.get(f"{prefix}_mount"), r.get(f"{prefix}_free_kb")
|
||||
if mount and kb is not None:
|
||||
free.setdefault(mount, kb)
|
||||
shown = ", ".join(f"{mount} has {kb // 1024} MB free" for mount, kb in free.items())
|
||||
return f"{text}; {shown}" if shown else text
|
||||
|
||||
|
||||
def microcode_summary(report: dict[str, Any]) -> str | None:
|
||||
state = {1: "current", 2: "update needs a reboot"}.get(report.get("needrestart_ucsta"))
|
||||
if state is None:
|
||||
@@ -557,6 +573,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
field("Sessions needing restart", r.get("sessions_need_restart")),
|
||||
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("Pending upgrades size", upgrade_space_summary(r)),
|
||||
field("Unfinished dpkg packages", r.get("dpkg_unfinished_packages"), code=True),
|
||||
field("Packages on hold", r.get("held_packages"), code=True),
|
||||
field("unattended-upgrades", unattended_upgrades_summary(r)),
|
||||
|
||||
Reference in new issue
Block a user