查看Redis集羣各項指標

1.依賴sshpass命令,所以需要先安裝sshpass-1.06-1.el7.x86_64.rpm

  • 可以同時查看多個Redis節點的指標

在這裏插入圖片描述

  • 授權
chmod +x check-redis.sh
#!/bin/sh
#set -ex

#日誌名稱
LOG_FILE=redis_check_$(date "+%Y%m%d").log
#註釋變量或密碼留空則執行腳本時輸入
SSHPASS=[password]
REDISPASS=[password]

if [[ ! -x $(command -v sshpass 2> /dev/null) ]]; then
  echo -e 'sshpass does not exist, you should install sshpass.'
  exit 0
fi

if [[ -z ${SSHPASS} ]]; then
  read -s -p 'Enter SSH password for hosts:' SSHPASS
  export SSHPASS
  echo
else
  export SSHPASS
fi

if [[ -z ${REDISPASS} ]]; then
  read -s -p 'Enter Redis password for hosts:' REDISPASS
  export REDISPASS
  echo
else
  export REDISPASS
fi

cat << EOF > ~/.ssh/config
StrictHostKeyChecking no
EOF

function format_output {
  COL=$(($(tput cols) - 2))
  IFS= read -r -d '\n' INPUT
  MAX_LEN=$(echo "${INPUT}" | awk '{print length($0)}' | awk 'BEGIN {max = 0} {if ($1+0 > max+0) {max=$1; content=$0} } END {print content}')
  if [[ ${MAX_LEN} -gt ${COL} ]]; then
    MAX_LEN=${COL}
  fi
  MLINE=$(awk -v len=${MAX_LEN} 'BEGIN{OFS="─"; NF=len+1; print}')
  echo "┌${MLINE}┐"
  IFS=''
  echo -n "${INPUT}" | while read LINE; do
    if [[ -z ${LINE} ]]; then
      echo "├${MLINE}┤"
    else
      printf "│%-${MAX_LEN}s│\n" "${LINE}"
    fi
  done
  echo -e "└${MLINE}┘\n"
}

function ping_host {
  ping -c 1 -W 1 $1 2>&1 > /dev/null || (echo -e "Host $1: Ping Failed.\n" && exit 1)
}


function get_redis_service {
  for HOST_IP in $(echo $1 | tr ',' ' '); do
    (ping_host ${HOST_IP} && sshpass -e ssh root@${HOST_IP} 'bash -s' << EOF) | format_output | tee -a ${LOG_FILE}
export LANG=en_US.UTF-8
echo -e "Redis Server: ${HOST_IP}\n"
echo -e "Redis Process:"
ps aux | grep [r]edis-server
echo -e "\nRedis Info:"
/opt/redis-5.0.3/src/redis-cli -a ${REDISPASS} info 2>&1 |sed -r 's/\r//' | grep -E 'used_memory:|used_memory_rss_human:\
|used_memory_peak:|connected_clients:|blocked_clients:|rejected_connections:|keyspace_hits:|keyspace_misses:\
|instantaneous_ops_per_sec:|rejected_connections:|rdb_last_bgsave_status:|aof_last_bgrewrite_status:'
EOF
  done
}

get_redis_service 192.168.1.1,192.168.1.2,192.168.1.3

rm -f ~/.ssh/config

腳本作者:wuli濤濤

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章