Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Network metrics.""" |
| 6 | |
Allen Li | 13bdf0c | 2017-03-02 15:18:16 -0800 | [diff] [blame] | 7 | from __future__ import absolute_import |
Allen Li | 13bdf0c | 2017-03-02 15:18:16 -0800 | [diff] [blame] | 8 | |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 9 | import collections |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 10 | import logging |
Allen Li | b6bb5f8 | 2017-08-21 15:11:57 -0700 | [diff] [blame] | 11 | import socket |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 12 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 13 | from chromite.third_party.infra_libs import ts_mon |
Mike Frysinger | cb56b64 | 2019-08-25 15:33:08 -0400 | [diff] [blame] | 14 | import psutil # pylint: disable=import-error |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 15 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 16 | from chromite.lib import metrics |
Mike Frysinger | 8d6a51d | 2021-02-12 07:40:03 -0500 | [diff] [blame] | 17 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 18 | |
| 19 | logger = logging.getLogger(__name__) |
| 20 | |
| 21 | _BOOT_TIME = psutil.boot_time() |
| 22 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 23 | _net_bytes_metric = metrics.CounterMetric( |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 24 | 'dev/net/bytes', start_time=_BOOT_TIME, |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 25 | description='Number of bytes up/down on interface.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 26 | _net_packets_metric = metrics.CounterMetric( |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 27 | 'dev/net/packets', start_time=_BOOT_TIME, |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 28 | description='Number of packets up/down on interface.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 29 | _net_errors_metric = metrics.CounterMetric( |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 30 | 'dev/net/errors', start_time=_BOOT_TIME, |
| 31 | description='Total number of errors up/down on interface.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 32 | _net_dropped_metric = metrics.CounterMetric( |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 33 | 'dev/net/dropped', start_time=_BOOT_TIME, |
| 34 | description='Total number of dropped packages up/down on interface.') |
| 35 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 36 | _net_if_isup_metric = metrics.BooleanMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 37 | 'dev/net/isup', |
| 38 | description='Whether interface is up or down.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 39 | _net_if_duplex_metric = metrics.GaugeMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 40 | 'dev/net/duplex', |
| 41 | description='Whether interface supports full or half duplex.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 42 | _net_if_speed_metric = metrics.GaugeMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 43 | 'dev/net/speed', |
| 44 | description='Network interface speed in Mb.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 45 | _net_if_mtu_metric = metrics.GaugeMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 46 | 'dev/net/mtu', |
| 47 | description='Network interface MTU in B.') |
| 48 | |
| 49 | |
| 50 | def collect_net_info(): |
| 51 | """Collect network metrics.""" |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 52 | _collect_net_io_duplex_counters() |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 53 | _collect_net_if_stats() |
Allen Li | b6bb5f8 | 2017-08-21 15:11:57 -0700 | [diff] [blame] | 54 | _collect_fqdn() |
Prathmesh Prabhu | 07f8087 | 2017-08-21 15:09:33 -0700 | [diff] [blame] | 55 | _collect_net_if_addrs() |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 56 | |
| 57 | |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 58 | # Network IO metrics to collect |
| 59 | _IOMetric = collections.namedtuple('_IOMetric', ['metric', 'up_counter_name', |
| 60 | 'down_counter_name']) |
| 61 | |
| 62 | _net_io_duplex_metrics = ( |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 63 | _IOMetric(metric=_net_bytes_metric, |
| 64 | up_counter_name='bytes_sent', |
| 65 | down_counter_name='bytes_recv'), |
| 66 | _IOMetric(metric=_net_packets_metric, |
| 67 | up_counter_name='packets_sent', |
| 68 | down_counter_name='packets_recv'), |
| 69 | _IOMetric(metric=_net_errors_metric, |
| 70 | up_counter_name='errout', |
| 71 | down_counter_name='errin'), |
| 72 | _IOMetric(metric=_net_dropped_metric, |
| 73 | up_counter_name='dropout', |
| 74 | down_counter_name='dropin'), |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 75 | ) |
| 76 | |
| 77 | |
| 78 | def _collect_net_io_duplex_counters(): |
| 79 | """Collect metrics for network IO duplex counters.""" |
| 80 | for nic, counters in _net_io_iter(): |
| 81 | fields = {'interface': nic} |
| 82 | for metric, up_counter_name, down_counter_name in _net_io_duplex_metrics: |
| 83 | try: |
| 84 | metric.set(getattr(counters, up_counter_name), |
| 85 | fields=dict(direction='up', **fields)) |
| 86 | metric.set(getattr(counters, down_counter_name), |
| 87 | fields=dict(direction='down', **fields)) |
Allen Li | 0719697 | 2018-05-08 13:49:08 -0700 | [diff] [blame] | 88 | except ts_mon.MonitoringDecreasingValueError as ex: |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 89 | # This normally shouldn't happen, but might if the network |
| 90 | # driver module is reloaded, so log an error and continue |
| 91 | # instead of raising an exception. |
| 92 | logger.warning(str(ex)) |
| 93 | |
| 94 | |
Allen Li | 4bb931f | 2017-05-04 13:11:56 -0700 | [diff] [blame] | 95 | def _net_io_iter(): |
| 96 | """Generate network IO information.""" |
| 97 | nics = psutil.net_io_counters(pernic=True) |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 98 | for nic, counters in nics.items(): |
Allen Li | 4bb931f | 2017-05-04 13:11:56 -0700 | [diff] [blame] | 99 | if _is_virtual_netif(nic): |
| 100 | continue |
| 101 | yield nic, counters |
| 102 | |
| 103 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 104 | _net_if_metrics = ( |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 105 | (_net_if_isup_metric, 'isup'), |
| 106 | (_net_if_duplex_metric, 'duplex'), |
| 107 | (_net_if_speed_metric, 'speed'), |
| 108 | (_net_if_mtu_metric, 'mtu'), |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 109 | ) |
| 110 | |
| 111 | |
| 112 | def _collect_net_if_stats(): |
| 113 | """Collect metrics for network interface stats.""" |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 114 | for nic, stats in psutil.net_if_stats().items(): |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 115 | if _is_virtual_netif(nic): |
| 116 | continue |
| 117 | fields = {'interface': nic} |
| 118 | for metric, counter_name in _net_if_metrics: |
| 119 | metric.set(getattr(stats, counter_name), fields=fields) |
| 120 | |
| 121 | |
Prathmesh Prabhu | 07f8087 | 2017-08-21 15:09:33 -0700 | [diff] [blame] | 122 | _net_if_addrs_metrics = metrics.StringMetric( |
| 123 | 'dev/net/address', |
| 124 | description='Network address of physical network interfaces.') |
| 125 | _family_field_strings = { |
| 126 | psutil.AF_LINK: 'AF_LINK', |
| 127 | socket.AF_INET: 'AF_INET', |
| 128 | socket.AF_INET6: 'AF_INET6', |
| 129 | } |
| 130 | |
| 131 | |
| 132 | def _collect_net_if_addrs(): |
| 133 | """Collects network addresses as metrics.""" |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 134 | for nic, addresses in psutil.net_if_addrs().items(): |
Prathmesh Prabhu | 07f8087 | 2017-08-21 15:09:33 -0700 | [diff] [blame] | 135 | if _is_virtual_netif(nic): |
| 136 | continue |
| 137 | for address in addresses: |
| 138 | fields = { |
| 139 | 'interface': nic, |
| 140 | 'family': _family_field_strings.get(address.family, 'UNKNOWN'), |
| 141 | } |
| 142 | _net_if_addrs_metrics.set(address.address, fields) |
| 143 | |
| 144 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 145 | def _is_virtual_netif(nic): |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 146 | """Return whether the network interface is virtual.""" |
| 147 | # TODO(ayatane): Use a different way of identifying virtual interfaces |
| 148 | return nic.startswith('veth') |
Allen Li | b6bb5f8 | 2017-08-21 15:11:57 -0700 | [diff] [blame] | 149 | |
| 150 | |
| 151 | _fqdn_metric = metrics.StringMetric('net/fqdn', description='FQDN') |
| 152 | |
| 153 | |
| 154 | def _collect_fqdn(): |
| 155 | fqdn = socket.getfqdn() |
| 156 | logging.debug('Got FQDN: %s', fqdn) |
| 157 | _fqdn_metric.set(fqdn) |