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

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

        # we need this for backwards compatibility
        if 'host' in self.config:
            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'])

        # 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 self.config['hosts']:
            if len(self.config['hosts']) == 1:
                # one host only, no need to have a prefix
                base_prefix = []
            else:
                matches = re.search('((.+)\@)?(.+)?', host)
                alias = matches.group(2)
                host = matches.group(3)

                if alias is None:
                    base_prefix = [re.sub('[:\.]', '_', host)]
                else:
                    base_prefix = [alias]

            try:
                if ReadPreference is None:
                    conn = pymongo.Connection(
                        host,
                        network_timeout=self.config['network_timeout'],
                        slave_okay=True
                    )
                else:
                    conn = pymongo.Connection(
                        host,
                        network_timeout=self.config['network_timeout'],
                        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{}

            serverStatus = conn.db.command('serverStatus')
            engineStatus = conn.db.command('engineStatus')
            data = dict(serverStatus.items() + engineStatus.items())

            self._publish_transformed(data, base_prefix)
            if str_to_bool(self.config['simple']):
                data = self._extract_simple_data(data)

            self._publish_dict_with_prefix(data, base_prefix)
            db_name_filter = re.compile(self.config['databases'])
            ignored_collections = re.compile(self.config['ignore_collections'])
            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
                    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 # we need this for backwards compatibility if 'host' in self . config : 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' ] ) # 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 self . config [ 'hosts' ] : if len ( self . config [ 'hosts' ] ) == 1 : # one host only, no need to have a prefix base_prefix = [ ] else : matches = re . search ( '((.+)\@)?(.+)?' , host ) alias = matches . group ( 2 ) host = matches . group ( 3 ) if alias is None : base_prefix = [ re . sub ( '[:\.]' , '_' , host ) ] else : base_prefix = [ alias ] try : if ReadPreference is None : conn = pymongo . Connection ( host , network_timeout = self . config [ 'network_timeout' ] , slave_okay = True ) else : conn = pymongo . Connection ( host , network_timeout = self . config [ 'network_timeout' ] , 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 { } serverStatus = conn . db . command ( 'serverStatus' ) engineStatus = conn . db . command ( 'engineStatus' ) data = dict ( serverStatus . items ( ) + engineStatus . items ( ) ) self . _publish_transformed ( data , base_prefix ) if str_to_bool ( self . config [ 'simple' ] ) : data = self . _extract_simple_data ( data ) self . _publish_dict_with_prefix ( data , base_prefix ) db_name_filter = re . compile ( self . config [ 'databases' ] ) ignored_collections = re . compile ( self . config [ 'ignore_collections' ] ) 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 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 )