(self)
Collect metrics
Collect metrics
Collect metrics
def collect(self):
        """
        Collect metrics
        """
        collected = {}
        files = []

        if isinstance(self.config['dir'], basestring):
            dirs = [d.strip() for d in self.config['dir'].split(',')]
        elif isinstance(self.config['dir'], list):
            dirs = self.config['dir']

        if isinstance(self.config['files'], basestring):
            files = [f.strip() for f in self.config['files'].split(',')]
        elif isinstance(self.config['files'], list):
            files = self.config['files']

        for sdir in dirs:
            for sfile in files:
                if sfile.endswith('conntrack_count'):
                    metric_name = 'ip_conntrack_count'
                elif sfile.endswith('conntrack_max'):
                    metric_name = 'ip_conntrack_max'
                else:
                    self.log.error('Unknown file for collection: %s', sfile)
                    continue
                fpath = os.path.join(sdir, sfile)
                if not os.path.exists(fpath):
                    continue
                try:
                    with open(fpath, "r") as fhandle:
                        metric = float(fhandle.readline().rstrip("\n"))
                        collected[metric_name] = metric
                except Exception as exception:
                    self.log.error("Failed to collect from '%s': %s",
                                   fpath,
                                   exception)
        if not collected:
            self.log.error('No metric was collected, looks like '
                           'nf_conntrack/ip_conntrack kernel module was '
                           'not loaded')
        else:
            for key in collected.keys():
                self.publish(key, collected[key])
def collect ( self ) : collected = { } files = [ ] if isinstance ( self . config [ 'dir' ] , basestring ) : dirs = [ d . strip ( ) for d in self . config [ 'dir' ] . split ( ',' ) ] elif isinstance ( self . config [ 'dir' ] , list ) : dirs = self . config [ 'dir' ] if isinstance ( self . config [ 'files' ] , basestring ) : files = [ f . strip ( ) for f in self . config [ 'files' ] . split ( ',' ) ] elif isinstance ( self . config [ 'files' ] , list ) : files = self . config [ 'files' ] for sdir in dirs : for sfile in files : if sfile . endswith ( 'conntrack_count' ) : metric_name = 'ip_conntrack_count' elif sfile . endswith ( 'conntrack_max' ) : metric_name = 'ip_conntrack_max' else : self . log . error ( 'Unknown file for collection: %s' , sfile ) continue fpath = os . path . join ( sdir , sfile ) if not os . path . exists ( fpath ) : continue try : with open ( fpath , "r" ) as fhandle : metric = float ( fhandle . readline ( ) . rstrip ( "\n" ) ) collected [ metric_name ] = metric except Exception as exception : self . log . error ( "Failed to collect from '%s': %s" , fpath , exception ) if not collected : self . log . error ( 'No metric was collected, looks like ' 'nf_conntrack/ip_conntrack kernel module was ' 'not loaded' ) else : for key in collected . keys ( ) : self . publish ( key , collected [ key ] )