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

+26 -7
View File
@@ -6,7 +6,7 @@
# rules can change without redeploying agents.
set -euo pipefail
AGENT_VERSION=0.1.0
AGENT_VERSION=0.2.0
# Prefix for every file the agent reads. Only the test suite sets this.
ROOT=${KERNELWATCH_ROOT:-}
@@ -122,20 +122,33 @@ running_kernel_pkg_version() {
awk '$1 == "ii" { print $2; exit }' || true
}
# Sets NR_KSTA / NR_KEXP from `needrestart -b`. KSTA: 0 unknown, 1 current,
# 2 ABI-compatible upgrade pending, 3 version upgrade pending. Optional: as a
# normal user it can't read Ubuntu's root-only /boot images and reports 0.
# Results of `needrestart -b`, written as root by kernelwatch-needrestart.service
# (which this agent's unit pulls in, so it runs just before each report). The
# agent never runs needrestart itself: unprivileged it can't read Ubuntu's
# root-only /boot images or other users' processes, and gives wrong answers.
NEEDRESTART_FILE=$ROOT/run/kernelwatch/needrestart
NEEDRESTART_MAX_AGE=7200
# Sets NR_* from the results file, if it's there and fresh. KSTA: 0 unknown,
# 1 current, 2 ABI-compatible upgrade pending, 3 version upgrade pending.
NR_OK=0
NR_KSTA=
NR_KEXP=
NR_SVCS=
NR_SESS=0
read_needrestart() {
command -v needrestart >/dev/null 2>&1 || return 0
local line
local mtime line
mtime=$(stat -c %Y "$NEEDRESTART_FILE" 2>/dev/null) || return 0
(($(date +%s) - mtime <= NEEDRESTART_MAX_AGE)) || return 0
NR_OK=1
while IFS= read -r line; do
case $line in
NEEDRESTART-KSTA:*) NR_KSTA=${line#*: } ;;
NEEDRESTART-KEXP:*) NR_KEXP=${line#*: } ;;
NEEDRESTART-SVC:*) NR_SVCS+=${line#*: }$'\n' ;;
NEEDRESTART-SESS:*) NR_SESS=$((NR_SESS + 1)) ;;
esac
done < <(needrestart -b -k -r l 2>/dev/null || true)
done <"$NEEDRESTART_FILE"
}
# Simulated dist-upgrade. Reads only: no locks, no cache files written.
@@ -200,6 +213,12 @@ build_report() {
printf '"kernel_newest_installed":%s,' "$(json_str "$newest")"
printf '"needrestart_ksta":%s,' "$(json_int "$NR_KSTA")"
printf '"needrestart_kexp":%s,' "$(json_str "$NR_KEXP")"
if ((NR_OK)); then
printf '"services_need_restart":%s,' "$(sort -u <<<"$NR_SVCS" | json_str_array)"
printf '"sessions_need_restart":%s,' "$NR_SESS"
else
printf '"services_need_restart":null,"sessions_need_restart":null,'
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)")"