Skip to content

Commit e8130f5

Browse files
[system-health] Improve code structure of system health CLIs (sonic-net#2453)
1 parent 00c01b3 commit e8130f5

File tree

2 files changed

+65
-135
lines changed

2 files changed

+65
-135
lines changed

show/system_health.py

+62-132
Original file line numberDiff line numberDiff line change
@@ -5,144 +5,82 @@
55
from tabulate import tabulate
66
import utilities_common.cli as clicommon
77

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
158

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:
3018
if os.geteuid():
3119
click.echo("Root privileges are required for this operation")
32-
return
20+
exit(1)
3321
from health_checker.manager import HealthCheckerManager
3422
from sonic_platform.chassis import Chassis
3523

24+
3625
manager = HealthCheckerManager()
3726
if not manager.config.config_file_exists():
3827
click.echo("System health configuration file not found, exit...")
39-
return
28+
exit(1)
29+
4030
chassis = Chassis()
4131
stat = manager.check(chassis)
4232
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")
7433

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
9435

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):
10437
click.echo("System status summary\n\n System status LED " + led)
10538
services_list = []
10639
fs_list = []
10740
device_list =[]
10841
for category, elements in stat.items():
10942
for element in elements:
11043
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)
11549
else:
11650
device_list.append(elements[element]['message'])
117-
if len(services_list) or len(fs_list):
51+
if services_list or fs_list:
11852
click.echo(" Services:\n Status: Not OK")
11953
else:
12054
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:
12860
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:]))
13265
else:
13366
click.echo(" Hardware:\n Status: OK")
13467

68+
def display_monitor_list(stat):
13569
click.echo('\nSystem services and devices monitor list\n')
13670
header = ['Name', 'Status', 'Type']
13771
table = []
138-
for category, elements in stat.items():
72+
for elements in stat.values():
13973
for element in sorted(elements.items(), key=lambda x: x[1]['status']):
14074
entry = []
14175
entry.append(element[0])
14276
entry.append(element[1]['status'])
14377
entry.append(element[1]['type'])
14478
table.append(entry)
14579
click.echo(tabulate(table, header))
80+
81+
82+
def display_ignore_list(manager):
83+
header = ['Name', 'Status', 'Type']
14684
click.echo('\nSystem services and devices ignore list\n')
14785
table = []
14886
if manager.config.ignore_services:
@@ -161,43 +99,35 @@ def detail():
16199
table.append(entry)
162100
click.echo(tabulate(table, header))
163101

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+
164126
@system_health.command()
165127
def monitor_list():
166128
"""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('\nSystem 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)
201131

202132

203133
@system_health.group('sysready-status',invoke_without_command=True)

tests/system_health_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_health_summary(self):
8484
System status LED red
8585
Services:
8686
Status: Not OK
87-
Not Running: 'telemetry', 'snmp_subagent'
87+
Not Running: telemetry, snmp_subagent
8888
Hardware:
8989
Status: OK
9090
"""
@@ -171,7 +171,7 @@ def test_health_detail(self):
171171
System status LED red
172172
Services:
173173
Status: Not OK
174-
Not Running: 'telemetry', 'sflowmgrd'
174+
Not Running: telemetry, sflowmgrd
175175
Hardware:
176176
Status: Not OK
177177
Reasons: Failed to get voltage minimum threshold data for PSU 1
@@ -243,7 +243,7 @@ def test_health_detail(self):
243243
System status LED red
244244
Services:
245245
Status: Not OK
246-
Not Running: 'telemetry', 'sflowmgrd'
246+
Not Running: telemetry, sflowmgrd
247247
Hardware:
248248
Status: OK
249249

0 commit comments

Comments
 (0)