[redis] 提取 cluster 集群 node 角色 - 迁

通过 redis 的 cluster nodes 命令解析出当前的主从状态

redis-cli -c -p 7000 cluster nodes

 

运行:

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: richard
# @Date:   2017-08-17 16:40:03
# @Last Modified by:   richard
# @Last Modified time: 2017-11-14 12:11:06

import commands
import os
DEBUG=""
CMD=" -c -p 7000 cluster nodes"

def get_redis_cli_cmd():
    ret = commands.getstatusoutput("which redis-cli")
    if ret[0] == 0:
        return ret[1]+CMD
    else:
        return False


def merge(master,slave):
    for each_m in master:
        print "Master: {ip_port},{state},{slot},{id}".format(**each_m)

        for each_s in slave:
            if each_s['master_id'] == each_m['id']:
                print "\tSlave: {ip_port},{state},{id}".format(**each_s)

def parse_content(content):
    master_list,slave_list,fail_list=[],[],[]
    for each in content:
        # print each
        if each.find("master") > -1 and each.find("fail") < 0 :
            master={}
            try:
                split_value = each.split()
                master['id'] = split_value[0]
                master['ip_port'] = split_value[1]
                master['state'] = split_value[-2]      
                master['slot'] = split_value[-1]      
            except IndexError,e:
                print e         
            else:
                master_list.append(master)

        if each.find("slave") > -1 and each.find("fail") < 0:
            slave={}
            try:
                split_value = each.split()
                slave['id'] = split_value[0]
                slave['ip_port'] = split_value[1]
                slave['master_id'] = split_value[3]
                slave['state'] = split_value[-1]
            except IndexError,e:
                print e
            else:
                slave_list.append(slave)

        if each.find("fail") > -1:
            print "Fail",each
    merge(master_list,slave_list)

if __name__ == '__main__':
    if DEBUG:
        f = open('/tmp/1','r')
        GET_INFO=f.readlines()
        parse_content(GET_INFO)
    else:
        redis_cli = get_redis_cli_cmd()
        if redis_cli:
            cmd = commands.getstatusoutput(redis_cli)
            if cmd[0] == 0:
                GET_INFO=cmd[1].split('\n')
                parse_content(GET_INFO)
            else:
                print("run error")
        else:
            print("can't get redis-cli")

  

 

posted @ 2017-11-08 12:01  richardzgt  阅读(524)  评论(0编辑  收藏  举报