From 3086bdd890accedc8369fa75322c3a28d9dd68f9 Mon Sep 17 00:00:00 2001 From: Mridul Bajpai Date: Mon, 19 Jun 2023 12:18:56 -0700 Subject: [PATCH 1/6] Voltage sensor monitoring CLI --- scripts/voltageshow | 61 +++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + show/platform.py | 9 +++++++ 3 files changed, 71 insertions(+) create mode 100755 scripts/voltageshow diff --git a/scripts/voltageshow b/scripts/voltageshow new file mode 100755 index 0000000000..4c26a26592 --- /dev/null +++ b/scripts/voltageshow @@ -0,0 +1,61 @@ +#!/usr/bin/python3 + +""" + Script to show fan status. +""" +from tabulate import tabulate +from swsscommon.swsscommon import SonicV2Connector +from natsort import natsorted + + +header = ['Sensor', 'Voltage(mV)', 'High TH', 'Low TH', 'Crit High TH', 'Crit Low TH', 'Warning', 'Timestamp'] + +VOLTAGE_TABLE_NAME = 'VOLTAGE_INFO' +VOLTAGE_FIELD_NAME = 'voltage' +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 VoltageShow(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, VOLTAGE_TABLE_NAME + '*') + if not keys: + print('Voltage Not detected\n') + 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(VOLTAGE_TABLE_NAME, key)) + continue + + name = key_list[1] + data_dict = self.db.get_all(self.db.STATE_DB, key) + table.append((name, + data_dict[VOLTAGE_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 voltage data available\n') + + +if __name__ == "__main__": + voltageShow = VoltageShow() + voltageShow.show() diff --git a/setup.py b/setup.py index 547b0fac7a..c6df084ab5 100644 --- a/setup.py +++ b/setup.py @@ -170,6 +170,7 @@ 'scripts/tempershow', 'scripts/tunnelstat', 'scripts/update_json.py', + 'scripts/voltageshow', 'scripts/voqutil', 'scripts/warm-reboot', 'scripts/watermarkstat', diff --git a/show/platform.py b/show/platform.py index c4f2c3a29c..e5c3f53c4f 100644 --- a/show/platform.py +++ b/show/platform.py @@ -137,6 +137,15 @@ def temperature(): cmd = ['tempershow'] clicommon.run_command(cmd) + +# 'voltage' subcommand ("show platform voltage") +@platform.command() +def voltage(): + """Show device voltage information""" + cmd = 'voltageshow' + clicommon.run_command(cmd) + + # 'firmware' subcommand ("show platform firmware") @platform.command( context_settings=dict( From 38135135d5186412c871dd2319a9920ee7f942e6 Mon Sep 17 00:00:00 2001 From: Mridul Bajpai Date: Mon, 19 Jun 2023 12:21:31 -0700 Subject: [PATCH 2/6] Fix typo --- scripts/voltageshow | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/voltageshow b/scripts/voltageshow index 4c26a26592..0297829248 100755 --- a/scripts/voltageshow +++ b/scripts/voltageshow @@ -1,7 +1,7 @@ #!/usr/bin/python3 """ - Script to show fan status. + Script to show voltage sensors info. """ from tabulate import tabulate from swsscommon.swsscommon import SonicV2Connector From 582a93aaceb13efc579e5ac4cfe8ee1469d80407 Mon Sep 17 00:00:00 2001 From: Mridul Bajpai Date: Tue, 27 Jun 2023 22:26:00 -0700 Subject: [PATCH 3/6] Add current sensor support --- scripts/currentshow | 61 +++++++++++++++++++++++++++++++++++++++++++++ scripts/voltageshow | 10 ++++---- show/platform.py | 10 +++++++- 3 files changed, 75 insertions(+), 6 deletions(-) create mode 100755 scripts/currentshow diff --git a/scripts/currentshow b/scripts/currentshow new file mode 100755 index 0000000000..45b7db63c4 --- /dev/null +++ b/scripts/currentshow @@ -0,0 +1,61 @@ +#!/usr/bin/python3 + +""" + Script to show fan status. +""" +from tabulate import tabulate +from swsscommon.swsscommon import SonicV2Connector +from natsort import natsorted + + +header = ['Sensor', 'Current(mA)', 'High TH', 'Low TH', 'Crit High TH', 'Crit Low TH', 'Warning', 'Timestamp'] + +CURRENT_TABLE_NAME = 'CURRENT_INFO' +CURRENT_FIELD_NAME = 'current' +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 CurrentShow(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, CURRENT_TABLE_NAME + '*') + if not keys: + print('Current Not detected\n') + 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(CURRENT_TABLE_NAME, key)) + continue + + name = key_list[1] + data_dict = self.db.get_all(self.db.STATE_DB, key) + table.append((name, + data_dict[CURRENT_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 current data available\n') + + +if __name__ == "__main__": + currentShow = CurrentShow() + currentShow.show() diff --git a/scripts/voltageshow b/scripts/voltageshow index 0297829248..c9766e21fa 100755 --- a/scripts/voltageshow +++ b/scripts/voltageshow @@ -1,14 +1,14 @@ #!/usr/bin/python3 """ - Script to show voltage sensors info. + Script to show Voltage Sensor status. """ from tabulate import tabulate from swsscommon.swsscommon import SonicV2Connector from natsort import natsorted -header = ['Sensor', 'Voltage(mV)', 'High TH', 'Low TH', 'Crit High TH', 'Crit Low TH', 'Warning', 'Timestamp'] +header = ['Sensor', 'Voltage', 'High TH', 'Low TH', 'Crit High TH', 'Crit Low TH', 'Warning', 'Timestamp'] VOLTAGE_TABLE_NAME = 'VOLTAGE_INFO' VOLTAGE_FIELD_NAME = 'voltage' @@ -40,8 +40,8 @@ class VoltageShow(object): name = key_list[1] data_dict = self.db.get_all(self.db.STATE_DB, key) - table.append((name, - data_dict[VOLTAGE_FIELD_NAME], + table.append((name, + data_dict[VOLTAGE_FIELD_NAME], data_dict[HIGH_THRESH_FIELD_NAME], data_dict[LOW_THRESH_FIELD_NAME], data_dict[CRIT_HIGH_THRESH_FIELD_NAME], @@ -49,7 +49,7 @@ class VoltageShow(object): data_dict[WARNING_STATUS_FIELD_NAME], data_dict[TIMESTAMP_FIELD_NAME] )) - + if table: print(tabulate(table, header, tablefmt='simple', stralign='right')) else: diff --git a/show/platform.py b/show/platform.py index e5c3f53c4f..477ce308d0 100644 --- a/show/platform.py +++ b/show/platform.py @@ -142,7 +142,15 @@ def temperature(): @platform.command() def voltage(): """Show device voltage information""" - cmd = 'voltageshow' + cmd = ['voltageshow'] + clicommon.run_command(cmd) + + +# 'current' subcommand ("show platform current") +@platform.command() +def current(): + """Show device current information""" + cmd = ['currentshow'] clicommon.run_command(cmd) From d1ae1d093f7202b7e3b2a530fb249f75e2ee9b41 Mon Sep 17 00:00:00 2001 From: Mridul Bajpai Date: Tue, 27 Jun 2023 22:28:55 -0700 Subject: [PATCH 4/6] Current sensor fix --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index c6df084ab5..fab54c6c76 100644 --- a/setup.py +++ b/setup.py @@ -171,6 +171,7 @@ 'scripts/tunnelstat', 'scripts/update_json.py', 'scripts/voltageshow', + 'scripts/currentshow', 'scripts/voqutil', 'scripts/warm-reboot', 'scripts/watermarkstat', From 7c60f480870d94ee726cb660f3e1857f4193fb6f Mon Sep 17 00:00:00 2001 From: Mridul Bajpai Date: Wed, 19 Jul 2023 16:22:12 -0700 Subject: [PATCH 5/6] Consolidated show commands. --- scripts/{currentshow => sensorshow} | 36 +++++++++++------ scripts/voltageshow | 61 ----------------------------- setup.py | 3 +- show/platform.py | 4 +- 4 files changed, 26 insertions(+), 78 deletions(-) rename scripts/{currentshow => sensorshow} (62%) delete mode 100755 scripts/voltageshow diff --git a/scripts/currentshow b/scripts/sensorshow similarity index 62% rename from scripts/currentshow rename to scripts/sensorshow index 45b7db63c4..d2b9cfba27 100755 --- a/scripts/currentshow +++ b/scripts/sensorshow @@ -1,17 +1,16 @@ #!/usr/bin/python3 """ - Script to show fan status. + 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', 'Current(mA)', 'High TH', 'Low TH', 'Crit High TH', 'Crit Low TH', 'Warning', 'Timestamp'] +header = ['Sensor', 'Voltage(mV)', 'High TH', 'Low TH', 'Crit High TH', 'Crit Low TH', 'Warning', 'Timestamp'] -CURRENT_TABLE_NAME = 'CURRENT_INFO' -CURRENT_FIELD_NAME = 'current' TIMESTAMP_FIELD_NAME = 'timestamp' HIGH_THRESH_FIELD_NAME = 'high_threshold' LOW_THRESH_FIELD_NAME = 'low_threshold' @@ -20,28 +19,35 @@ CRIT_LOW_THRESH_FIELD_NAME = 'critical_low_threshold' WARNING_STATUS_FIELD_NAME = 'warning_status' -class CurrentShow(object): - def __init__(self): +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' + header[1] = 'Voltage(mV)' + else: + self.table_name = 'CURRENT_INFO' + header[1] = 'Current(mA)' def show(self): - keys = self.db.keys(self.db.STATE_DB, CURRENT_TABLE_NAME + '*') + keys = self.db.keys(self.db.STATE_DB, self.table_name + '*') if not keys: - print('Current Not detected\n') + print('Sensor Not detected\n') 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(CURRENT_TABLE_NAME, key)) + 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[CURRENT_FIELD_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], @@ -53,9 +59,13 @@ class CurrentShow(object): if table: print(tabulate(table, header, tablefmt='simple', stralign='right')) else: - print('No current data available\n') + print('No sensor data available\n') if __name__ == "__main__": - currentShow = CurrentShow() - currentShow.show() + parser = argparse.ArgumentParser() + parser.add_argument("-t", "--type", help="sensor type", required=True, default="voltage", choices=['voltage', 'current']) + args = parser.parse_args() + + sensorShow = SensorShow(args.type) + sensorShow.show() diff --git a/scripts/voltageshow b/scripts/voltageshow deleted file mode 100755 index c9766e21fa..0000000000 --- a/scripts/voltageshow +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/python3 - -""" - Script to show Voltage Sensor status. -""" -from tabulate import tabulate -from swsscommon.swsscommon import SonicV2Connector -from natsort import natsorted - - -header = ['Sensor', 'Voltage', 'High TH', 'Low TH', 'Crit High TH', 'Crit Low TH', 'Warning', 'Timestamp'] - -VOLTAGE_TABLE_NAME = 'VOLTAGE_INFO' -VOLTAGE_FIELD_NAME = 'voltage' -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 VoltageShow(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, VOLTAGE_TABLE_NAME + '*') - if not keys: - print('Voltage Not detected\n') - 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(VOLTAGE_TABLE_NAME, key)) - continue - - name = key_list[1] - data_dict = self.db.get_all(self.db.STATE_DB, key) - table.append((name, - data_dict[VOLTAGE_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 voltage data available\n') - - -if __name__ == "__main__": - voltageShow = VoltageShow() - voltageShow.show() diff --git a/setup.py b/setup.py index fab54c6c76..3e8812346e 100644 --- a/setup.py +++ b/setup.py @@ -170,8 +170,7 @@ 'scripts/tempershow', 'scripts/tunnelstat', 'scripts/update_json.py', - 'scripts/voltageshow', - 'scripts/currentshow', + 'scripts/sensorshow', 'scripts/voqutil', 'scripts/warm-reboot', 'scripts/watermarkstat', diff --git a/show/platform.py b/show/platform.py index 477ce308d0..4718a49f8d 100644 --- a/show/platform.py +++ b/show/platform.py @@ -142,7 +142,7 @@ def temperature(): @platform.command() def voltage(): """Show device voltage information""" - cmd = ['voltageshow'] + cmd = ['sensorshow -t voltage'] clicommon.run_command(cmd) @@ -150,7 +150,7 @@ def voltage(): @platform.command() def current(): """Show device current information""" - cmd = ['currentshow'] + cmd = ['sensorshow -t current'] clicommon.run_command(cmd) From 14b3f4ae9febceb648eb3c57b0f2841bcfb89378 Mon Sep 17 00:00:00 2001 From: Mridul Bajpai Date: Tue, 8 Aug 2023 22:51:18 -0700 Subject: [PATCH 6/6] Addressed review comments --- scripts/sensorshow | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/scripts/sensorshow b/scripts/sensorshow index d2b9cfba27..453e4fd188 100755 --- a/scripts/sensorshow +++ b/scripts/sensorshow @@ -1,8 +1,8 @@ #!/usr/bin/python3 -""" +''' Script to show Voltage and Current Sensor status. -""" +''' from tabulate import tabulate from swsscommon.swsscommon import SonicV2Connector from natsort import natsorted @@ -17,6 +17,8 @@ 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): @@ -25,16 +27,16 @@ class SensorShow(object): self.db.connect(self.db.STATE_DB) self.field_name = type if type == "voltage": - self.table_name = 'VOLTAGE_INFO' + self.table_name = VOLTAGE_INFO_TABLE_NAME header[1] = 'Voltage(mV)' else: - self.table_name = 'CURRENT_INFO' + 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\n') + print('Sensor not detected') return table = [] @@ -59,13 +61,13 @@ class SensorShow(object): if table: print(tabulate(table, header, tablefmt='simple', stralign='right')) else: - print('No sensor data available\n') + print('No sensor data available') if __name__ == "__main__": parser = argparse.ArgumentParser() - parser.add_argument("-t", "--type", help="sensor type", required=True, default="voltage", choices=['voltage', 'current']) + parser.add_argument("-t", "--type", help="sensor type", required=True, choices=['voltage', 'current']) args = parser.parse_args() - sensorShow = SensorShow(args.type) - sensorShow.show() + sensor_show = SensorShow(args.type) + sensor_show.show()