From c28e29c80122feb6aa472dd5f1ddfe0c6ea116d0 Mon Sep 17 00:00:00 2001 From: Ramesh Raghupathy Date: Mon, 25 Nov 2024 13:47:25 -0800 Subject: [PATCH] Fixed the DB --- scripts/process-reboot-cause | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/scripts/process-reboot-cause b/scripts/process-reboot-cause index d835ee1d..02f92d93 100755 --- a/scripts/process-reboot-cause +++ b/scripts/process-reboot-cause @@ -105,7 +105,7 @@ def get_sorted_reboot_cause_files(dpu_history_path): files = os.listdir(dpu_history_path) # Filter and sort the files based on your criteria (e.g., by modification time) sorted_files = sorted( - [os.path.join(dpu_history_path, f) for f in files if f.endswith('.json')], + [os.path.join(dpu_history_path, f) for f in files if f.endswith('.txt')], key=os.path.getmtime, # Sort by modification time reverse=True # Most recent first ) @@ -116,17 +116,18 @@ def get_sorted_reboot_cause_files(dpu_history_path): def read_dpu_reboot_cause_files_and_save_chassis_state_db(): - """Retrieve reboot cause from history files and save them to StateDB.""" + """Retrieve reboot cause from history files and save them to chassisStateDB.""" + chassis_state_db = swsscommon.SonicV2Connector(host="redis_chassis.server", port=6380) + chassis_state_db.connect(chassis_state_db.CHASSIS_STATE_DB) + try: - # Get the DPUs from the platform configuration + # Use the helper API get_dpu_list from device_info when ready dpus = get_dpus() - history_dir = '/host/reboot-cause/module' for dpu in dpus: - dpu_history_path = os.path.join(history_dir, dpu) - # Get sorted reboot cause files for the DPU - reboot_files = get_sorted_reboot_cause_files(os.path.join(dpu_history_path, "history")) + dpu_history_dir = os.path.join('/host/reboot-cause/module', dpu , 'history') + reboot_files = get_sorted_reboot_cause_files(dpu_history_dir) for reboot_file in reboot_files: if os.path.isfile(reboot_file): @@ -134,18 +135,18 @@ def read_dpu_reboot_cause_files_and_save_chassis_state_db(): try: data = json.load(cause_file) # Ensure keys exist - if 'gen_time' not in data: - sonic_logger.log_warning(f"Missing 'gen_time' in data from {reboot_file}") + if 'name' not in data: + sonic_logger.log_warning(f"Missing 'time' in data from {reboot_file}") continue # Skip this file - _hash = f"{REBOOT_CAUSE_TABLE_NAME}|{data['gen_time']}" - state_db.set(state_db.STATE_DB, _hash, 'cause', data.get('cause', '')) - state_db.set(state_db.STATE_DB, _hash, 'time', data.get('time', '')) - state_db.set(state_db.STATE_DB, _hash, 'user', data.get('user', '')) - state_db.set(state_db.STATE_DB, _hash, 'comment', data.get('comment', '')) + _hash = f"{REBOOT_CAUSE_TABLE_NAME}|{dpu.upper()}|{data['name']}" + chassis_state_db.set(chassis_state_db.CHASSIS_STATE_DB, _hash, 'cause', data.get('cause', '')) + chassis_state_db.set(chassis_state_db.CHASSIS_STATE_DB, _hash, 'time', data.get('time', '')) + chassis_state_db.set(chassis_state_db.CHASSIS_STATE_DB, _hash, 'user', data.get('user', '')) + chassis_state_db.set(chassis_state_db.CHASSIS_STATE_DB, _hash, 'comment', data.get('comment', '')) except json.decoder.JSONDecodeError as je: - sonic_logger.log_info(f"Unable to process reload cause file {reboot_file}: {je}") + sonic_logger.log_info(f"Unable to process reboot-cause file {reboot_file}: {je}") continue # Skip this file except Exception as e: sonic_logger.log_err(f"Error reading DPU reboot causes: {e}")