Skip to content

Commit c139a23

Browse files
Junchao-Mellanoxlguohan
authored andcommitted
Add command line support for thermal control (sonic-net#777)
Add a "show platform fanstatus" command to allow user fetch FAN status data. Add a "show platform temperature" command to allow user fetch thermal status data.
1 parent 377881d commit c139a23

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

scripts/fanshow

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/python
2+
"""
3+
Script to show fan status.
4+
"""
5+
from __future__ import print_function
6+
7+
import argparse
8+
9+
from tabulate import tabulate
10+
from swsssdk import SonicV2Connector
11+
12+
13+
header = ['FAN', 'Speed', 'Direction', 'Presence', 'Status', 'Timestamp']
14+
15+
FAN_TABLE_NAME = 'FAN_INFO'
16+
SPEED_FIELD_NAME = 'speed'
17+
DIRECTION_FIELD_NAME = 'direction'
18+
PRESENCE_FIELD_NAME = 'presence'
19+
STATUS_FIELD_NAME = 'status'
20+
TIMESTAMP_FIELD_NAME = 'timestamp'
21+
22+
23+
class FanShow(object):
24+
def __init__(self):
25+
self.db = SonicV2Connector(host="127.0.0.1")
26+
self.db.connect(self.db.STATE_DB)
27+
28+
def show(self):
29+
keys = self.db.keys(self.db.STATE_DB, FAN_TABLE_NAME + '*')
30+
if not keys:
31+
print('Fan Not detected\n')
32+
return
33+
34+
table = []
35+
for key in keys:
36+
key_list = key.split('|')
37+
if len(key_list) != 2: # error data in DB, log it and ignore
38+
print('Warn: Invalid key in table FAN_INFO: {}'.format(key))
39+
continue
40+
41+
name = key_list[1]
42+
data_dict = self.db.get_all(self.db.STATE_DB, key)
43+
try:
44+
speed = float(data_dict[SPEED_FIELD_NAME])
45+
if speed > 100:
46+
speed = '{}RPM'.format(int(speed))
47+
else:
48+
speed = '{}%'.format(data_dict[SPEED_FIELD_NAME])
49+
except ValueError as e:
50+
print('Warn: cannot convert speed value from {}'.format(data_dict[SPEED_FIELD_NAME]))
51+
speed = data_dict[SPEED_FIELD_NAME]
52+
53+
presence = data_dict[PRESENCE_FIELD_NAME].lower()
54+
presence = 'Present' if presence == 'true' else 'Not Present'
55+
status = data_dict[STATUS_FIELD_NAME].lower()
56+
status = 'OK' if status == 'true' else 'Not OK'
57+
58+
table.append((name, speed, data_dict[DIRECTION_FIELD_NAME], presence, status, data_dict[TIMESTAMP_FIELD_NAME]))
59+
60+
if table:
61+
table.sort()
62+
print(tabulate(table, header, tablefmt='simple', stralign='right'))
63+
else:
64+
print('No fan status data available\n')
65+
66+
67+
if __name__ == "__main__":
68+
fanShow = FanShow()
69+
fanShow.show()

scripts/tempershow

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/python
2+
"""
3+
Script to show fan status.
4+
"""
5+
from __future__ import print_function
6+
7+
import argparse
8+
9+
from tabulate import tabulate
10+
from swsssdk import SonicV2Connector
11+
12+
13+
header = ['NAME', 'Temperature', 'High Threshold', 'Low Threshold', 'Critical High Threshold', 'Critical Low Threshold', 'Warning Status', 'Timestamp']
14+
15+
TEMPER_TABLE_NAME = 'TEMPERATURE_INFO'
16+
TEMPER_FIELD_NAME = 'temperature'
17+
TIMESTAMP_FIELD_NAME = 'timestamp'
18+
HIGH_THRESH_FIELD_NAME = 'high_threshold'
19+
LOW_THRESH_FIELD_NAME = 'low_threshold'
20+
CRIT_HIGH_THRESH_FIELD_NAME = 'critical_high_threshold'
21+
CRIT_LOW_THRESH_FIELD_NAME = 'critical_low_threshold'
22+
WARNING_STATUS_FIELD_NAME = 'warning_status'
23+
24+
25+
class TemperShow(object):
26+
def __init__(self):
27+
self.db = SonicV2Connector(host="127.0.0.1")
28+
self.db.connect(self.db.STATE_DB)
29+
30+
def show(self):
31+
keys = self.db.keys(self.db.STATE_DB, TEMPER_TABLE_NAME + '*')
32+
if not keys:
33+
print('Thermal Not detected\n')
34+
return
35+
36+
table = []
37+
for key in keys:
38+
key_list = key.split('|')
39+
if len(key_list) != 2: # error data in DB, log it and ignore
40+
print('Warn: Invalid key in table {}: {}'.format(TEMPER_TABLE_NAME, key))
41+
continue
42+
43+
name = key_list[1]
44+
data_dict = self.db.get_all(self.db.STATE_DB, key)
45+
table.append((name,
46+
data_dict[TEMPER_FIELD_NAME],
47+
data_dict[HIGH_THRESH_FIELD_NAME],
48+
data_dict[LOW_THRESH_FIELD_NAME],
49+
data_dict[CRIT_HIGH_THRESH_FIELD_NAME],
50+
data_dict[CRIT_LOW_THRESH_FIELD_NAME],
51+
data_dict[WARNING_STATUS_FIELD_NAME],
52+
data_dict[TIMESTAMP_FIELD_NAME]
53+
))
54+
55+
if table:
56+
table.sort()
57+
print(tabulate(table, header, tablefmt='simple', stralign='right'))
58+
else:
59+
print('No tempeature data available\n')
60+
61+
62+
if __name__ == "__main__":
63+
temperShow = TemperShow()
64+
temperShow.show()

setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
'scripts/dropconfig',
6868
'scripts/dropstat',
6969
'scripts/ecnconfig',
70+
'scripts/fanshow',
7071
'scripts/fast-reboot',
7172
'scripts/fast-reboot-dump.py',
7273
'scripts/fdbclear',
@@ -91,6 +92,7 @@
9192
'scripts/sfpshow',
9293
'scripts/syseeprom-to-json',
9394
'scripts/teamshow',
95+
'scripts/tempershow',
9496
'scripts/update_json.py',
9597
'scripts/warm-reboot',
9698
'scripts/watermarkstat',

show/main.py

+14
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,20 @@ def ssdhealth(device, verbose, vendor):
16701670
options += " -e" if vendor else ""
16711671
run_command(cmd + options, display_cmd=verbose)
16721672

1673+
# 'fan' subcommand ("show platform fan")
1674+
@platform.command()
1675+
def fan():
1676+
"""Show fan status information"""
1677+
cmd = 'fanshow'
1678+
run_command(cmd)
1679+
1680+
# 'temperature' subcommand ("show platform temperature")
1681+
@platform.command()
1682+
def temperature():
1683+
"""Show device temperature information"""
1684+
cmd = 'tempershow'
1685+
run_command(cmd)
1686+
16731687
#
16741688
# 'logging' command ("show logging")
16751689
#

0 commit comments

Comments
 (0)