78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Convert read-only Docker or Argo CD snapshots to status report rows."""
|
|
|
|
import json
|
|
import sys
|
|
|
|
|
|
def clean(value):
|
|
return str(value).replace("\t", " ").replace("\r", " ").replace("\n", " ")
|
|
|
|
|
|
def emit(status, name, detail):
|
|
print("\t".join(map(clean, (status, name, detail))))
|
|
|
|
|
|
def compose(lines):
|
|
count = 0
|
|
for line in lines:
|
|
if not line.strip():
|
|
continue
|
|
item = json.loads(line)
|
|
labels = dict(part.split("=", 1) for part in item.get("Labels", "").split(",") if "=" in part)
|
|
if labels.get("com.docker.compose.oneoff", "").lower() == "true":
|
|
continue
|
|
project = labels.get("com.docker.compose.project")
|
|
service = labels.get("com.docker.compose.service")
|
|
if not project or not service:
|
|
continue
|
|
state = item.get("State", "unknown").lower()
|
|
description = item.get("Status", state)
|
|
unhealthy = "(unhealthy)" in description.lower() or "(health: starting)" in description.lower()
|
|
status = "ok" if state == "running" and not unhealthy else "warn"
|
|
name = item.get("Names", service)
|
|
emit(status, f"{project}/{service} ({name})", description)
|
|
count += 1
|
|
if not count:
|
|
emit("skip", "Compose services", "No Compose-managed containers found; absent services cannot be assessed")
|
|
|
|
|
|
def applications(document):
|
|
items = document.get("items")
|
|
if not isinstance(items, list):
|
|
raise ValueError("Invalid Kubernetes application list")
|
|
if not items:
|
|
emit("skip", "Argo CD applications", "No Argo CD applications found")
|
|
for app in sorted(items, key=lambda item: item.get("metadata", {}).get("name", "")):
|
|
metadata, status = app.get("metadata", {}), app.get("status", {})
|
|
name = metadata.get("name", "unknown")
|
|
health = status.get("health", {}).get("status", "Unknown")
|
|
sync = status.get("sync", {}).get("status", "Unknown")
|
|
if health in ("Degraded", "Missing"):
|
|
severity = "fail"
|
|
elif health != "Healthy" or sync != "Synced":
|
|
severity = "warn"
|
|
else:
|
|
severity = "ok"
|
|
emit(severity, name, f"health={health}, sync={sync}")
|
|
|
|
|
|
def main():
|
|
try:
|
|
if len(sys.argv) != 2:
|
|
raise ValueError("Expected compose or applications")
|
|
if sys.argv[1] == "compose":
|
|
compose(sys.stdin)
|
|
elif sys.argv[1] == "applications":
|
|
applications(json.load(sys.stdin))
|
|
else:
|
|
raise ValueError("Unknown snapshot type")
|
|
except (ValueError, TypeError, AttributeError) as exc:
|
|
print(f"Cannot interpret status snapshot: {exc}", file=sys.stderr)
|
|
return 1
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|