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

pids=`ps  aux |grep org.elasticsearch.bootstrap.Elasticsearch|grep -v grep|awk '{print $2}'`
#ip=`ifconfig 2>/dev/null|sed -n 2p|awk  '{ print $2 }'|awk -F : '{ print $2 }'`
ip=`ip addr | grep 'state UP' -A2 | head -n3|tail -n1 | awk '{print $2}' | cut -f1 -d '/'`
discover_elasticsearch_json=""
cpname="elasticsearch_cluster"
for pid in $pids
do  
    if [ $pid ];then
        pwd=`ps aux|grep $pid|grep java|grep -v grep|awk '{for(i=1;i<=NF;++i){if(index($i,"Des.path.home")>=1){print $i}}}'|awk -F '=' '{print $2}'|head -1`
        ports=`netstat -lntp|grep $pid|awk '{print $4}'|awk -F ':' '{print $NF}'`
        #elasticsearch_info=
        #echo $ports
        for p in $ports
        do  
            node_list_x=""
            req_url='http://'$ip':'$p'/'
            #echo $req_url
            node_url='http://'$ip':'$p'/_cat/nodes?h=ip,port,name,pid,http_address'
            #echo $node_url
            cluster_url='http://'$ip':'$p'/_cat/health?h=cluster'                     
        node_cnt=`curl -XGET $node_url 2>/dev/null | wc -l`
            if [ "$node_cnt" -le "1" ]; then
        echo "[]"
        exit
        fi
        #echo $node_cnt
            #echo $node_cnt
            #echo $node_url
            #echo $cluster_url
            culster_name=`curl -XGET $cluster_url 2>/dev/null`
            #echo $culster_name
            response_code=`curl -s  $req_url -w%{http_code} 2>/dev/null -o /dev/null`
            if [[ "$response_code" -gt 199 ]];then
                 curl -XGET $node_url 2>/dev/null  >/tmp/tmp.txt
                 #cat  /tmp/tmp.txt
                 #node_list_x=""

                    while read line
                    do
                     #echo $line           
                     ip=`echo $line | awk '{print $1}'`
                     #port=`echo $line | awk '{print $2}'`
                     node_name=`echo $line | awk '{print $3}'`
                     pid=`echo $line | awk '{print $4}'`
                     port=`echo $line | awk '{print $5}' | cut -d : -f 2`
                     #port =`echo $line | awk '{print $5}'`
                     #echo $port

                     node_list="{'nodeName':'$node_name','nodeIp':'$ip','port':'$port'},"
                     node_list_x="$node_list_x$node_list"
                     #echo $node_list_x
                     done</tmp/tmp.txt

                    #echo $node_list_x
                node_list_res="[${node_list_x%,*}]"
                #node_list_res=`echo ${node_list_res//\'/\"}`
                #echo $node_list_res
                elasticsearch_info="{'cluserName':'$culster_name','nodeList':$node_list_res},"
                #echo $elasticsearch_info
                break
            fi
        done
    else
        echo "miss argument pid!" >2
    fi
    discover_elasticsearch_json="$discover_elasticsearch_json$elasticsearch_info"
done
result="[${discover_elasticsearch_json%,*}]"
result=`echo ${result//\'/\"}`
#echo $result
resutl_final="[{\"cpname\":\"$cpname\",\"flags\":$result}]"
echo $resutl_final

发表回复

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

Captcha Code