5fc7c54aefd867265.jpg_fo742.png

#! /bin/bash
export LANG=en_US.UTF-8

pids=`ps aux|grep mongod|grep -v java |grep -v solr|grep -v grep|grep -v ps | awk '{print $2}'`
discover_mongo_json=""
cpname="mongo_cluster"

function getpasswd()
{
if [ ! -f "/data/monitor/mongopwd" ];then
    conn_str=`echo ""`
    echo $conn_str
else 
  if [ -s /data/monitor/mongopwd ];then 
    conn_str=`echo ""`
    while read line 
    do
    port_cmd=`echo $line | cut -d "|" -f 2` 
    #user_cmd=`echo $line | cut -d : -f 2`  
    #passwd_cmd=`echo $line | cut -d : -f 3`
    #database_cmd=`echo $line | cut -d : -f 4`

    if [[ $port_cmd == $1 ]];then 
       #conn_str=$user_cmd:$passwd_cmd:$database_cmd
       conn_str=$line

    fi
    done </data/monitor/mongopwd

  else 
    conn_str=`echo ""`
  fi
  echo $conn_str
fi
}


for pid in $pids
do
    ports=`netstat -lntp|grep $pid|awk '{print $4}'|awk -F ':' '{print $NF}'`
    #psinfo=`lsof -p $pid|grep txt|head -n 1 |grep -v ps|grep -v grep`
    #config_path=`echo $psinfo|awk -F ' ' '{print $9}'`
    p_number=`echo $ports|awk '{print NF}'`

    if [ "$p_number" -gt 1 ];then
        ports=$ports|awk '{if($1>$2){print $2}else{print $1}}'
    else
        ports=$ports
    fi

    if [ "$ports" != "" ];then
        for p in $ports
        do

            #echo $p
            #echo $p
            conn_str=`echo $(getpasswd $p)`

            #echo $conn_str

            if [[ $conn_str != "" ]];then
                username=`echo $conn_str | cut -d "|" -f 3`
                passwd=`echo $conn_str | cut -d "|" -f 4`

                dbname=`echo $conn_str | cut -d "|" -f 5`
            else 
                username=""
                passwd=""
                dbname=""
            fi  


            #cluster_str="echo \"rs.status()\"| mongo -host localhost  --host  ${liu} | grep -i set | cut -d : -f 2 | cut -d \",\" -f 1"

            #cluserName=`echo "rs.status()"| mongo  --host localhost --port   $p -u'Monitor' -p'm0N!7o@' --authenticationDatabase admin --authenticationDatabase admin   | grep -i set | cut -d : -f 2 | cut -d "," -f 1`

            if [[ $conn_str != "" ]];then
                 cluserName=`echo "rs.status()"| mongo  --host localhost --port  $p -u$username -p$passwd  --authenticationDatabase $dbname  | grep -i set | cut -d : -f 2 | cut -d "," -f 1`

            else
                 cluserName=`echo "rs.status()"| mongo  --host localhost --port  $p | grep -i set | cut -d : -f 2 | cut -d "," -f 1`  

            fi
            #echo $cluserName



            node_list_x=""

            if [[ $conn_str != "" ]];then
                echo "rs.status()"| mongo  --host localhost --port  $p  -u$username -p$passwd  --authenticationDatabase $dbname   | grep -i name >/tmp/tmp.txt
            else
                echo "rs.status()"| mongo  --host localhost --port  $p    | grep -i name >/tmp/tmp.txt    
            fi


            while read line 
            do 
               node_ip=`echo $line | cut -d : -f 2 | sed 's/\"//g' | sed 's/,//g' | sed 's/^[ \t]//g'`
               #node_ip=`echo $line | cut -d : -fA 2 | sed 's/[,\"]//g'`
               #echo $node_ip
               node_port=`echo $line | cut -d : -f 3 | sed 's/\"//g' | sed 's/,//g'`

               node_name=${node_ip}_${node_port}
               #echo $node_name
               node_list="{'nodeName':'$node_name','nodeIp':'$node_ip','port':'$node_port'},"
               node_list_x="$node_list_x$node_list"

            done</tmp/tmp.txt
            node_list_res="[${node_list_x%,*}]"     
            discover_mongo_info="{'cluserName':$cluserName,'nodeList':$node_list_res},"
        done
    else
         discover_mongo_info=" "
    fi
    discover_mongo_json="$discover_mongo_json$discover_mongo_info"
done
result="[${discover_mongo_json%,*}]"
result=`echo ${result//\'/\"}`
resutl_final="[{\"cpname\":\"$cpname\",\"flags\":$result}]"
echo $resutl_final









发表评论

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

Captcha Code