60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
import json
|
|
import os
|
|
import platform
|
|
import subprocess
|
|
from collections import Counter
|
|
from datetime import UTC, datetime
|
|
|
|
|
|
def command(*args: str) -> str:
|
|
return subprocess.check_output(args, text=True).strip()
|
|
|
|
|
|
project = os.environ.get("COMPOSE_PROJECT_NAME", "mtusi2026lab5")
|
|
containers = [
|
|
json.loads(line)
|
|
for line in command(
|
|
"docker",
|
|
"ps",
|
|
"--format",
|
|
'{"name":"{{.Names}}","project":"{{.Label "com.docker.compose.project"}}"}',
|
|
).splitlines()
|
|
]
|
|
own = {row["name"] for row in containers if row["project"] == project}
|
|
stats = [
|
|
json.loads(line)
|
|
for line in command("docker", "stats", "--no-stream", "--format", "{{json .}}").splitlines()
|
|
]
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"at": datetime.now(UTC).isoformat(),
|
|
"runtime_sha": command("git", "rev-parse", "HEAD"),
|
|
"platform": platform.system(),
|
|
"architecture": platform.machine(),
|
|
"host_cpus": os.cpu_count(),
|
|
"host_load": os.getloadavg(),
|
|
"docker": command(
|
|
"docker",
|
|
"info",
|
|
"--format",
|
|
"{{.NCPU}} CPUs; {{.MemTotal}} bytes; {{.ServerVersion}}",
|
|
),
|
|
"own_project": project,
|
|
"own": [
|
|
{key: row[key] for key in ("Name", "CPUPerc", "MemUsage", "PIDs")}
|
|
for row in stats
|
|
if row["Name"] in own
|
|
],
|
|
"other_projects": dict(
|
|
Counter(
|
|
row["project"] or "unlabelled" for row in containers if row["name"] not in own
|
|
)
|
|
),
|
|
"sampling": "Shared Docker snapshot; no throughput claim",
|
|
},
|
|
ensure_ascii=False,
|
|
indent=2,
|
|
)
|
|
)
|