import os, subprocess, sys, time
def run(cmd, t=25):
    try:
        p=subprocess.run(cmd, shell=True, capture_output=True, timeout=t)
        return (p.stdout.decode('utf-8','replace') + p.stderr.decode('utf-8','replace'))
    except Exception as e:
        return "ERR:"+str(e)
def sec(title):
    print("\n===== "+title+" =====", flush=True)
print("=== SERVER_START "+time.strftime("%H:%M:%S")+" ===", flush=True)
sec("ID"); print(run("id; hostname; uname -a; cat /proc/1/cmdline 2>&1 | tr '\\0' ' '; echo"), flush=True)
sec("MOUNTINFO_HOST"); print(run("grep -E ' /host |docker.sock' /proc/self/mountinfo"), flush=True)
sec("LS_HOST"); print(run("ls -la /host 2>&1 | head -40"), flush=True)
sec("HOST_OS"); print(run("cat /host/etc/hostname 2>&1; echo ---; cat /host/etc/os-release 2>&1 | head -6"), flush=True)
sec("HOST_PROC_COUNT"); print(run("ls /host/proc 2>/dev/null | grep -E '^[0-9]+$' | wc -l; echo '--- p1'; tr '\\0' ' ' < /host/proc/1/cmdline 2>&1; echo"), flush=True)
sec("KUBELET"); print(run("ls -la /host/var/lib/kubelet 2>&1 | head -20; echo '--- pods'; ls -la /host/var/lib/kubelet/pods 2>&1 | head -20"), flush=True)
sec("K8S_ETC"); print(run("ls -la /host/etc/kubernetes 2>&1 | head; ls -la /host/etc/kubernetes/manifests 2>&1 | head -20; ls -la /host/var/run/secrets 2>&1"), flush=True)
sec("DOCKERDATA"); print(run("ls -la /host/var/lib/docker 2>&1 | head; ls -la /host/data 2>&1 | head; ls -la /host/docker 2>&1 | head; ls -la /host/var/lib/containerd 2>&1 | head"), flush=True)
sec("DOCKERSOCK"); print(run("ls -la /var/run/docker.sock 2>&1; ls -la /host/var/run/docker.sock 2>&1; docker ps -a 2>&1 | head -20"), flush=True)
sec("DONE"); print("KEEPALIVE", flush=True)
# keep alive
while True: time.sleep(3600)
