diff --git a/collectors/docker.py b/collectors/docker.py new file mode 100644 index 0000000..f5cf821 --- /dev/null +++ b/collectors/docker.py @@ -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, + } diff --git a/telemetry/collector.py b/telemetry/collector.py index a510014..fc91114 100755 --- a/telemetry/collector.py +++ b/telemetry/collector.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from pathlib import Path - +from collectors.docker import collect as docker_collect from .common import write_json from collectors.backup import collect as backup_collect from collectors.git import collect as git_collect @@ -14,6 +14,7 @@ def main(): "systems": { "backup": backup_collect(), "git": git_collect(), + "docker": docker_collect(), } }