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( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | "dev/net/bytes", |
| 25 | start_time=_BOOT_TIME, |
| 26 | description="Number of bytes up/down on interface.", |
| 27 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 28 | _net_packets_metric = metrics.CounterMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | "dev/net/packets", |
| 30 | start_time=_BOOT_TIME, |
| 31 | description="Number of packets up/down on interface.", |
| 32 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 33 | _net_errors_metric = metrics.CounterMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 34 | "dev/net/errors", |
| 35 | start_time=_BOOT_TIME, |
| 36 | description="Total number of errors up/down on interface.", |
| 37 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 38 | _net_dropped_metric = metrics.CounterMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | "dev/net/dropped", |
| 40 | start_time=_BOOT_TIME, |
| 41 | description="Total number of dropped packages up/down on interface.", |
| 42 | ) |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 43 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 44 | _net_if_isup_metric = metrics.BooleanMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | "dev/net/isup", description="Whether interface is up or down." |
| 46 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 47 | _net_if_duplex_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | "dev/net/duplex", |
| 49 | description="Whether interface supports full or half duplex.", |
| 50 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 51 | _net_if_speed_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | "dev/net/speed", description="Network interface speed in Mb." |
| 53 | ) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 54 | _net_if_mtu_metric = metrics.GaugeMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | "dev/net/mtu", description="Network interface MTU in B." |
| 56 | ) |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def collect_net_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | """Collect network metrics.""" |
| 61 | _collect_net_io_duplex_counters() |
| 62 | _collect_net_if_stats() |
| 63 | _collect_fqdn() |
| 64 | _collect_net_if_addrs() |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 65 | |
| 66 | |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 67 | # Network IO metrics to collect |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | _IOMetric = collections.namedtuple( |
| 69 | "_IOMetric", ["metric", "up_counter_name", "down_counter_name"] |
| 70 | ) |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 71 | |
| 72 | _net_io_duplex_metrics = ( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | _IOMetric( |
| 74 | metric=_net_bytes_metric, |
| 75 | up_counter_name="bytes_sent", |
| 76 | down_counter_name="bytes_recv", |
| 77 | ), |
| 78 | _IOMetric( |
| 79 | metric=_net_packets_metric, |
| 80 | up_counter_name="packets_sent", |
| 81 | down_counter_name="packets_recv", |
| 82 | ), |
| 83 | _IOMetric( |
| 84 | metric=_net_errors_metric, |
| 85 | up_counter_name="errout", |
| 86 | down_counter_name="errin", |
| 87 | ), |
| 88 | _IOMetric( |
| 89 | metric=_net_dropped_metric, |
| 90 | up_counter_name="dropout", |
| 91 | down_counter_name="dropin", |
| 92 | ), |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 93 | ) |
| 94 | |
| 95 | |
| 96 | def _collect_net_io_duplex_counters(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | """Collect metrics for network IO duplex counters.""" |
| 98 | for nic, counters in _net_io_iter(): |
| 99 | fields = {"interface": nic} |
| 100 | for ( |
| 101 | metric, |
| 102 | up_counter_name, |
| 103 | down_counter_name, |
| 104 | ) in _net_io_duplex_metrics: |
| 105 | try: |
| 106 | metric.set( |
| 107 | getattr(counters, up_counter_name), |
| 108 | fields=dict(direction="up", **fields), |
| 109 | ) |
| 110 | metric.set( |
| 111 | getattr(counters, down_counter_name), |
| 112 | fields=dict(direction="down", **fields), |
| 113 | ) |
| 114 | except ts_mon.MonitoringDecreasingValueError as ex: |
| 115 | # This normally shouldn't happen, but might if the network |
| 116 | # driver module is reloaded, so log an error and continue |
| 117 | # instead of raising an exception. |
| 118 | logger.warning(str(ex)) |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 119 | |
| 120 | |
Allen Li | 4bb931f | 2017-05-04 13:11:56 -0700 | [diff] [blame] | 121 | def _net_io_iter(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 122 | """Generate network IO information.""" |
| 123 | nics = psutil.net_io_counters(pernic=True) |
| 124 | for nic, counters in nics.items(): |
| 125 | if _is_virtual_netif(nic): |
| 126 | continue |
| 127 | yield nic, counters |
Allen Li | 4bb931f | 2017-05-04 13:11:56 -0700 | [diff] [blame] | 128 | |
| 129 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 130 | _net_if_metrics = ( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | (_net_if_isup_metric, "isup"), |
| 132 | (_net_if_duplex_metric, "duplex"), |
| 133 | (_net_if_speed_metric, "speed"), |
| 134 | (_net_if_mtu_metric, "mtu"), |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 135 | ) |
| 136 | |
| 137 | |
| 138 | def _collect_net_if_stats(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | """Collect metrics for network interface stats.""" |
| 140 | for nic, stats in psutil.net_if_stats().items(): |
| 141 | if _is_virtual_netif(nic): |
| 142 | continue |
| 143 | fields = {"interface": nic} |
| 144 | for metric, counter_name in _net_if_metrics: |
| 145 | metric.set(getattr(stats, counter_name), fields=fields) |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 146 | |
| 147 | |
Prathmesh Prabhu | 07f8087 | 2017-08-21 15:09:33 -0700 | [diff] [blame] | 148 | _net_if_addrs_metrics = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | "dev/net/address", |
| 150 | description="Network address of physical network interfaces.", |
| 151 | ) |
Prathmesh Prabhu | 07f8087 | 2017-08-21 15:09:33 -0700 | [diff] [blame] | 152 | _family_field_strings = { |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | psutil.AF_LINK: "AF_LINK", |
| 154 | socket.AF_INET: "AF_INET", |
| 155 | socket.AF_INET6: "AF_INET6", |
Prathmesh Prabhu | 07f8087 | 2017-08-21 15:09:33 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | def _collect_net_if_addrs(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 160 | """Collects network addresses as metrics.""" |
| 161 | for nic, addresses in psutil.net_if_addrs().items(): |
| 162 | if _is_virtual_netif(nic): |
| 163 | continue |
| 164 | for address in addresses: |
| 165 | fields = { |
| 166 | "interface": nic, |
| 167 | "family": _family_field_strings.get(address.family, "UNKNOWN"), |
| 168 | } |
| 169 | _net_if_addrs_metrics.set(address.address, fields) |
Prathmesh Prabhu | 07f8087 | 2017-08-21 15:09:33 -0700 | [diff] [blame] | 170 | |
| 171 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 172 | def _is_virtual_netif(nic): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | """Return whether the network interface is virtual.""" |
| 174 | # TODO(ayatane): Use a different way of identifying virtual interfaces |
| 175 | return nic.startswith("veth") |
Allen Li | b6bb5f8 | 2017-08-21 15:11:57 -0700 | [diff] [blame] | 176 | |
| 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | _fqdn_metric = metrics.StringMetric("net/fqdn", description="FQDN") |
Allen Li | b6bb5f8 | 2017-08-21 15:11:57 -0700 | [diff] [blame] | 179 | |
| 180 | |
| 181 | def _collect_fqdn(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 182 | fqdn = socket.getfqdn() |
| 183 | logging.debug("Got FQDN: %s", fqdn) |
| 184 | _fqdn_metric.set(fqdn) |