def connect_to_all_dbs_for_ns(namespace=DEFAULT_NAMESPACE):
    """
    The function connects to the DBs for a given namespace and
    returns the handle 
    
    For voq chassis systems, the db list includes databases from 
    supervisor card. Avoid connecting to these databases from linecards

    If no namespace is provided, it will connect to the db in the
    default namespace.
    In case of multi ASIC, the default namespace is the
    database instance running the on the host
    In case of single ASIC, the namespace has to be DEFAULT_NAMESPACE

    Returns:
        handle to all the dbs for a namespaces
    """
    db = swsscommon.SonicV2Connector(namespace=namespace)
    db_list = list(db.get_db_list())
    if not is_supervisor():
        try:
            db_list.remove('CHASSIS_APP_DB')
            db_list.remove('CHASSIS_STATE_DB')
        except Exception:
            pass

    for db_id in db_list:
        db.connect(db_id)
    return db
def metrics(port, json_output):
    """Show muxcable metrics <port>"""

    metrics_table_keys = {}
    per_npu_statedb = {}
    metrics_dict = {}

    # Getting all front asic namespace and correspding config and state DB connector

    namespaces = multi_asic.get_front_end_namespaces()
    for namespace in namespaces:
        asic_id = multi_asic.get_asic_index_from_namespace(namespace)
        # replace these with correct macros
        per_npu_statedb[asic_id] = swsscommon.SonicV2Connector(use_unix_socket_path=True, namespace=namespace)
        per_npu_statedb[asic_id].connect(per_npu_statedb[asic_id].STATE_DB)

        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:

        logical_port_list = platform_sfputil_helper.get_logical_list()

        if port not in logical_port_list:
            click.echo(("ERR: Not a valid logical port for muxcable firmware {}".format(port)))
            sys.exit(CONFIG_FAIL)

        asic_index = None
        if platform_sfputil is not None:
            asic_index = platform_sfputil_helper.get_asic_id_for_logical_port(port)
            if asic_index is None:
                # TODO this import is only for unit test purposes, and should be removed once sonic_platform_base
                # is fully mocked
                import sonic_platform_base.sonic_sfp.sfputilhelper
                asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper().get_asic_id_for_logical_port(port)
                if asic_index is None:
                    click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port))


        metrics_dict[asic_index] = per_npu_statedb[asic_index].get_all(
            per_npu_statedb[asic_index].STATE_DB, 'MUX_METRICS_TABLE|{}'.format(port))

        if json_output:
            click.echo("{}".format(json.dumps(metrics_dict[asic_index], indent=4)))
        else:
            print_data = []
            for key, val in metrics_dict[asic_index].items():
                print_port_data = []
                print_port_data.append(port)
                print_port_data.append(key)
                print_port_data.append(val)
                print_data.append(print_port_data)

            headers = ['PORT', 'EVENT', 'TIME']

            click.echo(tabulate(print_data, headers=headers))
    def state_db(self):
        """
        Returns the state DB connector.
        Initializes the connector during the first call
        """
        if self.state_db_connector is None:
            self.state_db_connector = swsscommon.SonicV2Connector(host='127.0.0.1')
            self.state_db_connector.connect(self.state_db_connector.STATE_DB)

        return self.state_db_connector
示例#4
0
    def post_unit_status(self, srv_name, srv_status, app_status, fail_reason,
                         update_time):
        if not self.state_db:
            self.state_db = swsscommon.SonicV2Connector(host='127.0.0.1')
            self.state_db.connect(self.state_db.STATE_DB)

        key = 'ALL_SERVICE_STATUS|{}'.format(srv_name)
        statusvalue = {}
        statusvalue['service_status'] = srv_status
        statusvalue['app_ready_status'] = app_status
        statusvalue['fail_reason'] = fail_reason
        statusvalue['update_time'] = update_time
        self.state_db.hmset(self.state_db.STATE_DB, key, statusvalue)
示例#5
0
    def post_system_status(self, state):
        try:
            if not self.state_db:
                self.state_db = swsscommon.SonicV2Connector(host='127.0.0.1')
                self.state_db.connect(self.state_db.STATE_DB)

            self.state_db.set(self.state_db.STATE_DB,
                              "SYSTEM_READY|SYSTEM_STATE", "Status", state)
            logger.log_info(
                "Posting system ready status {} to statedb".format(state))

        except Exception as e:
            logger.log_error("Unable to post system ready status: {}".format(
                str(e)))
示例#6
0
    def system_service(self):
        if not self.state_db:
            self.state_db = swsscommon.SonicV2Connector(host='127.0.0.1')
            self.state_db.connect(self.state_db.STATE_DB)

        myQ = mpmgr.Queue()
        try:
            monitor_system_bus = MonitorSystemBusTask(myQ)
            monitor_system_bus.task_run()

            monitor_statedb_table = MonitorStateDbTask(myQ)
            monitor_statedb_table.task_run()

        except Exception as e:
            logger.log_error("SubProcess-{}".format(str(e)))
            sys.exit(1)

        self.update_system_status()

        from queue import Empty
        # Queue to receive the STATEDB and Systemd state change event
        while not self.task_stopping_event.is_set():
            try:
                msg = myQ.get(timeout=QUEUE_TIMEOUT)
                event = msg["unit"]
                event_src = msg["evt_src"]
                event_time = msg["time"]
                logger.log_debug(
                    "Main process- received event:{} from source:{} time:{}".
                    format(event, event_src, event_time))
                logger.log_info("check_unit_status for [ " + event + " ] ")
                self.check_unit_status(event)
            except Empty:
                pass
            except Exception as e:
                logger.log_error("system_service" + str(e))

        #cleanup tables  "'ALL_SERVICE_STATUS*', 'SYSTEM_READY*'" from statedb
        self.state_db.delete_all_by_pattern(self.state_db.STATE_DB,
                                            "ALL_SERVICE_STATUS|*")
        self.state_db.delete_all_by_pattern(self.state_db.STATE_DB,
                                            "SYSTEM_READY|*")

        monitor_system_bus.task_stop()
        monitor_statedb_table.task_stop()
示例#7
0
    def get_app_ready_status(self, service):
        if not self.state_db:
            self.state_db = swsscommon.SonicV2Connector(host='127.0.0.1')
            self.state_db.connect(self.state_db.STATE_DB)
        if not self.config_db:
            self.config_db = swsscommon.ConfigDBConnector()
            self.config_db.connect()

        fail_reason = ""
        check_app_up_status = ""
        up_status_flag = ""
        configdb_feature_table = self.config_db.get_table('FEATURE')
        update_time = "-"

        if service not in configdb_feature_table.keys():
            pstate = "Up"
        else:
            check_app_up_status = configdb_feature_table[service].get(
                'check_up_status')
            if check_app_up_status is not None and (
                    check_app_up_status.lower()) == "true":
                up_status_flag = self.state_db.get(
                    self.state_db.STATE_DB, 'FEATURE|{}'.format(service),
                    'up_status')
                if up_status_flag is not None and (
                        up_status_flag.lower()) == "true":
                    pstate = "Up"
                else:
                    fail_reason = self.state_db.get(
                        self.state_db.STATE_DB, 'FEATURE|{}'.format(service),
                        'fail_reason')
                    if fail_reason is None:
                        fail_reason = "NA"
                    pstate = "Down"

                update_time = self.state_db.get(self.state_db.STATE_DB,
                                                'FEATURE|{}'.format(service),
                                                'update_time')
                if update_time is None:
                    update_time = "-"
            else:
                #Either check_up_status marked False or entry does not exist
                pstate = "Up"

        return pstate, fail_reason, update_time
示例#8
0
    def check_unit_status(self, event):
        #global dnsrvs_name
        if not self.state_db:
            self.state_db = swsscommon.SonicV2Connector(host='127.0.0.1')
            self.state_db.connect(self.state_db.STATE_DB)
        astate = "DOWN"

        full_srv_list = self.get_all_service_list()
        if event in full_srv_list:
            ustate = self.get_unit_status(event)
            if ustate == "OK" and system_allsrv_state == "UP":
                astate = "UP"
            elif ustate == "OK" and system_allsrv_state == "DOWN":
                if event in self.dnsrvs_name:
                    self.dnsrvs_name.remove(event)
                    if len(self.dnsrvs_name) == 0:
                        astate = "UP"
                    else:
                        astate = "DOWN"
            else:
                if event not in self.dnsrvs_name:
                    self.dnsrvs_name.add(event)
                astate = "DOWN"

            self.publish_system_status(astate)
        else:
            #if received event is not in current full service list but exists in STATE_DB & set,
            #then it should be removed from STATE_DB & set
            if event in self.dnsrvs_name:
                self.dnsrvs_name.remove(event)

            srv_name, last = event.split('.')
            key = 'ALL_SERVICE_STATUS|{}'.format(srv_name)
            key_exists = self.state_db.exists(self.state_db.STATE_DB, key)
            if key_exists == 1:
                self.state_db.delete(self.state_db.STATE_DB, key)

        return 0
示例#9
0
def mode(state, port, json_output):
    """Show muxcable summary information"""

    port_table_keys = {}
    y_cable_asic_table_keys = {}
    per_npu_configdb = {}
    per_npu_statedb = {}
    mux_tbl_cfg_db = {}

    # Getting all front asic namespace and correspding config and state DB connector

    namespaces = multi_asic.get_front_end_namespaces()
    for namespace in namespaces:
        asic_id = multi_asic.get_asic_index_from_namespace(namespace)
        # replace these with correct macros
        per_npu_configdb[asic_id] = ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
        per_npu_configdb[asic_id].connect()
        per_npu_statedb[asic_id] = swsscommon.SonicV2Connector(use_unix_socket_path=True, namespace=namespace)
        per_npu_statedb[asic_id].connect(per_npu_statedb[asic_id].STATE_DB)

        mux_tbl_cfg_db[asic_id] = per_npu_configdb[asic_id].get_table("MUX_CABLE")

        port_table_keys[asic_id] = per_npu_statedb[asic_id].keys(
            per_npu_statedb[asic_id].STATE_DB, 'MUX_CABLE_TABLE|*')

    if port is not None and port != "all":

        asic_index = None
        if platform_sfputil is not None:
            asic_index = platform_sfputil.get_asic_id_for_logical_port(port)
        if asic_index is None:
            # TODO this import is only for unit test purposes, and should be removed once sonic_platform_base
            # is fully mocked
            import sonic_platform_base.sonic_sfp.sfputilhelper
            asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper().get_asic_id_for_logical_port(port)
            if asic_index is None:
                click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port))
                sys.exit(CONFIG_FAIL)

        if per_npu_statedb[asic_index] is not None:
            y_cable_asic_table_keys = port_table_keys[asic_index]
            logical_key = "MUX_CABLE_TABLE"+"|"+port
            if logical_key in y_cable_asic_table_keys:
                port_status_dict = {}
                lookup_statedb_and_update_configdb(
                    per_npu_statedb[asic_index], per_npu_configdb[asic_index], port, state, port_status_dict)

                if json_output:
                    click.echo("{}".format(json.dumps(port_status_dict, indent=4)))
                else:
                    headers = ['port', 'state']
                    data = sorted([(k, v) for k, v in port_status_dict.items()])
                    click.echo(tabulate(data, headers=headers))

                sys.exit(CONFIG_SUCCESSFUL)

            else:
                click.echo("this is not a valid port present on mux_cable".format(port))
                sys.exit(CONFIG_FAIL)
        else:
            click.echo("there is not a valid asic table for this asic_index".format(asic_index))
            sys.exit(CONFIG_FAIL)

    elif port == "all" and port is not None:

        port_status_dict = {}
        for namespace in namespaces:
            asic_id = multi_asic.get_asic_index_from_namespace(namespace)
            for key in port_table_keys[asic_id]:
                logical_port = key.split("|")[1]
                lookup_statedb_and_update_configdb(
                    per_npu_statedb[asic_id], per_npu_configdb[asic_id], logical_port, state, port_status_dict)

            if json_output:
                click.echo("{}".format(json.dumps(port_status_dict, indent=4)))
            else:
                data = sorted([(k, v) for k, v in port_status_dict.items()])

                headers = ['port', 'state']
                click.echo(tabulate(data, headers=headers))

        sys.exit(CONFIG_SUCCESSFUL)
示例#10
0
def status(port, json_output):
    """Show muxcable status information"""

    port_table_keys = {}
    per_npu_statedb = {}
    muxcable_info_dict = {}

    # Getting all front asic namespace and correspding config and state DB connector

    namespaces = multi_asic.get_front_end_namespaces()
    for namespace in namespaces:
        asic_id = multi_asic.get_asic_index_from_namespace(namespace)
        per_npu_statedb[asic_id] = swsscommon.SonicV2Connector(
            use_unix_socket_path=True, namespace=namespace)
        per_npu_statedb[asic_id].connect(per_npu_statedb[asic_id].STATE_DB)

        port_table_keys[asic_id] = per_npu_statedb[asic_id].keys(
            per_npu_statedb[asic_id].STATE_DB, 'MUX_CABLE_TABLE|*')

    if port is not None:
        asic_index = None
        if platform_sfputil is not None:
            asic_index = platform_sfputil.get_asic_id_for_logical_port(port)
        if asic_index is None:
            # TODO this import is only for unit test purposes, and should be removed once sonic_platform_base
            # is fully mocked
            import sonic_platform_base.sonic_sfp.sfputilhelper
            asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper(
            ).get_asic_id_for_logical_port(port)
            if asic_index is None:
                click.echo(
                    "Got invalid asic index for port {}, cant retreive mux status"
                    .format(port))
                sys.exit(STATUS_FAIL)

        muxcable_info_dict[asic_index] = per_npu_statedb[asic_index].get_all(
            per_npu_statedb[asic_index].STATE_DB,
            'MUX_CABLE_TABLE|{}'.format(port))
        if muxcable_info_dict[asic_index] is not None:
            logical_key = "MUX_CABLE_TABLE" + "|" + port
            if logical_key in port_table_keys[asic_index]:

                if json_output:
                    port_status_dict = {}
                    port_status_dict["MUX_CABLE"] = {}

                    create_json_dump_per_port_status(port_status_dict,
                                                     muxcable_info_dict,
                                                     asic_index, port)

                    click.echo("{}".format(
                        json.dumps(port_status_dict, indent=4)))
                    sys.exit(STATUS_SUCCESSFUL)
                else:
                    print_data = []

                    create_table_dump_per_port_status(print_data,
                                                      muxcable_info_dict,
                                                      asic_index, port)

                    headers = ['PORT', 'STATUS', 'HEALTH']

                    click.echo(tabulate(print_data, headers=headers))
                    sys.exit(STATUS_SUCCESSFUL)
            else:
                click.echo(
                    "this is not a valid port present on mux_cable".format(
                        port))
                sys.exit(STATUS_FAIL)
        else:
            click.echo(
                "there is not a valid asic table for this asic_index".format(
                    asic_index))
            sys.exit(STATUS_FAIL)

    else:

        if json_output:
            port_status_dict = {}
            port_status_dict["MUX_CABLE"] = {}
            for namespace in namespaces:
                asic_id = multi_asic.get_asic_index_from_namespace(namespace)
                for key in port_table_keys[asic_id]:
                    port = key.split("|")[1]
                    muxcable_info_dict[asic_id] = per_npu_statedb[
                        asic_id].get_all(per_npu_statedb[asic_id].STATE_DB,
                                         'MUX_CABLE_TABLE|{}'.format(port))
                    create_json_dump_per_port_status(port_status_dict,
                                                     muxcable_info_dict,
                                                     asic_id, port)

            click.echo("{}".format(json.dumps(port_status_dict, indent=4)))
        else:
            print_data = []
            for namespace in namespaces:
                asic_id = multi_asic.get_asic_index_from_namespace(namespace)
                for key in port_table_keys[asic_id]:
                    port = key.split("|")[1]
                    muxcable_info_dict[asic_id] = per_npu_statedb[
                        asic_id].get_all(per_npu_statedb[asic_id].STATE_DB,
                                         'MUX_CABLE_TABLE|{}'.format(port))

                    create_table_dump_per_port_status(print_data,
                                                      muxcable_info_dict,
                                                      asic_id, port)

            headers = ['PORT', 'STATUS', 'HEALTH']
            click.echo(tabulate(print_data, headers=headers))

        sys.exit(STATUS_SUCCESSFUL)
示例#11
0
def packetloss(db, action, port):
    """config muxcable packetloss reset"""

    port = platform_sfputil_helper.get_interface_name(port, db)

    port_table_keys = {}
    mux_cable_table_keys = {}
    pck_loss_table_keys = {}
    per_npu_configdb = {}
    per_npu_statedb = {}

    # Getting all front asic namespace and correspding config and state DB connector

    namespaces = multi_asic.get_front_end_namespaces()
    for namespace in namespaces:
        asic_id = multi_asic.get_asic_index_from_namespace(namespace)
        # replace these with correct macros
        per_npu_configdb[asic_id] = ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
        per_npu_configdb[asic_id].connect()
        per_npu_statedb[asic_id] = swsscommon.SonicV2Connector(use_unix_socket_path=True, namespace=namespace)
        per_npu_statedb[asic_id].connect(per_npu_statedb[asic_id].STATE_DB)

        port_table_keys[asic_id] = per_npu_statedb[asic_id].keys(
            per_npu_statedb[asic_id].STATE_DB, 'LINK_PROBE_STATS|*')
        mux_cable_table_keys[asic_id] = per_npu_configdb[asic_id].get_table("MUX_CABLE").keys() # keys here are port names
    if port is not None and port != "all":

        asic_index = None
        if platform_sfputil is not None:
            asic_index = platform_sfputil.get_asic_id_for_logical_port(port)
        if asic_index is None:
            # TODO this import is only for unit test purposes, and should be removed once sonic_platform_base
            # is fully mocked
            import sonic_platform_base.sonic_sfp.sfputilhelper
            asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper().get_asic_id_for_logical_port(port)
            if asic_index is None:
                click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port))
                sys.exit(CONFIG_FAIL)

        if per_npu_statedb[asic_index] is not None:
            pck_loss_table_keys = port_table_keys[asic_index]
            logical_key = "LINK_PROBE_STATS|{}".format(port)
            if logical_key in pck_loss_table_keys:
                update_configdb_pck_loss_data(per_npu_configdb[asic_index], port, "reset")
                sys.exit(CONFIG_SUCCESSFUL)
            else:
                click.echo("this is not a valid port present on pck_loss_stats".format(port))
                sys.exit(CONFIG_FAIL)
        else:
            click.echo("there is not a valid asic table for this asic_index".format(asic_index))
            sys.exit(CONFIG_FAIL)

    elif port == "all" and port is not None:

        for namespace in namespaces:
            asic_id = multi_asic.get_asic_index_from_namespace(namespace)
            for key in port_table_keys[asic_id]:
                logical_port = key.split("|")[1]
                if logical_port in mux_cable_table_keys[asic_id]:
                    update_configdb_pck_loss_data(per_npu_configdb[asic_id], logical_port, "reset")

        sys.exit(CONFIG_SUCCESSFUL)
示例#12
0
def version(port):
    """Show muxcable firmware version"""

    port_table_keys = {}
    y_cable_asic_table_keys = {}
    per_npu_statedb = {}
    physical_port_list = []

    # Getting all front asic namespace and correspding config and state DB connector

    namespaces = multi_asic.get_front_end_namespaces()
    for namespace in namespaces:
        asic_id = multi_asic.get_asic_index_from_namespace(namespace)
        # replace these with correct macros
        per_npu_statedb[asic_id] = swsscommon.SonicV2Connector(
            use_unix_socket_path=True, namespace=namespace)
        per_npu_statedb[asic_id].connect(per_npu_statedb[asic_id].STATE_DB)

        port_table_keys[asic_id] = per_npu_statedb[asic_id].keys(
            per_npu_statedb[asic_id].STATE_DB, 'MUX_CABLE_TABLE|*')

    if port is not None:

        logical_port_list = platform_sfputil_helper.get_logical_list()

        if port not in logical_port_list:
            click.echo(
                ("ERR: Not a valid logical port for muxcable firmware {}".
                 format(port)))
            sys.exit(CONFIG_FAIL)

        asic_index = None
        if platform_sfputil is not None:
            asic_index = platform_sfputil_helper.get_asic_id_for_logical_port(
                port)
            if asic_index is None:
                # TODO this import is only for unit test purposes, and should be removed once sonic_platform_base
                # is fully mocked
                import sonic_platform_base.sonic_sfp.sfputilhelper
                asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper(
                ).get_asic_id_for_logical_port(port)
                if asic_index is None:
                    click.echo(
                        "Got invalid asic index for port {}, cant retreive mux status"
                        .format(port))

        if platform_sfputil is not None:
            physical_port_list = platform_sfputil_helper.logical_port_name_to_physical_port_list(
                port)

        if not isinstance(physical_port_list, list):
            click.echo(
                ("ERR: Unable to locate physical port information for {}".
                 format(port)))
            sys.exit(CONFIG_FAIL)

        if len(physical_port_list) != 1:
            click.echo(
                "ERR: Found multiple physical ports ({}) associated with {}".
                format(", ".join(physical_port_list), port))
            sys.exit(CONFIG_FAIL)

        mux_info_dict = {}
        physical_port = physical_port_list[0]
        if per_npu_statedb[asic_index] is not None:
            y_cable_asic_table_keys = port_table_keys[asic_index]
            logical_key = "MUX_CABLE_TABLE|{}".format(port)
            import sonic_y_cable.y_cable
            read_side = sonic_y_cable.y_cable.check_read_side(physical_port)
            if logical_key in y_cable_asic_table_keys:
                if read_side == 1:
                    get_firmware_dict(physical_port, 1, "self", mux_info_dict)
                    get_firmware_dict(physical_port, 2, "peer", mux_info_dict)
                    get_firmware_dict(physical_port, 0, "nic", mux_info_dict)
                    click.echo("{}".format(json.dumps(mux_info_dict,
                                                      indent=4)))
                elif read_side == 2:
                    get_firmware_dict(physical_port, 2, "self", mux_info_dict)
                    get_firmware_dict(physical_port, 1, "peer", mux_info_dict)
                    get_firmware_dict(physical_port, 0, "nic", mux_info_dict)
                    click.echo("{}".format(json.dumps(mux_info_dict,
                                                      indent=4)))
                else:
                    click.echo(
                        "Did not get a valid read_side for muxcable".format(
                            port))
                    sys.exit(CONFIG_FAIL)

            else:
                click.echo(
                    "this is not a valid port present on mux_cable".format(
                        port))
                sys.exit(CONFIG_FAIL)
        else:
            click.echo(
                "there is not a valid asic table for this asic_index".format(
                    asic_index))