0.1.5: card UPS; backup con marcatore robusto ai riavvii

This commit is contained in:
Carlo Baratto committed 2026-10-31 13:38:40 +01:00
1 parent 29a63a74d4
commit 8ed183c1fc
6 files changed
+186 -18

No files matched your search

+79 -12
View File
@@ -28,6 +28,10 @@ SCHEMA = "status/1"
DEFAULT_OUT = "/run/statusd/status.json"
DEFAULT_PORT = 8792
# Marcatore scritto dal backup (world-readable): unica fonte che sopravvive ai
# riavvii. Creato con una riga di root, vedi README.
BORG_MARKER = "/var/lib/borg-last-run"
# ── helper ────────────────────────────────────────────────────────────────
def run(cmd: list[str], timeout: float = 4.0) -> str:
@@ -161,7 +165,28 @@ def _docker_present() -> bool:
def backups_info() -> dict:
"""Stato dell'ultimo giro borg.timer (system unit), senza privilegi."""
"""Età dell'ultimo giro di backup, da fonti leggibili senza privilegi.
Ordine delle fonti, dalla più affidabile:
1. /var/lib/borg-last-run — marcatore scritto dal backup stesso;
sopravvive ai riavvii (i timestamp delle unit systemd NO: al boot
tornano vuoti, ed è il motivo per cui la card mostrava "—").
2. timestamp dell'unit borg.service — valido solo nello stesso avvio.
3. niente: age_hours null. Meglio un "—" onesto di un numero inventato.
"""
path = Path(BORG_MARKER)
last_run = None
age_h = None
source = None
try:
st = path.stat()
last_run = iso(st.st_mtime)
age_h = round((time.time() - st.st_mtime) / 3600.0, 1)
source = "marker"
except OSError:
pass
out = run(
[
"systemctl",
@@ -182,22 +207,63 @@ def backups_info() -> dict:
info = dict(
line.split("=", 1) for line in out.splitlines() if "=" in line
)
# borg.service è oneshot: quando è "dead" ActiveEnterTimestamp è vuoto e
# l'ultimo giro si legge da InactiveEnterTimestamp (fine esecuzione).
age_h = None
stamp = info.get("ActiveEnterTimestamp") or info.get("InactiveEnterTimestamp", "")
if stamp:
try:
dt = datetime.strptime(stamp, "%a %Y-%m-%d %H:%M:%S %Z")
age_h = round((datetime.now() - dt).total_seconds() / 3600.0, 1)
except ValueError:
pass
if age_h is None:
# borg.service è oneshot: da spento ActiveEnterTimestamp è vuoto e
# l'ultimo giro si legge da InactiveEnterTimestamp (fine esecuzione).
stamp = info.get("ActiveEnterTimestamp") or info.get("InactiveEnterTimestamp", "")
if stamp:
try:
dt = datetime.strptime(stamp, "%a %Y-%m-%d %H:%M:%S %Z")
age_h = round((datetime.now() - dt).total_seconds() / 3600.0, 1)
last_run = stamp
source = "systemd"
except ValueError:
pass
return {
"name": "borg",
"last_run": stamp or None,
"last_run": last_run,
"result": info.get("Result") or None,
"exit_code": int(info["ExecMainStatus"]) if info.get("ExecMainStatus") else None,
"age_hours": age_h,
"source": source,
}
def ups_info() -> dict | None:
"""Stato dell'UPS da NUT (upsc). Nessuna scrittura, nessun privilegio."""
names = run(["upsc", "-l"], timeout=5).split()
if not names:
return None
name = names[0]
raw = run(["upsc", name], timeout=8)
if not raw:
return {"name": name, "available": False}
vals = {}
for line in raw.splitlines():
key, _, value = line.partition(":")
if key and value.strip():
vals[key.strip()] = value.strip()
def num(key, cast=float):
try:
return cast(vals[key])
except (KeyError, ValueError):
return None
return {
"name": name,
"available": True,
"status": vals.get("ups.status"),
"charge_pct": num("battery.charge"),
"runtime_s": num("battery.runtime"),
"load_pct": num("ups.load"),
"input_voltage": num("input.voltage"),
"battery_voltage": num("battery.voltage"),
"input_frequency": num("input.frequency"),
"beeper": vals.get("ups.beeper.status"),
"driver": vals.get("driver.name"),
}
@@ -253,6 +319,7 @@ def collect() -> dict:
("services", services_info),
("containers", containers_info),
("backups", backups_info),
("ups", ups_info),
("access", access_info),
("updates", updates_info),
("net", vpn_info),