#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Probe temperatura/load CPU DS920 — HTTP GET /thermal su :9191. Avvio (sul DS920, utente daniely): nohup python3 ds920_thermal_probe.py >> /tmp/thermal-probe.log 2>&1 & O via Task Scheduler Synology all'avvio. """ from __future__ import annotations import json import os from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer PORT = int(os.environ.get("THERMAL_PROBE_PORT", "9191")) HWMON = os.environ.get("THERMAL_HWMON", "/sys/class/hwmon/hwmon0") def read_metrics() -> dict: temps = [] try: for name in sorted(os.listdir(HWMON)): if name.startswith("temp") and name.endswith("_input"): with open(os.path.join(HWMON, name), encoding="utf-8") as f: temps.append(int(f.read().strip()) / 1000.0) except OSError: pass load1, load5, load15 = os.getloadavg() nproc = os.cpu_count() or 4 return { "ok": True, "host": os.uname().nodename, "cpu_temp_c": max(temps) if temps else None, "temps_c": temps, "load1": load1, "load5": load5, "load15": load15, "nproc": nproc, "cpu_target_load": round(nproc * 0.75, 2), } class Handler(BaseHTTPRequestHandler): def log_message(self, fmt: str, *args) -> None: # noqa: A003 return def do_GET(self) -> None: # noqa: N802 if self.path.split("?")[0] not in ("/", "/thermal", "/health"): self.send_response(404) self.end_headers() return body = json.dumps(read_metrics()).encode() self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def main() -> None: server = ThreadingHTTPServer(("0.0.0.0", PORT), Handler) print(f"thermal probe listening on 0.0.0.0:{PORT}", flush=True) server.serve_forever() if __name__ == "__main__": main()