Atlas - collector.py

Home / usr / net / PulseWatch Lines: 1 | Size: 1594 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1import socket 2import time 3 4def _read_kv(path): 5 out = {} 6 with open(path) as f: 7 for line in f: 8 k, *v = line.split() 9 out[k.rstrip(":")] = int(v[0]) 10 return out 11 12def cpu_usage(): 13 with open("/proc/stat") as f: 14 cpu = list(map(int, f.readline().split()[1:])) 15 16 idle = cpu[3] + cpu[4] 17 total = sum(cpu) 18 return idle, total 19 20_prev_idle, _prev_total = cpu_usage() 21 22def cpu_percent(): 23 global _prev_idle, _prev_total 24 idle, total = cpu_usage() 25 26 didle = idle - _prev_idle 27 dtotal = total - _prev_total 28 29 _prev_idle, _prev_total = idle, total 30 return round(100 * (1 - didle / dtotal), 2) 31 32def mem_info(): 33 m = _read_kv("/proc/meminfo") 34 used = m["MemTotal"] - m["MemAvailable"] 35 return { 36 "total_kb": m["MemTotal"], 37 "used_kb": used, 38 "free_kb": m["MemAvailable"], 39 } 40 41def net_bytes(): 42 rx = tx = 0 43 with open("/proc/net/dev") as f: 44 for line in f.readlines()[2:]: 45 data = line.split() 46 rx += int(data[1]) 47 tx += int(data[9]) 48 return rx, tx 49 50def uptime(): 51 with open("/proc/uptime") as f: 52 return int(float(f.readline().split()[0])) 53 54def collect(): 55 rx, tx = net_bytes() 56 return { 57 "hostname": socket.gethostname(), 58 "timestamp": int(time.time()), 59 "status": "OK", 60 "faults": 0, 61 "cpu_percent": cpu_percent(), 62 "loadavg": open("/proc/loadavg").read().strip(), 63 "memory": mem_info(), 64 "uptime_sec": uptime(), 65 "net_rx_bytes": rx, 66 "net_tx_bytes": tx, 67 } 68
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.