Report services needing a restart via a separate root needrestart unit

needrestart has to inspect every process, which an unprivileged agent
can't (and unprivileged it gave false kernel results). Add
kernelwatch-needrestart.service: root with only CAP_SYS_PTRACE and
CAP_DAC_READ_SEARCH, PrivateNetwork=yes, list-only (-r l). It writes
`needrestart -b` output to /run/kernelwatch/needrestart; the agent unit
pulls it in before each report and reads the file (ignored if older than
2h). The agent no longer runs needrestart itself. Skipped cleanly when
needrestart isn't installed: the dashboard shows "?" and status is
unaffected.

Server: services_need_restart / sessions_need_restart fields, an
"update"-level reason, a Services column, and a note (not an escalation)
for hosts running an agent older than the one shipped with the server.

install.sh --with-needrestart installs needrestart and sets it to list
services after apt upgrades instead of prompting; deploy.sh passes
anything after `--` to install.sh. Agent 0.2.0, server 0.2.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-17 11:54:39 -04:00
1 parent 69d4cf6363
commit 834c1459c0
14 files changed
+258 -26

No files matched your search

+5 -1
View File
@@ -1,3 +1,7 @@
"""kernelwatch: fleet kernel and reboot status dashboard."""
__version__ = "0.1.0"
__version__ = "0.2.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.2.0"
+2
View File
@@ -27,6 +27,8 @@ FIELDS: dict[str, str] = {
"kernel_newest_installed": "str",
"needrestart_ksta": "int",
"needrestart_kexp": "str",
"services_need_restart": "strlist",
"sessions_need_restart": "int",
"reboot_required": "bool",
"reboot_required_pkgs": "strlist",
"upgradable_count": "int",
+24
View File
@@ -13,6 +13,7 @@ import re
from dataclasses import dataclass, field
from typing import Any
from . import AGENT_VERSION
from .config import Config
LEVELS = ("stale", "reboot", "update", "ok")
@@ -34,6 +35,10 @@ class Status:
self.level = level
self.reasons.append(reason)
def note(self, reason: str) -> None:
"""A reason worth showing that doesn't change the level."""
self.reasons.append(reason)
@property
def rank(self) -> int:
return _RANK[self.level]
@@ -93,6 +98,14 @@ def kernel_reboot_pending(report: dict[str, Any]) -> str | None:
return None
def _version_tuple(v: str) -> tuple[int, ...]:
return tuple(int(p) if p.isdigit() else 0 for p in v.split("."))
def agent_outdated(version: str | None) -> bool:
return bool(version) and _version_tuple(version) < _version_tuple(AGENT_VERSION)
def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
cfg: Config) -> Status:
status = Status()
@@ -116,10 +129,21 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
if sec := report.get("security_count"):
status.raise_to("update", f"{sec} security update{'s' if sec != 1 else ''} pending")
if services := report.get("services_need_restart"):
shown = ", ".join(s.removesuffix(".service") for s in services[:5])
more = f", +{len(services) - 5} more" if len(services) > 5 else ""
n = len(services)
status.raise_to("update", f"{n} service{'s' if n != 1 else ''} need{'' if n != 1 else 's'} "
f"restart: {shown}{more}")
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?")
if agent_outdated(report.get("agent_version")):
status.note(f"agent {report['agent_version']} is older than {AGENT_VERSION}; "
"run agent/deploy.sh")
return status
+15 -1
View File
@@ -84,6 +84,16 @@ def utc(ts: float | None) -> str:
return time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime(ts)) if ts else ""
def services_summary(report: dict[str, Any]) -> tuple[str, str]:
"""(short cell text, full description) for services needing a restart."""
services = report.get("services_need_restart")
if services is None:
return "?", "unknown: needrestart not installed (install.sh --with-needrestart)"
if not services:
return "0", "none"
return str(len(services)), ", ".join(services)
def badge(level: str) -> str:
return f'<span class="badge {e(level)}">{e(level)}</span>'
@@ -397,7 +407,7 @@ class Handler(BaseHTTPRequestHandler):
if not hosts else
'<div class="table-wrap"><table><thead><tr>'
"<th>Status</th><th>Host</th><th>OS</th><th>Running kernel</th>"
"<th>Newest installed</th><th>Updates</th><th>Uptime</th><th>Last seen</th>"
"<th>Newest installed</th><th>Updates</th><th>Services</th><th>Uptime</th><th>Last seen</th>"
"<th>Details</th></tr></thead><tbody>" + "".join(rows) + "</tbody></table></div>"
)
body = f'<main><div class="summary">{summary}</div>{table}</main>'
@@ -411,6 +421,7 @@ class Handler(BaseHTTPRequestHandler):
sec = r.get("security_count")
total = r.get("upgradable_count")
updates = "–" if total is None else f"{total}" + (f" ({sec} sec)" if sec else "")
services = services_summary(r)
return (
"<tr>"
f"<td>{badge(status.level)}</td>"
@@ -419,6 +430,7 @@ class Handler(BaseHTTPRequestHandler):
f"<td><code>{e(r.get('kernel_running') or '–')}</code></td>"
f"<td><code>{e(r.get('kernel_newest_installed') or '–')}</code></td>"
f"<td>{e(updates)}</td>"
f'<td title="{e(services[1])}">{e(services[0])}</td>'
f"<td>{e(duration(r.get('uptime_seconds')))}</td>"
f'<td title="{e(utc(h["received_at"]))}">{e(ago(h["received_at"], now))}</td>'
f'<td class="reasons">{"<br>".join(e(x) for x in status.reasons) or "–"}</td>'
@@ -469,6 +481,8 @@ class Handler(BaseHTTPRequestHandler):
field("Updates pending", r.get("upgradable_count")),
field("Security updates", r.get("security_count")),
field("Reboot-required packages", r.get("reboot_required_pkgs"), code=True),
field("Services needing restart", services_summary(r)[1]),
field("Sessions needing restart", r.get("sessions_need_restart")),
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),