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

redis cluster 安装

服务器:

IP主端口从端口
172.16.8.21570007001
172.16.8.21670007001
172.16.8.21770007001

1.文件下载

wget http://download.redis.io/releases/redis-5.0.6.tar.gz

2.安装

make
make install PREFIX=/usr/local/redis

3.内核参数修改

vm.overcommit_memory=1

net.core.somaxconn = 511

echo never > /sys/kernel/mm/transparent_hugepage/enabled
vim /etc/rc.local

4.创建目录

mkdir -p /opt/app/data/redis_7000
mkdir -p /opt/app/data/redis_7001

5.配置文件

/usr/local/redis/7000.conf

/usr/local/redis/7001.conf

6.参数修改

master:

port 7000
pidfile /var/run/redis_7000.pid
logfile “7000.log”
dir “/opt/app/data/redis_7000”
cluster-config-file nodes-7000.conf
auto-aof-rewrite-min-size 256mb
appendfilename “appendonly.aof”
cluster-enabled yes
appendonly yes
slowlog-log-slower-than 10000
bind 0.0.0.0

slave:

port 7001
pidfile /var/run/redis_7001.pid
logfile “7001.log”
dir “/opt/app/data/redis_7001”
cluster-config-file nodes-7001.conf
auto-aof-rewrite-min-size 256mb
appendfilename “appendonly.aof”
cluster-enabled yes
appendonly yes
slowlog-log-slower-than 10000
bind 0.0.0.0

7.启动redis

/usr/local/bin/redis-server /usr/local/redis/7000.conf
/usr/local/bin/redis-server /usr/local/redis/7001.conf

8.创建集群

redis-cli –cluster create 172.16.8.215:7000 172.16.8.215:7001 172.16.8.216:7000 172.16.8.216:7001 172.16.8.217:7000 172.16.8.217:7001 –cluster-replicas 1