Coverage for agentos/marketplace/skills/healthcheck/healthcheck.py: 78%
41 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 10:59 +0800
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 10:59 +0800
1"""
2healthcheck — System health checks.
4Actions: disk, memory, cpu, uptime, processes, all
5"""
7import subprocess
8import shutil
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(
19 f"Disk: {usage.used//(1024**3)}GB / {usage.total//(1024**3)}GB ({pct:.1f}%)"
20 )
22 if action in ("memory", "all"):
23 try:
24 r = subprocess.run(["free", "-h"], capture_output=True, text=True, timeout=5)
25 mem_line = [l for l in r.stdout.split("\n") if "Mem:" in l]
26 if mem_line:
27 results.append(f"Memory: {mem_line[0].split()[1]}/{mem_line[0].split()[2]}")
28 except Exception:
29 results.append("Memory: unavailable")
31 if action in ("cpu", "all"):
32 try:
33 r = subprocess.run(["uptime"], capture_output=True, text=True, timeout=5)
34 load = r.stdout.strip().split("load average:")[-1].strip() if r.stdout else "unknown"
35 results.append(f"CPU Load: {load}")
36 except Exception:
37 results.append("CPU: unavailable")
39 if action in ("uptime", "all"):
40 try:
41 r = subprocess.run(["uptime", "-p"], capture_output=True, text=True, timeout=5)
42 results.append(f"Uptime: {r.stdout.strip()}")
43 except Exception:
44 results.append("Uptime: unavailable")
46 if action in ("processes", "all"):
47 try:
48 r = subprocess.run(["ps", "aux", "--no-headers"], capture_output=True, text=True, timeout=5)
49 count = len([l for l in r.stdout.split("\n") if l.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"]