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 |
| 8 | from __future__ import print_function |
Allen Li | 13bdf0c | 2017-03-02 15:18:16 -0800 | [diff] [blame] | 9 | |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 10 | import collections |
| 11 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 12 | import psutil |
| 13 | |
| 14 | from chromite.lib import cros_logging as logging |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 15 | from chromite.lib import metrics |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 16 | |
| 17 | logger = logging.getLogger(__name__) |
| 18 | |
| 19 | _BOOT_TIME = psutil.boot_time() |
| 20 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 21 | _net_bytes_metric = metrics.CounterMetric( |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 22 | 'dev/net/bytes', start_time=_BOOT_TIME, |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 23 | description='Number of bytes up/down on interface.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 24 | _net_packets_metric = metrics.CounterMetric( |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 25 | 'dev/net/packets', start_time=_BOOT_TIME, |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 26 | description='Number of packets up/down on interface.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 27 | _net_errors_metric = metrics.CounterMetric( |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 28 | 'dev/net/errors', start_time=_BOOT_TIME, |
| 29 | description='Total number of errors up/down on interface.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 30 | _net_dropped_metric = metrics.CounterMetric( |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 31 | 'dev/net/dropped', start_time=_BOOT_TIME, |
| 32 | description='Total number of dropped packages up/down on interface.') |
| 33 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 34 | _net_if_isup_metric = metrics.BooleanMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 35 | 'dev/net/isup', |
| 36 | description='Whether interface is up or down.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 37 | _net_if_duplex_metric = metrics.GaugeMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 38 | 'dev/net/duplex', |
| 39 | description='Whether interface supports full or half duplex.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 40 | _net_if_speed_metric = metrics.GaugeMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 41 | 'dev/net/speed', |
| 42 | description='Network interface speed in Mb.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 43 | _net_if_mtu_metric = metrics.GaugeMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 44 | 'dev/net/mtu', |
| 45 | description='Network interface MTU in B.') |
| 46 | |
| 47 | |
| 48 | def collect_net_info(): |
| 49 | """Collect network metrics.""" |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 50 | _collect_net_io_duplex_counters() |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 51 | _collect_net_if_stats() |
| 52 | |
| 53 | |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 54 | # Network IO metrics to collect |
| 55 | _IOMetric = collections.namedtuple('_IOMetric', ['metric', 'up_counter_name', |
| 56 | 'down_counter_name']) |
| 57 | |
| 58 | _net_io_duplex_metrics = ( |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 59 | _IOMetric(metric=_net_bytes_metric, |
| 60 | up_counter_name='bytes_sent', |
| 61 | down_counter_name='bytes_recv'), |
| 62 | _IOMetric(metric=_net_packets_metric, |
| 63 | up_counter_name='packets_sent', |
| 64 | down_counter_name='packets_recv'), |
| 65 | _IOMetric(metric=_net_errors_metric, |
| 66 | up_counter_name='errout', |
| 67 | down_counter_name='errin'), |
| 68 | _IOMetric(metric=_net_dropped_metric, |
| 69 | up_counter_name='dropout', |
| 70 | down_counter_name='dropin'), |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 71 | ) |
| 72 | |
| 73 | |
| 74 | def _collect_net_io_duplex_counters(): |
| 75 | """Collect metrics for network IO duplex counters.""" |
| 76 | for nic, counters in _net_io_iter(): |
| 77 | fields = {'interface': nic} |
| 78 | for metric, up_counter_name, down_counter_name in _net_io_duplex_metrics: |
| 79 | try: |
| 80 | metric.set(getattr(counters, up_counter_name), |
| 81 | fields=dict(direction='up', **fields)) |
| 82 | metric.set(getattr(counters, down_counter_name), |
| 83 | fields=dict(direction='down', **fields)) |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 84 | except metrics.MonitoringDecreasingValueError as ex: |
Allen Li | eb10702 | 2017-05-04 13:19:17 -0700 | [diff] [blame] | 85 | # This normally shouldn't happen, but might if the network |
| 86 | # driver module is reloaded, so log an error and continue |
| 87 | # instead of raising an exception. |
| 88 | logger.warning(str(ex)) |
| 89 | |
| 90 | |
Allen Li | 4bb931f | 2017-05-04 13:11:56 -0700 | [diff] [blame] | 91 | def _net_io_iter(): |
| 92 | """Generate network IO information.""" |
| 93 | nics = psutil.net_io_counters(pernic=True) |
| 94 | for nic, counters in nics.iteritems(): |
| 95 | if _is_virtual_netif(nic): |
| 96 | continue |
| 97 | yield nic, counters |
| 98 | |
| 99 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 100 | _net_if_metrics = ( |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 101 | (_net_if_isup_metric, 'isup'), |
| 102 | (_net_if_duplex_metric, 'duplex'), |
| 103 | (_net_if_speed_metric, 'speed'), |
| 104 | (_net_if_mtu_metric, 'mtu'), |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 105 | ) |
| 106 | |
| 107 | |
| 108 | def _collect_net_if_stats(): |
| 109 | """Collect metrics for network interface stats.""" |
| 110 | for nic, stats in psutil.net_if_stats().iteritems(): |
| 111 | if _is_virtual_netif(nic): |
| 112 | continue |
| 113 | fields = {'interface': nic} |
| 114 | for metric, counter_name in _net_if_metrics: |
| 115 | metric.set(getattr(stats, counter_name), fields=fields) |
| 116 | |
| 117 | |
| 118 | def _is_virtual_netif(nic): |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 119 | """Return whether the network interface is virtual.""" |
| 120 | # TODO(ayatane): Use a different way of identifying virtual interfaces |
| 121 | return nic.startswith('veth') |