blob: eca344be7cdd7c75ae1f724bfa5c1b269de27d9f [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
9from __future__ import unicode_literals
10
Allen Li325c0762017-03-02 15:00:19 -080011import psutil
12
13from chromite.lib import cros_logging as logging
14from infra_libs import ts_mon
15
16logger = logging.getLogger(__name__)
17
18_BOOT_TIME = psutil.boot_time()
19
20_net_up_metric = ts_mon.CounterMetric(
21 'dev/net/bytes/up', start_time=_BOOT_TIME,
22 description='Number of bytes sent on interface.',
23 units=ts_mon.MetricsDataUnits.BYTES)
24_net_down_metric = ts_mon.CounterMetric(
25 'dev/net/bytes/down', start_time=_BOOT_TIME,
26 description='Number of Bytes received on '
27 'interface.',
28 units=ts_mon.MetricsDataUnits.BYTES)
29_net_err_up_metric = ts_mon.CounterMetric(
30 'dev/net/err/up', start_time=_BOOT_TIME,
31 description='Total number of errors when '
32 'sending (per interface).')
33_net_err_down_metric = ts_mon.CounterMetric(
34 'dev/net/err/down', start_time=_BOOT_TIME,
35 description='Total number of errors when '
36 'receiving (per interface).')
37_net_drop_up_metric = ts_mon.CounterMetric(
38 'dev/net/drop/up', start_time=_BOOT_TIME,
39 description='Total number of outgoing '
40 'packets that have been dropped.')
41_net_drop_down_metric = ts_mon.CounterMetric(
42 'dev/net/drop/down', start_time=_BOOT_TIME,
43 description='Total number of incoming '
44 'packets that have been dropped.')
45
46_net_if_isup_metric = ts_mon.BooleanMetric(
47 'dev/net/isup',
48 description='Whether interface is up or down.')
49_net_if_duplex_metric = ts_mon.GaugeMetric(
50 'dev/net/duplex',
51 description='Whether interface supports full or half duplex.')
52_net_if_speed_metric = ts_mon.GaugeMetric(
53 'dev/net/speed',
54 description='Network interface speed in Mb.')
55_net_if_mtu_metric = ts_mon.GaugeMetric(
56 'dev/net/mtu',
57 description='Network interface MTU in B.')
58
59
60def collect_net_info():
61 """Collect network metrics."""
62 _collect_net_io_counters()
63 _collect_net_if_stats()
64
65
66_net_io_metrics = (
67 (_net_up_metric, 'bytes_sent'),
68 (_net_down_metric, 'bytes_recv'),
69 (_net_err_up_metric, 'errout'),
70 (_net_err_down_metric, 'errin'),
71 (_net_drop_up_metric, 'dropout'),
72 (_net_drop_down_metric, 'dropin'),
73)
74
75
76def _collect_net_io_counters():
77 """Collect metrics for network IO counters."""
78 nics = psutil.net_io_counters(pernic=True)
79 for nic, counters in nics.iteritems():
80 if _is_virtual_netif(nic):
81 continue
82 fields = {'interface': nic}
83 for metric, counter_name in _net_io_metrics:
84 try:
85 metric.set(getattr(counters, counter_name), fields=fields)
86 except ts_mon.MonitoringDecreasingValueError as ex:
87 # 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
93_net_if_metrics = (
94 (_net_if_isup_metric, 'isup'),
95 (_net_if_duplex_metric, 'duplex'),
96 (_net_if_speed_metric, 'speed'),
97 (_net_if_mtu_metric, 'mtu'),
98)
99
100
101def _collect_net_if_stats():
102 """Collect metrics for network interface stats."""
103 for nic, stats in psutil.net_if_stats().iteritems():
104 if _is_virtual_netif(nic):
105 continue
106 fields = {'interface': nic}
107 for metric, counter_name in _net_if_metrics:
108 metric.set(getattr(stats, counter_name), fields=fields)
109
110
111def _is_virtual_netif(nic):
112 """Return whether the network interface is virtual."""
113 # TODO(ayatane): Use a different way of identifying virtual interfaces
114 return nic.startswith('veth')