[process-reboot-cause] Handle case if platform does not yet have sonic_platform implementation (#3126)

This commit is contained in:
Joe LeVeque 2019-07-05 17:53:49 -07:00 committed by Ying Xie
parent 4b5abd048b
commit c3932e501b

View File

@ -9,11 +9,8 @@
try: try:
import os import os
import pwd import pwd
import subprocess
import sys import sys
import syslog import syslog
import sonic_platform
from sonic_daemon_base.daemon_base import Logger
except ImportError as err: except ImportError as err:
raise ImportError("%s - required module not found" % str(err)) raise ImportError("%s - required module not found" % str(err))
@ -21,9 +18,6 @@ VERSION = "1.0"
SYSLOG_IDENTIFIER = "process-reboot-cause" SYSLOG_IDENTIFIER = "process-reboot-cause"
# Global logger instance
logger = Logger(SYSLOG_IDENTIFIER)
REBOOT_CAUSE_DIR = "/host/reboot-cause/" REBOOT_CAUSE_DIR = "/host/reboot-cause/"
REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "reboot-cause.txt" REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "reboot-cause.txt"
PREVIOUS_REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "previous-reboot-cause.txt" PREVIOUS_REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "previous-reboot-cause.txt"
@ -31,11 +25,33 @@ PREVIOUS_REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "previous-reboot-cause.txt"
UNKNOWN_REBOOT_CAUSE = "Unknown" UNKNOWN_REBOOT_CAUSE = "Unknown"
# ========================== Syslog wrappers ==========================
def log_info(msg):
syslog.openlog(SYSLOG_IDENTIFIER)
syslog.syslog(syslog.LOG_INFO, msg)
syslog.closelog()
def log_warning(msg):
syslog.openlog(SYSLOG_IDENTIFIER)
syslog.syslog(syslog.LOG_WARNING, msg)
syslog.closelog()
def log_error(msg):
syslog.openlog(SYSLOG_IDENTIFIER)
syslog.syslog(syslog.LOG_ERR, msg)
syslog.closelog()
# ============================= Functions =============================
def main(): def main():
logger.log_info("Starting up...") log_info("Starting up...")
if not os.geteuid() == 0: if not os.geteuid() == 0:
logger.log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name)) log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name))
sys.exit("This utility must be run as root") sys.exit("This utility must be run as root")
# Create REBOOT_CAUSE_DIR if it doesn't exist # Create REBOOT_CAUSE_DIR if it doesn't exist
@ -46,27 +62,43 @@ def main():
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE): if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE):
os.remove(PREVIOUS_REBOOT_CAUSE_FILE) os.remove(PREVIOUS_REBOOT_CAUSE_FILE)
# Check if the previous reboot was caused by hardware
platform = sonic_platform.platform.Platform()
chassis = platform.get_chassis()
hardware_reboot_cause, optional_details = chassis.get_reboot_cause()
# Set a default previous reboot cause # Set a default previous reboot cause
previous_reboot_cause = UNKNOWN_REBOOT_CAUSE previous_reboot_cause = UNKNOWN_REBOOT_CAUSE
if hardware_reboot_cause == chassis.REBOOT_CAUSE_NON_HARDWARE: # Until all platform vendors have provided sonic_platform packages,
# The reboot was not caused by hardware. If there is a REBOOT_CAUSE_FILE, it will # if there is no sonic_platform package installed, we only provide
# contain any software-related reboot info. We will use it as the previous cause. # software-related reboot causes.
try:
import sonic_platform
# Check if the previous reboot was caused by hardware
platform = sonic_platform.platform.Platform()
chassis = platform.get_chassis()
hardware_reboot_cause, optional_details = chassis.get_reboot_cause()
if hardware_reboot_cause == chassis.REBOOT_CAUSE_NON_HARDWARE:
# The reboot was not caused by hardware. If there is a REBOOT_CAUSE_FILE, it will
# contain any software-related reboot info. We will use it as the previous cause.
if os.path.isfile(REBOOT_CAUSE_FILE):
cause_file = open(REBOOT_CAUSE_FILE, "r")
previous_reboot_cause = cause_file.readline().rstrip('\n')
cause_file.close()
elif hardware_reboot_cause == chassis.REBOOT_CAUSE_HARDWARE_OTHER:
previous_reboot_cause = "{} ({})".format(hardware_reboot_cause, optional_details)
else:
previous_reboot_cause = hardware_reboot_cause
except ImportError as err:
log_warning("sonic_platform package not installed. Unable to detect hardware reboot causes.")
# If there is a REBOOT_CAUSE_FILE, it will contain any software-related
# reboot info. We will use it as the previous cause.
if os.path.isfile(REBOOT_CAUSE_FILE): if os.path.isfile(REBOOT_CAUSE_FILE):
cause_file = open(REBOOT_CAUSE_FILE, "r") cause_file = open(REBOOT_CAUSE_FILE, "r")
previous_reboot_cause = cause_file.readline().rstrip('\n') previous_reboot_cause = cause_file.readline().rstrip('\n')
cause_file.close() cause_file.close()
elif hardware_reboot_cause == chassis.REBOOT_CAUSE_HARDWARE_OTHER:
previous_reboot_cause = "{} ({})".format(hardware_reboot_cause, optional_details)
else:
previous_reboot_cause = hardware_reboot_cause
# Write the previous reboot cause to PREVIOUS_REBOOT_CAUSE_FILE # Write the previous reboot cause to PREVIOUS_REBOOT_CAUSE_FILE
prev_cause_file = open(PREVIOUS_REBOOT_CAUSE_FILE, "w") prev_cause_file = open(PREVIOUS_REBOOT_CAUSE_FILE, "w")
@ -74,7 +106,7 @@ def main():
prev_cause_file.close() prev_cause_file.close()
# Also log the previous reboot cause to the syslog # Also log the previous reboot cause to the syslog
logger.log_info("Previous reboot cause: {}".format(previous_reboot_cause)) log_info("Previous reboot cause: {}".format(previous_reboot_cause))
# Remove the old REBOOT_CAUSE_FILE # Remove the old REBOOT_CAUSE_FILE
os.remove(REBOOT_CAUSE_FILE) os.remove(REBOOT_CAUSE_FILE)