From 1021fdb0e1a5a542a02df2696fb9617e9eb5b9b5 Mon Sep 17 00:00:00 2001 From: Shailaja Date: Mon, 24 Jun 2024 15:39:06 +0530 Subject: [PATCH] disk space calculation using /sys/block/ --- agent.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/agent.py b/agent.py index 4f0e9fc..1f0597c 100644 --- a/agent.py +++ b/agent.py @@ -28,14 +28,26 @@ class ServerData: logging.basicConfig(level=logging.INFO) def get_ram_and_disk(self): - with open('/proc/meminfo', 'r') as f: - meminfo = f.read() - ram = int([x for x in meminfo.split('\n') if 'MemTotal' in x][0].split()[1]) // 1024 - with open('/proc/diskstats', 'r') as f: - diskstats = f.read() - disk = sum(int(x.split()[9]) for x in diskstats.split('\n') if x) * 512 // 10**9 - logging.info(f"RAM: {ram}MB, Disk: {disk}GB") - return ram, disk + # RAM information + with open('/proc/meminfo', 'r') as f: + meminfo = f.read() + ram = int([x for x in meminfo.split('\n') if 'MemTotal' in x][0].split()[1]) // 1024 + + # Disk space information + disk = 0 + for device in os.listdir('/sys/block'): + size_path = f'/sys/block/{device}/size' + if os.path.exists(size_path) and os.access(size_path, os.R_OK): + try: + with open(size_path, 'r') as f: + size = int(f.read().strip()) + disk += size + except Exception: + pass # Skip the device if any exception occurs + + disk = disk * 512 // (1024**3) # convert to GB + logging.info(f"RAM: {ram}MB, Disk: {disk}GB") + return ram, disk def get_cpu_count(self): cpu_count = 0