From f051a6e29defe6113af2cfbc33e80bc6cb25af28 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 | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/agent.py b/agent.py index 52a60f8..1b623ec 100644 --- a/agent.py +++ b/agent.py @@ -92,13 +92,25 @@ class ServerData: return [] def get_ram_and_disk(self): + # 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 - 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") + # Disk space information + disk = 0 + for device in os.listdir('/sys/block'): + device_path = '/sys/block/{}/device'.format(device) + size_path = '/sys/block/{}/size'.format(device) + if os.path.islink(device_path): + 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("RAM: {}MB, Disk: {}GB".format(ram, disk)) return ram, disk def get_cpu_count(self): @@ -112,12 +124,12 @@ class ServerData: with open('/proc/cpuinfo', 'r') as f: cpuinfo = f.read() cpu_count = cpuinfo.count('processor') - logging.info(f"CPU Count: {cpu_count}") + logging.info("CPU Count: {}".format(cpu_count)) return cpu_count def get_bandwidth(self): bandwidth = 2000 - logging.info(f"Bandwidth: {bandwidth}") + logging.info("Bandwidth: {}".format(bandwidth)) return bandwidth def get_public_ip(self): @@ -125,12 +137,12 @@ class ServerData: response = urllib.request.urlopen('https://api.ipify.org') return response.read().decode() except Exception as e: - logging.error(f"Failed to get public IP: {e}") + logging.error("Failed to get public IP: {}".format(e)) return '127.0.0.1' def get_os(self): os_id = 27 - logging.info(f"OS ID: {os_id}") + logging.info("OS ID: {}".format(os_id)) return os_id def create_post_data(self): @@ -184,10 +196,13 @@ class ServerData: for ram in ram_info: size = ram.get('Size', 'Unknown') speed = ram.get('Speed', 'Unknown') - ecc = 'Yes' if ram.get('Total Width') == '72 bits' and ram.get('Data Width') == '64 bits' else 'No' + configured_speed = ram.get('Configured Memory Speed', 'Unknown') + total_width = int(ram.get('Total Width', "0").split()[0]) + data_width = int(ram.get('Data Width', "0").split()[0]) + ecc = 'Yes' if total_width > data_width else 'No' serial_number = ram.get('Serial Number', 'Unknown') ram_type = ram.get('Type', 'Unknown') - ram_details.append("Size: {}, Speed: {}, ECC: {}, Serial Number: {}, Type: {}".format(size, speed, ecc, serial_number, ram_type)) + ram_details.append("Size: {}, Speed: {} @ {}, ECC: {}, Serial Number: {}, Type: {}".format(size, speed, configured_speed, ecc, serial_number, ram_type)) note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {}".format( chassis_model, chassis_serial, processor_model, processor_count, ' | '.join(ram_details)) @@ -220,7 +235,7 @@ class ServerManager: else: return response.read().decode() except urllib.error.HTTPError as e: - logging.error(f"Request failed with {e}") + logging.error("Request failed with {}".format(e)) raise def get_existing_servers(self): @@ -234,7 +249,7 @@ class ServerManager: return self.send_request('POST', '/api/notes', post_data) def update_note(self, post_data, service_id): - logging.info(f"Updating note with id {service_id}...") + logging.info("Updating note with id {}...".format(service_id)) return self.send_request('PUT', '/api/notes/' + str(service_id), post_data) def create_server(self, post_data): @@ -245,7 +260,7 @@ class ServerManager: # remove following keys from post_data for key in NON_UPDATABLE_KEYS: post_data.pop(key, None) - logging.info(f"Updating server with id {server_id}...") + logging.info("Updating server with id {}...".format(server_id)) return self.send_request('PUT', '/api/servers/' + str(server_id), post_data) def existing_server_id(self, post_data):