50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
#!/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,
|
|
}
|