Coverage for agentos/marketplace/skills/healthcheck/healthcheck.py: 78%

41 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-07-06 12:29 +0800

1""" 

2healthcheck — System health checks. 

3 

4Actions: disk, memory, cpu, uptime, processes, all 

5""" 

6 

7import shutil 

8import subprocess 

9from typing import Any 

10 

11 

12def run(action: str = "all", **kwargs: Any) -> str: 

13 results = [] 

14 

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}%)") 

19 

20 if action in ("memory", "all"): 

21 try: 

22 r = subprocess.run(["free", "-h"], capture_output=True, text=True, timeout=5) 

23 mem_line = [l for l in r.stdout.split("\n") if "Mem:" in l] 

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") 

28 

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") 

36 

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") 

43 

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([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") 

53 

54 if action not in ("disk", "memory", "cpu", "uptime", "processes", "all"): 

55 return f"[healthcheck] Unknown action: {action}" 

56 

57 return "\n".join(results) if results else "[healthcheck] No checks performed." 

58 

59 

60__all__ = ["run"]