Skip to content

Commit c3932e5

Browse files
jlevequeyxieca
authored andcommitted
[process-reboot-cause] Handle case if platform does not yet have sonic_platform implementation (#3126)
1 parent 4b5abd0 commit c3932e5

File tree

1 file changed

+54
-22
lines changed

1 file changed

+54
-22
lines changed

files/image_config/process-reboot-cause/process-reboot-cause

+54-22
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,49 @@
99
try:
1010
import os
1111
import pwd
12-
import subprocess
1312
import sys
1413
import syslog
15-
import sonic_platform
16-
from sonic_daemon_base.daemon_base import Logger
1714
except ImportError as err:
1815
raise ImportError("%s - required module not found" % str(err))
1916

2017
VERSION = "1.0"
2118

2219
SYSLOG_IDENTIFIER = "process-reboot-cause"
2320

24-
# Global logger instance
25-
logger = Logger(SYSLOG_IDENTIFIER)
26-
2721
REBOOT_CAUSE_DIR = "/host/reboot-cause/"
2822
REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "reboot-cause.txt"
2923
PREVIOUS_REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "previous-reboot-cause.txt"
3024

3125
UNKNOWN_REBOOT_CAUSE = "Unknown"
3226

3327

28+
# ========================== Syslog wrappers ==========================
29+
30+
def log_info(msg):
31+
syslog.openlog(SYSLOG_IDENTIFIER)
32+
syslog.syslog(syslog.LOG_INFO, msg)
33+
syslog.closelog()
34+
35+
36+
def log_warning(msg):
37+
syslog.openlog(SYSLOG_IDENTIFIER)
38+
syslog.syslog(syslog.LOG_WARNING, msg)
39+
syslog.closelog()
40+
41+
42+
def log_error(msg):
43+
syslog.openlog(SYSLOG_IDENTIFIER)
44+
syslog.syslog(syslog.LOG_ERR, msg)
45+
syslog.closelog()
46+
47+
48+
# ============================= Functions =============================
49+
3450
def main():
35-
logger.log_info("Starting up...")
51+
log_info("Starting up...")
3652

3753
if not os.geteuid() == 0:
38-
logger.log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name))
54+
log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name))
3955
sys.exit("This utility must be run as root")
4056

4157
# Create REBOOT_CAUSE_DIR if it doesn't exist
@@ -46,35 +62,51 @@ def main():
4662
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE):
4763
os.remove(PREVIOUS_REBOOT_CAUSE_FILE)
4864

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()
5565

5666
# Set a default previous reboot cause
5767
previous_reboot_cause = UNKNOWN_REBOOT_CAUSE
5868

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.
69+
# Until all platform vendors have provided sonic_platform packages,
70+
# if there is no sonic_platform package installed, we only provide
71+
# software-related reboot causes.
72+
try:
73+
import sonic_platform
74+
75+
# Check if the previous reboot was caused by hardware
76+
platform = sonic_platform.platform.Platform()
77+
78+
chassis = platform.get_chassis()
79+
80+
hardware_reboot_cause, optional_details = chassis.get_reboot_cause()
81+
82+
if hardware_reboot_cause == chassis.REBOOT_CAUSE_NON_HARDWARE:
83+
# The reboot was not caused by hardware. If there is a REBOOT_CAUSE_FILE, it will
84+
# contain any software-related reboot info. We will use it as the previous cause.
85+
if os.path.isfile(REBOOT_CAUSE_FILE):
86+
cause_file = open(REBOOT_CAUSE_FILE, "r")
87+
previous_reboot_cause = cause_file.readline().rstrip('\n')
88+
cause_file.close()
89+
elif hardware_reboot_cause == chassis.REBOOT_CAUSE_HARDWARE_OTHER:
90+
previous_reboot_cause = "{} ({})".format(hardware_reboot_cause, optional_details)
91+
else:
92+
previous_reboot_cause = hardware_reboot_cause
93+
except ImportError as err:
94+
log_warning("sonic_platform package not installed. Unable to detect hardware reboot causes.")
95+
96+
# If there is a REBOOT_CAUSE_FILE, it will contain any software-related
97+
# reboot info. We will use it as the previous cause.
6298
if os.path.isfile(REBOOT_CAUSE_FILE):
6399
cause_file = open(REBOOT_CAUSE_FILE, "r")
64100
previous_reboot_cause = cause_file.readline().rstrip('\n')
65101
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
70102

71103
# Write the previous reboot cause to PREVIOUS_REBOOT_CAUSE_FILE
72104
prev_cause_file = open(PREVIOUS_REBOOT_CAUSE_FILE, "w")
73105
prev_cause_file.write(previous_reboot_cause)
74106
prev_cause_file.close()
75107

76108
# Also log the previous reboot cause to the syslog
77-
logger.log_info("Previous reboot cause: {}".format(previous_reboot_cause))
109+
log_info("Previous reboot cause: {}".format(previous_reboot_cause))
78110

79111
# Remove the old REBOOT_CAUSE_FILE
80112
os.remove(REBOOT_CAUSE_FILE)

0 commit comments

Comments
 (0)