-
Notifications
You must be signed in to change notification settings - Fork 1
/
redislen.py
executable file
·66 lines (51 loc) · 1.58 KB
/
redislen.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
65
66
#!/usr/bin/env python
import redis
import sys
import os
def check_redis_length(queue_name, port=6380):
redis_client = redis.Redis(port=port)
return redis_client.llen(queue_name)
class RedisLengthConfig:
def __init__(self):
self.queue_name = "logstash"
self.port = 6380
config = RedisLengthConfig()
try:
import collectd
def logger(t, msg):
if t == 'err':
collectd.error('%s: %s' % (NAME, msg))
elif t == 'warn':
collectd.warning('%s: %s' % (NAME, msg))
elif t == 'info':
collectd.info('%s: %s' % (NAME, msg))
else:
collectd.notice('%s: %s' % (NAME, msg))
NAME = "redislen"
def config_callback(conf):
for node in conf.children:
if node.key == 'QueueName':
config.queue_name = node.values[0]
elif node.key == 'Port':
config.port = int(node.values[0])
else:
logger('info', "unknown config key in puppet module: %s" % node.key)
def read_callback():
queue_len = check_redis_length(config.queue_name,
port=config.port)
val = collectd.Values(plugin=NAME, type="gauge")
val.plugin_instance = config.queue_name
val.values = [ float(queue_len) ]
val.type = "gauge"
val.dispatch()
collectd.register_config(config_callback)
collectd.register_read(read_callback)
except ImportError:
## we're not running inside collectd
## it's ok
pass
if __name__ == "__main__":
port = int(os.getenv("REDIS_PORT", 6380))
queue_name = sys.argv[1]
queue_len = check_redis_length(queue_name, port=port)
print "length of %s: %d" % (queue_name, queue_len)