This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisqueues.py
57 lines (45 loc) · 1.64 KB
/
redisqueues.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
#!/usr/bin/env python
import optparse
import os
import sys
import time
try:
import simplejson as json
except ImportError:
import json
def main():
try:
import redis
except ImportError, ex:
sys.stderr.write('redisqueues.py requires the `redis` module\n')
return 1
opts = optparse.OptionParser()
opts.add_option('-p', '--prefix', dest='prefix',
help='Specify a prefix for the event labels (e.g. foo.system.load)')
opts.add_option('-s', '--suffix', dest='suffix',
help='Specify a suffix for the event labels (e.g. system.load.1min.hostname')
opts.add_option('--db', default=0, dest='db',
help='Specify a redis DB to query')
opts.add_option('--lists', dest='lists',
help='Specify a comma-separated list of redis keys to get the length of')
options, args = opts.parse_args()
prefix = ''
suffix = ''
if options.prefix:
prefix = '%s.' % options.prefix
if options.suffix:
suffix = '.%s' % options.suffix
if not options.lists:
sys.stderr.write('redisqueues.py requires the `--lists` parameter to properly report list lengths\n')
return 1
r = redis.Redis(host='localhost', port=6379, db=options.db)
now = time.time()
for queue in options.lists.split(','):
try:
value = r.llen(queue)
print '%(prefix)ssystem.redis.%(queue)s.length%(suffix)s %(value)s %(now)s' % locals()
except Exception, ex:
sys.stderr.write('Exception when calling `r.llen` in redisqueues.py: %s\n' % ex)
return 0
if __name__ == '__main__':
sys.exit(main())