52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import shutil
|
|
|
|
|
|
def collect():
|
|
load_1, _, _ = os.getloadavg()
|
|
|
|
memory_total = 0
|
|
memory_available = 0
|
|
|
|
with open("/proc/meminfo", "r", encoding="utf-8") as handle:
|
|
for line in handle:
|
|
key, value = line.split(":", 1)
|
|
kb = int(value.strip().split()[0])
|
|
|
|
if key == "MemTotal":
|
|
memory_total = kb
|
|
elif key == "MemAvailable":
|
|
memory_available = kb
|
|
|
|
memory_used_pct = round(
|
|
(1 - memory_available / memory_total) * 100
|
|
) if memory_total else 0
|
|
|
|
disk = shutil.disk_usage("/")
|
|
disk_used_pct = round(disk.used / disk.total * 100)
|
|
|
|
with open("/proc/uptime", "r", encoding="utf-8") as handle:
|
|
uptime_seconds = int(float(handle.read().split()[0]))
|
|
|
|
uptime_days = uptime_seconds // 86400
|
|
|
|
state = "online"
|
|
|
|
if memory_used_pct >= 85 or disk_used_pct >= 85:
|
|
state = "warning"
|
|
|
|
if memory_used_pct >= 95 or disk_used_pct >= 95:
|
|
state = "offline"
|
|
|
|
return {
|
|
"state": state,
|
|
"summary": f"{memory_used_pct}% memory",
|
|
"detail": f"{disk_used_pct}% disk · {uptime_days} days uptime",
|
|
"load_1": round(load_1, 2),
|
|
"memory_used_pct": memory_used_pct,
|
|
"disk_used_pct": disk_used_pct,
|
|
"uptime_days": uptime_days,
|
|
}
|