blob: 5aba0f1701a0b70de3040228654ce6d78062dd98 [file] [log] [blame]
Allen Li325c0762017-03-02 15:00:19 -08001# 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 Li13bdf0c2017-03-02 15:18:16 -08007from __future__ import absolute_import
8from __future__ import print_function
Allen Li13bdf0c2017-03-02 15:18:16 -08009
Allen Lieb107022017-05-04 13:19:17 -070010import collections
Allen Lib6bb5f82017-08-21 15:11:57 -070011import socket
Allen Lieb107022017-05-04 13:19:17 -070012
Allen Li325c0762017-03-02 15:00:19 -080013import psutil
14
15from chromite.lib import cros_logging as logging
Allen Lia9c6e802017-07-11 15:42:47 -070016from chromite.lib import metrics
Allen Li325c0762017-03-02 15:00:19 -080017
18logger = logging.getLogger(__name__)
19
20_BOOT_TIME = psutil.boot_time()
21
Allen Lia9c6e802017-07-11 15:42:47 -070022_net_bytes_metric = metrics.CounterMetric(
Allen Lieb107022017-05-04 13:19:17 -070023 'dev/net/bytes', start_time=_BOOT_TIME,
Allen Li22989bd2017-07-12 10:34:37 -070024 description='Number of bytes up/down on interface.')
Allen Lia9c6e802017-07-11 15:42:47 -070025_net_packets_metric = metrics.CounterMetric(
Allen Lieb107022017-05-04 13:19:17 -070026 'dev/net/packets', start_time=_BOOT_TIME,
Allen Li22989bd2017-07-12 10:34:37 -070027 description='Number of packets up/down on interface.')
Allen Lia9c6e802017-07-11 15:42:47 -070028_net_errors_metric = metrics.CounterMetric(
Allen Lieb107022017-05-04 13:19:17 -070029 'dev/net/errors', start_time=_BOOT_TIME,
30 description='Total number of errors up/down on interface.')
Allen Lia9c6e802017-07-11 15:42:47 -070031_net_dropped_metric = metrics.CounterMetric(
Allen Lieb107022017-05-04 13:19:17 -070032 'dev/net/dropped', start_time=_BOOT_TIME,
33 description='Total number of dropped packages up/down on interface.')
34
Allen Lia9c6e802017-07-11 15:42:47 -070035_net_if_isup_metric = metrics.BooleanMetric(
Allen Li325c0762017-03-02 15:00:19 -080036 'dev/net/isup',
37 description='Whether interface is up or down.')
Allen Lia9c6e802017-07-11 15:42:47 -070038_net_if_duplex_metric = metrics.GaugeMetric(
Allen Li325c0762017-03-02 15:00:19 -080039 'dev/net/duplex',
40 description='Whether interface supports full or half duplex.')
Allen Lia9c6e802017-07-11 15:42:47 -070041_net_if_speed_metric = metrics.GaugeMetric(
Allen Li325c0762017-03-02 15:00:19 -080042 'dev/net/speed',
43 description='Network interface speed in Mb.')
Allen Lia9c6e802017-07-11 15:42:47 -070044_net_if_mtu_metric = metrics.GaugeMetric(
Allen Li325c0762017-03-02 15:00:19 -080045 'dev/net/mtu',
46 description='Network interface MTU in B.')
47
48
49def collect_net_info():
50 """Collect network metrics."""
Allen Lieb107022017-05-04 13:19:17 -070051 _collect_net_io_duplex_counters()
Allen Li325c0762017-03-02 15:00:19 -080052 _collect_net_if_stats()
Allen Lib6bb5f82017-08-21 15:11:57 -070053 _collect_fqdn()
Allen Li325c0762017-03-02 15:00:19 -080054
55
Allen Lieb107022017-05-04 13:19:17 -070056# Network IO metrics to collect
57_IOMetric = collections.namedtuple('_IOMetric', ['metric', 'up_counter_name',
58 'down_counter_name'])
59
60_net_io_duplex_metrics = (
Allen Li22989bd2017-07-12 10:34:37 -070061 _IOMetric(metric=_net_bytes_metric,
62 up_counter_name='bytes_sent',
63 down_counter_name='bytes_recv'),
64 _IOMetric(metric=_net_packets_metric,
65 up_counter_name='packets_sent',
66 down_counter_name='packets_recv'),
67 _IOMetric(metric=_net_errors_metric,
68 up_counter_name='errout',
69 down_counter_name='errin'),
70 _IOMetric(metric=_net_dropped_metric,
71 up_counter_name='dropout',
72 down_counter_name='dropin'),
Allen Lieb107022017-05-04 13:19:17 -070073)
74
75
76def _collect_net_io_duplex_counters():
77 """Collect metrics for network IO duplex counters."""
78 for nic, counters in _net_io_iter():
79 fields = {'interface': nic}
80 for metric, up_counter_name, down_counter_name in _net_io_duplex_metrics:
81 try:
82 metric.set(getattr(counters, up_counter_name),
83 fields=dict(direction='up', **fields))
84 metric.set(getattr(counters, down_counter_name),
85 fields=dict(direction='down', **fields))
Allen Lia9c6e802017-07-11 15:42:47 -070086 except metrics.MonitoringDecreasingValueError as ex:
Allen Lieb107022017-05-04 13:19:17 -070087 # This normally shouldn't happen, but might if the network
88 # driver module is reloaded, so log an error and continue
89 # instead of raising an exception.
90 logger.warning(str(ex))
91
92
Allen Li4bb931f2017-05-04 13:11:56 -070093def _net_io_iter():
94 """Generate network IO information."""
95 nics = psutil.net_io_counters(pernic=True)
96 for nic, counters in nics.iteritems():
97 if _is_virtual_netif(nic):
98 continue
99 yield nic, counters
100
101
Allen Li325c0762017-03-02 15:00:19 -0800102_net_if_metrics = (
Allen Li22989bd2017-07-12 10:34:37 -0700103 (_net_if_isup_metric, 'isup'),
104 (_net_if_duplex_metric, 'duplex'),
105 (_net_if_speed_metric, 'speed'),
106 (_net_if_mtu_metric, 'mtu'),
Allen Li325c0762017-03-02 15:00:19 -0800107)
108
109
110def _collect_net_if_stats():
111 """Collect metrics for network interface stats."""
112 for nic, stats in psutil.net_if_stats().iteritems():
113 if _is_virtual_netif(nic):
114 continue
115 fields = {'interface': nic}
116 for metric, counter_name in _net_if_metrics:
117 metric.set(getattr(stats, counter_name), fields=fields)
118
119
120def _is_virtual_netif(nic):
Allen Li22989bd2017-07-12 10:34:37 -0700121 """Return whether the network interface is virtual."""
122 # TODO(ayatane): Use a different way of identifying virtual interfaces
123 return nic.startswith('veth')
Allen Lib6bb5f82017-08-21 15:11:57 -0700124
125
126_fqdn_metric = metrics.StringMetric('net/fqdn', description='FQDN')
127
128
129def _collect_fqdn():
130 fqdn = socket.getfqdn()
131 logging.debug('Got FQDN: %s', fqdn)
132 _fqdn_metric.set(fqdn)