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

1""" 

2healthcheck — System health checks. 

3 

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

5""" 

6 

7import subprocess 

8import shutil 

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( 

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

20 ) 

21 

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

30 

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

38 

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

45 

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

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