jarvis-cloud / backend /services /system_monitor.py
Jarvis2345's picture
Squash history — remove all prior commits (secret hygiene, S4)
a31f556
Raw
History Blame
1.68 kB
import asyncio
import psutil
from backend.ws.agent_ws import ws_manager
async def start_system_stats_loop():
while True:
try:
cpu = psutil.cpu_percent(interval=None)
ram = psutil.virtual_memory().percent
disk = psutil.disk_usage('/').percent
# Simple network diff (mocking sparklines requires history, but we can just send current stats and let UI keep history)
net_io = psutil.net_io_counters()
gpu = "N/A"
try:
import pynvml
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
gpu = int((meminfo.used / meminfo.total) * 100)
except Exception as e:
import logging; logging.getLogger(__name__).error(f"Swallowed exception: {e}")
await ws_manager.broadcast({
"event": "system:metrics",
"payload": {
"cpu": cpu,
"ram": ram,
"disk": disk,
"gpu": gpu,
"network": {
"sent": net_io.bytes_sent,
"recv": net_io.bytes_recv
}
}
})
except asyncio.CancelledError:
logging.info("System Monitor Loop cancelled, shutting down cleanly.")
break
except Exception as e:
logging.error(f"System Monitor Loop swallowed exception: {e}")
await asyncio.sleep(1)