From 25e17def399f65b280755f0a3a783678865a411b Mon Sep 17 00:00:00 2001 From: Alexander Allen Date: Fri, 4 Jun 2021 12:34:08 -0400 Subject: [PATCH] [show platform summary] Add chassis hardware info to platform summary and version (#1624) #### What I did I added chassis model number, serial number, and hardware revision to the commands `show platform summary` and `show version` #### How I did it I refactored and modified the existing `get_hw_info_dict()` function to make calls to STATE_DB and get the chassis information populated by https://github.com/Azure/sonic-platform-daemons/pull/183 script. The new refactored versions of `get_hw_info_dict()` are added here https://github.com/Azure/sonic-buildimage/pull/7652 --- doc/Command-Reference.md | 19 +++++++++++--- show/main.py | 33 +++++++++---------------- show/platform.py | 49 +++++++++++++++++++++++-------------- tests/show_platform_test.py | 22 ++++++++++++----- 4 files changed, 73 insertions(+), 50 deletions(-) diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index fa0afe36aa41..1d099631a911 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -441,6 +441,15 @@ This command displays relevant information as the SONiC and Linux kernel version Build date: Fri Mar 22 01:55:48 UTC 2019 Built by: johnar@jenkins-worker-4 + Platform: x86_64-mlnx_msn2700-r0 + HwSKU: Mellanox-SN2700 + ASIC: mellanox + ASIC Count: 1 + Serial Number: MT1822K07815 + Model Number: MSN2700-CS2FO + Hardware Rev: A1 + Uptime: 14:40:15 up 3 min, 1 user, load average: 1.26, 1.45, 0.66 + Docker images: REPOSITORY TAG IMAGE ID SIZE docker-syncd-brcm HEAD.32-21ea29a 434240daff6e 362MB @@ -686,9 +695,13 @@ This command displays a summary of the device's hardware platform - Example: ``` admin@sonic:~$ show platform summary - Platform: x86_64-dell_s6000_s1220-r0 - HwSKU: Force10-S6000 - ASIC: broadcom + Platform: x86_64-mlnx_msn2700-r0 + HwSKU: Mellanox-SN2700 + ASIC: mellanox + ASIC Count: 1 + Serial Number: MT1822K07815 + Model Number: MSN2700-CS2FO + Hardware Rev: A1 ``` **show platform syseeprom** diff --git a/show/main.py b/show/main.py index 3afca1c93a7b..ac2cb51ac96d 100755 --- a/show/main.py +++ b/show/main.py @@ -8,7 +8,7 @@ import utilities_common.cli as clicommon import utilities_common.multi_asic as multi_asic_util from natsort import natsorted -from sonic_py_common import device_info, multi_asic +from sonic_py_common import device_info from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector from tabulate import tabulate from utilities_common import util_base @@ -956,22 +956,9 @@ def logging(process, lines, follow, verbose): def version(verbose): """Show version information""" version_info = device_info.get_sonic_version_info() - - platform = device_info.get_platform() - hwsku = device_info.get_hwsku() - asic_type = version_info['asic_type'] - asic_count = multi_asic.get_num_asics() - - serial_number = None - db = SonicV2Connector() - db.connect(db.STATE_DB) - eeprom_table = db.get_all(db.STATE_DB, 'EEPROM_INFO|0x23') - if "Name" in eeprom_table and eeprom_table["Name"] == "Serial Number" and "Value" in eeprom_table: - serial_number = eeprom_table["Value"] - else: - serial_number_cmd = "sudo decode-syseeprom -s" - serial_number = subprocess.Popen(serial_number_cmd, shell=True, text=True, stdout=subprocess.PIPE).stdout.read() - + platform_info = device_info.get_platform_info() + chassis_info = platform.get_chassis_info() + sys_uptime_cmd = "uptime" sys_uptime = subprocess.Popen(sys_uptime_cmd, shell=True, text=True, stdout=subprocess.PIPE) @@ -981,11 +968,13 @@ def version(verbose): click.echo("Build commit: {}".format(version_info['commit_id'])) click.echo("Build date: {}".format(version_info['build_date'])) click.echo("Built by: {}".format(version_info['built_by'])) - click.echo("\nPlatform: {}".format(platform)) - click.echo("HwSKU: {}".format(hwsku)) - click.echo("ASIC: {}".format(asic_type)) - click.echo("ASIC Count: {}".format(asic_count)) - click.echo("Serial Number: {}".format(serial_number.strip())) + click.echo("\nPlatform: {}".format(platform_info['platform'])) + click.echo("HwSKU: {}".format(platform_info['hwsku'])) + click.echo("ASIC: {}".format(platform_info['asic_type'])) + click.echo("ASIC Count: {}".format(platform_info['asic_count'])) + click.echo("Serial Number: {}".format(chassis_info['serial'])) + click.echo("Model Number: {}".format(chassis_info['model'])) + click.echo("Hardware Revision: {}".format(chassis_info['revision'])) click.echo("Uptime: {}".format(sys_uptime.stdout.read().strip())) click.echo("\nDocker images:") cmd = 'sudo docker images --format "table {{.Repository}}\\t{{.Tag}}\\t{{.ID}}\\t{{.Size}}"' diff --git a/show/platform.py b/show/platform.py index 7e8f8f444c5d..739ef5f476f2 100644 --- a/show/platform.py +++ b/show/platform.py @@ -4,24 +4,33 @@ import click import utilities_common.cli as clicommon -from sonic_py_common import device_info, multi_asic +from sonic_py_common import device_info +# +# Helper functions +# -def get_hw_info_dict(): +def get_chassis_info(): """ - This function is used to get the HW info helper function + Attempts to get the chassis info via STATE_DB and falls back to direct Platform API calls. """ - hw_info_dict = {} - version_info = device_info.get_sonic_version_info() + chassis_info = device_info.get_chassis_info() + required_keys = ['serial', 'model', 'revision'] + failed_vals = ['', 'N/A'] + platform_chassis = None - hw_info_dict['platform'] = device_info.get_platform() - hw_info_dict['hwsku'] = device_info.get_hwsku() - hw_info_dict['asic_type'] = version_info['asic_type'] - hw_info_dict['asic_count'] = multi_asic.get_num_asics() - - return hw_info_dict + for k in required_keys: + if chassis_info.get(k, '') in failed_vals: + if platform_chassis is None: + import platform + platform_chassis = sonic_platform.platform.Platform().get_chassis() + try: + chassis_info[k] = getattr(platform_chassis, "get_".format(k))() + except AttributeError: + chassis_info[k] = 'N/A' + return chassis_info # # 'platform' group ("show platform ...") @@ -38,17 +47,19 @@ def platform(): @click.option('--json', is_flag=True, help="Output in JSON format") def summary(json): """Show hardware platform information""" - - hw_info_dict = {} - hw_info_dict = get_hw_info_dict() + platform_info = device_info.get_platform_info() + chassis_info = get_chassis_info() if json: - click.echo(clicommon.json_dump(hw_info_dict)) + click.echo(clicommon.json_dump({**platform_info, **chassis_info})) else: - click.echo("Platform: {}".format(hw_info_dict['platform'])) - click.echo("HwSKU: {}".format(hw_info_dict['hwsku'])) - click.echo("ASIC: {}".format(hw_info_dict['asic_type'])) - click.echo("ASIC Count: {}".format(hw_info_dict['asic_count'])) + click.echo("Platform: {}".format(platform_info['platform'])) + click.echo("HwSKU: {}".format(platform_info['hwsku'])) + click.echo("ASIC: {}".format(platform_info['asic_type'])) + click.echo("ASIC Count: {}".format(platform_info['asic_count'])) + click.echo("Serial Number: {}".format(chassis_info['serial'])) + click.echo("Model Number: {}".format(chassis_info['model'])) + click.echo("Hardware Revision: {}".format(chassis_info['revision'])) # 'syseeprom' subcommand ("show platform syseeprom") diff --git a/tests/show_platform_test.py b/tests/show_platform_test.py index 937059dcde19..4dcf73a9785d 100644 --- a/tests/show_platform_test.py +++ b/tests/show_platform_test.py @@ -26,6 +26,10 @@ class TestShowPlatform(object): TEST_PLATFORM = "x86_64-mlnx_msn2700-r0" TEST_HWSKU = "Mellanox-SN2700" TEST_ASIC_TYPE = "mellanox" + TEST_ASIC_COUNT = 1 + TEST_SERIAL = "MT1822K07815" + TEST_MODEL = "MSN2700-CS2FO" + TEST_REV = "A1" # Test 'show platform summary' def test_summary(self): @@ -33,12 +37,18 @@ def test_summary(self): Platform: {} HwSKU: {} ASIC: {} - """.format(self.TEST_PLATFORM, self.TEST_HWSKU, self.TEST_ASIC_TYPE) - - with mock.patch("show.platform.get_hw_info_dict", - return_value={"platform": self.TEST_PLATFORM, "hwsku": self.TEST_HWSKU, "asic_type": self.TEST_ASIC_TYPE}): - result = CliRunner().invoke(show.cli.commands["platform"].commands["summary"], []) - assert result.output == textwrap.dedent(expected_output) + ASIC Count: {} + Serial Number: {} + Model Number: {} + Hardware Revision: {} + """.format(self.TEST_PLATFORM, self.TEST_HWSKU, self.TEST_ASIC_TYPE, self.TEST_ASIC_COUNT, self.TEST_SERIAL, self.TEST_MODEL, self.TEST_REV) + + with mock.patch("sonic_py_common.device_info.get_platform_info", + return_value={"platform": self.TEST_PLATFORM, "hwsku": self.TEST_HWSKU, "asic_type": self.TEST_ASIC_TYPE, "asic_count": self.TEST_ASIC_COUNT}): + with mock.patch("show.platform.get_chassis_info", + return_value={"serial": self.TEST_SERIAL, "model": self.TEST_MODEL, "revision": self.TEST_REV}): + result = CliRunner().invoke(show.cli.commands["platform"].commands["summary"], []) + assert result.output == textwrap.dedent(expected_output) class TestShowPlatformPsu(object):