Skip to content
Newer
Older
100644 87 lines (67 sloc) 2.26 KB
3d0257d @atdt Initial commit
authored Aug 4, 2012
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 Python Gmond Module for UDP
5
6 :copyright: (c) 2012 Wikimedia Foundation
7 :author: Ori Livneh <ori@wikimedia.org>
8 :license: GPL
9 """
10 from __future__ import print_function
11
16bcffe @atdt Use worker thread to update stats
authored Aug 4, 2012
12 import logging
13 from ast import literal_eval
14 from threading import Timer
3d0257d @atdt Initial commit
authored Aug 4, 2012
15
16
16bcffe @atdt Use worker thread to update stats
authored Aug 5, 2012
17 UPDATE_INTERVAL = 5 # seconds
18
3d0257d @atdt Initial commit
authored Aug 4, 2012
19 defaults = {
4fafa64 @atdt Change default slope type to 'both'
authored Aug 4, 2012
20 "slope" : "both",
3d0257d @atdt Initial commit
authored Aug 4, 2012
21 "time_max" : 60,
397f4cc @atdt %d -> %u
authored Aug 5, 2012
22 "format" : "%u",
ca02d30 @atdt Clearly label UDP metrics
authored Aug 4, 2012
23 "value_type" : "uint",
24 "groups" : "network,udp",
3d0257d @atdt Initial commit
authored Aug 4, 2012
25 "units" : "packets"
26 }
27
28 udp_fields = {
ca02d30 @atdt Clearly label UDP metrics
authored Aug 5, 2012
29 "InDatagrams" : "UDP Packets Received",
30 "NoPorts" : "UDP Packets to Unknown Port Received",
31 "InErrors" : "UDP Packet Receive Errors",
32 "OutDatagrams" : "UDP Packets Sent"
3d0257d @atdt Initial commit
authored Aug 4, 2012
33 }
34
16bcffe @atdt Use worker thread to update stats
authored Aug 5, 2012
35 netstats = {}
36
3d0257d @atdt Initial commit
authored Aug 4, 2012
37
16bcffe @atdt Use worker thread to update stats
authored Aug 5, 2012
38 def get_netstats():
39 """Parse /proc/net/snmp"""
3d0257d @atdt Initial commit
authored Aug 4, 2012
40 with open('/proc/net/snmp', 'rt') as snmp:
41 raw = {}
42 for line in snmp:
43 key, vals = line.split(':', 1)
44 key = key.lower()
45 vals = vals.strip().split()
46 raw.setdefault(key, []).append(vals)
47 return dict((k, dict(zip(*vs))) for (k, vs) in raw.items())
48
49
16bcffe @atdt Use worker thread to update stats
authored Aug 5, 2012
50 def update_stats():
51 """Update netstats and schedule the next run"""
52 netstats.update(get_netstats())
53 logging.info("Updated: %s", netstats['udp'])
54 Timer(UPDATE_INTERVAL, update_stats).start()
55
56
3d0257d @atdt Initial commit
authored Aug 4, 2012
57 def metric_handler(name):
58 """Get value of particular metric; part of Gmond interface"""
16bcffe @atdt Use worker thread to update stats
authored Aug 5, 2012
59 logging.debug('metric_handler(): %s', name)
60 return literal_eval(netstats['udp'][name])
3d0257d @atdt Initial commit
authored Aug 4, 2012
61
62
63 def metric_init(params):
64 """Initialize; part of Gmond interface"""
65 descriptors = []
66 defaults['call_back'] = metric_handler
67 for name, description in udp_fields.items():
68 descriptor = dict(name=name, description=description)
69 descriptor.update(defaults)
70 descriptors.append(descriptor)
16bcffe @atdt Use worker thread to update stats
authored Aug 5, 2012
71 update_stats()
3d0257d @atdt Initial commit
authored Aug 4, 2012
72 return descriptors
73
16bcffe @atdt Use worker thread to update stats
authored Aug 5, 2012
74
3d0257d @atdt Initial commit
authored Aug 4, 2012
75 def metric_cleanup():
76 """Teardown; part of Gmond interface"""
77 pass
78
79
80 if __name__ == '__main__':
81 # When invoked as standalone script, run a self-test by querying each
82 # metric descriptor and printing it out.
16bcffe @atdt Use worker thread to update stats
authored Aug 5, 2012
83 logging.basicConfig(level=logging.DEBUG)
3d0257d @atdt Initial commit
authored Aug 4, 2012
84 for metric in metric_init({}):
85 value = metric['call_back'](metric['name'])
86 print(( "%s => " + metric['format'] ) % ( metric['name'], value ))
Something went wrong with that request. Please try again.