5
5
from tabulate import tabulate
6
6
import utilities_common .cli as clicommon
7
7
8
- #
9
- # 'system-health' command ("show system-health")
10
- #
11
- @click .group (name = 'system-health' , cls = clicommon .AliasedGroup )
12
- def system_health ():
13
- """Show system-health information"""
14
- return
15
8
16
- @system_health .command ()
17
- def summary ():
18
- """Show system-health summary information"""
19
- # Mock the redis for unit test purposes #
20
- try :
21
- if os .environ ["UTILITIES_UNIT_TESTING" ] == "1" :
22
- modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
23
- sys .path .insert (0 , modules_path )
24
- from tests .system_health_test import MockerManager
25
- from tests .system_health_test import MockerChassis
26
- HealthCheckerManager = MockerManager
27
- Chassis = MockerChassis
28
- except Exception :
29
- # Normal run... #
9
+ def get_system_health_status ():
10
+ if os .environ ["UTILITIES_UNIT_TESTING" ] == "1" :
11
+ modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
12
+ sys .path .insert (0 , modules_path )
13
+ from tests .system_health_test import MockerManager
14
+ from tests .system_health_test import MockerChassis
15
+ HealthCheckerManager = MockerManager
16
+ Chassis = MockerChassis
17
+ else :
30
18
if os .geteuid ():
31
19
click .echo ("Root privileges are required for this operation" )
32
- return
20
+ exit ( 1 )
33
21
from health_checker .manager import HealthCheckerManager
34
22
from sonic_platform .chassis import Chassis
35
23
24
+
36
25
manager = HealthCheckerManager ()
37
26
if not manager .config .config_file_exists ():
38
27
click .echo ("System health configuration file not found, exit..." )
39
- return
28
+ exit (1 )
29
+
40
30
chassis = Chassis ()
41
31
stat = manager .check (chassis )
42
32
chassis .initizalize_system_led ()
43
- led = chassis .get_status_led ()
44
- click .echo ("System status summary\n \n System status LED " + led )
45
- services_list = []
46
- fs_list = []
47
- device_list = []
48
- for category , elements in stat .items ():
49
- for element in elements :
50
- if elements [element ]['status' ] != "OK" :
51
- if 'Running' in elements [element ]['message' ]:
52
- services_list .append (element )
53
- elif 'Accessible' in elements [element ]['message' ]:
54
- fs_list .append (element )
55
- else :
56
- device_list .append (elements [element ]['message' ])
57
- if len (services_list ) or len (fs_list ):
58
- click .echo (" Services:\n Status: Not OK" )
59
- else :
60
- click .echo (" Services:\n Status: OK" )
61
- if len (services_list ):
62
- services_list_string = str (services_list )
63
- click .echo (" Not Running: " + services_list_string .replace ("[" , "" ).replace (']' , "" ))
64
- if len (fs_list ):
65
- fs_list_string = str (fs_list )
66
- click .echo (" Not Accessible: " + fs_list_string .replace ("[" , "" ).replace (']' , "" ))
67
- if len (device_list ):
68
- click .echo (" Hardware:\n Status: Not OK" )
69
- click .echo (" Reasons: " + device_list .pop ())
70
- while len (device_list ):
71
- click .echo ("\t " + device_list .pop ())
72
- else :
73
- click .echo (" Hardware:\n Status: OK" )
74
33
75
- @system_health .command ()
76
- def detail ():
77
- """Show system-health detail information"""
78
- # Mock the redis for unit test purposes #
79
- try :
80
- if os .environ ["UTILITIES_UNIT_TESTING" ] == "1" :
81
- modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
82
- sys .path .insert (0 , modules_path )
83
- from tests .system_health_test import MockerManager
84
- from tests .system_health_test import MockerChassis
85
- HealthCheckerManager = MockerManager
86
- Chassis = MockerChassis
87
- except Exception :
88
- # Normal run... #
89
- if os .geteuid ():
90
- click .echo ("Root privileges are required for this operation" )
91
- return
92
- from health_checker .manager import HealthCheckerManager
93
- from sonic_platform .chassis import Chassis
34
+ return manager , chassis , stat
94
35
95
- manager = HealthCheckerManager ()
96
- if not manager .config .config_file_exists ():
97
- click .echo ("System health configuration file not found, exit..." )
98
- return
99
- chassis = Chassis ()
100
- stat = manager .check (chassis )
101
- #summary output
102
- chassis .initizalize_system_led ()
103
- led = chassis .get_status_led ()
36
+ def display_system_health_summary (stat , led ):
104
37
click .echo ("System status summary\n \n System status LED " + led )
105
38
services_list = []
106
39
fs_list = []
107
40
device_list = []
108
41
for category , elements in stat .items ():
109
42
for element in elements :
110
43
if elements [element ]['status' ] != "OK" :
111
- if 'Running' in elements [element ]['message' ]:
112
- services_list .append (element )
113
- elif 'Accessible' in elements [element ]['message' ]:
114
- fs_list .append (element )
44
+ if category == 'Services' :
45
+ if 'Accessible' in elements [element ]['message' ]:
46
+ fs_list .append (element )
47
+ else :
48
+ services_list .append (element )
115
49
else :
116
50
device_list .append (elements [element ]['message' ])
117
- if len ( services_list ) or len ( fs_list ) :
51
+ if services_list or fs_list :
118
52
click .echo (" Services:\n Status: Not OK" )
119
53
else :
120
54
click .echo (" Services:\n Status: OK" )
121
- if len (services_list ):
122
- services_list_string = str (services_list )
123
- click .echo (" Not Running: " + services_list_string .replace ("[" , "" ).replace (']' , "" ))
124
- if len (fs_list ):
125
- fs_list_string = str (fs_list )
126
- click .echo (" Not Accessible: " + fs_list_string .replace ("[" , "" ).replace (']' , "" ))
127
- if len (device_list ):
55
+ if services_list :
56
+ click .echo (" Not Running: " + ', ' .join (services_list ))
57
+ if fs_list :
58
+ click .echo (" Not Accessible: " + ', ' .join (fs_list ))
59
+ if device_list :
128
60
click .echo (" Hardware:\n Status: Not OK" )
129
- click .echo (" Reasons: " + device_list .pop ())
130
- while len (device_list ):
131
- click .echo ("\t " + device_list .pop ())
61
+ device_list .reverse ()
62
+ click .echo (" Reasons: " + device_list [0 ])
63
+ if len (device_list ) > 1 :
64
+ click .echo ('\n ' .join (("\t " + x ) for x in device_list [1 :]))
132
65
else :
133
66
click .echo (" Hardware:\n Status: OK" )
134
67
68
+ def display_monitor_list (stat ):
135
69
click .echo ('\n System services and devices monitor list\n ' )
136
70
header = ['Name' , 'Status' , 'Type' ]
137
71
table = []
138
- for category , elements in stat .items ():
72
+ for elements in stat .values ():
139
73
for element in sorted (elements .items (), key = lambda x : x [1 ]['status' ]):
140
74
entry = []
141
75
entry .append (element [0 ])
142
76
entry .append (element [1 ]['status' ])
143
77
entry .append (element [1 ]['type' ])
144
78
table .append (entry )
145
79
click .echo (tabulate (table , header ))
80
+
81
+
82
+ def display_ignore_list (manager ):
83
+ header = ['Name' , 'Status' , 'Type' ]
146
84
click .echo ('\n System services and devices ignore list\n ' )
147
85
table = []
148
86
if manager .config .ignore_services :
@@ -161,43 +99,35 @@ def detail():
161
99
table .append (entry )
162
100
click .echo (tabulate (table , header ))
163
101
102
+ #
103
+ # 'system-health' command ("show system-health")
104
+ #
105
+ @click .group (name = 'system-health' , cls = clicommon .AliasedGroup )
106
+ def system_health ():
107
+ """Show system-health information"""
108
+ return
109
+
110
+ @system_health .command ()
111
+ def summary ():
112
+ """Show system-health summary information"""
113
+ _ , chassis , stat = get_system_health_status ()
114
+ display_system_health_summary (stat , chassis .get_status_led ())
115
+
116
+
117
+ @system_health .command ()
118
+ def detail ():
119
+ """Show system-health detail information"""
120
+ manager , chassis , stat = get_system_health_status ()
121
+ display_system_health_summary (stat , chassis .get_status_led ())
122
+ display_monitor_list (stat )
123
+ display_ignore_list (manager )
124
+
125
+
164
126
@system_health .command ()
165
127
def monitor_list ():
166
128
"""Show system-health monitored services and devices name list"""
167
- # Mock the redis for unit test purposes #
168
- try :
169
- if os .environ ["UTILITIES_UNIT_TESTING" ] == "1" :
170
- modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
171
- sys .path .insert (0 , modules_path )
172
- from tests .system_health_test import MockerManager
173
- from tests .system_health_test import MockerChassis
174
- HealthCheckerManager = MockerManager
175
- Chassis = MockerChassis
176
- except Exception :
177
- # Normal run... #
178
- if os .geteuid ():
179
- click .echo ("Root privileges are required for this operation" )
180
- return
181
- from health_checker .manager import HealthCheckerManager
182
- from sonic_platform .chassis import Chassis
183
-
184
- manager = HealthCheckerManager ()
185
- if not manager .config .config_file_exists ():
186
- click .echo ("System health configuration file not found, exit..." )
187
- return
188
- chassis = Chassis ()
189
- stat = manager .check (chassis )
190
- click .echo ('\n System services and devices monitor list\n ' )
191
- header = ['Name' , 'Status' , 'Type' ]
192
- table = []
193
- for category , elements in stat .items ():
194
- for element in sorted (elements .items (), key = lambda x : x [1 ]['status' ]):
195
- entry = []
196
- entry .append (element [0 ])
197
- entry .append (element [1 ]['status' ])
198
- entry .append (element [1 ]['type' ])
199
- table .append (entry )
200
- click .echo (tabulate (table , header ))
129
+ _ , _ , stat = get_system_health_status ()
130
+ display_monitor_list (stat )
201
131
202
132
203
133
@system_health .group ('sysready-status' ,invoke_without_command = True )
0 commit comments