diff --git a/scripts/service-health-check.py b/scripts/service-health-check.py index 82b659e..57cd4f2 100644 --- a/scripts/service-health-check.py +++ b/scripts/service-health-check.py @@ -1,14 +1,9 @@ ---- - -# 14. scripts/service-health-check.py - -```python #!/usr/bin/env python3 +import json import socket import time -import json -from datetime import datetime +from datetime import datetime, timezone SERVICES = [ {"name": "Proxmox", "host": "10.10.30.250", "port": 8006}, @@ -18,6 +13,7 @@ SERVICES = [ {"name": "Jellyfin", "host": "10.10.30.143", "port": 30013}, ] + def check_tcp(host, port, timeout=3): start = time.time() @@ -25,33 +21,41 @@ def check_tcp(host, port, timeout=3): with socket.create_connection((host, port), timeout=timeout): latency_ms = round((time.time() - start) * 1000, 2) return True, latency_ms - except Exception: + except OSError: return False, None + +def utc_now(): + return datetime.now(timezone.utc).isoformat() + + def main(): results = [] for service in SERVICES: online, latency = check_tcp(service["host"], service["port"]) - results.append({ - "name": service["name"], - "host": service["host"], - "port": service["port"], - "online": online, - "latency_ms": latency, - "checked_at": datetime.utcnow().isoformat() + "Z" - }) + results.append( + { + "name": service["name"], + "host": service["host"], + "port": service["port"], + "online": online, + "latency_ms": latency, + "checked_at": utc_now(), + } + ) summary = { - "checked_at": datetime.utcnow().isoformat() + "Z", + "checked_at": utc_now(), "total_services": len(results), - "online_services": sum(1 for r in results if r["online"]), - "offline_services": sum(1 for r in results if not r["online"]), - "services": results + "online_services": sum(1 for result in results if result["online"]), + "offline_services": sum(1 for result in results if not result["online"]), + "services": results, } print(json.dumps(summary, indent=2)) + if __name__ == "__main__": main() \ No newline at end of file