discover_redis_cluster.sh感知redis集群

  sre

#! /bin/bash
export LANG=en_US.UTF-8
ip=`ip addr | grep 'state UP' -A2 | head -n3|tail -n1 | awk '{print 2}' | cut -f1 -d '/'`
# ip=`ifconfig 2>/dev/null|sed -n 2p|awk  '{ print2 }'|awk -F : '{ print 2 }'`
pids=`ps aux | grep -v grep | grep -v ps.sh|grep redis-server | grep : | grep -v defunct | awk '{print2}'`
discover_redis_json=""
CURSTR=(ps aux | grep "1" | grep -v grep)
curStr=(echoCURSTR | awk '{cc=index(0,"redis-server");if(cc>-1){print substr(0,cc);}}')
UW=(echocurStr| grep ":")
cpname="redis_cluster"

function getpasswd()
{
if [ ! -f "/data/monitor/rdspwd" ];then
    passwd=`echo ""`
    echo passwd
else  if [ -s /data/monitor/rdspwd ];then    passwd=`echo ""`
    while read line    do
    port_cmd=`echoline | cut -d : -f 1`
    passwd_cmd=`echo line | cut -d : -f 2`

    if [[port_cmd == 1 ]];then       passwd=passwd_cmd

    fi
    done </data/monitor/rdspwd

  else 
    passwd=`echo ""`
  fi
  echo passwd
fi
}

for pid inpids
do
    if [[ "UW" != "" ]];then
        PORT=(netstat -lnp | grep -v grep|grep redis-s | grep pid | awk '{print4}' | awk -F ':' '{print (NF)}' |head -1)

    fi

    if [[ "PORT" = "" ]];then
        continue
    else

        passwd_true=`echo (getpasswdPORT)`


        if [[ passwd_true == "" ]];then
             conn_str=`echo ""`

        else
             conn_str=`echo "-a "passwd_true`
        fi

        runid=`echo "info"  | redis-cli -p PORTconn_str | grep run_id | cut -d : -f 2 | sed 's/[[:space:]]//g'`
        tcp_port=`echo "info"  | redis-cli -p PORTconn_str | grep tcp_port | cut -d : -f 2 | sed 's/[[:space:]]//g'`
        #echo runid
        roles=`echo "info"  | redis-cli -pPORT conn_str | grep role | cut -d : -f 2 | sed 's/[[:space:]]//g'`        #echoroles
        redis_mode=`echo "info"  | redis-cli -p PORTconn_str | grep redis_mode | cut -d : -f 2 | sed 's/[[:space:]]//g'`
        #echo redis_mode
        culster_name=redis_ip"_"tcp_port
        if [[ "runid" != "" ]];then

          if [[ "roles" == "master" ]];then                 node_name_var="master"                           node_list_x="{'nodeName':'node_name_var','nodeIp':'ip','port':'PORT'},"      
              echo "info replication"  | redis-cli -p PORTconn_str  | grep -v connected | grep slave >/tmp/tmp.txt
              while read line 
              do
                node_name=`echo line | cut -d : -f 1`
                ip_var=`echoline | cut -d : -f 2 | cut -d , -f 1 | cut -d = -f 2`
                port_var=`echo line | cut -d : -f 2 | cut -d , -f 2 | cut -d = -f 2`
                node_list="{'nodeName':'node_name','nodeIp':'ip_var','port':'port_var'},"
                node_list_x="node_list_xnode_list"                
              done</tmp/tmp.txt                   
              node_list_res="[{node_list_x%,*}]"                discover_redis_info="{'cluserName':'culster_name','nodeList':node_list_res},"                  else
               discover_redis_info=""             fi                             else                       discover_redis_info=""
        fi                 discover_redis_json="discover_redis_jsondiscover_redis_info"
    fi  
done
result="[{discover_redis_json%,*}]"
#echo result
result_cnt=`echoresult | awk  '{ print length(result)'}`
#echoresult_cnt
if [[ result_cnt == 2 ]];then
   echo "[]"
else   result=`echo{result//\'/\"}`
   resutl_final="[{\"cpname\":\"cpname\",\"flags\":result}]"
   echo $resutl_final
fi



LEAVE A COMMENT

Captcha Code