diff --git a/scripts/tempershow b/scripts/tempershow index 0711bce817..77b79795d2 100644 --- a/scripts/tempershow +++ b/scripts/tempershow @@ -3,6 +3,9 @@ """ Script to show fan status. """ +import argparse +import json + from tabulate import tabulate from swsscommon.swsscommon import SonicV2Connector from natsort import natsorted @@ -25,13 +28,14 @@ class TemperShow(object): self.db = SonicV2Connector(host="127.0.0.1") self.db.connect(self.db.STATE_DB) - def show(self): + def show(self, output_json): keys = self.db.keys(self.db.STATE_DB, TEMPER_TABLE_NAME + '*') if not keys: print('Thermal Not detected\n') return table = [] + json_output = [] for key in natsorted(keys): key_list = key.split('|') if len(key_list) != 2: # error data in DB, log it and ignore @@ -40,22 +44,47 @@ class TemperShow(object): 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 output_json: + json_output.append({ + "Sensor": name, + "Temperature": data_dict[TEMPER_FIELD_NAME], + "High_TH": data_dict[HIGH_THRESH_FIELD_NAME], + "Low_TH": data_dict[LOW_THRESH_FIELD_NAME], + "Crit_High_TH": data_dict[CRIT_HIGH_THRESH_FIELD_NAME], + "Crit_Low_TH": data_dict[CRIT_LOW_THRESH_FIELD_NAME], + "Warning": data_dict[WARNING_STATUS_FIELD_NAME], + "Timestamp": data_dict[TIMESTAMP_FIELD_NAME] + }) + + else: + 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: + if output_json: + print(json.dumps(json_output, indent=2)) + elif table: print(tabulate(table, header, tablefmt='simple', stralign='right')) else: - print('No tempeature data available\n') - + print('No temperature data available\n') + if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Display the temperature Sensor information', + formatter_class=argparse.RawTextHelpFormatter, + epilog=""" +Examples: + tempershow -j +""") + + parser.add_argument('-j', '--json', action='store_true', help='Display output in JSON format') + args = parser.parse_args() + output_json = args.json temperShow = TemperShow() - temperShow.show() + temperShow.show(output_json) diff --git a/show/platform.py b/show/platform.py index d3e45c90c4..7a4d870794 100644 --- a/show/platform.py +++ b/show/platform.py @@ -150,9 +150,12 @@ def fan(): # 'temperature' subcommand ("show platform temperature") @platform.command() -def temperature(): +@click.option('-j', '--json', is_flag=True, help="Output in JSON format") +def temperature(json): """Show device temperature information""" cmd = ['tempershow'] + if json: + cmd += ["-j"] clicommon.run_command(cmd) diff --git a/tests/show_platform_test.py b/tests/show_platform_test.py index 2707ac7012..2e8fc35437 100644 --- a/tests/show_platform_test.py +++ b/tests/show_platform_test.py @@ -51,6 +51,32 @@ def test_summary(self): assert result.output == textwrap.dedent(expected_output) +class TestShowPlatformTemperature(object): + """ + Note: `show platform temperature` simply calls the `tempershow` utility and + passes a variety of options. Here we test that the utility is called + with the appropriate option(s). The functionality of the underlying + `tempershow` utility is expected to be tested by a separate suite of unit tests + """ + def test_temperature(self): + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], []) + assert mock_run_command.call_count == 1 + mock_run_command.assert_called_with(['tempershow']) + + def test_temperature_json(self): + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], ['--json']) + assert mock_run_command.call_count == 1 + mock_run_command.assert_called_with(['tempershow', '-j']) + + def test_temperature_short_json(self): + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], ['-j']) + assert mock_run_command.call_count == 1 + mock_run_command.assert_called_with(['tempershow', '-j']) + + class TestShowPlatformPsu(object): """ Note: `show platform psustatus` simply calls the `psushow` utility and