def _report_net_metrics(self, container, tags):
        """Find container network metrics by looking at /proc/$PID/net/dev of the container process."""
        if self._disable_net_metrics:
            self.log.debug("Network metrics are disabled. Skipping")
            return

        proc_net_file = os.path.join(container['_proc_root'], 'net/dev')

        try:
            if container['Id'] in self.network_mappings:
                networks = self.network_mappings[container['Id']]
            else:
                networks = self.docker_util.get_container_network_mapping(container)
                if not networks:
                    networks = {'eth0': 'bridge'}
                self.network_mappings[container['Id']] = networks
        except Exception as e:
            # Revert to previous behaviour if the method is missing or failing
            # Debug message will only appear once per container, then the cache is used
            self.log.debug("Failed to build docker network mapping, using failsafe. Exception: {0}".format(e))
            networks = {'eth0': 'bridge'}
            self.network_mappings[container['Id']] = networks

        try:
            with open(proc_net_file, 'r') as fp:
                lines = fp.readlines()
                """Two first lines are headers:
                Inter-|   Receive                                                |  Transmit
                 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
                """
                for l in lines[2:]:
                    cols = l.split(':', 1)
                    interface_name = str(cols[0]).strip()
                    if interface_name in networks:
                        net_tags = tags + ['docker_network:'+networks[interface_name]]
                        x = cols[1].split()
                        m_func = FUNC_MAP[RATE][self.use_histogram]
                        m_func(self, "docker.net.bytes_rcvd", long(x[0]), net_tags)
                        m_func(self, "docker.net.bytes_sent", long(x[8]), net_tags)

        except IOError as e:
            # It is possible that the container got stopped between the API call and now
            self.log.debug("Cannot read network interface file, container likely raced to finish : {0}".format(e))
def _report_net_metrics ( self , container , tags ) : if self . _disable_net_metrics : self . log . debug ( "Network metrics are disabled. Skipping" ) return proc_net_file = os . path . join ( container [ '_proc_root' ] , 'net/dev' ) try : if container [ 'Id' ] in self . network_mappings : networks = self . network_mappings [ container [ 'Id' ] ] else : networks = self . docker_util . get_container_network_mapping ( container ) if not networks : networks = { 'eth0' : 'bridge' } self . network_mappings [ container [ 'Id' ] ] = networks except Exception as e : self . log . debug ( "Failed to build docker network mapping, using failsafe. Exception: {0}" . format ( e ) ) networks = { 'eth0' : 'bridge' } self . network_mappings [ container [ 'Id' ] ] = networks try : with open ( proc_net_file , 'r' ) as fp : lines = fp . readlines ( ) for l in lines [ 2 : ] : cols = l . split ( ':' , 1 ) interface_name = str ( cols [ 0 ] ) . strip ( ) if interface_name in networks : net_tags = tags + [ 'docker_network:' + networks [ interface_name ] ] x = cols [ 1 ] . split ( ) m_func = FUNC_MAP [ RATE ] [ self . use_histogram ] m_func ( self , "docker.net.bytes_rcvd" , long ( x [ 0 ] ) , net_tags ) m_func ( self , "docker.net.bytes_sent" , long ( x [ 8 ] ) , net_tags ) except IOError as e : self . log . debug ( "Cannot read network interface file, container likely raced to finish : {0}" . format ( e ) )
Find container network metrics by looking at /proc/$PID/net/dev of the container process.