Skip to content

Commit

Permalink
Fixed the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
rameshraghupathy committed Nov 25, 2024
1 parent 093bf00 commit c28e29c
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions scripts/process-reboot-cause
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -116,36 +116,37 @@ 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):
with open(reboot_file, "r") as cause_file:
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}")
Expand Down

0 comments on commit c28e29c

Please sign in to comment.