(self)
Collect and publish netfilter counters
Collect and publish netfilter counters
Collect and publish netfilter counters
def collect(self):
        """
        Collect and publish netfilter counters
        """
        cmd = [self.config['bin'], "list"]

        if str_to_bool(self.config['reset']):
            cmd.append("reset")

        if str_to_bool(self.config['use_sudo']):
            cmd.insert(0, self.config['sudo_cmd'])

        # We avoid use of the XML format to mtaintain compatbility with older
        # versions of nfacct and also to avoid the bug where pkts and bytes were
        # flipped

        # Each line is of the format:
        # { pkts = 00000000000001121700, bytes = 00000000000587037355 } = ipv4;
        matcher = re.compile("{ pkts = (.*), bytes = (.*) } = (.*);")
        lines = Popen(cmd, stdout=PIPE).communicate()[0].strip().splitlines()

        for line in lines:
            matches = re.match(matcher, line)
            if matches:
                num_packets = int(matches.group(1))
                num_bytes = int(matches.group(2))
                name = matches.group(3)
                self.publish(name + ".pkts", num_packets)
                self.publish(name + ".bytes", num_bytes)
def collect ( self ) : cmd = [ self . config [ 'bin' ] , "list" ] if str_to_bool ( self . config [ 'reset' ] ) : cmd . append ( "reset" ) if str_to_bool ( self . config [ 'use_sudo' ] ) : cmd . insert ( 0 , self . config [ 'sudo_cmd' ] ) # We avoid use of the XML format to mtaintain compatbility with older # versions of nfacct and also to avoid the bug where pkts and bytes were # flipped # Each line is of the format: # { pkts = 00000000000001121700, bytes = 00000000000587037355 } = ipv4; matcher = re . compile ( "{ pkts = (.*), bytes = (.*) } = (.*);" ) lines = Popen ( cmd , stdout = PIPE ) . communicate ( ) [ 0 ] . strip ( ) . splitlines ( ) for line in lines : matches = re . match ( matcher , line ) if matches : num_packets = int ( matches . group ( 1 ) ) num_bytes = int ( matches . group ( 2 ) ) name = matches . group ( 3 ) self . publish ( name + ".pkts" , num_packets ) self . publish ( name + ".bytes" , num_bytes )