Skip to content

Commit 51fb339

Browse files
yozhao101raphaelt-nvidia
authored andcommitted
[determine-reboot-cause] Support 'Kernel Panic' reboot cause (sonic-net#7153)
Signed-off-by: Yong Zhao yozhao@microsoft.com Why I did it If device reboot was caused by kernel panic, then we need retrieve and store the key information into the symbol file previous-reboot-cause.json. The CLI show reboot-cause will read this file to get the reason of previous reboot. This PR is related to PR in sonic-utilities repo: sonic-net/sonic-utilities#1486 How I did it The string variable previous_reboot_cause will be parsed to check whether it contains the keyword Kernel Panic. If it did, then store the keyword and time information into a dictionary. How to verify it I verified this change on a virtual testbed. admin@vlab-01:/host/reboot-cause$ more previous-reboot-cause.json {"gen_time": "2021_03_24_23_22_35", "cause": "Kernel Panic", "user": "N/A", "time": "Wed 24 Mar 2021 11:22:03 PM UTC", "comment": "N/A"} admin@vlab-01:/host/reboot-cause$ show reboot-cause Kernel Panic [Time: Wed 24 Mar 2021 11:22:03 PM UTC]
1 parent dcd9d36 commit 51fb339

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/sonic-host-services/scripts/determine-reboot-cause

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,15 @@ def find_hardware_reboot_cause():
127127

128128

129129
def get_reboot_cause_dict(previous_reboot_cause, comment, gen_time):
130-
# resultant dictionary
130+
"""Store the key infomation of device reboot into a dictionary by parsing the string in
131+
previous_reboot_cause.
132+
133+
If user issused a command to reboot device, then user, command and time will be
134+
stored into a dictionary.
135+
136+
If device was rebooted due to the kernel panic, then the string `Kernel Panic`
137+
and time will be stored into a dictionary.
138+
"""
131139
reboot_cause_dict = {}
132140
reboot_cause_dict['gen_time'] = gen_time
133141
reboot_cause_dict['cause'] = previous_reboot_cause
@@ -142,7 +150,12 @@ def get_reboot_cause_dict(previous_reboot_cause, comment, gen_time):
142150
reboot_cause_dict['cause'] = match.group(1)
143151
reboot_cause_dict['user'] = match.group(2)
144152
reboot_cause_dict['time'] = match.group(3)
145-
153+
elif re.search(r'Kernel Panic', previous_reboot_cause):
154+
match = re.search(r'Kernel Panic \[Time: (.*)\]', previous_reboot_cause)
155+
if match is not None:
156+
reboot_cause_dict['cause'] = "Kernel Panic"
157+
reboot_cause_dict['time'] = match.group(1)
158+
146159
return reboot_cause_dict
147160

148161

src/sonic-host-services/tests/determine-reboot-cause_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
GEN_TIME_WATCHDOG = "2020_10_22_03_15_08"
5656
REBOOT_CAUSE_USER = "User issued 'reboot' command [User: admin, Time: Thu Oct 22 03:11:08 UTC 2020]"
5757
GEN_TIME_USER = "2020_10_22_03_14_07"
58+
REBOOT_CAUSE_KERNEL_PANIC = "Kernel Panic [Time: Sun Mar 28 13:45:12 UTC 2021]"
59+
GEN_TIME_KERNEL_PANIC = "2021_3_28_13_48_49"
60+
5861

5962
EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE = "warm-reboot"
6063
EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER = "User issued 'warm-reboot' command [User: admin, Time: Mon Nov 2 22:37:45 UTC 2020]"
@@ -64,6 +67,7 @@
6467

6568
EXPECTED_WATCHDOG_REBOOT_CAUSE_DICT = {'comment': '', 'gen_time': '2020_10_22_03_15_08', 'cause': 'Watchdog', 'user': 'N/A', 'time': 'N/A'}
6669
EXPECTED_USER_REBOOT_CAUSE_DICT = {'comment': '', 'gen_time': '2020_10_22_03_14_07', 'cause': 'reboot', 'user': 'admin', 'time': 'Thu Oct 22 03:11:08 UTC 2020'}
70+
EXPECTED_KERNEL_PANIC_REBOOT_CAUSE_DICT = {'comment': '', 'gen_time': '2021_3_28_13_48_49', 'cause': 'Kernel Panic', 'user': 'N/A', 'time': 'Sun Mar 28 13:45:12 UTC 2021'}
6771

6872

6973
class TestDetermineRebootCause(object):
@@ -114,3 +118,7 @@ def test_get_reboot_cause_dict_watchdog(self):
114118
def test_get_reboot_cause_dict_user(self):
115119
reboot_cause_dict = determine_reboot_cause.get_reboot_cause_dict(REBOOT_CAUSE_USER, "", GEN_TIME_USER)
116120
assert reboot_cause_dict == EXPECTED_USER_REBOOT_CAUSE_DICT
121+
122+
def test_get_reboot_cause_dict_kernel_panic(self):
123+
reboot_cause_dict = determine_reboot_cause.get_reboot_cause_dict(REBOOT_CAUSE_KERNEL_PANIC, "", GEN_TIME_KERNEL_PANIC)
124+
assert reboot_cause_dict == EXPECTED_KERNEL_PANIC_REBOOT_CAUSE_DICT

0 commit comments

Comments
 (0)