(self)
Collect number values from db.serverStatus()
Collect number values from db.serverStatus()
Collect number values from db . serverStatus ()
def collect(self):
        """Collect number values from db.serverStatus()"""

        if pymongo is None:
            self.log.error('Unable to import pymongo')
            return

        hosts = self.config.get('hosts')

        # Convert a string config value to be an array
        if isinstance(hosts, basestring):
            hosts = [hosts]

        # we need this for backwards compatibility
        if 'host' in self.config:
            hosts = [self.config['host']]

        # convert network_timeout to integer
        if self.config['network_timeout']:
            self.config['network_timeout'] = int(
                self.config['network_timeout'])

        # convert collection_sample_rate to float
        if self.config['collection_sample_rate']:
            self.config['collection_sample_rate'] = float(
                self.config['collection_sample_rate'])

        # use auth if given
        if 'user' in self.config:
            user = self.config['user']
        else:
            user = None

        if 'passwd' in self.config:
            passwd = self.config['passwd']
        else:
            passwd = None

        for host in hosts:
            matches = re.search('((.+)\@)?(.+)?', host)
            alias = matches.group(2)
            host = matches.group(3)

            if alias is None:
                if len(hosts) == 1:
                    # one host only, no need to have a prefix
                    base_prefix = []
                else:
                    base_prefix = [re.sub('[:\.]', '_', host)]
            else:
                base_prefix = [alias]

            try:
                # Ensure that the SSL option is a boolean.
                if type(self.config['ssl']) is str:
                    self.config['ssl'] = str_to_bool(self.config['ssl'])

                if ReadPreference is None:
                    conn = pymongo.MongoClient(
                        host,
                        socketTimeoutMS=self.config['network_timeout'],
                        ssl=self.config['ssl'],
                    )
                else:
                    conn = pymongo.MongoClient(
                        host,
                        socketTimeoutMS=self.config['network_timeout'],
                        ssl=self.config['ssl'],
                        read_preference=ReadPreference.SECONDARY,
                    )
            except Exception as e:
                self.log.error('Couldnt connect to mongodb: %s', e)
                continue

            # try auth
            if user:
                try:
                    conn.admin.authenticate(user, passwd)
                except Exception as e:
                    self.log.error(
                        'User auth given, but could not autheticate' +
                        ' with host: %s, err: %s' % (host, e))
                    return{}

            data = conn.db.command('serverStatus')
            self._publish_transformed(data, base_prefix)
            if str_to_bool(self.config['simple']):
                data = self._extract_simple_data(data)
            if str_to_bool(self.config['replica']):
                try:
                    replset_data = conn.admin.command('replSetGetStatus')
                    self._publish_replset(replset_data, base_prefix)
                except pymongo.errors.OperationFailure as e:
                    self.log.error('error getting replica set status', e)

            self._publish_dict_with_prefix(data, base_prefix)
            db_name_filter = re.compile(self.config['databases'])
            ignored_collections = re.compile(self.config['ignore_collections'])
            sample_threshold = self.MAX_CRC32 * self.config[
                'collection_sample_rate']
            for db_name in conn.database_names():
                if not db_name_filter.search(db_name):
                    continue
                db_stats = conn[db_name].command('dbStats')
                db_prefix = base_prefix + ['databases', db_name]
                self._publish_dict_with_prefix(db_stats, db_prefix)
                for collection_name in conn[db_name].collection_names():
                    if ignored_collections.search(collection_name):
                        continue
                    if (self.config['collection_sample_rate'] < 1 and (
                            zlib.crc32(collection_name) & 0xffffffff
                    ) > sample_threshold):
                        continue

                    collection_stats = conn[db_name].command('collstats',
                                                             collection_name)
                    if str_to_bool(self.config['translate_collections']):
                        collection_name = collection_name.replace('.', '_')
                    collection_prefix = db_prefix + [collection_name]
                    self._publish_dict_with_prefix(collection_stats,
                                                   collection_prefix)
def collect ( self ) : if pymongo is None : self . log . error ( 'Unable to import pymongo' ) return hosts = self . config . get ( 'hosts' ) # Convert a string config value to be an array if isinstance ( hosts , basestring ) : hosts = [ hosts ] # we need this for backwards compatibility if 'host' in self . config : hosts = [ self . config [ 'host' ] ] # convert network_timeout to integer if self . config [ 'network_timeout' ] : self . config [ 'network_timeout' ] = int ( self . config [ 'network_timeout' ] ) # convert collection_sample_rate to float if self . config [ 'collection_sample_rate' ] : self . config [ 'collection_sample_rate' ] = float ( self . config [ 'collection_sample_rate' ] ) # use auth if given if 'user' in self . config : user = self . config [ 'user' ] else : user = None if 'passwd' in self . config : passwd = self . config [ 'passwd' ] else : passwd = None for host in hosts : matches = re . search ( '((.+)\@)?(.+)?' , host ) alias = matches . group ( 2 ) host = matches . group ( 3 ) if alias is None : if len ( hosts ) == 1 : # one host only, no need to have a prefix base_prefix = [ ] else : base_prefix = [ re . sub ( '[:\.]' , '_' , host ) ] else : base_prefix = [ alias ] try : # Ensure that the SSL option is a boolean. if type ( self . config [ 'ssl' ] ) is str : self . config [ 'ssl' ] = str_to_bool ( self . config [ 'ssl' ] ) if ReadPreference is None : conn = pymongo . MongoClient ( host , socketTimeoutMS = self . config [ 'network_timeout' ] , ssl = self . config [ 'ssl' ] , ) else : conn = pymongo . MongoClient ( host , socketTimeoutMS = self . config [ 'network_timeout' ] , ssl = self . config [ 'ssl' ] , read_preference = ReadPreference . SECONDARY , ) except Exception as e : self . log . error ( 'Couldnt connect to mongodb: %s' , e ) continue # try auth if user : try : conn . admin . authenticate ( user , passwd ) except Exception as e : self . log . error ( 'User auth given, but could not autheticate' + ' with host: %s, err: %s' % ( host , e ) ) return { } data = conn . db . command ( 'serverStatus' ) self . _publish_transformed ( data , base_prefix ) if str_to_bool ( self . config [ 'simple' ] ) : data = self . _extract_simple_data ( data ) if str_to_bool ( self . config [ 'replica' ] ) : try : replset_data = conn . admin . command ( 'replSetGetStatus' ) self . _publish_replset ( replset_data , base_prefix ) except pymongo . errors . OperationFailure as e : self . log . error ( 'error getting replica set status' , e ) self . _publish_dict_with_prefix ( data , base_prefix ) db_name_filter = re . compile ( self . config [ 'databases' ] ) ignored_collections = re . compile ( self . config [ 'ignore_collections' ] ) sample_threshold = self . MAX_CRC32 * self . config [ 'collection_sample_rate' ] for db_name in conn . database_names ( ) : if not db_name_filter . search ( db_name ) : continue db_stats = conn [ db_name ] . command ( 'dbStats' ) db_prefix = base_prefix + [ 'databases' , db_name ] self . _publish_dict_with_prefix ( db_stats , db_prefix ) for collection_name in conn [ db_name ] . collection_names ( ) : if ignored_collections . search ( collection_name ) : continue if ( self . config [ 'collection_sample_rate' ] < 1 and ( zlib . crc32 ( collection_name ) & 0xffffffff ) > sample_threshold ) : continue collection_stats = conn [ db_name ] . command ( 'collstats' , collection_name ) if str_to_bool ( self . config [ 'translate_collections' ] ) : collection_name = collection_name . replace ( '.' , '_' ) collection_prefix = db_prefix + [ collection_name ] self . _publish_dict_with_prefix ( collection_stats , collection_prefix )