Skip to content

Instantly share code, notes, and snippets.

@dotSlashLu
Last active September 13, 2017 02:56
Show Gist options
  • Save dotSlashLu/806b111fd38029bfa21d4eaaac0c9a3e to your computer and use it in GitHub Desktop.
Save dotSlashLu/806b111fd38029bfa21d4eaaac0c9a3e to your computer and use it in GitHub Desktop.
waf redis llen monitor
import os
import sys
import time
abspath = os.path.split(os.path.realpath(__file__))[0]
sys.path.append("%s/lib" % abspath)
import redis
import gevent
from gevent import monkey; monkey.patch_all()
INTERVAL = 10
KEYS = ['QUEUE_0', 'QUEUE_1', 'QUEUE_2', 'QUEUE_3', 'QUEUE_4',
'HTTP_PACKETS', 'HTTP_PACKETS_QUEUE']
PORTS = [9001, 9002, 9003, 9004]
LOG_PATH = './log'
rs = {}
fhs = {}
for p in PORTS:
rs[p] = redis.StrictRedis(host='localhost', port=p)
fhs[p] = open(os.path.join(LOG_PATH, str(p)), 'a')
def check(p, r):
try:
jobs = [gevent.spawn(lambda x: (x, r.llen(x)), k) for k in KEYS]
gevent.joinall(jobs)
res = [job.value for job in jobs]
return (p, res)
except Exception as e:
return None
while True:
jobs = [gevent.spawn(check, p, rs[p]) for p in PORTS]
gevent.joinall(jobs)
res = [job.value for job in jobs]
for port_res in res:
if not port_res:
continue
(port, queues) = port_res
fhs[port].write('======== %s ========\n' % time.strftime('%m-%d %H:%M:%S'))
for q in queues:
fhs[port].write('%s: %s\n' % q)
fhs[port].write('\n\n')
fhs[port].flush()
gevent.sleep(INTERVAL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment