#!/usr/bin/python
import sys
import getopt
import json
import os
import commands
import time
import re

def get_arginfo(argv):
    try:
        options,args = getopt.getopt(argv , "hf:u:p:" , ["help" , "ip="  , "user=" , "passwd="])
    except getopt.GetoptError:
        sys.exit()
    arginfo = {}
    for option,value in options:
        value_1 = format(value)
        option_1 = format(option)
        arginfo[option_1] = value_1
    return arginfo

def getcluster_redisinfo(ip,port,passwd=""):
    ip = str(ip)
    p = str(port)
    passwd = str(passwd)
    cluster_info = {}
    cluster_info["cpname"] = "redis_cluster"
    ip_port = ip + ":" + p
    if ( passwd == "" ):
        conn_str = ""
    else :
        conn_str = " -a " + passwd

    runid_cmd = "echo \"info\"  | redis-cli -h " + ip +  "  -p " +  p + "  " + conn_str +  " | grep run_id | cut -d : -f 2"
    roles_cmd = "echo \"info\"  | redis-cli  -h " + ip + " -p " + p + " " + conn_str +  " | grep role | cut -d : -f 2 | sed 's/[[:space:]]//g'"
    redis_mode_cmd = "echo \"info\"  | redis-cli -h " + ip + " -p " + p +  "  " + conn_str +  " | grep redis_mode | cut -d : -f 2 | sed 's/[[:space:]]//g'"
    used_cpu_user = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"used_cpu_user\" | cut -d : -f 2"
    used_memory_human = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"used_memory_human\" | cut -d : -f 2"
    connected_clients = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"connected_clients\" | cut -d : -f 2"
    blocked_clients = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"blocked_clients\" | cut -d : -f 2"
    qps = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"instantaneous_ops_per_sec\" | cut -d : -f 2"
    repl_backlog_size = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"repl_backlog_size\" | cut -d : -f 2"
    #connected_clients = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"connected_clients\" | cut -d : -f 2"
    #used_memory_rss = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"used_memory_rss\" | cut -d : -f 2"
    #used_memory = "echo \" info\" | redis-cli -h " + ip + " -p " + p + " " + conn_str + " | grep -w \"used_memory\" | cut -d : -f 2"
    #maxclients = "echo \" CONFIG GET maxclients \" | redis-cli -h 192.168.131.51  -p 6414 | sed -n 2p"




    (status, result) = commands.getstatusoutput(runid_cmd)
    runid = result

    (status, result) = commands.getstatusoutput(roles_cmd)
    roles = result
    (status, result) = commands.getstatusoutput(redis_mode_cmd)
    redis_mode = result

    pid_cmd = "echo \"info\"  | redis-cli -h " + ip + " -p " + p +  "  " + conn_str + " | grep process_id | cut -d : -f 2 | sed 's/[[:space:]]//g'"
    (status, result) = commands.getstatusoutput(pid_cmd)
    pid = result


    cluster_info["FLAGPID"] = pid


    if ( runid != " " ):

        if ( roles == "master" ):

            replication_cmd = "echo \"info\"  | redis-cli -h  " + ip + " -p " + p +  "  " + conn_str + " | grep connected_slaves | cut -d : -f 2 | sed 's/[[:space:]]//g'"

            master_cmd =  "echo \"info replication \"  | redis-cli -h " + ip + " -p " + p +  "  " + conn_str + " | grep master_repl_offset | cut -d : -f 2 | sed 's/[[:space:]]//g'"

            #slave_cmd =   "echo \" info replication \" | redis-cli -h " + ip + " -p  " + p +  "  " + conn_str + " | grep -v connected_slaves |  grep slave | cut -d : -f 2 | cut -d \",\" -f 4 | cut -d \"=\" -f 2"
            slave_cmd =  "echo \" info replication \" | redis-cli -h " + ip + " -p  " + p +  "  " + conn_str + " | grep -v connected_slaves |  grep slave |  grep -v slave_expires_tracked_keys"



            (status, result) = commands.getstatusoutput(replication_cmd)

            replication_cnt = int(result)
            if ( replication_cnt == 0 ) :
                cluster_info["CLUSTER_HEALTH"] = "yellow"
            else :
                cluster_info["CLUSTER_HEALTH"] = "green"

            (status, result) = commands.getstatusoutput(used_cpu_user)
            cluster_info["used_cpu"] = result.strip("\r")

            (status, result) = commands.getstatusoutput(used_memory_human)
            cluster_info["used_memory"] = result.strip("\r")

            (status, result) = commands.getstatusoutput(connected_clients)
            connected_clients = int(result.strip("\r"))

            (status, result) = commands.getstatusoutput(blocked_clients)
            cluster_info["blocked_clients"] = int(result.strip("\r"))

            (status, result) = commands.getstatusoutput(repl_backlog_size)
            cluster_info["repl_backlog_size"] = result.strip("\r")

            # (status, result) = commands.getstatusoutput(used_memory_rss)
            # cluster_info["used_memory_rss"] = result.strip("\r")

            #(status, result) = commands.getstatusoutput(maxclients)
            #maxclients = int(result)
            cluster_info["connected_clients"] = connected_clients

            # (status, result) = commands.getstatusoutput(used_memory)
            # cluster_info["used_memory"] = result.strip("\r")

            (status, result) = commands.getstatusoutput(qps)
            cluster_info["qps"] = result.strip("\r")



            (status, result) = commands.getstatusoutput(master_cmd)
            master_offet = int(result)

            (status, result) = commands.getstatusoutput(slave_cmd)
            slave_list = result.split("\n")
            slave_offet_1 = []
            for  slave_off in slave_list :
                slave = int(slave_off.split(",")[3].split("=")[1])
                slave_offet_1.append(slave)
            slave_offet_1.sort()
            #print "%s" %slave_offet_1
            slave_offet = slave_offet_1[0]

            cluster_info["second_behind_master"] = master_offet - slave_offet

            if ( replication_cnt < 1 ):
                cluster_info["replicate_status"] = "red"
            else :
                cluster_info["replicate_status"] = "green"
        else :
            cluster_info["ignore"] = "1"
    else :
        cluster_info["error"] = "Unable connect to the redis cluster。"

    etime_cmd = "ps -eo pid,etime| grep " + pid + "| awk '{if($1=='" +pid + "') $1=\"\";print $0}'"
    starttime_cmd = "ps -eo pid,lstart| grep " + pid + "| awk '{if($1=='" + pid + "') $1=\"\";print $0}'"
    (status, result) = commands.getstatusoutput(etime_cmd)
    cluster_info["etime"] = result
    (status, result) = commands.getstatusoutput(starttime_cmd)
    cluster_info["starttime"] = result
    jsonStr = json.dumps(cluster_info)

    if ( len(jsonStr) >= 0 ):
        print "%s" % jsonStr
        return 1
    else:
        return 0

if __name__ == '__main__':
    arginfo = get_arginfo(sys.argv[1:])
    # hostinfo = arginfo["-I"]
    local_ip_cmd = "ip addr | grep 'state UP' -A2 | head -n3|tail -n1 | awk '{print $2}' | cut -f1 -d '/'"
    #local_ip_cmd = "ifconfig 2>/dev/null|sed -n 2p|awk  '{ print $2 }'|awk -F : '{ print $2 }'"
    (status, result) = commands.getstatusoutput(local_ip_cmd)
    local_ip = result
    port_list = []
    ip = arginfo["-f"].split(",")

    for ip_info in ip:
        remode_ip = ip_info.split(":")[0]
        remode_port = ip_info.split(":")[1]
        if (local_ip == remode_ip):
            port_list.append(remode_port)
    for key in arginfo:
        #print "%s" %key
        if ( key == "-p" ):
            passwd = arginfo["-p"]
        else:
            passwd = ""

    for port in port_list:
        # print "%s" %port
        # print "%s" %local_ip
        flag = getcluster_redisinfo(local_ip, port,passwd)
        if (flag == 1):
            break





发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code