Debian Codegraph @

gmond/python_modules/network/multi_interface.py

raw
1 # This module allows you to collect per network interface network stats. Out
2 # of the box Ganglia provides only aggregate network metrics. This module will
3 # give you rx (receive) and tx (transmit) stats for every interface it encounters
4 # e.g.
5
6 # value for rx_bytes_lo is 21847.3723
7 # value for rx_pkts_lo is 17.5771
8 # value for rx_errs_lo is 0.0000
9 # value for rx_drops_lo is 0.0000
10 # value for tx_bytes_lo is 21847.3723
11 # value for tx_pkts_lo is 17.5771
12 # value for tx_errs_lo is 0.0000
13 # value for tx_drops_lo is 0.0000
14 # value for rx_bytes_eth0 is 0.0000
15 # value for rx_pkts_eth0 is 0.0000
16 # value for rx_errs_eth0 is 0.0000
17 # value for rx_drops_eth0 is 0.0000
18 # value for tx_bytes_eth0 is 0.0000
19 # value for tx_pkts_eth0 is 0.0000
20 # value for tx_errs_eth0 is 0.0000
21 # value for tx_drops_eth0 is 0.0000
22 # value for rx_bytes_eth1 is 0.0000
23 # value for rx_pkts_eth1 is 0.0000
24 # value for rx_errs_eth1 is 0.0000
25 # value for rx_drops_eth1 is 0.0000
26 # value for tx_bytes_eth1 is 0.0000
27 # value for tx_pkts_eth1 is 0.0000
28 # value for tx_errs_eth1 is 0.0000
29 # value for tx_drops_eth1 is 0.0000
30
31 # You can run the multi_interface.py script by hand to see all of the metrics.
32
33 # In addition this script can be used to override "default" bytes_in, bytes_out, pkts_in and pkts_out
34 # metrics. To do that you will need to
35
36 # * set send_aggregate_bytes_packets to True in multi_interface.pyconf
37 # * Uncomment bytes_in, bytes_out metrics to be sent in in multi_interface.pyconf
38 # * Comment out those same metrics you uncommented in step above in gmond.conf so they don't override
39 # each other
40
41
42 import re
43 import time
44 import sys
45 import os
46 import copy
47
48 PARAMS = {}
49
50 METRICS = {
51 'time': 0,
52 'data': {}
53 }
54 LAST_METRICS = copy.deepcopy(METRICS)
55 METRICS_CACHE_MAX = 5
56
57 INTERFACES = []
58 descriptors = []
59
60 stats_tab = {
61 "rx_bytes": 0,
62 "rx_pkts": 1,
63 "rx_errs": 2,
64 "rx_drops": 3,
65 "tx_bytes": 8,
66 "tx_pkts": 9,
67 "tx_errs": 10,
68 "tx_drops": 11,
69 }
70
71 # Where to get the stats from
72 net_stats_file = "/proc/net/dev"
73
74
75 def create_desc(skel, prop):
76 d = skel.copy()
77 for k, v in prop.iteritems():
78 d[k] = v
79 return d
80
81
82 def metric_init(params):
83 global descriptors
84 global INTERFACES
85
86 # INTERFACES = params.get('interfaces')
87 watch_interfaces = params.get('interfaces')
88 excluded_interfaces = params.get('excluded_interfaces')
89 get_interfaces(watch_interfaces,excluded_interfaces)
90
91 # print INTERFACES
92 time_max = 60
93
94 Desc_Skel = {
95 'name' : 'XXX',
96 'call_back' : get_delta,
97 'time_max' : 60,
98 'value_type' : 'float',
99 'format' : '%.0f',
100 'units' : '/s',
101 'slope' : 'both', # zero|positive|negative|both
102 'description' : 'XXX',
103 'groups' : 'network',
104 }
105
106 for dev in INTERFACES:
107 descriptors.append(create_desc(Desc_Skel, {
108 "name" : "rx_bytes_" + dev,
109 "units" : "bytes/sec",
110 "description" : "received bytes per sec",
111 }))
112 descriptors.append(create_desc(Desc_Skel, {
113 "name" : "rx_pkts_" + dev,
114 "units" : "pkts/sec",
115 "description" : "received packets per sec",
116 }))
117 descriptors.append(create_desc(Desc_Skel, {
118 "name" : "rx_errs_" + dev,
119 "units" : "pkts/sec",
120 "description" : "received error packets per sec",
121 }))
122 descriptors.append(create_desc(Desc_Skel, {
123 "name" : "rx_drops_" + dev,
124 "units" : "pkts/sec",
125 "description" : "receive packets dropped per sec",
126 }))
127
128 descriptors.append(create_desc(Desc_Skel, {
129 "name" : "tx_bytes_" + dev,
130 "units" : "bytes/sec",
131 "description" : "transmitted bytes per sec",
132 }))
133 descriptors.append(create_desc(Desc_Skel, {
134 "name" : "tx_pkts_" + dev,
135 "units" : "pkts/sec",
136 "description" : "transmitted packets per sec",
137 }))
138 descriptors.append(create_desc(Desc_Skel, {
139 "name" : "tx_errs_" + dev,
140 "units" : "pkts/sec",
141 "description" : "transmitted error packets per sec",
142 }))
143 descriptors.append(create_desc(Desc_Skel, {
144 "name" : "tx_drops_" + dev,
145 "units" : "pkts/sec",
146 "description" : "transmitted dropped packets per sec",
147 }))
148
149 if params['send_aggregate_bytes_packets']:
150 descriptors.append(create_desc(Desc_Skel, {
151 "name" : "pkts_in",
152 "units" : "pkts/sec",
153 "call_back" : get_aggregates,
154 "description" : "Packets Received",
155 }))
156 descriptors.append(create_desc(Desc_Skel, {
157 "name" : "pkts_out",
158 "units" : "pkts/sec",
159 "call_back" : get_aggregates,
160 "description" : "Packets Sent",
161 }))
162 descriptors.append(create_desc(Desc_Skel, {
163 "name" : "bytes_in",
164 "units" : "bytes/sec",
165 "call_back" : get_aggregates,
166 "description" : "Bytes Received",
167 }))
168 descriptors.append(create_desc(Desc_Skel, {
169 "name" : "bytes_out",
170 "units" : "bytes/sec",
171 "call_back" : get_aggregates,
172 "description" : "Bytes Sent",
173 }))
174
175 return descriptors
176
177
178 def metric_cleanup():
179 '''Clean up the metric module.'''
180 pass
181
182
183 ###################################################################################
184 # Build a list of interfaces
185 ###################################################################################
186
187
188 def get_interfaces(watch_interfaces, excluded_interfaces):
189 global INTERFACES
190 if_excluded = 0
191
192 # check if particular interfaces have been specifieid. Watch only those
193 if watch_interfaces != "":
194 INTERFACES = watch_interfaces.split(" ")
195 else:
196 if excluded_interfaces != "":
197 excluded_if_list = excluded_interfaces.split(" ")
198 f = open(net_stats_file, "r")
199 for line in f:
200 # Find only lines with :
201 if re.search(":", line):
202 a = line.split(":")
203 dev_name = a[0].lstrip()
204
205 # Determine if interface is excluded by name or regex
206 for ex in excluded_if_list:
207 if re.match(ex, dev_name):
208 if_excluded = 1
209
210 if not if_excluded:
211 INTERFACES.append(dev_name)
212 if_excluded = 0
213 return 0
214
215
216 ###################################################################################
217 # Returns aggregate values for pkts and bytes sent and received. It should be
218 # used to override the default Ganglia mod_net module. It will generate bytes_in
219 # bytes_out, pkts_in and pkts_out.
220 ###################################################################################
221 def get_aggregates(name):
222
223 # get metrics
224 [curr_metrics, last_metrics] = get_metrics()
225
226 # Determine the index of metric we need
227 if name == "bytes_in":
228 index = stats_tab["rx_bytes"]
229 elif name == "bytes_out":
230 index = stats_tab["tx_bytes"]
231 elif name == "pkts_out":
232 index = stats_tab["tx_pkts"]
233 elif name == "pkts_in":
234 index = stats_tab["rx_pkts"]
235 else:
236 return 0
237
238 sum = 0
239
240 # Loop through the list of interfaces we care for
241 for iface in INTERFACES:
242 try:
243 delta = (float(curr_metrics['data'][iface][index]) - float(last_metrics['data'][iface][index])) / (curr_metrics['time'] - last_metrics['time'])
244 if delta < 0:
245 print name + " is less 0"
246 delta = 0
247 except KeyError:
248 delta = 0.0
249
250 sum += delta
251
252 return sum
253
254
255 def get_metrics():
256 """Return all metrics"""
257
258 global METRICS, LAST_METRICS
259
260 if (time.time() - METRICS['time']) > METRICS_CACHE_MAX:
261
262 try:
263 file = open(net_stats_file, 'r')
264
265 except IOError:
266 return 0
267
268 # convert to dict
269 metrics = {}
270 for line in file:
271 if re.search(":", line):
272 a = line.split(":")
273 dev_name = a[0].lstrip()
274 metrics[dev_name] = re.split("\s+", a[1].lstrip())
275
276 # update cache
277 LAST_METRICS = copy.deepcopy(METRICS)
278 METRICS = {
279 'time': time.time(),
280 'data': metrics
281 }
282
283 return [METRICS, LAST_METRICS]
284
285
286 def get_delta(name):
287 """Return change over time for the requested metric"""
288
289 # get metrics
290 [curr_metrics, last_metrics] = get_metrics()
291
292 # Names will be in the format of tx/rx underscore metric_name underscore interface
293 # e.g. tx_bytes_eth0
294 parts = name.split("_")
295 iface = parts[2]
296 name = parts[0] + "_" + parts[1]
297
298 index = stats_tab[name]
299
300 try:
301 delta = (float(curr_metrics['data'][iface][index]) - float(last_metrics['data'][iface][index])) / (curr_metrics['time'] - last_metrics['time'])
302 if delta < 0:
303 print name + " is less 0"
304 delta = 0
305 except KeyError:
306 delta = 0.0
307
308 return delta
309
310
311 if __name__ == '__main__':
312 try:
313 params = {
314 "interfaces": "",
315 "excluded_interfaces": "dummy",
316 "send_aggregate_bytes_packets": True,
317 "debug": True,
318 }
319 metric_init(params)
320 while True:
321 for d in descriptors:
322 v = d['call_back'](d['name'])
323 print ('value for %s is ' + d['format']) % (d['name'], v)
324 time.sleep(5)
325 except StandardError:
326 print sys.exc_info()[0]
327 os._exit(1)