mirror of
https://github.com/ced4568/ceds-observability-stack.git
synced 2026-08-12 22:34:03 +00:00
Update service-health-check.py
This commit is contained in:
@@ -1,14 +1,9 @@
|
|||||||
---
|
|
||||||
|
|
||||||
# 14. scripts/service-health-check.py
|
|
||||||
|
|
||||||
```python
|
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import json
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
import json
|
from datetime import datetime, timezone
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
SERVICES = [
|
SERVICES = [
|
||||||
{"name": "Proxmox", "host": "10.10.30.250", "port": 8006},
|
{"name": "Proxmox", "host": "10.10.30.250", "port": 8006},
|
||||||
@@ -18,6 +13,7 @@ SERVICES = [
|
|||||||
{"name": "Jellyfin", "host": "10.10.30.143", "port": 30013},
|
{"name": "Jellyfin", "host": "10.10.30.143", "port": 30013},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def check_tcp(host, port, timeout=3):
|
def check_tcp(host, port, timeout=3):
|
||||||
start = time.time()
|
start = time.time()
|
||||||
|
|
||||||
@@ -25,33 +21,41 @@ def check_tcp(host, port, timeout=3):
|
|||||||
with socket.create_connection((host, port), timeout=timeout):
|
with socket.create_connection((host, port), timeout=timeout):
|
||||||
latency_ms = round((time.time() - start) * 1000, 2)
|
latency_ms = round((time.time() - start) * 1000, 2)
|
||||||
return True, latency_ms
|
return True, latency_ms
|
||||||
except Exception:
|
except OSError:
|
||||||
return False, None
|
return False, None
|
||||||
|
|
||||||
|
|
||||||
|
def utc_now():
|
||||||
|
return datetime.now(timezone.utc).isoformat()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
for service in SERVICES:
|
for service in SERVICES:
|
||||||
online, latency = check_tcp(service["host"], service["port"])
|
online, latency = check_tcp(service["host"], service["port"])
|
||||||
|
|
||||||
results.append({
|
results.append(
|
||||||
|
{
|
||||||
"name": service["name"],
|
"name": service["name"],
|
||||||
"host": service["host"],
|
"host": service["host"],
|
||||||
"port": service["port"],
|
"port": service["port"],
|
||||||
"online": online,
|
"online": online,
|
||||||
"latency_ms": latency,
|
"latency_ms": latency,
|
||||||
"checked_at": datetime.utcnow().isoformat() + "Z"
|
"checked_at": utc_now(),
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
summary = {
|
summary = {
|
||||||
"checked_at": datetime.utcnow().isoformat() + "Z",
|
"checked_at": utc_now(),
|
||||||
"total_services": len(results),
|
"total_services": len(results),
|
||||||
"online_services": sum(1 for r in results if r["online"]),
|
"online_services": sum(1 for result in results if result["online"]),
|
||||||
"offline_services": sum(1 for r in results if not r["online"]),
|
"offline_services": sum(1 for result in results if not result["online"]),
|
||||||
"services": results
|
"services": results,
|
||||||
}
|
}
|
||||||
|
|
||||||
print(json.dumps(summary, indent=2))
|
print(json.dumps(summary, indent=2))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user