#!/bin/bash #监控redis是否存活的脚本 while [ 1 ]; do a=`lsof -i:6379 |grep 'redis-ser' |grep -v grep |grep -v agent|sort | tail -1 | cut -f 1 -d' '` if [ "$a" != 'redis-ser' ];then { /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf & echo redis restart at `date` >> /data/redis/data/redisLive.log } fi sleep 5 done ###Redis监控和告警 ###健康状态 redis_stat.sh #!/bin/bash host="127.0.0.1" passwd1="123456" passwd2="123456" passwd3="123456" if [ $# -lt 4 ];then echo "Usage: bash $0 -p [6379] -s section [-k] keys [-d] db [-o] opt" exit 1 else port=6379 db='' opt='' while [ $# -gt 0 ] do if [ $1 == "-p" ];then port=$2 fi if [ $1 == "-s" ];then section=$2 fi if [ $1 == "-k" ];then key=$2 fi if [ $1 == "-d" ];then db=$2 fi if [ $1 == "-o" ];then opt=$2 fi shift 2 done fi case $port in 6379) passwd=$passwd1 ;; 6380) passwd=$passwd2 ;; 6381) passwd=$passwd3 ;; *) passwd="" ;; esac if [[ $section == "Keyspace" && $key == "dbs" ]];then cmd="/usr/local/redis/bin/redis-cli -p $port -a $passwd info Keyspace |grep '^db[0-9]{1,2}:*'|wc -l" elif [[ ! -z $db && ! -z $opt ]];then cmd="/usr/local/redis/bin/redis-cli -p $port -a $passwd info Keyspace |grep $db |tr -s ',' ' '|grep $opt|cut -d '=' -f 2" #cmd="/usr/local/redis/bin/redis-cli -p $port -a $passwd info Keyspace |grep $db |tr -s ',' ' ' |awk -F [=] -v akey=$opt '/$akey/{print """$NF"""}'" else cmd="/usr/local/redis/bin/redis-cli -p $port -a $passwd info $section|grep ${key}: |cut -d ':' -f 2" fi #echo $cmd eval $cmd ### redis-cli -h 127.0.0.1 -p 6379 -a zvIZ31Yn3G info all | awk -F ':' redis-cli用法: # redis-cli -h 127.0.0.1 -p 6379 -a passwoed //连接指定的redis # redis-cli info //没有密码的情况下,打印本地redis-srv的状态信息 # redis-cli info Keyspace //查看指定状态域的信息 ###redis3.0集群监控脚本 http://blog.itpub.net/20625855/viewspace-1847453/ #!/bin/bash ######变量定义#################################### auth="ASF6tp73yF5VPGVhHJuF" #认证密码 configdir='/data/redis_cluster' #配置目录 bindir='/usr/local/redis-3.0.3/bin/' #redis安装目录 client="$bindir/redis-cli " # max_memory=0 #初始化最大内存 flag=0 #输入参数是否有误标识 #集群ip list list=( 172.16.106.76:6379 172.16.106.78:6379 172.16.106.77:6379 ) is_sendms=$2 #是否发送短信报警:默认不发送;=1: 发送 ################################################### ###报警函数定义#################################### sendms(){ if [ "$2" == "1" ]; then #自己填写报警方式 echo "报警:$1" fi } export -f sendms ################################################### ###redis状态统计函数############################### statistics_redis(){ local ip=`echo $1 | awk -F':' '{print $1}'` local port=`echo $1 | awk -F':' '{print $2}'` local is_slowlog=0 last_slowlog_time=`$client -h $ip -p $port slowlog get 1 |awk '{if(NR==2) print $1}'` current_time=`date +%s` if [ "$last_slowlog_time" != "" ]; then if [ $((last_slowlog_time+60*10)) -gt $current_time ]; then is_slowlog=1 fi fi $client -h $ip -p $port info all | awk -F ':' -v max_memory=$max_memory -v addr=$1 -v is_sendms=$2 -v is_slowlog=$is_slowlog '{ if($0~/uptime_in_seconds:/) uptime=$2; else if($0~/connected_clients:/) cnt_clients=$2; else if($0~/used_memory:/) used_memory=$2; else if($0~/used_memory_rss:/) used_memory_rss=$2; else if($0~/used_memory_peak:/) used_memory_peak=$2; else if($0~/mem_fragmentation_ratio:/) mem_ratio=$2; else if($0~/rdb_last_bgsave_status:/) bgsave_status=$2; else if($0~/aof_last_write_status:/) aof_write_status=$2; else if($0~/aof_last_bgrewrite_status:/) aof_bgrewrite_status=$2; else if($0~/instantaneous_ops_per_sec:/) ops_per_sec=$2; else if($0~/keyspace_hits:/) keyspace_hits=$2; else if($0~/keyspace_misses:/) keyspace_misses=$2; else if($0~/:keys=/) keys=keys" "$2; }END{ printf("