Coverage for agentos/marketplace/skills/healthcheck/healthcheck.py: 12%
41 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 23:17 +0800
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 23:17 +0800
1"""
2healthcheck — System health checks.
4Actions: disk, memory, cpu, uptime, processes, all
5"""
7import shutil
8import subprocess
9from typing import Any
12def run(action: str = "all", **kwargs: Any) -> str:
13 results = []
15 if action in ("disk", "all"):
16 usage = shutil.disk_usage("/")
17 pct = usage.used / usage.total * 100
18 results.append(f"Disk: {usage.used//(1024**3)}GB / {usage.total//(1024**3)}GB ({pct:.1f}%)")
20 if action in ("memory", "all"):
21 try:
22 r = subprocess.run(["free", "-h"], capture_output=True, text=True, timeout=5)
23 mem_line = [ln for ln in r.stdout.split("\n") if "Mem:" in ln]
24 if mem_line:
25 results.append(f"Memory: {mem_line[0].split()[1]}/{mem_line[0].split()[2]}")
26 except Exception:
27 results.append("Memory: unavailable")
29 if action in ("cpu", "all"):
30 try:
31 r = subprocess.run(["uptime"], capture_output=True, text=True, timeout=5)
32 load = r.stdout.strip().split("load average:")[-1].strip() if r.stdout else "unknown"
33 results.append(f"CPU Load: {load}")
34 except Exception:
35 results.append("CPU: unavailable")
37 if action in ("uptime", "all"):
38 try:
39 r = subprocess.run(["uptime", "-p"], capture_output=True, text=True, timeout=5)
40 results.append(f"Uptime: {r.stdout.strip()}")
41 except Exception:
42 results.append("Uptime: unavailable")
44 if action in ("processes", "all"):
45 try:
46 r = subprocess.run(
47 ["ps", "aux", "--no-headers"], capture_output=True, text=True, timeout=5
48 )
49 count = len([ln for ln in r.stdout.split("\n") if ln.strip()])
50 results.append(f"Processes: {count}")
51 except Exception:
52 results.append("Processes: unavailable")
54 if action not in ("disk", "memory", "cpu", "uptime", "processes", "all"):
55 return f"[healthcheck] Unknown action: {action}"
57 return "\n".join(results) if results else "[healthcheck] No checks performed."
60__all__ = ["run"]