1+ """Module that defines all needed classes and functions for netdev collector."""
12import netifaces
23
34from p3exporter .collector import CollectorBase , CollectorConfig
45from prometheus_client .core import CounterMetricFamily , InfoMetricFamily
56
67
78class NetdevCollector (CollectorBase ):
9+ """Netdev collector class."""
810
911 def __init__ (self , config : CollectorConfig ):
1012 """Instanciate a NetdevCollector object."""
11-
1213 super (NetdevCollector , self ).__init__ (config )
1314
1415 self .whitelist = self .opts .pop ("whitelist" , [])
@@ -21,7 +22,6 @@ def collect(self):
2122
2223 Returns several info, couter and gauge metrics for interfaces.
2324 """
24-
2525 self .ifaces = netifaces .interfaces ()
2626 self .iface_stats = _get_iface_stats ()
2727
@@ -69,7 +69,6 @@ def _get_iface_stats():
6969 :return: Returns a dict of dicts. One dict for each interface and one key value pair for each interface statistic in the inner dict.
7070 :rtype: dict
7171 """
72-
7372 ifaces = {}
7473 f = open ("/proc/net/dev" )
7574 data = f .read ()
@@ -79,26 +78,26 @@ def _get_iface_stats():
7978 if len (i .strip ()) > 0 :
8079 x = i .split ()
8180 k = {
82- x [0 ][:len ( x [0 ])- 1 ]: {
83- "tx" : {
84- "bytes" : int (x [1 ]),
85- "packets" : int (x [2 ]),
86- "errs" : int (x [3 ]),
87- "drop" : int (x [4 ]),
88- "fifo" : int (x [5 ]),
89- "frame" : int (x [6 ]),
90- "compressed" : int (x [7 ]),
91- "multicast" : int (x [8 ])
81+ x [0 ][:len (x [0 ]) - 1 ]: {
82+ "tx" : {
83+ "bytes" : int (x [1 ]),
84+ "packets" : int (x [2 ]),
85+ "errs" : int (x [3 ]),
86+ "drop" : int (x [4 ]),
87+ "fifo" : int (x [5 ]),
88+ "frame" : int (x [6 ]),
89+ "compressed" : int (x [7 ]),
90+ "multicast" : int (x [8 ])
9291 },
93- "rx" : {
94- "bytes" : int (x [9 ]),
95- "packets" : int (x [10 ]),
96- "errs" : int (x [11 ]),
97- "drop" : int (x [12 ]),
98- "fifo" : int (x [13 ]),
99- "frame" : int (x [14 ]),
100- "compressed" : int (x [15 ]),
101- "multicast" : int (x [16 ])
92+ "rx" : {
93+ "bytes" : int (x [9 ]),
94+ "packets" : int (x [10 ]),
95+ "errs" : int (x [11 ]),
96+ "drop" : int (x [12 ]),
97+ "fifo" : int (x [13 ]),
98+ "frame" : int (x [14 ]),
99+ "compressed" : int (x [15 ]),
100+ "multicast" : int (x [16 ])
102101 }
103102 }
104103 }
0 commit comments