redis慢查询监控

写了个慢查询监控,配合zabbix即可监控慢查询。代码如下:

# _*_ coding: utf-8 _*_
#By zone
import redis,configparser,json,time

with open('redis.info', 'r') as f:
    redisInfo = dict(line.strip().split(':') for line in f if line)

def check_json_value(dic_list,k):
    if isinstance(dic_list,list):
         for i in range(len(dic_list)):
            t1=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(dic_list[i]['start_time']))
            t2=bytes(dic_list[i]['duration']/float(1000))
            dic_list[i]['start_time']=t1
            dic_list[i]['duration']=t2+"ms"
            print(dic_list[i])

for k,v in redisInfo.items():
    try:
        r=redis.StrictRedis(host='127.0.0.1',port=k,password=v,decode_responses=True)
    except Exception,e:
        print e.message    
    #SLOWLOG_LEN=r.slowlog_len()
    SLOWLOG_GET=r.slowlog_get()
    print(k+':')
    SLOWLOG=check_json_value(SLOWLOG_GET,'start_time')
    SLOWLOG_RESET=r.slowlog_reset()  

说明:redis的账号密码存放在文件redis.info中,格式为pord:password,可换行填写多个实例。

port1:password1
port2:password2
port2:password2
......

执行效果:

duration:执行耗时,单位毫秒
start_time:慢查询发生时间
command:慢查询命令
id:慢查询编号

发表评论

电子邮件地址不会被公开。