redis监控脚本

#注意修改密码

#role字段 60代表主库 30代表从库

#!/bin/bash

REDISCLI=”/usr/local/redis/bin/redis-cli”
HOST=$1
PORT=$2


if [[ $# == 3 ]];then
case $3 in
cluster_state)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” cluster info 2>/dev/null | grep -w “cluster_state” | awk -F’:’ ‘{print $2}’| grep -c ok)
echo $result
;;
role)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info replication 2>/dev/null | grep -w “role” | awk -F’:’ ‘{print $2}’)
if [[ “$result” =~ “master” ]];then
echo 60
elif [[ “$result” =~ “slave” ]];then
echo 30
else
echo 0
fi
;;
connected_clients)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “connected_clients” | awk -F’:’ ‘{print $2}’)
echo $result
;;
blocked_clients)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “blocked_clients” | awk -F’:’ ‘{print $2}’)
echo $result
;;
used_memory)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null| grep -w “used_memory” | awk -F’:’ ‘{print $2}’)
echo $result
;;
used_memory_peak)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “used_memory_peak” | awk -F’:’ ‘{print $2}’)
echo $result
;;
mem_fragmentation_ratio)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “mem_fragmentation_ratio” | awk -F’:’ ‘{print $2}’)
echo $result
;;
aof_current_size)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “aof_current_size” | awk -F’:’ ‘{print $2}’)
echo $result
;;
rejected_connections)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “rejected_connections” | awk -F’:’ ‘{print $2}’)
echo $result
;;
expired_keys)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “expired_keys” | awk -F’:’ ‘{print $2}’)
echo $result
;;
evicted_keys)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “evicted_keys” | awk -F’:’ ‘{print $2}’)
echo $result
;;
uptime_in_seconds)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “uptime_in_seconds” | awk -F’:’ ‘{print $2}’)
echo $result
;;
db0)
result=$($REDISCLI -h $HOST -p $PORT -c -a “xxxxxxxxxxxxxxxxxx” info 2>/dev/null | grep -w “db0” | awk -F’=|,’ ‘{print $2}’)
echo $result
;;
*)
echo -e “\033[33mUsage: $0 {cluster_state|connected_clients|blocked_clients|used_memory|used_memory_peak|mem_fragmentation_ratio|aof_current_size|rejected_connections|expired_keys|evicted_keys|uptime_in_seconds|db0|}\033[0m”
;;
esac
fi