|
7 | 7 |
|
8 | 8 | import os
|
9 | 9 | import sys
|
| 10 | +import natsort |
| 11 | +import ast |
10 | 12 |
|
| 13 | +import subprocess |
11 | 14 | import click
|
12 | 15 | import sonic_platform
|
13 | 16 | import sonic_platform_base.sonic_sfp.sfputilhelper
|
| 17 | +from swsscommon.swsscommon import SonicV2Connector |
14 | 18 | from natsort import natsorted
|
15 | 19 | from sonic_py_common import device_info, logger, multi_asic
|
16 | 20 | from tabulate import tabulate
|
@@ -615,6 +619,136 @@ def presence(port):
|
615 | 619 | click.echo(tabulate(output_table, table_header, tablefmt="simple"))
|
616 | 620 |
|
617 | 621 |
|
| 622 | +# 'error-status' subcommand |
| 623 | +def fetch_error_status_from_platform_api(port): |
| 624 | + """Fetch the error status from platform API and return the output as a string |
| 625 | + Args: |
| 626 | + port: the port whose error status will be fetched. |
| 627 | + None represents for all ports. |
| 628 | + Returns: |
| 629 | + A string consisting of the error status of each port. |
| 630 | + """ |
| 631 | + if port is None: |
| 632 | + logical_port_list = natsort.natsorted(platform_sfputil.logical) |
| 633 | + # Create a list containing the logical port names of all ports we're interested in |
| 634 | + generate_sfp_list_code = \ |
| 635 | + "sfp_list = chassis.get_all_sfps()\n" |
| 636 | + else: |
| 637 | + physical_port_list = logical_port_name_to_physical_port_list(port) |
| 638 | + logical_port_list = [port] |
| 639 | + # Create a list containing the logical port names of all ports we're interested in |
| 640 | + generate_sfp_list_code = \ |
| 641 | + "sfp_list = [chassis.get_sfp(x) for x in {}]\n".format(physical_port_list) |
| 642 | + |
| 643 | + # Code to initialize chassis object |
| 644 | + init_chassis_code = \ |
| 645 | + "import sonic_platform.platform\n" \ |
| 646 | + "platform = sonic_platform.platform.Platform()\n" \ |
| 647 | + "chassis = platform.get_chassis()\n" |
| 648 | + |
| 649 | + # Code to fetch the error status |
| 650 | + get_error_status_code = \ |
| 651 | + "try:\n"\ |
| 652 | + " errors=['{}:{}'.format(sfp.index, sfp.get_error_description()) for sfp in sfp_list]\n" \ |
| 653 | + "except NotImplementedError as e:\n"\ |
| 654 | + " errors=['{}:{}'.format(sfp.index, 'OK (Not implemented)') for sfp in sfp_list]\n" \ |
| 655 | + "print(errors)\n" |
| 656 | + |
| 657 | + get_error_status_command = "docker exec pmon python3 -c \"{}{}{}\"".format( |
| 658 | + init_chassis_code, generate_sfp_list_code, get_error_status_code) |
| 659 | + # Fetch error status from pmon docker |
| 660 | + try: |
| 661 | + output = subprocess.check_output(get_error_status_command, shell=True, universal_newlines=True) |
| 662 | + except subprocess.CalledProcessError as e: |
| 663 | + click.Abort("Error! Unable to fetch error status for SPF modules. Error code = {}, error messages: {}".format(e.returncode, e.output)) |
| 664 | + return None |
| 665 | + |
| 666 | + output_list = output.split('\n') |
| 667 | + for output_str in output_list: |
| 668 | + # The output of all SFP error status are a list consisting of element with convention of '<sfp no>:<error status>' |
| 669 | + # Besides, there can be some logs captured during the platform API executing |
| 670 | + # So, first of all, we need to skip all the logs until find the output list of SFP error status |
| 671 | + if output_str[0] == '[' and output_str[-1] == ']': |
| 672 | + output_list = ast.literal_eval(output_str) |
| 673 | + break |
| 674 | + |
| 675 | + output_dict = {} |
| 676 | + for output in output_list: |
| 677 | + sfp_index, error_status = output.split(':') |
| 678 | + output_dict[int(sfp_index)] = error_status |
| 679 | + |
| 680 | + output = [] |
| 681 | + for logical_port_name in logical_port_list: |
| 682 | + physical_port_list = logical_port_name_to_physical_port_list(logical_port_name) |
| 683 | + port_name = get_physical_port_name(logical_port_name, 1, False) |
| 684 | + |
| 685 | + output.append([port_name, output_dict.get(physical_port_list[0])]) |
| 686 | + |
| 687 | + return output |
| 688 | + |
| 689 | +def fetch_error_status_from_state_db(port, state_db): |
| 690 | + """Fetch the error status from STATE_DB and return them in a list. |
| 691 | + Args: |
| 692 | + port: the port whose error status will be fetched. |
| 693 | + None represents for all ports. |
| 694 | + Returns: |
| 695 | + A list consisting of tuples (port, description) and sorted by port. |
| 696 | + """ |
| 697 | + status = {} |
| 698 | + if port: |
| 699 | + status[port] = state_db.get_all(state_db.STATE_DB, 'TRANSCEIVER_STATUS|{}'.format(port)) |
| 700 | + else: |
| 701 | + ports = state_db.keys(state_db.STATE_DB, 'TRANSCEIVER_STATUS|*') |
| 702 | + for key in ports: |
| 703 | + status[key.split('|')[1]] = state_db.get_all(state_db.STATE_DB, key) |
| 704 | + |
| 705 | + sorted_ports = natsort.natsorted(status) |
| 706 | + output = [] |
| 707 | + for port in sorted_ports: |
| 708 | + statestring = status[port].get('status') |
| 709 | + description = status[port].get('error') |
| 710 | + if statestring == '1': |
| 711 | + description = 'OK' |
| 712 | + elif statestring == '0': |
| 713 | + description = 'Unplugged' |
| 714 | + elif description == 'N/A': |
| 715 | + log.log_error("Inconsistent state found for port {}: state is {} but error description is N/A".format(port, statestring)) |
| 716 | + description = 'Unknown state: {}'.format(statestring) |
| 717 | + |
| 718 | + output.append([port, description]) |
| 719 | + |
| 720 | + return output |
| 721 | + |
| 722 | +@show.command() |
| 723 | +@click.option('-p', '--port', metavar='<port_name>', help="Display SFP error status for port <port_name> only") |
| 724 | +@click.option('-hw', '--fetch-from-hardware', 'fetch_from_hardware', is_flag=True, default=False, help="Fetch the error status from hardware directly") |
| 725 | +def error_status(port, fetch_from_hardware): |
| 726 | + """Display error status of SFP transceiver(s)""" |
| 727 | + output_table = [] |
| 728 | + table_header = ["Port", "Error Status"] |
| 729 | + |
| 730 | + # Create a list containing the logical port names of all ports we're interested in |
| 731 | + if port and platform_sfputil.is_logical_port(port) == 0: |
| 732 | + click.echo("Error: invalid port '{}'\n".format(port)) |
| 733 | + click.echo("Valid values for port: {}\n".format(str(platform_sfputil.logical))) |
| 734 | + sys.exit(ERROR_INVALID_PORT) |
| 735 | + |
| 736 | + if fetch_from_hardware: |
| 737 | + output_table = fetch_error_status_from_platform_api(port) |
| 738 | + else: |
| 739 | + # Connect to STATE_DB |
| 740 | + state_db = SonicV2Connector(host='127.0.0.1') |
| 741 | + if state_db is not None: |
| 742 | + state_db.connect(state_db.STATE_DB) |
| 743 | + else: |
| 744 | + click.echo("Failed to connect to STATE_DB") |
| 745 | + return |
| 746 | + |
| 747 | + output_table = fetch_error_status_from_state_db(port, state_db) |
| 748 | + |
| 749 | + click.echo(tabulate(output_table, table_header, tablefmt='simple')) |
| 750 | + |
| 751 | + |
618 | 752 | # 'lpmode' subcommand
|
619 | 753 | @show.command()
|
620 | 754 | @click.option('-p', '--port', metavar='<port_name>', help="Display SFP low-power mode status for port <port_name> only")
|
|
0 commit comments