62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import urllib.request
|
|
import http.cookiejar
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
REMOTE = r"""#!/bin/bash
|
|
echo '=== mem ==='
|
|
free -h
|
|
grep -E 'MemTotal|MemAvailable|SwapTotal|SwapFree' /proc/meminfo
|
|
echo '=== virtio ports ==='
|
|
ls -la /dev/virtio-ports 2>/dev/null || echo 'no virtio-ports'
|
|
ls -la /dev/vport* 2>/dev/null || true
|
|
echo '=== ga status ==='
|
|
systemctl status qemu-guest-agent --no-pager -l 2>&1 | head -40
|
|
journalctl -u qemu-guest-agent -n 30 --no-pager 2>&1 | tail -40
|
|
echo '=== try start ==='
|
|
systemctl start qemu-guest-agent 2>&1 || true
|
|
sleep 1
|
|
systemctl is-active qemu-guest-agent || true
|
|
systemctl status qemu-guest-agent --no-pager -l 2>&1 | head -25
|
|
"""
|
|
|
|
|
|
def main() -> int:
|
|
# public metrics
|
|
for base in ("https://dy.nomadro.com", "https://dy.nomadro.cn"):
|
|
cj = http.cookiejar.CookieJar()
|
|
op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
|
req = urllib.request.Request(
|
|
base + "/api/auth/login",
|
|
data=json.dumps({"username": "hackrobot", "password": "Xiao4669805@"}).encode(),
|
|
headers={"Content-Type": "application/json"},
|
|
method="POST",
|
|
)
|
|
op.open(req, timeout=25)
|
|
raw = op.open(base + "/api/ops/host-metrics", timeout=25).read()
|
|
print(base, raw.decode()[:220], flush=True)
|
|
|
|
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)
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/tmp/_ga_diag.sh", "w") as f:
|
|
f.write(REMOTE)
|
|
sftp.close()
|
|
_, o, e = c.exec_command("echo '4669805' | sudo -S -p '' bash /tmp/_ga_diag.sh", timeout=60)
|
|
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
|
|
c.close()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|