diff --git a/README.md b/README.md index 87eb2a7..f17180d 100644 --- a/README.md +++ b/README.md @@ -465,6 +465,7 @@ sudo kernelwatch remove-user USER sudo kernelwatch list-users sudo kernelwatch backup PATH write a consistent copy of the database (safe while running) sudo kernelwatch scheduled-backup dated copy in backup_dir, then prune old ones (what the timer runs) +sudo kernelwatch notify-backup-failure send the failed-backup notification now (also runs on failure) sudo kernelwatch check-alerts check for stale, recovered, reboot and update alerts now sudo kernelwatch test-alert send a test notification to the configured ntfy topic ``` @@ -505,6 +506,17 @@ journalctl -u kernelwatch-backup # a failed run shows up here sudo systemctl start kernelwatch-backup # back up now ``` +If a run fails, whether from an error, the 10-minute timeout, an out-of-memory +kill or a crash, systemd starts `kernelwatch-backup-failed.service` +(`OnFailure=`), which sends a high-priority message to your ntfy topic naming the +newest backup still on disk. The cause is in `journalctl -u kernelwatch-backup`. +Without `ntfy_url` it only logs that it had nothing to send to. If ntfy itself is +unreachable at that moment, the failure shows up only in the journal +(`journalctl -u kernelwatch-backup-failed`). It can't tell you that the timer +never ran at all, for example because the host was down; that needs a heartbeat +monitor. You can check delivery with +`sudo kernelwatch notify-backup-failure`, which sends the same message. + These copies sit on the dashboard host, so they protect against a corrupted or mistakenly changed database but not against losing the machine. Copy `/var/lib/kernelwatch/backups/` elsewhere with whatever you already back up with. diff --git a/server/install.sh b/server/install.sh index d97594d..70b80e9 100755 --- a/server/install.sh +++ b/server/install.sh @@ -45,7 +45,8 @@ install -d -m 0750 -o kernelwatch -g kernelwatch "$root/var/lib/kernelwatch" install -m 0644 "$here/kernelwatch.service" "$root/etc/systemd/system/" install -m 0644 "$here/kernelwatch-alerts.service" "$here/kernelwatch-alerts.timer" \ - "$here/kernelwatch-backup.service" "$here/kernelwatch-backup.timer" "$root/etc/systemd/system/" + "$here/kernelwatch-backup.service" "$here/kernelwatch-backup.timer" \ + "$here/kernelwatch-backup-failed.service" "$root/etc/systemd/system/" systemctl daemon-reload systemctl enable kernelwatch.service systemctl restart kernelwatch.service diff --git a/server/kernelwatch-backup-failed.service b/server/kernelwatch-backup-failed.service new file mode 100644 index 0000000..952ccce --- /dev/null +++ b/server/kernelwatch-backup-failed.service @@ -0,0 +1,38 @@ +[Unit] +Description=Notify that the kernelwatch backup failed +Wants=network-online.target +After=network-online.target + +[Service] +Type=oneshot +User=kernelwatch +Group=kernelwatch +Environment=KERNELWATCH_CONFIG=/etc/kernelwatch/server.toml +ExecStart=/usr/bin/python3 -I /opt/kernelwatch/kernelwatch-server notify-backup-failure +TimeoutStartSec=2min +StateDirectory=kernelwatch +StateDirectoryMode=0750 + +CapabilityBoundingSet= +AmbientCapabilities= +NoNewPrivileges=yes +ProtectSystem=strict +ProtectHome=yes +PrivateTmp=yes +PrivateDevices=yes +ProtectKernelTunables=yes +ProtectKernelModules=yes +ProtectKernelLogs=yes +ProtectControlGroups=yes +ProtectClock=yes +ProtectHostname=yes +ProtectProc=invisible +RestrictNamespaces=yes +RestrictRealtime=yes +RestrictSUIDSGID=yes +LockPersonality=yes +MemoryDenyWriteExecute=yes +RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 +SystemCallArchitectures=native +SystemCallFilter=@system-service +UMask=0077 diff --git a/server/kernelwatch-backup.service b/server/kernelwatch-backup.service index db5e12f..8295482 100644 --- a/server/kernelwatch-backup.service +++ b/server/kernelwatch-backup.service @@ -1,5 +1,8 @@ [Unit] Description=Back up the kernelwatch database +# Any failure, including the timeout, an OOM kill or a crash, sends an ntfy +# message (a no-op if ntfy isn't configured). +OnFailure=kernelwatch-backup-failed.service [Service] Type=oneshot diff --git a/server/server.toml.example b/server/server.toml.example index 649ed8b..6ffa3a1 100644 --- a/server/server.toml.example +++ b/server/server.toml.example @@ -18,6 +18,7 @@ report_retention_days = 30 # and deletes copies this many days old or older (0 keeps them all). Keep the # directory under /var/lib/kernelwatch; the unit can't write elsewhere. Copy the # files off this machine yourself: they don't protect against losing it. +# A failed backup run sends an ntfy message (if ntfy_url is set). backup_dir = "/var/lib/kernelwatch/backups" backup_keep_days = 7 diff --git a/tests/test_install.py b/tests/test_install.py index e1bf1c3..4bed931 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -329,6 +329,15 @@ class ServerInstallTest(InstallCase): for command in commands: self.assertRegex(help_text, rf"\b{re.escape(command)}\b") + def test_on_failure_targets_are_shipped_units(self): + """A typo'd OnFailure= is silently ignored by systemd, so check the names.""" + units = {n for n in os.listdir(SERVER_DIR) if n.endswith((".service", ".timer"))} + targets = set() + for unit in units: + targets.update(re.findall(r"^OnFailure=(\S+)$", read(os.path.join(SERVER_DIR, unit)), re.M)) + self.assertIn("kernelwatch-backup-failed.service", targets) + self.assertEqual(sorted(targets - units), []) + def test_existing_user_is_not_recreated(self): self.install(KW_ID_RC="0") self.assertFalse([c for c in self.calls() if c.startswith("useradd")])