From 2c19f2eefc0b2d83ecae3ed6f3591c40318361b3 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Mon, 1 Jul 2024 10:59:13 +0530 Subject: [PATCH] Added nvme details --- agent.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/agent.py b/agent.py index 4c6a7fb..3f98381 100644 --- a/agent.py +++ b/agent.py @@ -30,6 +30,7 @@ class ServerData: self.public_ip = self.get_public_ip() self.dmidecode_data = self.parse_dmidecode_output() self.hdparm_data = self.parse_hdparm_output() + self.nvme_data = self.parse_nvme_devices() logging.basicConfig(level=logging.INFO) def parse_dmidecode_output(self): @@ -103,8 +104,8 @@ class ServerData: # Get the list of devices try: - device_list = subprocess.check_output(['lsblk', '-d', '-o', 'NAME'], stderr=subprocess.STDOUT, universal_newlines=True).split()[1:] - except subprocess.CalledProcessError as e: + device_list = [device for device in os.listdir('/sys/block') if device.startswith('sd')] + except OSError as e: logging.error("Failed to get device list: {}".format(e)) return devices @@ -135,6 +136,53 @@ class ServerData: return devices + def parse_nvme_devices(self): + ''' + Example nvme id-ctrl output: + + NVME Identify Controller: + vid : 0x144d + ssvid : 0x144d + sn : S3RVNA0K502408F + mn : Samsung SSD 970 EVO Plus 1TB + fr : 2B2QEXM7 + rab : 2 + .... + ''' + devices = {} + + # Check if nvme exists + if shutil.which('nvme') is None: + print("nvme not found") + return devices + + # Get the list of nvme devices starting with 'nvme' + nvme_devices = [device for device in os.listdir('/dev') if device.startswith('nvme')] + + for device in nvme_devices: + device_path = '/dev/' + device + + try: + # Get the output of nvme id-ctrl command + output = subprocess.check_output(['nvme', 'id-ctrl', device_path], stderr=subprocess.STDOUT, universal_newlines=True) + except subprocess.CalledProcessError as e: + print(f"Failed to get nvme id-ctrl output for {device_path}: {e}") + continue + + # Split the output into lines + lines = output.split('\n') + + # Parse each line + device_info = {} + for line in lines: + if ':' in line: + key, value = line.split(':', 1) + device_info[key.strip()] = value.strip() + + devices[device_path] = device_info + + return devices + def get_ram_and_disk(self): # RAM information with open('/proc/meminfo', 'r') as f: @@ -254,9 +302,15 @@ class ServerData: for device, details in self.hdparm_data.items(): sata_details.append("Device: {}, Model: {}, Serial: {}, Checksum: {}, Buffer Size: {}, Form Factor: {}".format( device, details['model_number'], details['serial_number'], details['checksum'], details['buffer_size'], details['form_factor'])) + + # NVMe Storage media + nvme_details = [] + for device, details in self.nvme_data.items(): + nvme_details.append("Device: {}, Model: {}, Serial: {}, Firmware: {}".format( + device, details.get('mn', 'Unknown'), details.get('sn', 'Unknown'), details.get('fr', 'Unknown'))) - note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {} ||| SATA Details {}".format( - chassis_model, chassis_serial, processor_model, processor_count, ' | '.join(ram_details), ' | '.join(sata_details)) + note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {} ||| SATA Details {} ||| NVME Details {}".format( + chassis_model, chassis_serial, processor_model, processor_count, ' | '.join(ram_details), ' | '.join(sata_details), ' | '.join(nvme_details)) note_data = { 'note': note,