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

42 statements  

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

1""" 

2healthcheck — System health checks. 

3 

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

5""" 

6 

7import subprocess 

8import shutil 

9import os 

10from typing import Any 

11 

12 

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

14 results = [] 

15 

16 if action in ("disk", "all"): 

17 usage = shutil.disk_usage("/") 

18 pct = usage.used / usage.total * 100 

19 results.append( 

20 f"Disk: {usage.used//(1024**3)}GB / {usage.total//(1024**3)}GB ({pct:.1f}%)" 

21 ) 

22 

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

24 try: 

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

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

27 if mem_line: 

28 results.append(f"Memory: {mem_line[0].split()[1]}/{mem_line[0].split()[2]}") 

29 except Exception: 

30 results.append("Memory: unavailable") 

31 

32 if action in ("cpu", "all"): 

33 try: 

34 r = subprocess.run(["uptime"], capture_output=True, text=True, timeout=5) 

35 load = r.stdout.strip().split("load average:")[-1].strip() if r.stdout else "unknown" 

36 results.append(f"CPU Load: {load}") 

37 except Exception: 

38 results.append("CPU: unavailable") 

39 

40 if action in ("uptime", "all"): 

41 try: 

42 r = subprocess.run(["uptime", "-p"], capture_output=True, text=True, timeout=5) 

43 results.append(f"Uptime: {r.stdout.strip()}") 

44 except Exception: 

45 results.append("Uptime: unavailable") 

46 

47 if action in ("processes", "all"): 

48 try: 

49 r = subprocess.run(["ps", "aux", "--no-headers"], capture_output=True, text=True, timeout=5) 

50 count = len([l for l in r.stdout.split("\n") if l.strip()]) 

51 results.append(f"Processes: {count}") 

52 except Exception: 

53 results.append("Processes: unavailable") 

54 

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

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

57 

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

59 

60 

61__all__ = ["run"]