Add RAM usage in GB and swap metrics to agent, server, UI and CSV export

This commit is contained in:
2026-07-17 17:10:47 +07:00
parent 513b8ede84
commit 76f74201e5
12 changed files with 272 additions and 35 deletions
+46 -9
View File
@@ -147,15 +147,31 @@ def get_load_avg():
return None
def get_top_processes(limit=10):
def get_top_processes(limit=10, processes=None):
"""Return top processes by CPU usage.
If ``processes`` is provided, it should be a list of psutil.Process objects
whose CPU baseline has already been established (cpu_percent called once).
The actual CPU usage is measured with the next call to cpu_percent().
"""
if processes is None:
processes = []
for p in psutil.process_iter(["pid", "name"]):
try:
p.cpu_percent(interval=None)
processes.append(p)
except Exception:
continue
time.sleep(1)
procs = []
for p in psutil.process_iter(["pid", "name", "cpu_percent", "memory_percent"]):
for p in processes:
try:
procs.append({
"pid": p.info["pid"],
"name": p.info["name"],
"cpu_percent": round(p.info["cpu_percent"] or 0, 1),
"ram_percent": round((p.info["memory_percent"] or 0), 1),
"pid": p.pid,
"name": p.name(),
"cpu_percent": round(p.cpu_percent(interval=None), 1),
"ram_percent": round(p.memory_percent(), 1),
})
except Exception:
continue
@@ -166,6 +182,21 @@ def get_top_processes(limit=10):
def collect():
cfg = load_config()
sessions = get_active_sessions()
# Establish per-process CPU baseline first. The 1-second interval below for
# system CPU also gives us meaningful per-process CPU percentages without
# an extra delay.
baseline_procs = []
for p in psutil.process_iter(["pid", "name"]):
try:
p.cpu_percent(interval=None)
baseline_procs.append(p)
except Exception:
continue
vm = psutil.virtual_memory()
sm = psutil.swap_memory()
payload = {
"hostname": socket.gethostname(),
"username": get_display_username(sessions),
@@ -175,12 +206,18 @@ def collect():
"public_ip": get_public_ip() if cfg.get("send_public_ip") else None,
"uptime_sec": get_uptime(),
"cpu_percent": psutil.cpu_percent(interval=1),
"ram_percent": psutil.virtual_memory().percent,
"ram_total_gb": round(psutil.virtual_memory().total / (2**30), 1),
"ram_percent": vm.percent,
"ram_total_gb": round(vm.total / (2**30), 1),
"ram_used_gb": round(vm.used / (2**30), 1),
"ram_available_gb": round(vm.available / (2**30), 1),
"swap_total_gb": round(sm.total / (2**30), 1),
"swap_used_gb": round(sm.used / (2**30), 1),
"swap_free_gb": round(sm.free / (2**30), 1),
"swap_percent": sm.percent,
"disk_info": get_disk_info(),
"cpu_temp": get_cpu_temp(),
"load_avg": get_load_avg(),
"processes": get_top_processes(10),
"processes": get_top_processes(10, baseline_procs),
}
return payload, cfg