Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voltage and Current Sensors CLI #2941

Merged
merged 12 commits into from
Sep 7, 2023
73 changes: 73 additions & 0 deletions scripts/sensorshow
Original file line number Diff line number Diff line change
@@ -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']
bmridul marked this conversation as resolved.
Show resolved Hide resolved

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")
bmridul marked this conversation as resolved.
Show resolved Hide resolved
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()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
'scripts/tempershow',
'scripts/tunnelstat',
'scripts/update_json.py',
'scripts/sensorshow',
'scripts/voqutil',
'scripts/warm-reboot',
'scripts/watermarkstat',
Expand Down
17 changes: 17 additions & 0 deletions show/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading