Skip to content

Commit

Permalink
Add command line support for thermal control (sonic-net#777)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Junchao-Mellanox authored and lguohan committed Jan 24, 2020
1 parent 377881d commit c139a23
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
69 changes: 69 additions & 0 deletions scripts/fanshow
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/python
"""
Script to show fan status.
"""
from __future__ import print_function

import argparse

from tabulate import tabulate
from swsssdk import SonicV2Connector


header = ['FAN', 'Speed', 'Direction', 'Presence', 'Status', 'Timestamp']

FAN_TABLE_NAME = 'FAN_INFO'
SPEED_FIELD_NAME = 'speed'
DIRECTION_FIELD_NAME = 'direction'
PRESENCE_FIELD_NAME = 'presence'
STATUS_FIELD_NAME = 'status'
TIMESTAMP_FIELD_NAME = 'timestamp'


class FanShow(object):
def __init__(self):
self.db = SonicV2Connector(host="127.0.0.1")
self.db.connect(self.db.STATE_DB)

def show(self):
keys = self.db.keys(self.db.STATE_DB, FAN_TABLE_NAME + '*')
if not keys:
print('Fan Not detected\n')
return

table = []
for key in keys:
key_list = key.split('|')
if len(key_list) != 2: # error data in DB, log it and ignore
print('Warn: Invalid key in table FAN_INFO: {}'.format(key))
continue

name = key_list[1]
data_dict = self.db.get_all(self.db.STATE_DB, key)
try:
speed = float(data_dict[SPEED_FIELD_NAME])
if speed > 100:
speed = '{}RPM'.format(int(speed))
else:
speed = '{}%'.format(data_dict[SPEED_FIELD_NAME])
except ValueError as e:
print('Warn: cannot convert speed value from {}'.format(data_dict[SPEED_FIELD_NAME]))
speed = data_dict[SPEED_FIELD_NAME]

presence = data_dict[PRESENCE_FIELD_NAME].lower()
presence = 'Present' if presence == 'true' else 'Not Present'
status = data_dict[STATUS_FIELD_NAME].lower()
status = 'OK' if status == 'true' else 'Not OK'

table.append((name, speed, data_dict[DIRECTION_FIELD_NAME], presence, status, data_dict[TIMESTAMP_FIELD_NAME]))

if table:
table.sort()
print(tabulate(table, header, tablefmt='simple', stralign='right'))
else:
print('No fan status data available\n')


if __name__ == "__main__":
fanShow = FanShow()
fanShow.show()
64 changes: 64 additions & 0 deletions scripts/tempershow
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/python
"""
Script to show fan status.
"""
from __future__ import print_function

import argparse

from tabulate import tabulate
from swsssdk import SonicV2Connector


header = ['NAME', 'Temperature', 'High Threshold', 'Low Threshold', 'Critical High Threshold', 'Critical Low Threshold', 'Warning Status', 'Timestamp']

TEMPER_TABLE_NAME = 'TEMPERATURE_INFO'
TEMPER_FIELD_NAME = 'temperature'
TIMESTAMP_FIELD_NAME = 'timestamp'
HIGH_THRESH_FIELD_NAME = 'high_threshold'
LOW_THRESH_FIELD_NAME = 'low_threshold'
CRIT_HIGH_THRESH_FIELD_NAME = 'critical_high_threshold'
CRIT_LOW_THRESH_FIELD_NAME = 'critical_low_threshold'
WARNING_STATUS_FIELD_NAME = 'warning_status'


class TemperShow(object):
def __init__(self):
self.db = SonicV2Connector(host="127.0.0.1")
self.db.connect(self.db.STATE_DB)

def show(self):
keys = self.db.keys(self.db.STATE_DB, TEMPER_TABLE_NAME + '*')
if not keys:
print('Thermal Not detected\n')
return

table = []
for key in keys:
key_list = key.split('|')
if len(key_list) != 2: # error data in DB, log it and ignore
print('Warn: Invalid key in table {}: {}'.format(TEMPER_TABLE_NAME, key))
continue

name = key_list[1]
data_dict = self.db.get_all(self.db.STATE_DB, key)
table.append((name,
data_dict[TEMPER_FIELD_NAME],
data_dict[HIGH_THRESH_FIELD_NAME],
data_dict[LOW_THRESH_FIELD_NAME],
data_dict[CRIT_HIGH_THRESH_FIELD_NAME],
data_dict[CRIT_LOW_THRESH_FIELD_NAME],
data_dict[WARNING_STATUS_FIELD_NAME],
data_dict[TIMESTAMP_FIELD_NAME]
))

if table:
table.sort()
print(tabulate(table, header, tablefmt='simple', stralign='right'))
else:
print('No tempeature data available\n')


if __name__ == "__main__":
temperShow = TemperShow()
temperShow.show()
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'scripts/dropconfig',
'scripts/dropstat',
'scripts/ecnconfig',
'scripts/fanshow',
'scripts/fast-reboot',
'scripts/fast-reboot-dump.py',
'scripts/fdbclear',
Expand All @@ -91,6 +92,7 @@
'scripts/sfpshow',
'scripts/syseeprom-to-json',
'scripts/teamshow',
'scripts/tempershow',
'scripts/update_json.py',
'scripts/warm-reboot',
'scripts/watermarkstat',
Expand Down
14 changes: 14 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,20 @@ def ssdhealth(device, verbose, vendor):
options += " -e" if vendor else ""
run_command(cmd + options, display_cmd=verbose)

# 'fan' subcommand ("show platform fan")
@platform.command()
def fan():
"""Show fan status information"""
cmd = 'fanshow'
run_command(cmd)

# 'temperature' subcommand ("show platform temperature")
@platform.command()
def temperature():
"""Show device temperature information"""
cmd = 'tempershow'
run_command(cmd)

#
# 'logging' command ("show logging")
#
Expand Down

0 comments on commit c139a23

Please sign in to comment.