#!/usr/bin/python
#-*- coding:utf-8 -*-
# by MO
#DATA 2018-08-02

import redis
import datetime


RedisHostMessage={
    'test':[{'Host':'***',
          'Port':6379,
          'Pwd':None}],

}


#查询slowlog
def SlowlogGet(*args):
    slowlog = connect.slowlog_get()
    for msg in slowlog:
        print(Host)
        for key in msg:
            if key == 'start_time':
                print(key, ':', datetime.datetime.fromtimestamp(msg[key]))        #进行时间转换,unix时间转换
                continue
            print(key,':',msg[key])
        print('----------ID-----------')
    # print(slowlog)

#查询redis配置
def ConfigGet(*args):
    config = connect.config_get(pattern='slowlog*')
    for key in config:
        print(key,config[key])

#修改redis配置
def ConfigSet(*args):
    # connect.config_set('slowlog-log-slower-than',50000)       #配置slowlog阈值时间50ms
    connect.slowlog_reset()                                     #重置slowlog list





for key in RedisHostMessage:
    for message in RedisHostMessage[key]:
        Host=message['Host']
        Port=message['Port']
        Pwd=message['Pwd']

        connect = redis.Redis(host=Host, port=Port, password=Pwd)
        # print(Host,Port,Pwd)
        # SlowlogGet(connect)
        ConfigSet(connect)
        SlowlogGet(connect)