#!/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_mongodbinfo(ip,port,username,passwd):
    ip = str(ip)
    p = str(port)

    if ( passwd != "" and username != "" ):
        conn_str = "  -u   " + username + "  -p  " + passwd

    else :
        conn_str = ""
    cluster_info = {}
    cluster_info["cpname"] = "mongo_cluster"
    ip_port = ip + ":" + p
    cluster_cmd ="echo \"rs.status()\"| mongo " +  ip_port + conn_str + " | grep -i set | cut -d : -f 2 | cut -d , -f 1 | sed 's/\"//g'"
    #print "%s" %cluster_cmd

    second_behand_master_cmd="echo \"rs.printSlaveReplicationInfo()\"| mongo " +  ip_port + conn_str +  " | grep -i secs| awk '{print $1}'"

    health_cmd="echo \"rs.status()\"| mongo " + ip_port + conn_str +  " | grep -i health | cut -d : -f 2 | sed 's/,//g'"

    roles_cmd="echo \"rs.status()\"| mongo " + ip_port + conn_str +  "  | grep -i stateStr | cut -d : -f 2 | sed 's/,//g' | sed 's/\"//g' | sed 's/[[:space:]]//g'"

    mem_cmd = "echo \"db.serverStatus().mem.resident\"| mongo " + ip_port + conn_str + "  | grep -v MongoDB | grep -v  connecting | grep -v bye"
    con_cmd = "echo \"db.serverStatus().connections.available\"| mongo " + ip_port + conn_str + "  | grep -v MongoDB | grep -v  connecting | grep -v bye"
    tps_cmd = "echo \"db.serverStatus().opcountersRepl.insert  + db.serverStatus().opcountersRepl.update + db.serverStatus().opcountersRepl.delete\"| mongo " + ip_port + conn_str + "  | grep -v MongoDB | grep -v  connecting | grep -v bye"
    qps_cmd = "echo \"db.serverStatus().opcountersRepl.query + db.serverStatus().opcountersRepl.getmore\"| mongo " + ip_port + conn_str + "  | grep -v MongoDB | grep -v  connecting | grep -v bye"

    queue_cmd = "echo \"db.serverStatus().globalLock.currentQueue.total\"| mongo " + ip_port + conn_str + "  | grep -v MongoDB | grep -v  connecting | grep -v bye"
    aliveclient_cmd = "echo \"db.serverStatus().globalLock.activeClients.total\"| mongo " + ip_port + conn_str + "  | grep -v MongoDB | grep -v  connecting | grep -v bye"
    (status, result) = commands.getstatusoutput(mem_cmd)
    cluster_info["mongo_mem"] = result

    (status, result) = commands.getstatusoutput(con_cmd)
    cluster_info["mongo_avail_conn"] = result

    (status, result) = commands.getstatusoutput(tps_cmd)
    #print "%s" %result
    cluster_info["mongo_tps"] = result

    (status, result) = commands.getstatusoutput(qps_cmd)
    #print "%s" %result
    cluster_info["mongo_qps"] = result

    (status, result) = commands.getstatusoutput(queue_cmd)
    #print "%s" %result
    cluster_info["mongo_queue"] = result


    (status, result) = commands.getstatusoutput(aliveclient_cmd)
    cluster_info["mongo_activeClient"] = result

    #get cluster name
    (status, result) = commands.getstatusoutput(cluster_cmd)

    cluster_name = str(result)


    cluster_info["cluster_name"] = cluster_name.strip()


    #get health_cmd
    (status, result) = commands.getstatusoutput(health_cmd)
    health = result.split("\n")
    health_len = len(health)
    i = 0
    cnt = 0
    while ( i < health_len ):
        if ( int(health[i].strip()) == 0 ):
            cnt = cnt + 1
        i = i + 1
    if ( cnt == 0 ):
        cluster_info["CLUSTER_HEALTH"] = "green"
    elif ( cnt < health_len - 2 ):
        cluster_info["CLUSTER_HEALTH"] = "yellow"
    else :
        cluster_info["CLUSTER_HEALTH"] = "red"

    #print "%s" %health
    #get roles_cmd
    (status, result) = commands.getstatusoutput(roles_cmd)
    roles = result.split("\n")
    roles_len = len(roles)
    normal = ["SECONDARY","PRIMARY","ARBITER"]
    j = 0
    role_cnt = 0
    while ( j < roles_len ):
        if ( str(roles[j]) not in normal ):
            role_cnt = role_cnt + 1
        j = j + 1

    if ( role_cnt != 0 ):
        cluster_info["replicate_status"] = "red"
    else :
        cluster_info["replicate_status"] = "green"

    #get second_behand_master_cmd
    (status, result) = commands.getstatusoutput(second_behand_master_cmd)
    cluster_info["second_behind_master"] = result

    #get pid
    pid_cmd="echo \"db.serverStatus()\" | mongo  " + ip_port + " | grep -i  pid | cut -d : -f 2 | cut -d \"(\" -f 2 | cut -d \")\" -f 1"
    (status, result) = commands.getstatusoutput(pid_cmd)
    pid = result
    cluster_info["FLAGPID"] = pid
    #print "%s" %result

    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) > 1 ):
        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 key in arginfo:
        if ( key == "-p" ):
            passwd = arginfo["-p"]
            break
        else :
            passwd = ""

    for key in arginfo:
        if ( key == "-u" ):
            username = arginfo["-u"]
            break
        else :
            username = ""

    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 port in port_list:
        #print "%s" %port
        #print "%s" %local_ip
        flag = getcluster_mongodbinfo(local_ip,port,username,passwd)
        if ( flag == 1 ):
            break




发表回复

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

Captcha Code