|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# process-reboot-cause |
| 4 | +# |
| 5 | +# Program designed to run once, soon after system boot which will |
| 6 | +# determine the cause of the previous reboot and store it to the disk, |
| 7 | +# |
| 8 | + |
| 9 | +try: |
| 10 | + import os |
| 11 | + import pwd |
| 12 | + import subprocess |
| 13 | + import sys |
| 14 | + import syslog |
| 15 | + import sonic_platform |
| 16 | + from sonic_daemon_base.daemon_base import Logger |
| 17 | +except ImportError as err: |
| 18 | + raise ImportError("%s - required module not found" % str(err)) |
| 19 | + |
| 20 | +VERSION = "1.0" |
| 21 | + |
| 22 | +SYSLOG_IDENTIFIER = "process-reboot-cause" |
| 23 | + |
| 24 | +# Global logger instance |
| 25 | +logger = Logger(SYSLOG_IDENTIFIER) |
| 26 | + |
| 27 | +REBOOT_CAUSE_DIR = "/host/reboot-cause/" |
| 28 | +REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "reboot-cause.txt" |
| 29 | +PREVIOUS_REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "previous-reboot-cause.txt" |
| 30 | + |
| 31 | +UNKNOWN_REBOOT_CAUSE = "Unknown" |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + logger.log_info("Starting up...") |
| 36 | + |
| 37 | + if not os.geteuid() == 0: |
| 38 | + logger.log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name)) |
| 39 | + sys.exit("This utility must be run as root") |
| 40 | + |
| 41 | + # Create REBOOT_CAUSE_DIR if it doesn't exist |
| 42 | + if not os.path.exists(REBOOT_CAUSE_DIR): |
| 43 | + os.makedirs(REBOOT_CAUSE_DIR) |
| 44 | + |
| 45 | + # Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists |
| 46 | + if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE): |
| 47 | + os.remove(PREVIOUS_REBOOT_CAUSE_FILE) |
| 48 | + |
| 49 | + # Check if the previous reboot was caused by hardware |
| 50 | + platform = sonic_platform.platform.Platform() |
| 51 | + |
| 52 | + chassis = platform.get_chassis() |
| 53 | + |
| 54 | + hardware_reboot_cause, optional_details = chassis.get_reboot_cause() |
| 55 | + |
| 56 | + # Set a default previous reboot cause |
| 57 | + previous_reboot_cause = UNKNOWN_REBOOT_CAUSE |
| 58 | + |
| 59 | + if hardware_reboot_cause == chassis.REBOOT_CAUSE_NON_HARDWARE: |
| 60 | + # The reboot was not caused by hardware. If there is a REBOOT_CAUSE_FILE, it will |
| 61 | + # contain any software-related reboot info. We will use it as the previous cause. |
| 62 | + if os.path.isfile(REBOOT_CAUSE_FILE): |
| 63 | + cause_file = open(REBOOT_CAUSE_FILE, "r") |
| 64 | + previous_reboot_cause = cause_file.readline().rstrip('\n') |
| 65 | + cause_file.close() |
| 66 | + elif hardware_reboot_cause == chassis.REBOOT_CAUSE_HARDWARE_OTHER: |
| 67 | + previous_reboot_cause = "{} ({})".format(hardware_reboot_cause, optional_details) |
| 68 | + else: |
| 69 | + previous_reboot_cause = hardware_reboot_cause |
| 70 | + |
| 71 | + # Write the previous reboot cause to PREVIOUS_REBOOT_CAUSE_FILE |
| 72 | + prev_cause_file = open(PREVIOUS_REBOOT_CAUSE_FILE, "w") |
| 73 | + prev_cause_file.write(previous_reboot_cause) |
| 74 | + prev_cause_file.close() |
| 75 | + |
| 76 | + # Also log the previous reboot cause to the syslog |
| 77 | + logger.log_info("Previous reboot cause: {}".format(previous_reboot_cause)) |
| 78 | + |
| 79 | + # Remove the old REBOOT_CAUSE_FILE |
| 80 | + os.remove(REBOOT_CAUSE_FILE) |
| 81 | + |
| 82 | + # Write a new default reboot cause file for the next reboot |
| 83 | + cause_file = open(REBOOT_CAUSE_FILE, "w") |
| 84 | + cause_file.write(UNKNOWN_REBOOT_CAUSE) |
| 85 | + cause_file.close() |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + main() |
0 commit comments