Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion host_modules/showtech.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def info(self, date):
print("%Error: Host side: Failed: " + str(err.returncode))
return err.returncode, output

output_file_match = re.search('\/var\/.*dump.*\.gz', result.stdout)
output_file_match = re.search(r'/var/.*dump.*\.gz', result.stdout)
output_filename = output_file_match.group()
return result.returncode, output_filename

10 changes: 6 additions & 4 deletions scripts/process-reboot-cause
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ USER_ISSUED_REBOOT_CAUSE_REGEX ="User issued \'{}\' command [User: {}, Time: {}]
REBOOT_CAUSE_UNKNOWN = "Unknown"
REBOOT_CAUSE_TABLE_NAME = "REBOOT_CAUSE"
MAX_HISTORY_FILES = 10
NPU_REBOOT_CAUSE_DB_FIELDS = ("cause", "time", "user", "comment")
DPU_REBOOT_CAUSE_DB_FIELDS = NPU_REBOOT_CAUSE_DB_FIELDS + ("boot_id", "device")

REDIS_HOSTIP = "127.0.0.1"
state_db = None
Expand Down Expand Up @@ -73,10 +75,10 @@ def read_reboot_cause_files_and_save_to_db(device='npu'):
sonic_logger.log_warning(f"Missing 'name' in reboot-cause file")
continue # Skip this file
_hash = f"{REBOOT_CAUSE_TABLE_NAME}|{device.upper()}|{data['name']}"
db.set(table, _hash, 'cause', data.get('cause', ''))
db.set(table, _hash, 'time', data.get('time', ''))
db.set(table, _hash, 'user', data.get('user', ''))
db.set(table, _hash, 'comment', data.get('comment', ''))
db_fields = (NPU_REBOOT_CAUSE_DB_FIELDS if device == 'npu'
else DPU_REBOOT_CAUSE_DB_FIELDS)
for field in db_fields:
db.set(table, _hash, field, data.get(field, ''))
except json.decoder.JSONDecodeError as je:
sonic_logger.log_error("Unable to process reload cause file {}: {}".format(x, je))
pass
Expand Down
18 changes: 16 additions & 2 deletions tests/process-reboot-cause_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_invalid_json(
output = mock_stdout.getvalue()

# Test read_reboot_cause_files_and_save_to_db - smartswitch
@patch("builtins.open", new_callable=mock_open, read_data='{"cause": "Non-Hardware", "user": "admin", "name": "2024_12_13_01_12_36", "comment": "Switch rebooted DPU", "device": "DPU0", "time": "Fri Dec 13 01:12:36 AM UTC 2024"}')
@patch("builtins.open", new_callable=mock_open, read_data='{"cause": "Non-Hardware", "user": "admin", "name": "2024_12_13_01_12_36", "comment": "Switch rebooted DPU", "device": "DPU0", "time": "Fri Dec 13 01:12:36 AM UTC 2024", "boot_id": "e4252288-be0d-40ec-8338-d1e5ec206771"}')
@patch("os.listdir", return_value=["file1.json"])
@patch("os.path.isfile", return_value=True)
@patch("os.path.exists", return_value=True)
Expand All @@ -103,7 +103,21 @@ def test_read_reboot_cause_files_and_save_to_db(

# Simulate running the script
with patch.object(sys, "argv", ["process-reboot-cause"]):
process_reboot_cause.read_reboot_cause_files_and_save_to_db('dpu1')
process_reboot_cause.read_reboot_cause_files_and_save_to_db('dpu0')

mock_db.set.assert_any_call(
mock_db.CHASSIS_STATE_DB,
"REBOOT_CAUSE|DPU0|2024_12_13_01_12_36",
"boot_id",
"e4252288-be0d-40ec-8338-d1e5ec206771",
)

mock_db.set.assert_any_call(
mock_db.CHASSIS_STATE_DB,
"REBOOT_CAUSE|DPU0|2024_12_13_01_12_36",
"device",
"DPU0",
)

# Test read_reboot_cause_files_and_save_to_db - smartswitch - name not in data
@patch("builtins.open", new_callable=mock_open, read_data='{"cause": "Non-Hardware", "user": "admin", "comment": "Switch rebooted DPU", "device": "DPU0", "time": "Fri Dec 13 01:12:36 AM UTC 2024"}')
Expand Down
Loading