Add ntfy alerts for stale hosts and reporting recovery
CI / tests (Python 3.10) (pull_request) Successful in 19s
CI / tests (Python 3.11) (pull_request) Successful in 17s
CI / tests (Python 3.12) (pull_request) Successful in 17s
CI / tests (Python 3.13) (pull_request) Successful in 18s
CI / lint (pull_request) Successful in 3s

This commit is contained in:
patrick committed 2026-10-24 15:15:48 -04:00
1 parent bd0db302b0
commit 47f93968e9
11 files changed
+631 -4

No files matched your search

+25 -1
View File
@@ -3,10 +3,12 @@
from __future__ import annotations
import dataclasses
import math
import os
import re
from dataclasses import dataclass
from typing import Any
from urllib.parse import urlsplit
try:
import tomllib
@@ -35,6 +37,11 @@ class Config:
# Honour X-Forwarded-For (for login rate limiting). Enable only behind a proxy.
trust_proxy_headers: bool = False
# Full ntfy topic URL; empty disables stale/recovery notifications.
ntfy_url: str = ""
ntfy_token: str = ""
ntfy_timeout_seconds: float = 10.0
def load(path: str | None = None) -> Config:
path = path or os.environ.get("KERNELWATCH_CONFIG") or DEFAULT_PATH
@@ -60,7 +67,24 @@ def load(path: str | None = None) -> Config:
if not isinstance(value, expected) or (expected is int and isinstance(value, bool)):
raise SystemExit(f"{path}: {name} must be {expected.__name__}")
values[name] = value
return Config(**values)
cfg = Config(**values)
if not math.isfinite(cfg.stale_after_hours) or cfg.stale_after_hours <= 0:
raise SystemExit(f"{path}: stale_after_hours must be positive and finite")
if not math.isfinite(cfg.ntfy_timeout_seconds) or cfg.ntfy_timeout_seconds <= 0:
raise SystemExit(f"{path}: ntfy_timeout_seconds must be positive and finite")
if any(ord(c) < 32 or ord(c) == 127 for c in cfg.ntfy_url + cfg.ntfy_token):
raise SystemExit(f"{path}: ntfy settings must not contain control characters")
if cfg.ntfy_url:
try:
url = urlsplit(cfg.ntfy_url)
valid = (url.scheme in ("http", "https") and url.hostname and url.port != 0
and url.path.strip("/") and not url.username and not url.password
and not url.query and not url.fragment and not any(c.isspace() for c in cfg.ntfy_url))
except ValueError:
valid = False
if not valid:
raise SystemExit(f"{path}: ntfy_url must be an HTTP(S) topic URL without credentials, query or fragment")
return cfg
_KEY_VALUE = re.compile(r"^([A-Za-z0-9_-]+)\s*=\s*(.*)$")