(self, conn, custom_queries, tags, instance)
Perform custom queries to collect additional metrics like number of result and duration of the query
Perform custom queries to collect additional metrics like number of result and duration of the query
Perform custom queries to collect additional metrics like number of result and duration of the query
def _perform_custom_queries(self, conn, custom_queries, tags, instance):
        """
        Perform custom queries to collect additional metrics like number of result and duration of the query
        """
        for query in custom_queries:
            name = query.get("name")
            if name is None:
                self.log.error("`name` field is required for custom query")
                continue
            search_base = query.get("search_base")
            if search_base is None:
                self.log.error("`search_base` field is required for custom query #%s", name)
                continue
            search_filter = query.get("search_filter")
            if search_filter is None:
                self.log.error("`search_filter` field is required for custom query #%s", name)
                continue
            attrs = query.get("attributes")
            if "username" in query:
                username = query.get("username")
                password = query.get("password")
                if not username:
                    # username is an empty string, we want anonymous bind
                    username = None
                    password = None
            else:
                # username not specified, we want to reuse the credentials for the monitor backend
                username = instance.get("username")
                password = instance.get("password")

            try:
                # Rebind with different credentials
                auth_method = ldap3.SIMPLE if username else ldap3.ANONYMOUS
                if username is None:
                    conn.user = None
                res = conn.rebind(user=username, password=password, authentication=auth_method)
                if not res:
                    raise ldap3.core.exceptions.LDAPBindError("Error binding to server: {}".format(conn.result))
            except ldap3.core.exceptions.LDAPBindError:
                self.log.exception("Could not rebind to server at %s to perform query %s", instance.get("url"), name)
                continue

            try:
                # Perform the search query
                conn.search(search_base, search_filter, attributes=attrs)
            except ldap3.core.exceptions.LDAPException:
                self.log.exception("Unable to perform search query for %s", name)
                continue

            query_tags = ['query:{}'.format(name)]
            query_tags.extend(tags)
            query_time = self._get_query_time(conn)
            results = len(conn.entries)
            self.gauge("{}.query.duration".format(self.METRIC_PREFIX), query_time, tags=query_tags)
            self.gauge("{}.query.entries".format(self.METRIC_PREFIX), results, tags=query_tags)
def _perform_custom_queries ( self , conn , custom_queries , tags , instance ) : for query in custom_queries : name = query . get ( "name" ) if name is None : self . log . error ( "`name` field is required for custom query" ) continue search_base = query . get ( "search_base" ) if search_base is None : self . log . error ( "`search_base` field is required for custom query #%s" , name ) continue search_filter = query . get ( "search_filter" ) if search_filter is None : self . log . error ( "`search_filter` field is required for custom query #%s" , name ) continue attrs = query . get ( "attributes" ) if "username" in query : username = query . get ( "username" ) password = query . get ( "password" ) if not username : # username is an empty string, we want anonymous bind username = None password = None else : # username not specified, we want to reuse the credentials for the monitor backend username = instance . get ( "username" ) password = instance . get ( "password" ) try : # Rebind with different credentials auth_method = ldap3 . SIMPLE if username else ldap3 . ANONYMOUS if username is None : conn . user = None res = conn . rebind ( user = username , password = password , authentication = auth_method ) if not res : raise ldap3 . core . exceptions . LDAPBindError ( "Error binding to server: {}" . format ( conn . result ) ) except ldap3 . core . exceptions . LDAPBindError : self . log . exception ( "Could not rebind to server at %s to perform query %s" , instance . get ( "url" ) , name ) continue try : # Perform the search query conn . search ( search_base , search_filter , attributes = attrs ) except ldap3 . core . exceptions . LDAPException : self . log . exception ( "Unable to perform search query for %s" , name ) continue query_tags = [ 'query:{}' . format ( name ) ] query_tags . extend ( tags ) query_time = self . _get_query_time ( conn ) results = len ( conn . entries ) self . gauge ( "{}.query.duration" . format ( self . METRIC_PREFIX ) , query_time , tags = query_tags ) self . gauge ( "{}.query.entries" . format ( self . METRIC_PREFIX ) , results , tags = query_tags )