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:
1 parent
69d4cf6363
commit
834c1459c0
14 files changed
+258
-26
No files matched your search
+43
-2
@@ -100,6 +100,47 @@ class AgentReportTest(AgentCase):
|
||||
self.assertEqual(r["kernel_upgradable"], ["linux-image-6.12.60+deb13-amd64"])
|
||||
self.assertAlmostEqual(r["apt_lists_age_seconds"], 7200, delta=60)
|
||||
self.assertEqual(r["uptime_seconds"], 93784)
|
||||
self.assertIsNone(r["services_need_restart"], "no needrestart results: unknown")
|
||||
self.assertIsNone(r["sessions_need_restart"])
|
||||
|
||||
def write_needrestart(self, text, age=60):
|
||||
d = os.path.join(self.root, "run/kernelwatch")
|
||||
os.makedirs(d, exist_ok=True)
|
||||
path = os.path.join(d, "needrestart")
|
||||
with open(path, "w") as f:
|
||||
f.write(text)
|
||||
t = time.time() - age
|
||||
os.utime(path, (t, t))
|
||||
|
||||
def test_needrestart_results(self):
|
||||
self.write_needrestart(
|
||||
"NEEDRESTART-VER: 3.6\nNEEDRESTART-KCUR: 6.12.48+deb13-amd64\n"
|
||||
"NEEDRESTART-KEXP: 6.12.57+deb13-amd64\nNEEDRESTART-KSTA: 3\n"
|
||||
"NEEDRESTART-SVC: ssh.service\nNEEDRESTART-SVC: nginx.service\n"
|
||||
"NEEDRESTART-SVC: nginx.service\nNEEDRESTART-SESS: patrick @ session #3\n")
|
||||
r = self.report()
|
||||
self.assertEqual(r["needrestart_ksta"], 3)
|
||||
self.assertEqual(r["needrestart_kexp"], "6.12.57+deb13-amd64")
|
||||
self.assertEqual(r["services_need_restart"], ["nginx.service", "ssh.service"])
|
||||
self.assertEqual(r["sessions_need_restart"], 1)
|
||||
|
||||
def test_needrestart_nothing_to_restart(self):
|
||||
self.write_needrestart("NEEDRESTART-VER: 3.6\nNEEDRESTART-KSTA: 1\n")
|
||||
r = self.report()
|
||||
self.assertEqual(r["services_need_restart"], [])
|
||||
self.assertEqual(r["sessions_need_restart"], 0)
|
||||
|
||||
def test_stale_needrestart_results_ignored(self):
|
||||
self.write_needrestart("NEEDRESTART-KSTA: 3\nNEEDRESTART-SVC: ssh.service\n", age=3 * 3600)
|
||||
r = self.report()
|
||||
self.assertIsNone(r["needrestart_ksta"])
|
||||
self.assertIsNone(r["services_need_restart"])
|
||||
|
||||
def test_agent_never_runs_needrestart_itself(self):
|
||||
marker = os.path.join(self.tmp, "needrestart-ran")
|
||||
stub(self.bin, "needrestart", f"touch {marker}\n")
|
||||
self.report()
|
||||
self.assertFalse(os.path.exists(marker))
|
||||
|
||||
def test_ubuntu_with_needrestart_and_reboot_required(self):
|
||||
with open(os.path.join(self.root, "etc/os-release"), "w") as f:
|
||||
@@ -109,8 +150,8 @@ class AgentReportTest(AgentCase):
|
||||
"*) printf 'ii linux-image-6.8.0-85-generic\\n"
|
||||
"ii linux-image-unsigned-6.8.0-100-generic\\n';; esac\n")
|
||||
stub(self.bin, "apt-get", "echo 'NOTE: This is only a simulation!'\n")
|
||||
stub(self.bin, "needrestart", "printf 'NEEDRESTART-VER: 3.6\\nNEEDRESTART-KCUR: 6.8.0-85-generic\\n"
|
||||
"NEEDRESTART-KEXP: 6.8.0-100-generic\\nNEEDRESTART-KSTA: 3\\n'\n")
|
||||
self.write_needrestart("NEEDRESTART-VER: 3.6\nNEEDRESTART-KCUR: 6.8.0-85-generic\n"
|
||||
"NEEDRESTART-KEXP: 6.8.0-100-generic\nNEEDRESTART-KSTA: 3\n")
|
||||
open(os.path.join(self.root, "run/reboot-required"), "w").close()
|
||||
with open(os.path.join(self.root, "run/reboot-required.pkgs"), "w") as f:
|
||||
f.write('linux-image-6.8.0-100-generic\nlinux-base\nlinux-base\n')
|
||||
|
||||
Reference in new issue
Block a user