From 43c9560448f472ed88d16da7ae5de7d1289146f5 Mon Sep 17 00:00:00 2001 From: zhangjing Date: Mon, 14 Feb 2022 21:16:55 +0000 Subject: [PATCH 1/2] add last switchover time to show muxcable status --- show/muxcable.py | 52 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/show/muxcable.py b/show/muxcable.py index 6d4b1bab76..a1190f2edb 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -275,7 +275,7 @@ def get_switch_name(config_db): sys.exit(STATUS_FAIL) -def create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, muxcable_health_dict, asic_index, port): +def create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, muxcable_health_dict, muxcable_metrics_dict, asic_index, port): status_value = get_value_for_key_in_dict(muxcable_info_dict[asic_index], port, "state", "MUX_CABLE_TABLE") port_name = platform_sfputil_helper.get_interface_alias(port, db) @@ -284,18 +284,33 @@ def create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, m health_value = get_value_for_key_in_dict(muxcable_health_dict[asic_index], port, "state", "MUX_LINKMGR_TABLE") port_status_dict["MUX_CABLE"][port_name]["HEALTH"] = health_value + last_switch_end_time = "" + if "linkmgrd_switch_standby_end" in muxcable_metrics_dict[asic_index]: + last_switch_end_time = muxcable_metrics_dict[asic_index].get("linkmgrd_switch_standby_end") + elif "linkmgrd_switch_active_end" in muxcable_metrics_dict[asic_index]: + last_switch_end_time = muxcable_metrics_dict[asic_index].get("linkmgrd_switch_active_end") + port_status_dict["MUX_CABLE"][port_name]["LAST_SWITCHOVER_TIME"] = last_switch_end_time -def create_table_dump_per_port_status(db, print_data, muxcable_info_dict, muxcable_health_dict, asic_index, port): +def create_table_dump_per_port_status(db, print_data, muxcable_info_dict, muxcable_health_dict, muxcable_metrics_dict, asic_index, port): print_port_data = [] status_value = get_value_for_key_in_dict(muxcable_info_dict[asic_index], port, "state", "MUX_CABLE_TABLE") #status_value = get_value_for_key_in_tbl(y_cable_asic_table, port, "status") health_value = get_value_for_key_in_dict(muxcable_health_dict[asic_index], port, "state", "MUX_LINKMGR_TABLE") + + last_switch_end_time = "" + if "linkmgrd_switch_standby_end" in muxcable_metrics_dict[asic_index]: + last_switch_end_time = muxcable_metrics_dict[asic_index].get("linkmgrd_switch_standby_end") + elif "linkmgrd_switch_active_end" in muxcable_metrics_dict[asic_index]: + last_switch_end_time = muxcable_metrics_dict[asic_index].get("linkmgrd_switch_active_end") + port_status_dict["MUX_CABLE"][port_name]["LAST_SWITCHOVER_TIME"] = last_switch_end_time + port_name = platform_sfputil_helper.get_interface_alias(port, db) print_port_data.append(port_name) print_port_data.append(status_value) print_port_data.append(health_value) + print_port_data.append(last_switch_end_time) print_data.append(print_port_data) @@ -336,9 +351,11 @@ def status(db, port, json_output): port_table_keys = {} port_health_table_keys = {} + port_metrics_table_keys = {} per_npu_statedb = {} muxcable_info_dict = {} muxcable_health_dict = {} + muxcable_metrics_dict = {} # Getting all front asic namespace and correspding config and state DB connector @@ -352,6 +369,8 @@ def status(db, port, json_output): per_npu_statedb[asic_id].STATE_DB, 'MUX_CABLE_TABLE|*') port_health_table_keys[asic_id] = per_npu_statedb[asic_id].keys( per_npu_statedb[asic_id].STATE_DB, 'MUX_LINKMGR_TABLE|*') + port_metrics_table_keys[asic_id] = per_npu_statedb[asic_id].keys( + per_npu_statedb[asic_id].STATE_DB, 'MUX_METRICS_TABLE|*') if port is not None: asic_index = None @@ -371,27 +390,33 @@ def status(db, port, json_output): per_npu_statedb[asic_index].STATE_DB, 'MUX_CABLE_TABLE|{}'.format(port)) muxcable_health_dict[asic_index] = per_npu_statedb[asic_index].get_all( per_npu_statedb[asic_index].STATE_DB, 'MUX_LINKMGR_TABLE|{}'.format(port)) + muxcable_metrics_dict[asic_index] = per_npu_statedb[asic_index].get_all( + per_npu_statedb[asic_index].STATE_DB, 'MUX_METRICS_TABLE|{}'.format(port)) if muxcable_info_dict[asic_index] is not None: logical_key = "MUX_CABLE_TABLE|{}".format(port) logical_health_key = "MUX_LINKMGR_TABLE|{}".format(port) + logical_metrics_key = "MUX_METRICS_TABLE|{}".format(port) if logical_key in port_table_keys[asic_index] and logical_health_key in port_health_table_keys[asic_index]: + + if logical_metrics_key not in port_metrics_table_keys[asic_index]: + muxcable_metrics_dict[asic_index] = {} if json_output: port_status_dict = {} port_status_dict["MUX_CABLE"] = {} create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, - muxcable_health_dict, asic_index, port) - + muxcable_health_dict, muxcable_metrics_dict, asic_index, port) + port_status_dict[] click.echo("{}".format(json.dumps(port_status_dict, indent=4))) sys.exit(STATUS_SUCCESSFUL) else: print_data = [] create_table_dump_per_port_status(db, print_data, muxcable_info_dict, - muxcable_health_dict, asic_index, port) + muxcable_health_dict, muxcable_metrics_dict, asic_index, port) - headers = ['PORT', 'STATUS', 'HEALTH'] + headers = ['PORT', 'STATUS', 'HEALTH', 'LAST_SWITCHOVER_TIME'] click.echo(tabulate(print_data, headers=headers)) sys.exit(STATUS_SUCCESSFUL) @@ -416,8 +441,12 @@ def status(db, port, json_output): per_npu_statedb[asic_id].STATE_DB, 'MUX_CABLE_TABLE|{}'.format(port)) muxcable_health_dict[asic_id] = per_npu_statedb[asic_id].get_all( per_npu_statedb[asic_id].STATE_DB, 'MUX_LINKMGR_TABLE|{}'.format(port)) + muxcable_metrics_dict[asic_id] = per_npu_statedb[asic_id].get_all( + per_npu_statedb[asic_id].STATE_DB, 'MUX_METRICS_TABLE|{}'.format(port)) + if not muxcable_metrics_dict[asic_id]: + muxcable_metrics_dict[asic_id] = {} create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, - muxcable_health_dict, asic_id, port) + muxcable_health_dict, muxcable_metrics_dict, asic_id, port) click.echo("{}".format(json.dumps(port_status_dict, indent=4))) else: @@ -430,11 +459,14 @@ def status(db, port, json_output): per_npu_statedb[asic_id].STATE_DB, 'MUX_LINKMGR_TABLE|{}'.format(port)) muxcable_info_dict[asic_id] = per_npu_statedb[asic_id].get_all( per_npu_statedb[asic_id].STATE_DB, 'MUX_CABLE_TABLE|{}'.format(port)) - + muxcable_metrics_dict[asic_id] = per_npu_statedb[asic_id].get_all( + per_npu_statedb[asic_id].STATE_DB, 'MUX_METRICS_TABLE|{}'.format(port)) + if not muxcable_metrics_dict[asic_id]: + muxcable_metrics_dict[asic_id] = {} create_table_dump_per_port_status(db, print_data, muxcable_info_dict, - muxcable_health_dict, asic_id, port) + muxcable_health_dict, muxcable_metrics_dict, asic_id, port) - headers = ['PORT', 'STATUS', 'HEALTH'] + headers = ['PORT', 'STATUS', 'HEALTH', 'LAST_SWITCHOVER_TIME'] click.echo(tabulate(print_data, headers=headers)) sys.exit(STATUS_SUCCESSFUL) From dc0377791ed936df5dedcaee5d19528dc3922b8c Mon Sep 17 00:00:00 2001 From: zhangjing Date: Mon, 14 Feb 2022 23:41:22 +0000 Subject: [PATCH 2/2] update unit tests --- show/muxcable.py | 3 +-- tests/muxcable_test.py | 48 ++++++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/show/muxcable.py b/show/muxcable.py index a1190f2edb..13662ca04e 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -304,7 +304,6 @@ def create_table_dump_per_port_status(db, print_data, muxcable_info_dict, muxcab last_switch_end_time = muxcable_metrics_dict[asic_index].get("linkmgrd_switch_standby_end") elif "linkmgrd_switch_active_end" in muxcable_metrics_dict[asic_index]: last_switch_end_time = muxcable_metrics_dict[asic_index].get("linkmgrd_switch_active_end") - port_status_dict["MUX_CABLE"][port_name]["LAST_SWITCHOVER_TIME"] = last_switch_end_time port_name = platform_sfputil_helper.get_interface_alias(port, db) print_port_data.append(port_name) @@ -407,7 +406,7 @@ def status(db, port, json_output): create_json_dump_per_port_status(db, port_status_dict, muxcable_info_dict, muxcable_health_dict, muxcable_metrics_dict, asic_index, port) - port_status_dict[] + click.echo("{}".format(json.dumps(port_status_dict, indent=4))) sys.exit(STATUS_SUCCESSFUL) else: diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 498b4761ef..b05c61e587 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -25,9 +25,9 @@ tabular_data_status_output_expected = """\ -PORT STATUS HEALTH ----------- -------- --------- -Ethernet0 active healthy +PORT STATUS HEALTH LAST_SWITCHOVER_TIME +---------- -------- --------- --------------------------- +Ethernet0 active healthy 2021-May-13 10:01:15.696728 Ethernet4 standby healthy Ethernet8 standby unhealthy Ethernet12 unknown unhealthy @@ -36,9 +36,9 @@ """ tabular_data_status_output_expected_alias = """\ -PORT STATUS HEALTH ------- -------- --------- -etp1 active healthy +PORT STATUS HEALTH LAST_SWITCHOVER_TIME +------ -------- --------- --------------------------- +etp1 active healthy 2021-May-13 10:01:15.696728 etp2 standby healthy etp3 standby unhealthy etp4 unknown unhealthy @@ -51,27 +51,33 @@ "MUX_CABLE": { "Ethernet0": { "STATUS": "active", - "HEALTH": "healthy" + "HEALTH": "healthy", + "LAST_SWITCHOVER_TIME": "2021-May-13 10:01:15.696728" }, "Ethernet4": { "STATUS": "standby", - "HEALTH": "healthy" + "HEALTH": "healthy", + "LAST_SWITCHOVER_TIME": "" }, "Ethernet8": { "STATUS": "standby", - "HEALTH": "unhealthy" + "HEALTH": "unhealthy", + "LAST_SWITCHOVER_TIME": "" }, "Ethernet12": { "STATUS": "unknown", - "HEALTH": "unhealthy" + "HEALTH": "unhealthy", + "LAST_SWITCHOVER_TIME": "" }, "Ethernet16": { "STATUS": "standby", - "HEALTH": "healthy" + "HEALTH": "healthy", + "LAST_SWITCHOVER_TIME": "" }, "Ethernet32": { "STATUS": "active", - "HEALTH": "healthy" + "HEALTH": "healthy", + "LAST_SWITCHOVER_TIME": "" } } } @@ -82,27 +88,33 @@ "MUX_CABLE": { "etp1": { "STATUS": "active", - "HEALTH": "healthy" + "HEALTH": "healthy", + "LAST_SWITCHOVER_TIME": "2021-May-13 10:01:15.696728" }, "etp2": { "STATUS": "standby", - "HEALTH": "healthy" + "HEALTH": "healthy", + "LAST_SWITCHOVER_TIME": "" }, "etp3": { "STATUS": "standby", - "HEALTH": "unhealthy" + "HEALTH": "unhealthy", + "LAST_SWITCHOVER_TIME": "" }, "etp4": { "STATUS": "unknown", - "HEALTH": "unhealthy" + "HEALTH": "unhealthy", + "LAST_SWITCHOVER_TIME": "" }, "etp5": { "STATUS": "standby", - "HEALTH": "healthy" + "HEALTH": "healthy", + "LAST_SWITCHOVER_TIME": "" }, "etp9": { "STATUS": "active", - "HEALTH": "healthy" + "HEALTH": "healthy", + "LAST_SWITCHOVER_TIME": "" } } }