#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") def run(c: paramiko.SSHClient, cmd: str, timeout: int = 90) -> str: print(f"\n$ {cmd[:160]}", flush=True) _, o, e = c.exec_command(cmd, timeout=timeout) out = (o.read() + e.read()).decode("utf-8", "replace") print(out[-8000:] if len(out) > 8000 else out, flush=True) return out def main() -> int: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, allow_agent=False, look_for_keys=False) sp = "echo '4669805' | sudo -S -p '' " run(c, sp + "free -h") run(c, sp + "grep -E 'MemTotal|MemAvailable|SwapTotal|SwapFree|Committed_AS|CommitLimit' /proc/meminfo") run(c, "curl -sS -m 8 http://127.0.0.1:8014/api/ops/host-metrics || curl -sS -m 8 http://127.0.0.1:8014/api/host/metrics || true") run(c, sp + "docker ps -a --filter name=neko --format 'table {{.Names}}\\t{{.Status}}\\t{{.RunningFor}}'") run(c, sp + "docker stats --no-stream --format 'table {{.Name}}\\t{{.MemUsage}}\\t{{.MemPerc}}\\t{{.CPUPerc}}'") run( c, sp + "docker inspect douyinweb-neko-3 --format 'Status={{.State.Status}} OOM={{.State.OOMKilled}} Exit={{.State.ExitCode}} Restart={{.RestartCount}} Started={{.State.StartedAt}} Error={{.State.Error}}' 2>/dev/null || echo missing-neko-3", ) run(c, sp + "dmesg -T 2>/dev/null | grep -iE 'oom|Out of memory|Killed process' | tail -40 || true") run(c, sp + "journalctl -k --since '3 days ago' 2>/dev/null | grep -iE 'oom|Killed process' | tail -30 || true") run( c, sp + "bash -lc 'CID=$(docker ps -q --filter name=douyinweb-neko-3 | head -1); echo CID=$CID; " "if [ -n \"$CID\" ]; then docker exec $CID ps aux --sort=-%mem 2>/dev/null | head -20; " "docker logs --tail 60 $CID 2>&1 | tail -60; fi'", ) c.close() return 0 if __name__ == "__main__": raise SystemExit(main())