Skip to content

Commit 732dc09

Browse files
authored
Added json support for show platform temperature (sonic-net#3874)
What I did Added json support for show platform temperature How I did it How to verify it admin@sonic:~$ show platform temperature Sensor Temperature High TH Low TH Crit High TH Crit Low TH Warning Timestamp ------------------------ ------------- --------- -------- -------------- ------------- --------- ----------------- CB_temp(0x4B) 29.5 80.0 N/A N/A N/A False 20240923 00:32:07 CPU_Core_0_temp 46.0 82.0 N/A 104.0 N/A False 20240923 00:32:07 CPU_Core_1_temp 46.0 82.0 N/A 104.0 N/A False 20240923 00:32:07 CPU_Core_2_temp 46.0 82.0 N/A 104.0 N/A False 20240923 00:32:07 CPU_Core_3_temp 45.0 82.0 N/A 104.0 N/A False 20240923 00:32:07 CPU_Package_temp 46.0 82.0 N/A 104.0 N/A False 20240923 00:32:07 FB_temp(0x4C) 35.0 80.0 N/A N/A N/A False 20240923 00:32:07 MB_FrontMAC_temp(0x49) 28.5 80.0 N/A N/A N/A False 20240923 00:32:07 MB_LeftCenter_temp(0x4A) 28.0 80.0 N/A N/A N/A False 20240923 00:32:07 MB_RearMAC_temp(0x48) 30.5 80.0 N/A N/A N/A False 20240923 00:32:07 PSU-1 temp sensor 1 N/A N/A N/A N/A N/A False 20240923 00:32:07 PSU-2 temp sensor 1 37.0 80.0 N/A N/A N/A False 20240923 00:32:07 admin@sonic:~$ show platform temperature --help Usage: show platform temperature [OPTIONS] Show device temperature information Options: --json Output in JSON format -h, -?, --help Show this message and exit. admin@sonic:~$ show platform temperature --json [ { "Sensor": "CB_temp(0x4B)", "Temperature": "29.5", "High_TH": "80.0", "Low_TH": "N/A", "Crit_High_TH": "N/A", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "CPU_Core_0_temp", "Temperature": "46.0", "High_TH": "82.0", "Low_TH": "N/A", "Crit_High_TH": "104.0", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "CPU_Core_1_temp", "Temperature": "46.0", "High_TH": "82.0", "Low_TH": "N/A", "Crit_High_TH": "104.0", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "CPU_Core_2_temp", "Temperature": "46.0", "High_TH": "82.0", "Low_TH": "N/A", "Crit_High_TH": "104.0", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "CPU_Core_3_temp", "Temperature": "45.0", "High_TH": "82.0", "Low_TH": "N/A", "Crit_High_TH": "104.0", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "CPU_Package_temp", "Temperature": "46.0", "High_TH": "82.0", "Low_TH": "N/A", "Crit_High_TH": "104.0", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "FB_temp(0x4C)", "Temperature": "35.0", "High_TH": "80.0", "Low_TH": "N/A", "Crit_High_TH": "N/A", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "MB_FrontMAC_temp(0x49)", "Temperature": "28.5", "High_TH": "80.0", "Low_TH": "N/A", "Crit_High_TH": "N/A", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "MB_LeftCenter_temp(0x4A)", "Temperature": "28.0", "High_TH": "80.0", "Low_TH": "N/A", "Crit_High_TH": "N/A", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "MB_RearMAC_temp(0x48)", "Temperature": "30.5", "High_TH": "80.0", "Low_TH": "N/A", "Crit_High_TH": "N/A", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "PSU-1 temp sensor 1", "Temperature": "N/A", "High_TH": "N/A", "Low_TH": "N/A", "Crit_High_TH": "N/A", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" }, { "Sensor": "PSU-2 temp sensor 1", "Temperature": "37.0", "High_TH": "80.0", "Low_TH": "N/A", "Crit_High_TH": "N/A", "Crit_Low_TH": "N/A", "Warning": "False", "Timestamp": "20240923 00:32:07" } ]
1 parent bacff45 commit 732dc09

File tree

3 files changed

+109
-17
lines changed

3 files changed

+109
-17
lines changed

scripts/tempershow

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"""
44
Script to show fan status.
55
"""
6+
import argparse
7+
import json
8+
69
from tabulate import tabulate
710
from swsscommon.swsscommon import SonicV2Connector
811
from natsort import natsorted
@@ -25,13 +28,14 @@ class TemperShow(object):
2528
self.db = SonicV2Connector(host="127.0.0.1")
2629
self.db.connect(self.db.STATE_DB)
2730

28-
def show(self):
31+
def show(self, output_json):
2932
keys = self.db.keys(self.db.STATE_DB, TEMPER_TABLE_NAME + '*')
3033
if not keys:
3134
print('Thermal Not detected\n')
3235
return
3336

3437
table = []
38+
json_output = []
3539
for key in natsorted(keys):
3640
key_list = key.split('|')
3741
if len(key_list) != 2: # error data in DB, log it and ignore
@@ -40,22 +44,47 @@ class TemperShow(object):
4044

4145
name = key_list[1]
4246
data_dict = self.db.get_all(self.db.STATE_DB, key)
43-
table.append((name,
44-
data_dict[TEMPER_FIELD_NAME],
45-
data_dict[HIGH_THRESH_FIELD_NAME],
46-
data_dict[LOW_THRESH_FIELD_NAME],
47-
data_dict[CRIT_HIGH_THRESH_FIELD_NAME],
48-
data_dict[CRIT_LOW_THRESH_FIELD_NAME],
49-
data_dict[WARNING_STATUS_FIELD_NAME],
50-
data_dict[TIMESTAMP_FIELD_NAME]
51-
))
47+
if output_json:
48+
json_output.append({
49+
"Sensor": name,
50+
"Temperature": data_dict[TEMPER_FIELD_NAME],
51+
"High_TH": data_dict[HIGH_THRESH_FIELD_NAME],
52+
"Low_TH": data_dict[LOW_THRESH_FIELD_NAME],
53+
"Crit_High_TH": data_dict[CRIT_HIGH_THRESH_FIELD_NAME],
54+
"Crit_Low_TH": data_dict[CRIT_LOW_THRESH_FIELD_NAME],
55+
"Warning": data_dict[WARNING_STATUS_FIELD_NAME],
56+
"Timestamp": data_dict[TIMESTAMP_FIELD_NAME]
57+
})
58+
59+
else:
60+
table.append((name,
61+
data_dict[TEMPER_FIELD_NAME],
62+
data_dict[HIGH_THRESH_FIELD_NAME],
63+
data_dict[LOW_THRESH_FIELD_NAME],
64+
data_dict[CRIT_HIGH_THRESH_FIELD_NAME],
65+
data_dict[CRIT_LOW_THRESH_FIELD_NAME],
66+
data_dict[WARNING_STATUS_FIELD_NAME],
67+
data_dict[TIMESTAMP_FIELD_NAME]
68+
))
5269

53-
if table:
70+
if output_json:
71+
print(json.dumps(json_output, indent=2))
72+
elif table:
5473
print(tabulate(table, header, tablefmt='simple', stralign='right'))
5574
else:
56-
print('No tempeature data available\n')
57-
75+
print('No temperature data available\n')
5876

77+
5978
if __name__ == "__main__":
79+
parser = argparse.ArgumentParser(description='Display the temperature Sensor information',
80+
formatter_class=argparse.RawTextHelpFormatter,
81+
epilog="""
82+
Examples:
83+
tempershow -j
84+
""")
85+
86+
parser.add_argument('-j', '--json', action='store_true', help='Display output in JSON format')
87+
args = parser.parse_args()
88+
output_json = args.json
6089
temperShow = TemperShow()
61-
temperShow.show()
90+
temperShow.show(output_json)

show/platform.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import json
23
import subprocess
34
import sys
45

@@ -150,11 +151,19 @@ def fan():
150151

151152
# 'temperature' subcommand ("show platform temperature")
152153
@platform.command()
153-
def temperature():
154+
@click.option('-j', '--json', 'output_json', is_flag=True, help="Output in JSON format")
155+
def temperature(output_json):
154156
"""Show device temperature information"""
155157
cmd = ['tempershow']
156-
clicommon.run_command(cmd)
157-
158+
if output_json:
159+
output, _ = clicommon.run_command(cmd+["-j"], return_cmd=True)
160+
try:
161+
data = json.loads(output)
162+
click.echo(clicommon.json_dump(data))
163+
except json.JSONDecodeError as e:
164+
click.echo(f"Error: Invalid JSON output: {e}", err=True)
165+
else:
166+
clicommon.run_command(cmd)
158167

159168
# 'voltage' subcommand ("show platform voltage")
160169
@platform.command()

tests/show_platform_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import json
23
import sys
34
import textwrap
45
from unittest import mock
@@ -51,6 +52,59 @@ def test_summary(self):
5152
assert result.output == textwrap.dedent(expected_output)
5253

5354

55+
class TestShowPlatformTemperature(object):
56+
"""
57+
Note: `show platform temperature` simply calls the `tempershow` utility and
58+
passes a variety of options. Here we test that the utility is called
59+
with the appropriate option(s). The functionality of the underlying
60+
`tempershow` utility is expected to be tested by a separate suite of unit tests
61+
"""
62+
rc_output = """\
63+
[
64+
{
65+
"Sensor": "CB_temp(0x4B)",
66+
"Temperature": "29.5",
67+
"High_TH": "80.0",
68+
"Low_TH": "N/A",
69+
"Crit_High_TH": "N/A",
70+
"Crit_Low_TH": "N/A",
71+
"Warning": "False",
72+
"Timestamp": "20240923 00:32:07"
73+
},
74+
{
75+
"Sensor": "CPU_Core_0_temp",
76+
"Temperature": "46.0",
77+
"High_TH": "82.0",
78+
"Low_TH": "N/A",
79+
"Crit_High_TH": "104.0",
80+
"Crit_Low_TH": "N/A",
81+
"Warning": "False",
82+
"Timestamp": "20240923 00:32:07"
83+
}
84+
]
85+
"""
86+
87+
def test_temperature(self):
88+
with mock.patch('utilities_common.cli.run_command') as mock_run_command:
89+
CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], [])
90+
assert mock_run_command.call_count == 1
91+
mock_run_command.assert_called_with(['tempershow'])
92+
93+
def test_temperature_json(self):
94+
with mock.patch('utilities_common.cli.run_command', return_value=(self.rc_output, 0)) as mock_run_command:
95+
result = CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], ['--json'])
96+
assert json.loads(result.output) == json.loads(self.rc_output)
97+
assert mock_run_command.call_count == 1
98+
mock_run_command.assert_called_with(['tempershow', '-j'], return_cmd=True)
99+
100+
def test_temperature_short_json(self):
101+
with mock.patch('utilities_common.cli.run_command', return_value=(self.rc_output, 0)) as mock_run_command:
102+
result = CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], ['-j'])
103+
assert json.loads(result.output) == json.loads(self.rc_output)
104+
assert mock_run_command.call_count == 1
105+
mock_run_command.assert_called_with(['tempershow', '-j'], return_cmd=True)
106+
107+
54108
class TestShowPlatformPsu(object):
55109
"""
56110
Note: `show platform psustatus` simply calls the `psushow` utility and

0 commit comments

Comments
 (0)