Add heartbeat pings for the alert check and the nightly backup
heartbeat_url is pinged after every alert check that sent its notifications without failures, and backup_heartbeat_url after every successful nightly backup, so an external monitor (healthchecks.io, Uptime Kuma) notices when the alert timer or the backup stops running. A failing heartbeat is logged without the URL and never fails the work it reports on. kernelwatch test-heartbeat pings each configured URL once. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e3013921d1
commit
60b8c6bff7
4 files changed
+257
-1
No files matched your search
@@ -29,7 +29,25 @@ def cmd_serve(cfg, args) -> int:
|
||||
|
||||
|
||||
def cmd_check_alerts(cfg, args) -> int:
|
||||
return int(alerts.check(cfg) > 0)
|
||||
failures = alerts.check(cfg)
|
||||
# Skipped when deliveries failed, so the monitor also notices a broken alert pipeline.
|
||||
if cfg.heartbeat_url and not failures:
|
||||
alerts.ping(cfg.heartbeat_url)
|
||||
return int(failures > 0)
|
||||
|
||||
|
||||
def cmd_test_heartbeat(cfg, args) -> int:
|
||||
urls = [(name, url) for name, url in (("heartbeat_url", cfg.heartbeat_url),
|
||||
("backup_heartbeat_url", cfg.backup_heartbeat_url)) if url]
|
||||
if not urls:
|
||||
print("kernelwatch: neither heartbeat_url nor backup_heartbeat_url is configured", file=sys.stderr)
|
||||
return 1
|
||||
failed = 0
|
||||
for name, url in urls:
|
||||
ok = alerts.ping(url)
|
||||
failed += not ok
|
||||
print(f"{name}: {'ok' if ok else 'FAILED'}")
|
||||
return int(failed > 0)
|
||||
|
||||
|
||||
def cmd_test_alert(cfg, args) -> int:
|
||||
@@ -66,6 +84,8 @@ def cmd_scheduled_backup(cfg, args) -> int:
|
||||
print(f"kernelwatch: scheduled backup failed: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
print(f"Backed up to {dest}")
|
||||
if cfg.backup_heartbeat_url:
|
||||
alerts.ping(cfg.backup_heartbeat_url)
|
||||
return 0
|
||||
|
||||
|
||||
@@ -183,6 +203,8 @@ def main(argv: list[str] | None = None) -> int:
|
||||
p = sub.add_parser("backup", help="write a consistent copy of the database to PATH")
|
||||
p.add_argument("path")
|
||||
p.set_defaults(func=cmd_backup)
|
||||
sub.add_parser("test-heartbeat", help="ping the configured heartbeat URLs once and report").set_defaults(
|
||||
func=cmd_test_heartbeat)
|
||||
sub.add_parser("notify-backup-failure", help="send the failed-backup notification "
|
||||
"(run by systemd when kernelwatch-backup.service fails)").set_defaults(
|
||||
func=cmd_notify_backup_failure)
|
||||
|
||||
Reference in new issue
Block a user