Files
buryan-infra/ops/audit/audit360_max_packet.py
T
architect 768ba209f6
lint / hygiene (push) Failing after 19s
lint / hygiene (pull_request) Failing after 23s
standard / check (push) Successful in 10s
standard / check (pull_request) Successful in 10s
[ARCH] Audit 360 service, collectors, snapshots and sprint tool
2026-11-26 21:45:23 +03:00

146 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Maximal Buryans Hub audit packet: live + stopped + Gitea compose + automations."""
import json
import os
import subprocess
import sys
from datetime import datetime, timezone
from pathlib import Path
sys.path.insert(0, "/root/buryan-infra/ops/audit")
import audit360_full_packet as base
try:
import yaml
except Exception:
yaml = None
REPO = Path("/root/buryan-infra")
OUT = REPO / "ops/audit/AUDIT-360-MAX.json"
def sh(cmd, timeout=25):
try:
return subprocess.run(cmd, shell=True, text=True, capture_output=True, timeout=timeout).stdout.strip()
except Exception:
return ""
def all_containers():
out = []
for ct in base.cts():
if ct.get("status") != "running":
continue
raw = sh("pct exec %s -- docker ps -a --format '{{json .}}'" % ct["vmid"], timeout=15)
for ln in raw.splitlines():
try:
d = json.loads(ln)
except Exception:
continue
out.append({"ct": ct["vmid"], "ct_name": ct["name"], "name": d.get("Names"),
"image": d.get("Image"), "state": d.get("State"),
"status": d.get("Status"), "ports": d.get("Ports")})
return out
def compose_inventory():
if yaml is None:
return {"error": "pyyaml missing", "files": [], "services": []}
files, services = [], []
for p in sorted((REPO / "deploy").rglob("*compose*.yml")):
rel = str(p.relative_to(REPO))
try:
data = yaml.safe_load(p.read_text(encoding="utf-8")) or {}
except Exception as e:
files.append({"file": rel, "error": str(e)[:200]})
continue
svcs = data.get("services") or {}
files.append({"file": rel, "services": len(svcs)})
for name, spec in svcs.items():
spec = spec or {}
services.append({
"file": rel, "service": name,
"image": spec.get("image"), "container_name": spec.get("container_name"),
"ports": spec.get("ports"), "depends_on": spec.get("depends_on"),
"networks": spec.get("networks"),
})
return {"files": files, "services": services}
def automations():
p = REPO / "ops/automations/registry.yaml"
if not p.exists() or yaml is None:
return {"count": 0, "items": []}
try:
data = yaml.safe_load(p.read_text(encoding="utf-8")) or {}
except Exception as e:
return {"error": str(e)[:200], "count": 0, "items": []}
items = data if isinstance(data, list) else (data.get("items") or data.get("automations") or data)
if isinstance(items, dict):
items = list(items.values())
return {"count": len(items) if isinstance(items, list) else 0, "items": items}
def repo_meta():
files = [x for x in sh("git -C %s ls-files" % REPO).splitlines() if x]
return {
"head": sh("git -C %s rev-parse --short HEAD" % REPO),
"branch": sh("git -C %s branch --show-current" % REPO),
"remote": sh("git -C %s remote get-url origin" % REPO),
"dirty": [x for x in sh("git -C %s status --short" % REPO).splitlines() if x],
"tracked_files": len(files),
"files": files,
}
def main():
a = base.audit_rows()
running = base.containers()
allc = all_containers()
minic = []
for c in allc:
minic.append({"ct": c["ct"], "name": c["name"], "state": c["state"], "ports": c["ports"]})
packet = {
"schema": "audit360/max/v1",
"generated_at": datetime.now(timezone.utc).isoformat(),
"host": {"name": "pve", "ip": "192.168.1.10", "audit_run_id": a["run_id"]},
"gitea": repo_meta(),
"cts": base.cts(),
"vms": base.vms(),
"containers_running": running,
"containers_all": minic,
"compose": compose_inventory(),
"automations": automations(),
"systemd": base.systemd_services(),
"nginx_domains": base.domains(),
"routes": base.routes(),
"owners": base.owners(),
"integrations": a["links"],
"kuma": base.kuma(),
"databases": base.db_rows(),
"backups": base.backup_files(),
"audit": a,
"arena_items": base.arena_items(a["run_id"]),
}
packet["summary"] = {
"cts": len(packet["cts"]), "vms": len(packet["vms"]),
"containers_running": len(packet["containers_running"]),
"containers_all": len(packet["containers_all"]),
"compose_services": len(packet["compose"].get("services", [])),
"automations": packet["automations"].get("count", 0),
"systemd": len(packet["systemd"]), "domains": len(packet["nginx_domains"]),
"routes": len(packet["routes"]), "kuma": len(packet["kuma"]),
"databases": len(packet["databases"]), "owners": len(packet["owners"]),
"integrations": len(packet["integrations"]), "gitea_files": len(packet["gitea"]["files"]),
"findings": len(a["findings"]), "capabilities": len(a["capabilities"]),
"decisions": len(a["decisions"]), "experiments": len(a["experiments"]),
"arena_items": len(packet["arena_items"]),
}
OUT.write_text(json.dumps(packet, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(packet["summary"], ensure_ascii=False))
print("bytes", OUT.stat().st_size)
if __name__ == "__main__":
main()