From 064d7816071695f08b2b8fe5e43dbedd7963e301 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Tue, 25 Jun 2024 13:25:53 +0530 Subject: [PATCH 1/6] Create function to create note data --- agent.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/agent.py b/agent.py index 978ec7a..5aa776a 100644 --- a/agent.py +++ b/agent.py @@ -161,6 +161,13 @@ class ServerData: logging.info("Post data created") return post_data + def create_note_data(self, server_id): + note_data = { + 'service_id': server_id, + 'note': 'Bla bla bla' + } + return note_data + class ServerManager: def __init__(self, host, api_key): @@ -249,10 +256,7 @@ def main(): logging.info('Server does not exist, Creating...') logging.info(server_manager.create_server(post_data)) - note_data = { - 'service_id': server_id, - 'note': 'Bla bla bla' - } + note_data = server_data.create_note_data(server_id) try: note = server_manager.get_note(server_id) except urllib.error.HTTPError: -- 2.45.1 From fa67103b2023128bfcd5c954828045babae54629 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Tue, 25 Jun 2024 14:52:36 +0530 Subject: [PATCH 2/6] Added chachis and processor info in note --- agent.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/agent.py b/agent.py index 5aa776a..ceadc58 100644 --- a/agent.py +++ b/agent.py @@ -162,9 +162,27 @@ class ServerData: return post_data def create_note_data(self, server_id): + chassis_info = None + for section in self.dmidecode_data: + if section['DMIType'] == 1: + chassis_info = section + break + if chassis_info: + chassis_model = chassis_info.get('Product Name', 'Unknown') + chassis_serial = chassis_info.get('Serial Number', 'Unknown') + else: + chassis_model = chassis_serial = 'Unknown' + + processor_info = [section for section in self.dmidecode_data if section['DMIType'] == 4] + processor_model = processor_info[0].get('Version', 'Unknown') if processor_info else 'Unknown' + processor_count = len(processor_info) + + note = "Chassis Model: {}, Serial Number: {}\nProcessor Model: {}, Count: {}\nRAM Details:\n{}".format( + chassis_model, chassis_serial, processor_model, processor_count, '\n'.join(['R1', 'R2', 'R3'])) + note_data = { 'service_id': server_id, - 'note': 'Bla bla bla' + 'note': note, } return note_data -- 2.45.1 From 3940cbdbded17ed35dce504e8b2330ac94f31f40 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Tue, 25 Jun 2024 15:15:03 +0530 Subject: [PATCH 3/6] refactor: encaps create/update logic in upsert --- agent.py | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/agent.py b/agent.py index ceadc58..2987fd1 100644 --- a/agent.py +++ b/agent.py @@ -244,6 +244,36 @@ class ServerManager: return server['id'] return None + def upsert_server(self, post_data): + server_id = self.existing_server_id(post_data) + + if server_id: + logging.info('Server already exists with id: {}, Updating...'.format(server_id)) + response = self.update_server(post_data, server_id) + else: + logging.info('Server does not exist, Creating...') + response = self.create_server(post_data) + + # Extract the server_id from the response + server_id = response.get('server_id', None) + if server_id is None: + logging.error('Failed to get server_id from response: {}'.format(response)) + raise ValueError('Failed to get server_id from response') + + return server_id + + def upsert_note(self, note_data, server_id): + note_data['service_id'] = server_id + try: + note = self.get_note(server_id) + except urllib.error.HTTPError: + note = None + + if note: + return self.update_note(note_data, server_id) + else: + return self.create_note(note_data) + def validate_env_vars(): api_key = os.getenv('AGENT_API') host = os.getenv('HOST') @@ -263,26 +293,11 @@ def main(): server_manager = ServerManager(host, api_key) - # Check if the server already exists - server_id = server_manager.existing_server_id(post_data) - - # If the server exists, update it - if server_id: - logging.info('Server already exists with id: {}, Updating...'.format(server_id)) - logging.info(server_manager.update_server(post_data, server_id)) - else: - logging.info('Server does not exist, Creating...') - logging.info(server_manager.create_server(post_data)) + server_id = server_manager.upsert_server(post_data) + logging.info('Server id: {}'.format(server_id)) note_data = server_data.create_note_data(server_id) - try: - note = server_manager.get_note(server_id) - except urllib.error.HTTPError: - note = None - if note: - server_manager.update_note(note_data, server_id) - else: - server_manager.create_note(note_data) + server_manager.upsert_note(note_data, server_id) if __name__ == '__main__': main() -- 2.45.1 From f10d0b53b05a0e162df0c27500ff6ce5e89e64c7 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Tue, 25 Jun 2024 15:32:51 +0530 Subject: [PATCH 4/6] minor fixes --- agent.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/agent.py b/agent.py index 2987fd1..a3b7b9d 100644 --- a/agent.py +++ b/agent.py @@ -3,6 +3,8 @@ import urllib.request import logging import json import http.client +import subprocess +import re NON_UPDATABLE_KEYS = [ 'server_type', @@ -25,10 +27,10 @@ class ServerData: def __init__(self): self.hostname = os.uname().nodename self.public_ip = self.get_public_ip() - self.dmidecode_data = parse_dmidecode_output() + self.dmidecode_data = self.parse_dmidecode_output() logging.basicConfig(level=logging.INFO) - def parse_dmidecode_output(): + def parse_dmidecode_output(self): ''' Example dmidecode output: @@ -177,7 +179,7 @@ class ServerData: processor_model = processor_info[0].get('Version', 'Unknown') if processor_info else 'Unknown' processor_count = len(processor_info) - note = "Chassis Model: {}, Serial Number: {}\nProcessor Model: {}, Count: {}\nRAM Details:\n{}".format( + note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {}".format( chassis_model, chassis_serial, processor_model, processor_count, '\n'.join(['R1', 'R2', 'R3'])) note_data = { @@ -255,7 +257,7 @@ class ServerManager: response = self.create_server(post_data) # Extract the server_id from the response - server_id = response.get('server_id', None) + server_id = json.loads(response).get('server_id', None) if server_id is None: logging.error('Failed to get server_id from response: {}'.format(response)) raise ValueError('Failed to get server_id from response') -- 2.45.1 From c5474b1f3c3c4c06696425712913c087ae11df46 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Tue, 25 Jun 2024 15:35:33 +0530 Subject: [PATCH 5/6] refactor - remove server_id from create_note_data --- agent.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/agent.py b/agent.py index a3b7b9d..fc3a8ad 100644 --- a/agent.py +++ b/agent.py @@ -163,7 +163,7 @@ class ServerData: logging.info("Post data created") return post_data - def create_note_data(self, server_id): + def create_note_data(self): chassis_info = None for section in self.dmidecode_data: if section['DMIType'] == 1: @@ -183,7 +183,6 @@ class ServerData: chassis_model, chassis_serial, processor_model, processor_count, '\n'.join(['R1', 'R2', 'R3'])) note_data = { - 'service_id': server_id, 'note': note, } return note_data @@ -298,7 +297,7 @@ def main(): server_id = server_manager.upsert_server(post_data) logging.info('Server id: {}'.format(server_id)) - note_data = server_data.create_note_data(server_id) + note_data = server_data.create_note_data() server_manager.upsert_note(note_data, server_id) if __name__ == '__main__': -- 2.45.1 From 61909258a0fbbc8fb11cd1e11498fb4a2f4ae85b Mon Sep 17 00:00:00 2001 From: Swapnil Date: Tue, 25 Jun 2024 17:45:07 +0530 Subject: [PATCH 6/6] Added RAM info in notes --- agent.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/agent.py b/agent.py index fc3a8ad..52a60f8 100644 --- a/agent.py +++ b/agent.py @@ -179,8 +179,18 @@ class ServerData: processor_model = processor_info[0].get('Version', 'Unknown') if processor_info else 'Unknown' processor_count = len(processor_info) + ram_info = [section for section in self.dmidecode_data if section['DMIType'] == 17] + ram_details = [] + 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' + 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)) + note = "Chassis Model: {} | Serial Number: {} ||| Processor Model: {} | Count: {} ||| RAM Details: {}".format( - chassis_model, chassis_serial, processor_model, processor_count, '\n'.join(['R1', 'R2', 'R3'])) + chassis_model, chassis_serial, processor_model, processor_count, ' | '.join(ram_details)) note_data = { 'note': note, -- 2.45.1