diff --git a/scripts/sensorshow b/scripts/sensorshow new file mode 100755 index 0000000000..453e4fd188 --- /dev/null +++ b/scripts/sensorshow @@ -0,0 +1,73 @@ +#!/usr/bin/python3 + +''' + Script to show Voltage and Current Sensor status. +''' +from tabulate import tabulate +from swsscommon.swsscommon import SonicV2Connector +from natsort import natsorted +import argparse + + +header = ['Sensor', 'Voltage(mV)', 'High TH', 'Low TH', 'Crit High TH', 'Crit Low TH', 'Warning', 'Timestamp'] + +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' +VOLTAGE_INFO_TABLE_NAME = 'VOLTAGE_INFO' +CURRENT_INFO_TABLE_NAME = 'CURRENT_INFO' + + +class SensorShow(object): + def __init__(self, type): + self.db = SonicV2Connector(host="127.0.0.1") + self.db.connect(self.db.STATE_DB) + self.field_name = type + if type == "voltage": + self.table_name = VOLTAGE_INFO_TABLE_NAME + header[1] = 'Voltage(mV)' + else: + self.table_name = CURRENT_INFO_TABLE_NAME + header[1] = 'Current(mA)' + + def show(self): + keys = self.db.keys(self.db.STATE_DB, self.table_name + '*') + if not keys: + print('Sensor not detected') + return + + table = [] + for key in natsorted(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(self.table_name, key)) + continue + + name = key_list[1] + data_dict = self.db.get_all(self.db.STATE_DB, key) + table.append((name, + data_dict[self.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: + print(tabulate(table, header, tablefmt='simple', stralign='right')) + else: + print('No sensor data available') + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("-t", "--type", help="sensor type", required=True, choices=['voltage', 'current']) + args = parser.parse_args() + + sensor_show = SensorShow(args.type) + sensor_show.show() diff --git a/setup.py b/setup.py index 547b0fac7a..3e8812346e 100644 --- a/setup.py +++ b/setup.py @@ -170,6 +170,7 @@ 'scripts/tempershow', 'scripts/tunnelstat', 'scripts/update_json.py', + 'scripts/sensorshow', 'scripts/voqutil', 'scripts/warm-reboot', 'scripts/watermarkstat', diff --git a/show/platform.py b/show/platform.py index c4f2c3a29c..4718a49f8d 100644 --- a/show/platform.py +++ b/show/platform.py @@ -137,6 +137,23 @@ def temperature(): cmd = ['tempershow'] clicommon.run_command(cmd) + +# 'voltage' subcommand ("show platform voltage") +@platform.command() +def voltage(): + """Show device voltage information""" + cmd = ['sensorshow -t voltage'] + clicommon.run_command(cmd) + + +# 'current' subcommand ("show platform current") +@platform.command() +def current(): + """Show device current information""" + cmd = ['sensorshow -t current'] + clicommon.run_command(cmd) + + # 'firmware' subcommand ("show platform firmware") @platform.command( context_settings=dict(