Exemplo n.º 1
0
    def network(meta):
        """
        network -- Docker network meta report leaves out IP addresses since they will be different per container.
        """

        # collect info for all the alive interfaces
        for interface in alive_interfaces():
            addresses = netifaces.ifaddresses(interface)
            interface_info = {
                'name': interface,
                'mac': addresses.get(netifaces.AF_LINK, [{}])[0].get('addr')  # Collect MAC address.
            }

            meta['network']['interfaces'].append(interface_info)

        # get default interface name
        netstat_out, _ = subp.call("netstat -nr | egrep -i '^0.0.0.0|default'", check=False)
        if len(netstat_out) and netstat_out[0]:
            first_matched_line = netstat_out[0]
            default_interface = first_matched_line.split(' ')[-1]
        elif len(meta['network']['interfaces']):
            default_interface = meta['network']['interfaces'][0]['name']
        else:
            default_interface = None

        meta['network']['default'] = default_interface
Exemplo n.º 2
0
    def network(meta):
        """ network """

        # collect info for all hte alive interfaces
        for interface in alive_interfaces():
            addresses = netifaces.ifaddresses(interface)
            interface_info = {'name': interface}

            # collect ipv4 and ipv6 addresses
            for proto, key in {
                    'ipv4': netifaces.AF_INET,
                    'ipv6': netifaces.AF_INET6
            }.iteritems():
                # get the first address
                protocol_data = addresses.get(key, [{}])[0]
                if protocol_data:
                    addr = protocol_data.get('addr').split('%').pop(0)
                    netmask = protocol_data.get('netmask')

                    try:
                        prefixlen = netaddr.IPNetwork(
                            '%s/%s' % (addr, netmask)).prefixlen
                    except:
                        prefixlen = None

                    interface_info[proto] = {
                        'netmask': netmask,
                        'address': addr,
                        'prefixlen': prefixlen
                    }

            # collect mac address
            interface_info['mac'] = addresses.get(netifaces.AF_LINK,
                                                  [{}])[0].get('addr')

            meta['network']['interfaces'].append(interface_info)

        # get default interface name
        netstat_out, _ = subp.call("netstat -nr | egrep -i '^0.0.0.0|default'",
                                   check=False)
        if len(netstat_out) and netstat_out[0]:
            first_matched_line = netstat_out[0]
            default_interface = first_matched_line.split(' ')[-1]
        elif len(meta['network']['interfaces']):
            default_interface = meta['network']['interfaces'][0]['name']
        else:
            default_interface = None

        meta['network']['default'] = default_interface
Exemplo n.º 3
0
    def network(meta):
        """ network """

        # collect info for all the alive interfaces
        for interface in alive_interfaces():
            addresses = netifaces.ifaddresses(interface)
            interface_info = {
                'name': interface
            }

            # collect ipv4 and ipv6 addresses
            for proto, key in {
                'ipv4': netifaces.AF_INET,
                'ipv6': netifaces.AF_INET6
            }.iteritems():
                # get the first address
                protocol_data = addresses.get(key, [{}])[0]
                if protocol_data:
                    addr = protocol_data.get('addr').split('%').pop(0)
                    netmask = protocol_data.get('netmask')

                    try:
                        prefixlen = netaddr.IPNetwork('%s/%s' % (addr, netmask)).prefixlen
                    except:
                        prefixlen = None

                    interface_info[proto] = {
                        'netmask': netmask,
                        'address': addr,
                        'prefixlen': prefixlen
                    }

            # collect mac address
            interface_info['mac'] = addresses.get(netifaces.AF_LINK, [{}])[0].get('addr')

            meta['network']['interfaces'].append(interface_info)

        # get default interface name
        netstat_out, _ = subp.call("netstat -nr | egrep -i '^0.0.0.0|default'", check=False)
        if len(netstat_out) and netstat_out[0]:
            first_matched_line = netstat_out[0]
            default_interface = first_matched_line.split(' ')[-1]
        elif len(meta['network']['interfaces']):
            default_interface = meta['network']['interfaces'][0]['name']
        else:
            default_interface = None

        meta['network']['default'] = default_interface
Exemplo n.º 4
0
    def net_io_counters(self):
        """
        net io counters

        total counters do not include lo interface
        """
        totals = defaultdict(int)
        metrics = {
            'packets_sent': 'system.net.packets_out.count',
            'packets_recv': 'system.net.packets_in.count',
            'bytes_sent': 'system.net.bytes_sent',
            'bytes_recv': 'system.net.bytes_rcvd',
            'errin': 'system.net.packets_in.error',
            'errout': 'system.net.packets_out.error',
            'dropin': 'system.net.drops_in.count',
            'dropout': 'system.net.drops_out.count'
        }

        net_io_counters = psutil.net_io_counters(pernic=True)
        for interface in host.alive_interfaces():
            io = net_io_counters.get(interface)

            if not io:
                continue

            for method, metric in metrics.iteritems():
                new_stamp, new_value = time.time(), getattr(io, method)
                prev_stamp, prev_value = self.previous_counters.get(
                    interface, {}).get(metric, (None, None))

                if prev_stamp and new_value >= prev_value:
                    delta_value = new_value - prev_value
                    metric_full_name = '%s|%s' % (metric, interface)
                    self.object.statsd.incr(metric_full_name, delta_value)

                    # collect total values
                    if not interface.startswith('lo'):
                        totals[metric] += delta_value

                self.previous_counters[interface][metric] = (new_stamp,
                                                             new_value)

        # send total values
        for metric, value in totals.iteritems():
            self.object.statsd.incr(metric, value)
Exemplo n.º 5
0
    def network(self):
        """ network """
        # collect info for all the alive interfaces
        for interface in alive_interfaces():
            addresses = netifaces.ifaddresses(interface)
            interface_info = {'name': interface}

            # collect addresses (if not running in a container)
            if not self.in_container:
                for proto, key in (('ipv4', netifaces.AF_INET),
                                   ('ipv6', netifaces.AF_INET6)):
                    protocol_data = addresses.get(key, [{}])[0]
                    if protocol_data:
                        interface_info[proto] = {
                            'address':
                            protocol_data.get('addr').split('%').pop(0),
                            'netmask': protocol_data.get('netmask')
                        }
                        try:
                            address = '%(address)s/%(netmask)s' % interface_info[
                                proto]
                            interface_info[proto][
                                'prefixlen'] = netaddr.IPNetwork(
                                    address).prefixlen
                        except:
                            interface_info[proto]['prefixlen'] = None

            # collect MAC address
            interface_info['mac'] = addresses.get(netifaces.AF_LINK,
                                                  [{}])[0].get('addr')

            self.meta['network']['interfaces'].append(interface_info)

        # get default interface name
        netstat_out, _ = subp.call("netstat -nr | egrep -i '^0.0.0.0|default'",
                                   check=False)
        if netstat_out and netstat_out[0]:
            first_matched_line = netstat_out[0]
            default_interface = first_matched_line.split(' ')[-1]
        elif self.meta['network']['interfaces']:
            default_interface = self.meta['network']['interfaces'][0]['name']
        else:
            default_interface = None

        self.meta['network']['default'] = default_interface
Exemplo n.º 6
0
    def net_io_counters(self):
        """
        net io counters

        total counters do not include lo interface
        """
        totals = defaultdict(int)
        metrics = {
            'packets_sent': 'system.net.packets_out.count',
            'packets_recv': 'system.net.packets_in.count',
            'bytes_sent': 'system.net.bytes_sent',
            'bytes_recv': 'system.net.bytes_rcvd',
            'errin': 'system.net.packets_in.error',
            'errout': 'system.net.packets_out.error',
            'dropin': 'system.net.drops_in.count',
            'dropout': 'system.net.drops_out.count'
        }

        net_io_counters = psutil.net_io_counters(pernic=True)
        for interface in host.alive_interfaces():
            io = net_io_counters.get(interface)

            for method, metric in metrics.iteritems():
                new_stamp, new_value = time.time(), getattr(io, method)
                prev_stamp, prev_value = self.previous_values.get(interface, {}).get(metric, [None, None])

                if prev_stamp and new_value >= prev_value:
                    delta_value = new_value - prev_value
                    metric_full_name = '%s|%s' % (metric, interface)
                    self.object.statsd.incr(metric_full_name, delta_value)

                    # collect total values
                    if not interface.startswith('lo'):
                        totals[metric] += delta_value

                self.previous_values[interface][metric] = [new_stamp, new_value]

        # send total values
        for metric, value in totals.iteritems():
            self.object.statsd.incr(metric, value)
Exemplo n.º 7
0
    def network(self):
        """ network """
        # collect info for all the alive interfaces
        for interface in alive_interfaces():
            addresses = netifaces.ifaddresses(interface)
            interface_info = {'name': interface}

            # collect addresses (if not running in a container)
            if not self.in_container:
                for proto, key in (('ipv4', netifaces.AF_INET), ('ipv6', netifaces.AF_INET6)):
                    protocol_data = addresses.get(key, [{}])[0]
                    if protocol_data:
                        interface_info[proto] = {
                            'address': protocol_data.get('addr').split('%').pop(0),
                            'netmask': protocol_data.get('netmask')
                        }
                        try:
                            address = '%(address)s/%(netmask)s' % interface_info[proto]
                            interface_info[proto]['prefixlen'] = netaddr.IPNetwork(address).prefixlen
                        except:
                            interface_info[proto]['prefixlen'] = None

            # collect MAC address
            interface_info['mac'] = addresses.get(netifaces.AF_LINK, [{}])[0].get('addr')

            self.meta['network']['interfaces'].append(interface_info)

        # get default interface name
        netstat_out, _ = subp.call("netstat -nr | egrep -i '^0.0.0.0|default'", check=False)
        if netstat_out and netstat_out[0]:
            first_matched_line = netstat_out[0]
            default_interface = first_matched_line.split(' ')[-1]
        elif self.meta['network']['interfaces']:
            default_interface = self.meta['network']['interfaces'][0]['name']
        else:
            default_interface = None

        self.meta['network']['default'] = default_interface