#!/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 ( i1=='" +pid + "') 1=\"\";print0}'"
    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 '{print2}' | cut -f1 -d '/'"
    #local_ip_cmd = "ifconfig 2>/dev/null|sed -n 2p|awk  '{ print 2 }'|awk -F : '{ print2 }'"
    (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