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
39 changes: 23 additions & 16 deletions show/reboot_cause.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@


PREVIOUS_REBOOT_CAUSE_FILE = "/host/reboot-cause/previous-reboot-cause.json"
jleveque marked this conversation as resolved.
Show resolved Hide resolved
USER_ISSUED_REBOOT_CAUSE_REGEX ="User issued \'{}\' command [User: {}, Time: {}]"

def read_reboot_cause_file():
result = ""
reboot_cause = None

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

return reboot_cause

#
# 'reboot-cause' group ("show reboot-cause")
Expand All @@ -26,18 +30,21 @@ def read_reboot_cause_file():
def reboot_cause(ctx):
"""Show cause of most recent reboot"""
if ctx.invoked_subcommand is None:
reboot_cause = ""
# Read the previous reboot cause
data = read_reboot_cause_file()
if data['user'] == "N/A":
if data['cause'] == "Kernel Panic":
reboot_cause = "{} (Time: {})".format(data['cause'], data['time'])
else:
reboot_cause = "{}".format(data['cause'])
else:
reboot_cause = USER_ISSUED_REBOOT_CAUSE_REGEX.format(data['cause'], data['user'], data['time'])
reboot_cause_str = ""

# Read the previous reboot reason
jleveque marked this conversation as resolved.
Show resolved Hide resolved
reboot_cause = read_reboot_cause_file()

if reboot_cause and "cause" in reboot_cause.keys():
reboot_cause_str = "Cause: {}".format(reboot_cause["cause"])

if "user" in reboot_cause.keys() and reboot_cause["user"] != "N/A":
reboot_cause_str += ", User: {}".format(reboot_cause["user"])

if "time" in reboot_cause.keys() and reboot_cause["time"] != "N/A":
reboot_cause_str += ", Time: {}".format(reboot_cause["time"])
Copy link
Contributor

@jleveque jleveque Mar 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for "Cause:` prefix. Also, we lose the "User issued '' command" syntax here. Instead we just see the command.

Suggest keeping format the same as before.

Something like the following:

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>' command".format(reboot_cause)
else:
    reboot_cause_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)

    reboot_cause_str += "]"


click.echo(reboot_cause)
click.echo(reboot_cause_str)

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