def db_connect_configdb(namespace=None):
    """
    Connect to configdb
    """
    try:
        if namespace is not None:
            swsscommon.SonicDBConfig.load_sonic_global_db_config(
                namespace=namespace)
        config_db = swsscommon.ConfigDBConnector(use_unix_socket_path=True,
                                                 namespace=namespace)
    except Exception as e:
        return None
    if config_db is None:
        return None
    try:
        """
        This could be blocking during the config load_minigraph phase,
        as the CONFIG_DB_INITIALIZED is not yet set in the configDB.
        We can ignore the check by using config_db.db_connect('CONFIG_DB') instead
        """
        # Connect only if available & initialized
        config_db.connect(wait_for_init=False)
    except Exception as e:
        config_db = None
    return config_db
Esempio n. 2
0
    def config_db(self):
        """
        Returns config DB connector.
        Initializes the connector during the first call
        """
        if self.config_db_connector is None:
            self.config_db_connector = swsscommon.ConfigDBConnector()
            self.config_db_connector.connect()

        return self.config_db_connector
def connect_config_db_for_ns(namespace=DEFAULT_NAMESPACE):
    """
    The function connects to the config DB for a given namespace and
    returns the handle
    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

    Returns:
      handle to the config_db for a namespace
    """
    config_db = swsscommon.ConfigDBConnector(namespace=namespace)
    config_db.connect()
    return config_db
Esempio n. 4
0
    def get_running_db_connector(cls):
        """ Returns running DB connector. """

        # In chroot we can connect to a running
        # DB via TCP socket, we should ignore this case.
        if in_chroot():
            return None

        if cls._running_db_conn is None:
            try:
                cls._running_db_conn = swsscommon.ConfigDBConnector()
                cls._running_db_conn.connect()
            except RuntimeError:
                # Failed to connect to DB.
                cls._running_db_conn = None

        return cls._running_db_conn
Esempio n. 5
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
    def check_services(self, config):
        """Check status of critical services and critical processes

        Args:
            config (config.Config): Health checker configuration.
        """
        if not self.config_db:
            self.config_db = swsscommon.ConfigDBConnector()
            self.config_db.connect()
        feature_table = self.config_db.get_table("FEATURE")
        expected_running_containers, self.container_feature_dict = self.get_expected_running_containers(
            feature_table)
        current_running_containers = self.get_current_running_containers()

        newly_disabled_containers = set(
            self.container_critical_processes.keys()).difference(
                expected_running_containers)
        for newly_disabled_container in newly_disabled_containers:
            self.container_critical_processes.pop(newly_disabled_container)

        self.save_critical_process_cache()

        not_running_containers = expected_running_containers.difference(
            current_running_containers)
        for container in not_running_containers:
            self.set_object_not_ok(
                'Service', container,
                "Container '{}' is not running".format(container))

        if not self.container_critical_processes:
            # Critical process is empty, not expect
            self.set_object_not_ok('Service', 'system',
                                   'no critical process found')
            return

        for container, critical_process_list in self.container_critical_processes.items(
        ):
            self.check_process_existence(container, critical_process_list,
                                         config, feature_table)

        for bad_container in self.bad_containers:
            self.set_object_not_ok(
                'Service', bad_container,
                'Syntax of critical_processes file is incorrect')
Esempio n. 7
0
    def get_all_service_list(self):

        if not self.config_db:
            self.config_db = swsscommon.ConfigDBConnector()
            self.config_db.connect()

        dir_list = []
        #add the services from the below targets
        targets = [
            "/etc/systemd/system/multi-user.target.wants",
            "/etc/systemd/system/sonic.target.wants"
        ]
        for path in targets:
            dir_list += [
                os.path.basename(i)
                for i in glob.glob('{}/*.service'.format(path))
            ]

        #add the enabled docker services from config db feature table
        feature_table = self.config_db.get_table("FEATURE")
        for srv in feature_table.keys():
            if feature_table[srv]["state"] not in [
                    "disabled", "always_disabled"
            ]:
                srvext = srv + ".service"
                if srvext not in dir_list:
                    dir_list.append(srvext)

        self.config.load_config()
        if self.config and self.config.ignore_services:
            for srv in self.config.ignore_services:
                if srv in dir_list:
                    dir_list.remove(srv)

        dir_list.sort()
        return dir_list