9
9
try :
10
10
import os
11
11
import pwd
12
- import subprocess
13
12
import sys
14
13
import syslog
15
- import sonic_platform
16
- from sonic_daemon_base .daemon_base import Logger
17
14
except ImportError as err :
18
15
raise ImportError ("%s - required module not found" % str (err ))
19
16
20
17
VERSION = "1.0"
21
18
22
19
SYSLOG_IDENTIFIER = "process-reboot-cause"
23
20
24
- # Global logger instance
25
- logger = Logger (SYSLOG_IDENTIFIER )
26
-
27
21
REBOOT_CAUSE_DIR = "/host/reboot-cause/"
28
22
REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "reboot-cause.txt"
29
23
PREVIOUS_REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "previous-reboot-cause.txt"
30
24
31
25
UNKNOWN_REBOOT_CAUSE = "Unknown"
32
26
33
27
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
+
34
50
def main ():
35
- logger . log_info ("Starting up..." )
51
+ log_info ("Starting up..." )
36
52
37
53
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 ))
39
55
sys .exit ("This utility must be run as root" )
40
56
41
57
# Create REBOOT_CAUSE_DIR if it doesn't exist
@@ -46,35 +62,51 @@ def main():
46
62
if os .path .exists (PREVIOUS_REBOOT_CAUSE_FILE ):
47
63
os .remove (PREVIOUS_REBOOT_CAUSE_FILE )
48
64
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
65
56
66
# Set a default previous reboot cause
57
67
previous_reboot_cause = UNKNOWN_REBOOT_CAUSE
58
68
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.
62
98
if os .path .isfile (REBOOT_CAUSE_FILE ):
63
99
cause_file = open (REBOOT_CAUSE_FILE , "r" )
64
100
previous_reboot_cause = cause_file .readline ().rstrip ('\n ' )
65
101
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
102
71
103
# Write the previous reboot cause to PREVIOUS_REBOOT_CAUSE_FILE
72
104
prev_cause_file = open (PREVIOUS_REBOOT_CAUSE_FILE , "w" )
73
105
prev_cause_file .write (previous_reboot_cause )
74
106
prev_cause_file .close ()
75
107
76
108
# 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 ))
78
110
79
111
# Remove the old REBOOT_CAUSE_FILE
80
112
os .remove (REBOOT_CAUSE_FILE )
0 commit comments