redis监控脚本
#!/bin/bash
. /etc/profile &> /dev/null
R_PORT=6379
RIP='-h 127.0.0.1'
RPW='-a'
RCONF=/usr/local/redis/etc/redis.conf
status(){
R_COMMAND=$1
redis-cli $RIP -p $R_PORT info > /tmp/redis.tmp
REDIS_STAT_VALUE=$(grep -w "$R_COMMAND" /tmp/redis.tmp |cut -d ':' -f2)
echo $REDIS_STAT_VALUE
}
c_client_use(){ #连接数使用比率,大于1时redis不能再新建连接
max_cli=`sed -rn "s#.*maxclients[[:blank:]]([[:digit:]])#\1#p" $RCONF`
c_cli=`status connected_clients`
echo $c_cli $max_cli|awk '{printf("%.2f\n", ($1/$2)*100)}'
}
k_hit(){ #key的命中率
hit=`status keyspace_hits`
miss=`status keyspace_misses`
echo $hit $miss|awk '{printf("%.2f\n", $1/($1+$2)*100)}'
}
m_use_rate(){ #内存使用比率
use=`status used_memory`
total_mem=`status maxmemory`
echo $use $total_mem|awk '{printf("%.2f\n", $1/$2*100)}'
}
var='status c_client_use k_hit m_use_rate'
for i in $var ;do
if [[ $1 == 'status' ]];then
status $2
num=1
break
elif [[ $i == $1 ]];then
$i
num=1
break
else
num=0
fi
done
if [[ $num == '0' ]];then
echo " 例: $0 `echo $var|tr -t ' ' '|'`"
fi