Add Docker health collector

This commit is contained in:
2026-08-03 16:21:13 -04:00
parent a4f9787e77
commit 757ac99908
2 changed files with 51 additions and 1 deletions
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
import json
import subprocess
def collect():
result = subprocess.run(
["docker", "ps", "-a", "--format", "{{json .}}"],
capture_output=True,
text=True,
)
if result.returncode != 0:
return {
"state": "offline",
"summary": "Docker error",
"detail": result.stderr.strip(),
}
containers = [
json.loads(line)
for line in result.stdout.splitlines()
if line.strip()
]
running = sum(1 for item in containers if item["State"] == "running")
unhealthy = sum(
1 for item in containers
if "unhealthy" in item.get("Status", "").lower()
)
exited = sum(1 for item in containers if item["State"] == "exited")
if unhealthy or exited:
state = "warning"
detail = f"{unhealthy} unhealthy, {exited} exited"
else:
state = "online"
detail = "All containers running"
return {
"state": state,
"summary": f"{running}/{len(containers)} running",
"detail": detail,
"running": running,
"total": len(containers),
"unhealthy": unhealthy,
"exited": exited,
}