Report when kernelwatch's own helper units keep failing

kernelwatch-apt-update.service failed hourly on most hosts for weeks
without anything showing it. The agent only Wants= its helper units, so
the report goes out regardless, with stale lists or (for needrestart)
results that quietly turn "unknown".

Each helper (apt-update, needrestart) now runs kernelwatch-unit-result
as its ExecStopPost=, which systemd runs after failed runs too. It
writes "RESULT EXIT_CODE EXIT_STATUS FAILURES" to
/run/kernelwatch/<name>.result, FAILURES counting failed runs in a row.
The agent (0.10.0) reports results from the last two hours as
helper_units. On the server, one or two failures are a note (an admin's
apt can hold the lock now and then); three in a row put the host in
update, naming the unit and its exit status and pointing at journalctl.
The host page gets a "kernelwatch helpers" row.

The Livepatch unit isn't tracked: it fails by design where the snap is
installed but Livepatch isn't enabled, and Livepatch has its own check.

The apt-update unit now has RuntimeDirectory=kernelwatch (preserved,
like the others) for its result file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-11-01 15:23:05 -05:00
1 parent ad310a4948
commit f88261d2f5
18 files changed
+303 -8

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@ CONF=${KERNELWATCH_DEPLOY_CONF:-$here/deploy.conf}
HOSTS_FILE=${KERNELWATCH_HOSTS:-$here/hosts}
FILES=(kernelwatch-agent kernelwatch-agent.service kernelwatch-agent.timer
kernelwatch-needrestart.service kernelwatch-livepatch kernelwatch-livepatch.service
kernelwatch-apt-update.service install.sh)
kernelwatch-apt-update.service kernelwatch-unit-result install.sh)
NAME_RE='^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$'
SERVER_URL=
+1
View File
@@ -82,6 +82,7 @@ mkdir -p "$root/usr/local/sbin" "$root/etc/systemd/system"
install -d -m 0755 "$etc"
install -m 0755 "$here/kernelwatch-agent" "$root/usr/local/sbin/kernelwatch-agent"
install -m 0755 "$here/kernelwatch-livepatch" "$root/usr/local/sbin/kernelwatch-livepatch"
install -m 0755 "$here/kernelwatch-unit-result" "$root/usr/local/sbin/kernelwatch-unit-result"
install -m 0644 "$here/kernelwatch-agent.service" "$here/kernelwatch-agent.timer" \
"$here/kernelwatch-needrestart.service" "$here/kernelwatch-livepatch.service" \
"$here/kernelwatch-apt-update.service" "$root/etc/systemd/system/"
+25 -1
View File
@@ -6,7 +6,7 @@
# rules can change without redeploying agents.
set -euo pipefail
AGENT_VERSION=0.9.0
AGENT_VERSION=0.10.0
# Prefix for every file the agent reads. Only the test suite sets this.
ROOT=${KERNELWATCH_ROOT:-}
@@ -180,6 +180,29 @@ read_livepatch() {
done <"$LIVEPATCH_FILE"
}
# How kernelwatch's helper units last ended, as recorded by kernelwatch-unit-result
# (their ExecStopPost=): one "NAME RESULT EXIT_CODE EXIT_STATUS FAILURES" line per
# unit that ran recently, e.g. "apt-update exit-code exited 100 3", FAILURES being
# failed runs in a row. This unit only Wants= them, so when they fail the report
# still goes out, with stale or missing data, and nothing else would say why.
# Livepatch isn't listed: its unit fails by design where the snap is installed
# but Livepatch isn't enabled, and its health is reported on its own.
HELPER_UNITS=(apt-update needrestart)
HELPER_MAX_AGE=7200
helper_units() {
local name file mtime now result= code= status= failures=
now=$(date +%s)
for name in "${HELPER_UNITS[@]}"; do
file=$ROOT/run/kernelwatch/$name.result
mtime=$(stat -c %Y "$file" 2>/dev/null) || continue
((now - mtime <= HELPER_MAX_AGE)) || continue
read -r result code status failures _ <"$file" || [[ -n $result ]] || continue
[[ $result =~ ^[a-z-]{1,32}$ && $code =~ ^[a-z-]{1,16}$ &&
$status =~ ^[A-Za-z0-9-]{1,16}$ && $failures =~ ^[0-9]{1,6}$ ]] || continue
printf '%s %s %s %s %s\n' "$name" "$result" "$code" "$status" "$failures"
done
}
# String, or null when empty.
json_str_or_null() {
if [[ -n $1 ]]; then json_str "$1"; else printf 'null'; fi
@@ -446,6 +469,7 @@ build_report() {
printf '"apt_lists_age_seconds":%s,' "$(json_int "$(apt_lists_age)")"
printf '"security_repo_suite":%s,' "$(json_str_or_null "$sec_suite")"
printf '"security_repo_age_seconds":%s,' "$(json_int "$sec_age")"
printf '"helper_units":%s,' "$(helper_units | json_str_array)"
printf '"uptime_seconds":%s' "$(json_int "$(uptime_seconds)")"
printf '}\n'
}
+7
View File
@@ -19,6 +19,13 @@ ExecStart=/usr/bin/apt-get -q update
# age; Debian has nothing else that writes it. lists/partial, the agent's last
# resort, changes even when the update fails.
ExecStartPost=/bin/sh -c 'mkdir -p /var/lib/apt/periodic && touch /var/lib/apt/periodic/update-success-stamp'
# Records how the run ended, success or not, so the agent can report failures,
# which are otherwise silent while apt-daily keeps the lists fresh.
ExecStopPost=/usr/local/sbin/kernelwatch-unit-result apt-update
RuntimeDirectory=kernelwatch
RuntimeDirectoryMode=0755
RuntimeDirectoryPreserve=yes
UMask=0022
TimeoutStartSec=5min
Nice=10
IOSchedulingClass=idle
+3
View File
@@ -8,6 +8,9 @@ ConditionPathExists=/usr/sbin/needrestart
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'
# Records how the run ended, success or not, so the agent can report failures:
# otherwise the results just go missing and the dashboard shows "unknown".
ExecStopPost=/usr/local/sbin/kernelwatch-unit-result needrestart
RuntimeDirectory=kernelwatch
RuntimeDirectoryMode=0755
RuntimeDirectoryPreserve=yes
+29
View File
@@ -0,0 +1,29 @@
#!/bin/sh
# Records how a kernelwatch helper unit's run ended, for the agent to report.
# Run as the unit's ExecStopPost=, which systemd runs after every run, failed or
# not, with SERVICE_RESULT, EXIT_CODE and EXIT_STATUS set (systemd.exec(5)).
#
# Usage: kernelwatch-unit-result NAME
# Writes /run/kernelwatch/NAME.result as "RESULT EXIT_CODE EXIT_STATUS FAILURES",
# e.g. "exit-code exited 100 3", where FAILURES counts failed runs in a row and
# is 0 after a success. A missing value is written as "-".
set -u
name=${1:-}
case $name in
'' | *[!a-z0-9-]*)
echo "usage: kernelwatch-unit-result NAME" >&2
exit 2
;;
esac
file=${KERNELWATCH_RUN_DIR:-/run/kernelwatch}/$name.result
result=${SERVICE_RESULT:--}
failures=0
if [ "$result" != success ]; then
failures=$(awk '$4 ~ /^[0-9]+$/ { print $4 }' "$file" 2>/dev/null)
failures=$((${failures:-0} + 1))
fi
printf '%s %s %s %s\n' "$result" "${EXIT_CODE:--}" "${EXIT_STATUS:--}" "$failures" >"$file.new" &&
mv "$file.new" "$file"