#! /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  '{ print $2 }'|awk -F : '{ print $2 }'`
pids=`ps aux | grep -v grep | grep -v ps.sh|grep redis-server | grep : | grep -v defunct | awk '{print $2}'`
discover_redis_json=""
CURSTR=$(ps aux | grep "$1" | grep -v grep)
curStr=$(echo $CURSTR | awk '{cc=index($0,"redis-server");if(cc>-1){print substr($0,cc);}}')
UW=$(echo $curStr| 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=`echo $line | 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 in $pids
do
    if [[ "$UW" != "" ]];then
        PORT=$(netstat -lnp | grep -v grep|grep redis-s | grep $pid | awk '{print $4}' | awk -F ':' '{print $(NF)}' |head -1)

    fi

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

        passwd_true=`echo $(getpasswd $PORT)`


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

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

        runid=`echo "info"  | redis-cli -p $PORT $conn_str | grep run_id | cut -d : -f 2 | sed 's/[[:space:]]//g'`
        tcp_port=`echo "info"  | redis-cli -p $PORT $conn_str | grep tcp_port | cut -d : -f 2 | sed 's/[[:space:]]//g'`
        #echo $runid
        roles=`echo "info"  | redis-cli -p $PORT $conn_str | grep role | cut -d : -f 2 | sed 's/[[:space:]]//g'` 
        #echo $roles
        redis_mode=`echo "info"  | redis-cli -p $PORT $conn_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 $PORT $conn_str  | grep -v connected | grep slave >/tmp/tmp.txt
              while read line 
              do
                node_name=`echo $line | cut -d : -f 1`
                ip_var=`echo $line | 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_x$node_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_json$discover_redis_info"
    fi  
done
result="[${discover_redis_json%,*}]"
#echo $result
result_cnt=`echo $result | awk  '{ print length($result)'}`
#echo $result_cnt
if [[ $result_cnt == 2 ]];then
   echo "[]"
else 
   result=`echo ${result//\'/\"}`
   resutl_final="[{\"cpname\":\"$cpname\",\"flags\":$result}]"
   echo $resutl_final
fi



发表评论

您的电子邮箱地址不会被公开。

Captcha Code