0.1.5: card UPS; backup con marcatore robusto ai riavvii
This commit is contained in:
1 parent
29a63a74d4
commit
8ed183c1fc
6 files changed
+186
-18
No files matched your search
+35
-1
@@ -18,6 +18,34 @@ Copia il collector in `~/.local/share/statod/`, genera il token in
|
||||
`~/.config/statusd/token` (0600) e attiva le due unit utente. La sopravvivenza
|
||||
al logout richiede `loginctl enable-linger <utente>`.
|
||||
|
||||
## Marcatore dell'ultimo backup (una volta, richiede root)
|
||||
|
||||
I timestamp delle unit systemd si **azzerano a ogni riavvio**, `/var/log/borg`
|
||||
è `borg:borg 750` e il journal non è leggibile senza i gruppi `adm` o
|
||||
`systemd-journal`: senza un marcatore, dopo un reboot l'età del backup è
|
||||
semplicemente ignota. Il drop-in crea `/var/lib/borg-last-run` (leggibile da
|
||||
tutti) al termine di ogni giro:
|
||||
|
||||
```sh
|
||||
sudo mkdir -p /etc/systemd/system/borg.service.d
|
||||
sudo tee /etc/systemd/system/borg.service.d/10-lastrun.conf >/dev/null <<'EOF'
|
||||
[Service]
|
||||
ExecStartPost=/bin/sh -c 'date -Is > /var/lib/borg-last-run'
|
||||
EOF
|
||||
sudo systemctl daemon-reload
|
||||
```
|
||||
|
||||
Finché il marcatore non esiste, `backups.age_hours` resta `null` e l'app mostra
|
||||
"—" con la nota "ora del giro non registrata": è voluto, meglio di un numero
|
||||
inventato.
|
||||
|
||||
## UPS (NUT)
|
||||
|
||||
Lo stato arriva da `upsc -l` + `upsc <nome>`: nessuna configurazione da fare
|
||||
nell'app finché NUT è attivo (`nut-server`, `nut-monitor`). Se `upsc -l` non
|
||||
elenca dispositivi, la sezione `ups` è `null` e la card mostra "nessun UPS
|
||||
rilevato".
|
||||
|
||||
## Esposizione
|
||||
|
||||
`statusd-http.service` ascolta su `0.0.0.0:8792`. Il firewall YunoHost ha
|
||||
@@ -57,7 +85,13 @@ Nessun log HTTP: il token non finisce su disco. Il server non ha altre rotte.
|
||||
"containers": { "running": [ { "name": "…", "status": "Up 2 days", "ok": true } ],
|
||||
"available": true },
|
||||
"backups": { "name": "borg", "last_run": "…", "result": "success",
|
||||
"exit_code": 0, "age_hours": 41.5 },
|
||||
"exit_code": 0, "age_hours": 41.5,
|
||||
"source": "marker" },
|
||||
"ups": { "name": "ups", "available": true, "status": "OL",
|
||||
"charge_pct": 100.0, "runtime_s": 3000.0, "load_pct": 2.0,
|
||||
"input_voltage": 246.5, "battery_voltage": 13.4,
|
||||
"input_frequency": 50.1, "beeper": "enabled",
|
||||
"driver": "nutdrv_qx" },
|
||||
"services": { "failed": [ { "unit": "…", "description": "…" } ] },
|
||||
"access": { "ssh_failed_24h": 0 },
|
||||
"updates": { "total": 0, "security": 0, "packages": [],
|
||||
|
||||
+79
-12
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user