Add notify-backup-failure command

Sends a high-priority ntfy message that the nightly backup failed, naming the
newest backup still on disk. It exits 1 if the message can't be delivered and 0
(with a note on stderr) when ntfy isn't configured.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Sonnet 5.5 committed 2026-10-30 11:12:22 -04:00
1 parent 6652a4d795
commit 8a9a3ca56d
4 files changed
+90

No files matched your search

+24
View File
@@ -3,6 +3,7 @@
from __future__ import annotations
import fcntl
import socket
import sqlite3
import sys
import time
@@ -158,3 +159,26 @@ def _check_status(cfg: Config, conn: sqlite3.Connection) -> int:
db.save_status_alert(conn, name, level, since, level)
print(f"kernelwatch: {level} notification sent for {name}")
return failures
def notify_backup_failure(cfg: Config) -> int:
"""Tell the ntfy topic the nightly backup failed. Returns 1 if that couldn't be sent.
Run by systemd (OnFailure= on kernelwatch-backup.service), so it also covers
failures the backup command can't report itself: a timeout, an out-of-memory
kill, a crash. The cause is in `journalctl -u kernelwatch-backup`."""
if not cfg.ntfy_url:
print("kernelwatch: backup failed, but ntfy_url is not configured; no notification sent",
file=sys.stderr)
return 0
latest = db.latest_backup(cfg.backup_dir)
message = (f"The nightly kernelwatch database backup on {socket.gethostname()} failed.\n"
f"Newest backup still on disk: {latest or 'none'}.\n"
"Cause: journalctl -u kernelwatch-backup")
try:
publish(cfg, "kernelwatch: nightly backup failed", message, stale=True, tag="floppy_disk")
except DeliveryError as exc:
print(f"kernelwatch: backup failure notification failed: {exc}", file=sys.stderr)
return 1
print("kernelwatch: backup failure notification sent")
return 0