Add Docker health collector
This commit is contained in:
@@ -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,
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from collectors.docker import collect as docker_collect
|
||||||
from .common import write_json
|
from .common import write_json
|
||||||
from collectors.backup import collect as backup_collect
|
from collectors.backup import collect as backup_collect
|
||||||
from collectors.git import collect as git_collect
|
from collectors.git import collect as git_collect
|
||||||
@@ -14,6 +14,7 @@ def main():
|
|||||||
"systems": {
|
"systems": {
|
||||||
"backup": backup_collect(),
|
"backup": backup_collect(),
|
||||||
"git": git_collect(),
|
"git": git_collect(),
|
||||||
|
"docker": docker_collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user