-
Notifications
You must be signed in to change notification settings - Fork 5
/
beanstalkd.py
49 lines (38 loc) · 1.36 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
import time
import beanstalkc
from checks import AgentCheck
class BeanstalkdCheck(AgentCheck):
def get_tube_stats(self):
stats = {}
for tube in self.client.tubes():
tube_stats = self.client.stats_tube(tube)
tube_stats = self.prefix_keys(tube, tube_stats)
stats.update(tube_stats)
return stats
def prefix_keys(self, tube_name, stats):
'''
Our plugin output must be a flat dict. Since each tube returns the
same key/value stats we must prefix key names with the tube name e.g.
the key total-jobs for tube 'email_signup' becomes email_signup-total-jobs.
'''
new_dict = {}
for k, v in stats.items():
key = '%s-%s' % (tube_name, k)
new_dict[key] = v
return new_dict
def check(self, instance):
# Connect to Beanstalkd
try:
host = instance.get('host')
port = instance.get('port')
self.client = beanstalkc.Connection(host=host, port=port)
except:
self.log.info("Can't connect to Beanstalkd")
return
# Get the stats
stats = self.client.stats()
stats.update(self.get_tube_stats())
for k, v in stats.items():
self.gauge('beanstalkd.%s' % k, v)
# Close the connection
self.client.close();