forked from mxx1111/Homelab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost.py
More file actions
82 lines (71 loc) · 2.41 KB
/
Copy pathhost.py
File metadata and controls
82 lines (71 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""本机 CPU / 内存 / 负载 / 温度 / 运行时长"""
import os
import time
_prev_cpu = None
def _read_cpu_total():
with open("/proc/stat", encoding="utf-8") as fp:
parts = fp.readline().split()
values = [int(x) for x in parts[1:]]
idle = values[3] + (values[4] if len(values) > 4 else 0)
return sum(values), idle
def _cpu_percent():
global _prev_cpu
total, idle = _read_cpu_total()
if _prev_cpu is None:
_prev_cpu = (total, idle)
return None
d_total = total - _prev_cpu[0]
d_idle = idle - _prev_cpu[1]
_prev_cpu = (total, idle)
if d_total <= 0:
return None
return round((1 - d_idle / d_total) * 100, 1)
def _meminfo():
info = {}
with open("/proc/meminfo", encoding="utf-8") as fp:
for line in fp:
key, _, rest = line.partition(":")
info[key] = int(rest.split()[0]) * 1024
total = info.get("MemTotal", 0)
available = info.get("MemAvailable", 0)
return {
"total": total,
"available": available,
"used": total - available,
"percent": round((total - available) / total * 100, 1) if total else 0,
}
def _temperature():
"""取所有热区最高值,读不到返回 None"""
best = None
base = "/sys/class/thermal"
if not os.path.isdir(base):
return None
for name in os.listdir(base):
if not name.startswith("thermal_zone"):
continue
try:
with open(f"{base}/{name}/temp", encoding="utf-8") as fp:
value = int(fp.read().strip()) / 1000
if 0 < value < 150 and (best is None or value > best):
best = value
except (OSError, ValueError):
continue
return round(best, 1) if best is not None else None
def collect(_cfg):
try:
with open("/proc/loadavg", encoding="utf-8") as fp:
load = [float(x) for x in fp.read().split()[:3]]
with open("/proc/uptime", encoding="utf-8") as fp:
uptime = float(fp.read().split()[0])
return {
"ok": True,
"cpu_percent": _cpu_percent(),
"cpu_cores": os.cpu_count(),
"memory": _meminfo(),
"load": load,
"uptime_seconds": int(uptime),
"temperature": _temperature(),
"ts": time.time(),
}
except Exception as exc: # noqa: BLE001
return {"ok": False, "error": str(exc)}