Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[reboot] User-friendly reboot cause message for kernel panic #1486

Merged
merged 10 commits into from
Mar 28, 2021
2 changes: 1 addition & 1 deletion scripts/reboot
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REBOOT_TIME=$(date)
VMCORE_FILE=/proc/vmcore
if [ -e $VMCORE_FILE -a -s $VMCORE_FILE ]; then
echo "We have a /proc/vmcore, then we just kdump'ed"
echo "User issued 'kdump' command [User: kdump, Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE}
echo "Kernel Panic (Time: ${REBOOT_TIME})" > ${REBOOT_CAUSE_FILE}
jleveque marked this conversation as resolved.
Show resolved Hide resolved
sync
PLATFORM=$(grep -oP 'sonic_platform=\K\S+' /proc/cmdline)
if [ ! -z "${PLATFORM}" -a -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then
Expand Down
49 changes: 36 additions & 13 deletions show/reboot_cause.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import utilities_common.cli as clicommon


PREVIOUS_REBOOT_CAUSE_FILE = "/host/reboot-cause/previous-reboot-cause.json"
USER_ISSUED_REBOOT_CAUSE_REGEX ="User issued \'{}\' command [User: {}, Time: {}]"
PREVIOUS_REBOOT_CAUSE_FILE_PATH = "/host/reboot-cause/previous-reboot-cause.json"

def read_reboot_cause_file():
result = ""
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE):
with open(PREVIOUS_REBOOT_CAUSE_FILE) as f:
result = json.load(f)
return result
reboot_cause_dict = {}

if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE_PATH):
with open(PREVIOUS_REBOOT_CAUSE_FILE_PATH) as prev_reboot_cause_file:
try:
reboot_cause_dict = json.load(prev_reboot_cause_file)
except json.JSONDecodeError as err:
click.echo("Failed to load JSON file '{}'!".format(PREVIOUS_REBOOT_CAUSE_FILE_PATH))
jleveque marked this conversation as resolved.
Show resolved Hide resolved

return reboot_cause_dict

#
# 'reboot-cause' group ("show reboot-cause")
Expand All @@ -26,15 +30,34 @@ def read_reboot_cause_file():
def reboot_cause(ctx):
"""Show cause of most recent reboot"""
if ctx.invoked_subcommand is None:
reboot_cause = ""
reboot_cause_str = ""

# Read the previous reboot cause
data = read_reboot_cause_file()
if data['user'] == "N/A":
reboot_cause = "{}".format(data['cause'])
reboot_cause_dict = read_reboot_cause_file()

reboot_cause = reboot_cause_dict.get("cause", "Unknown")
reboot_user = reboot_cause_dict.get("user", "N/A")
reboot_time = reboot_cause_dict.get("time", "N/A")

if reboot_user != "N/A":
reboot_cause_str = "User issued '{}' command".format(reboot_cause)
else:
reboot_cause = USER_ISSUED_REBOOT_CAUSE_REGEX.format(data['cause'], data['user'], data['time'])
reboot_cuase_str = reboot_cause

if reboot_user != "N/A" or reboot_time != "N/A":
reboot_cause_str += "["

if reboot_user != "N/A":
reboot_cause_str += "User: {}".format(reboot_user)
if reboot_time != "N/A":
reboot_cause_str += ", "

if reboot_time != "N/A":
reboot_cause_str += "Time: {}".format(reboot_time)

click.echo(reboot_cause)
reboot_cause_str += "]"

click.echo(reboot_cause_str)

# 'history' subcommand ("show reboot-cause history")
@reboot_cause.command()
Expand Down