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

+12 -4
View File
@@ -7,6 +7,7 @@
# agent/deploy.sh --register new01 register on the dashboard, then install
# agent/deploy.sh --rotate web01 issue a new token, then reinstall
# agent/deploy.sh --dry-run show what would happen
# agent/deploy.sh -- --with-needrestart pass the rest to install.sh
#
# Settings: agent/deploy.conf (see deploy.conf.example). Hosts: agent/hosts,
# one per line: `<ssh target> [kernelwatch name]`; the name defaults to the
@@ -17,7 +18,8 @@ set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
CONF=${KERNELWATCH_DEPLOY_CONF:-$here/deploy.conf}
HOSTS_FILE=${KERNELWATCH_HOSTS:-$here/hosts}
FILES=(kernelwatch-agent kernelwatch-agent.service kernelwatch-agent.timer install.sh)
FILES=(kernelwatch-agent kernelwatch-agent.service kernelwatch-agent.timer
kernelwatch-needrestart.service install.sh)
NAME_RE='^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$'
SERVER_URL=
@@ -97,7 +99,7 @@ deploy_one() {
if ((dry_run)); then
if [[ $mode == upgrade ]]; then
echo " copy agent to $target, run install.sh (keeps config and token)" >&2
echo " copy agent to $target, run install.sh ${install_args[*]} (keeps config and token)" >&2
else
echo " on $DASHBOARD: kernelwatch ${mode/register/add-host} ${name:-<its hostname -s>}" >&2
echo " copy agent and token to $target, run install.sh --server $SERVER_URL" >&2
@@ -120,7 +122,7 @@ deploy_one() {
return 1
fi
local -a install=(sudo "$dir/install.sh")
local -a install=(sudo "$dir/install.sh" "${install_args[@]}")
if [[ $mode != upgrade ]]; then
# 2. Get a token from the dashboard and hand it over on stdin, never argv.
say " $mode $name on $DASHBOARD"
@@ -143,13 +145,19 @@ deploy_one() {
fi
}
usage() { sed -n '2,15s/^# \{0,1\}//p' "$0"; }
usage() { sed -n '2,16s/^# \{0,1\}//p' "$0"; }
mode=upgrade
dry_run=0
targets=()
install_args=()
while (($#)); do
case $1 in
--)
shift
install_args=("$@")
break
;;
--register) mode=register ;;
--rotate) mode=rotate ;;
--dry-run) dry_run=1 ;;
+21 -2
View File
@@ -2,6 +2,11 @@
# Install or upgrade kernelwatch-agent on a Debian 12/13 or Ubuntu 22.04/24.04 host.
#
# sudo ./install.sh --server https://kernelwatch.example.internal [--ca ca.pem]
# [--with-needrestart]
#
# --with-needrestart installs needrestart (Ubuntu has it already) so the
# dashboard can show services needing a restart, and sets it to only list
# services after apt upgrades instead of asking interactively.
#
# The host token (from `kernelwatch add-host <name>` on the server) is read
# from --token-file, the KERNELWATCH_TOKEN environment variable, or a prompt,
@@ -13,13 +18,15 @@ here=$(cd "$(dirname "$0")" && pwd)
server=
ca=
token_file=
with_needrestart=0
while (($#)); do
case $1 in
--server) server=${2:?--server needs a URL}; shift 2 ;;
--ca) ca=${2:?--ca needs a file}; shift 2 ;;
--token-file) token_file=${2:?--token-file needs a file}; shift 2 ;;
-h | --help) sed -n '2,9s/^# \{0,1\}//p' "$0"; exit 0 ;;
--with-needrestart) with_needrestart=1; shift ;;
-h | --help) sed -n '2,14s/^# \{0,1\}//p' "$0"; exit 0 ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
done
@@ -36,10 +43,22 @@ esac
if ! command -v curl >/dev/null; then
apt-get install -y --no-install-recommends curl ca-certificates
fi
if ((with_needrestart)); then
if [[ ! -x /usr/sbin/needrestart ]]; then
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends needrestart
fi
install -d -m 0755 /etc/needrestart/conf.d
cat >/etc/needrestart/conf.d/kernelwatch.conf <<'NRCONF'
# Installed by kernelwatch: after apt upgrades, list services that need a
# restart instead of asking. Delete this file to restore the default.
$nrconf{restart} = 'l';
NRCONF
fi
install -d -m 0755 /etc/kernelwatch
install -m 0755 "$here/kernelwatch-agent" /usr/local/sbin/kernelwatch-agent
install -m 0644 "$here/kernelwatch-agent.service" "$here/kernelwatch-agent.timer" /etc/systemd/system/
install -m 0644 "$here/kernelwatch-agent.service" "$here/kernelwatch-agent.timer" \
"$here/kernelwatch-needrestart.service" /etc/systemd/system/
if [[ -n $ca ]]; then
install -m 0644 "$ca" /etc/kernelwatch/ca.pem
+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)")"
+2 -2
View File
@@ -1,7 +1,7 @@
[Unit]
Description=Report kernel and update status to kernelwatch
Wants=network-online.target
After=network-online.target apt-daily.service
Wants=network-online.target kernelwatch-needrestart.service
After=network-online.target apt-daily.service kernelwatch-needrestart.service
[Service]
Type=oneshot
+40
View File
@@ -0,0 +1,40 @@
[Unit]
Description=Check for services needing a restart (for kernelwatch)
# Pulled in by kernelwatch-agent.service before each report. Skipped cleanly
# when needrestart isn't installed; the dashboard then shows "unknown".
ConditionPathExists=/usr/sbin/needrestart
[Service]
Type=oneshot
# -b batch output, -r l list only: never restarts anything.
ExecStart=/bin/sh -c 'needrestart -b -r l > /run/kernelwatch/needrestart.new && mv /run/kernelwatch/needrestart.new /run/kernelwatch/needrestart'
RuntimeDirectory=kernelwatch
RuntimeDirectoryMode=0755
RuntimeDirectoryPreserve=yes
UMask=0022
# Root, but only with what needrestart needs to inspect every process
# (/proc/<pid>/maps of other users) and nothing else. No network at all.
User=root
CapabilityBoundingSet=CAP_SYS_PTRACE CAP_DAC_READ_SEARCH
AmbientCapabilities=
NoNewPrivileges=yes
PrivateNetwork=yes
ProtectSystem=strict
ProtectHome=read-only
PrivateTmp=yes
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectKernelLogs=yes
ProtectControlGroups=yes
ProtectClock=yes
ProtectHostname=yes
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes
LockPersonality=yes
MemoryDenyWriteExecute=yes
RestrictAddressFamilies=AF_UNIX
SystemCallArchitectures=native
SystemCallFilter=@system-service