discover_elasticsearch_cluster.sh感知elasticsearch集群

  sre

#! /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  '{ print2 }'|awk -F : '{ print 2 }'`
ip=`ip addr | grep 'state UP' -A2 | head -n3|tail -n1 | awk '{print2}' | 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 '=' '{print2}'|head -1`
        ports=`netstat -lntp|grep pid|awk '{print4}'|awk -F ':' '{print NF}'`
        #elasticsearch_info=
        #echoports
        for p in ports
        do             node_list_x=""
            req_url='http://'ip':'p'/'
            #echoreq_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 -XGETnode_url 2>/dev/null | wc -l`
            if [ "node_cnt" -le "1" ]; then
        echo "[]"
        exit
        fi
        #echonode_cnt
            #echo node_cnt
            #echonode_url
            #echo cluster_url
            culster_name=`curl -XGETcluster_url 2>/dev/null`
            #echo culster_name
            response_code=`curl -sreq_url -w%{http_code} 2>/dev/null -o /dev/null`
            if [[ "response_code" -gt 199 ]];then
                 curl -XGETnode_url 2>/dev/null  >/tmp/tmp.txt
                 #cat  /tmp/tmp.txt
                 #node_list_x=""

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

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

                    #echo node_list_x
                node_list_res="[{node_list_x%,*}]"
                #node_list_res=`echo {node_list_res//\'/\"}`
                #echonode_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_jsonelasticsearch_info"
done
result="[{discover_elasticsearch_json%,*}]"
result=`echo {result//\'/\"}`
#echoresult
resutl_final="[{\"cpname\":\"cpname\",\"flags\":result}]"
echo $resutl_final


LEAVE A COMMENT

Captcha Code