-
Notifications
You must be signed in to change notification settings - Fork 5
/
beanstalkd.py
64 lines (52 loc) · 2.01 KB
/
beanstalkd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# Plugin to collectd statistics from beanstalkd
#
import collectd
# beanstalkc needs yaml to work correctly.
# This import ensures a 'fail early' behaviour.
import yaml
from beanstalkc import Connection
class Beanstalk(object):
def __init__(self):
self.plugin_name = "beanstalkd"
self.host = '127.0.0.1'
self.port = 11300
self.tubes_prefix = ['default']
def submit(self, type, instance, value, tube=None):
if tube:
plugin_instance = '%s-%s' % (self.port, tube)
else:
plugin_instance = str(self.port)
v = collectd.Values()
v.plugin = self.plugin_name
v.plugin_instance = plugin_instance
v.type = type
v.type_instance = instance
v.values = [value, ]
v.dispatch()
def do_server_status(self):
conn = Connection(self.host, self.port)
srv_stats = conn.stats()
for cmd in ('put', 'reserve-with-timeout', 'delete'):
self.submit('counter', cmd, srv_stats['cmd-%s' % cmd])
self.submit('counter', 'total_jobs', srv_stats['total-jobs'])
for tube in conn.tubes():
for prefix in self.tubes_prefix:
if tube.startswith(prefix):
tube_stats = conn.stats_tube(tube)
self.submit('records', 'current_ready', tube_stats['current-jobs-ready'], tube)
self.submit('counter', 'total_jobs', tube_stats['total-jobs'], tube)
conn.close()
def config(self, obj):
for node in obj.children:
if node.key == 'Port':
self.port = int(node.values[0])
elif node.key == 'Host':
self.host = node.values[0]
elif node.key == 'tubes_prefix':
self.tubes_prefix = node.values
else:
collectd.warning("beanstalkd plugin: Unkown configuration key %s" % node.key)
beanstalkd = Beanstalk()
collectd.register_read(beanstalkd.do_server_status)
collectd.register_config(beanstalkd.config)