(1)统计redis连接信息
echo 'client list' | /apps/svr/redis/bin/redis-cli -h 127.0.0.1 -p 6379 |awk '{print $1}' | awk -F '[=,:]' '{print $2}' | sort -n | uniq -c |sort -nr
(2)当redis连接满了,client list都没法执行,那么是用netsta命令查看。
netstat -na| grep 6379 | awk '{print $5}' | awk -F":" '{print $1}' | sort | uniq -c
(3)当redis实例cpu较高的时间,捕获redis正在执行的命令
#!/user/bin/env bash port=$1 rpid=`ps aux | grep redis-server| grep -v grep | grep $port | awk '{print $2}'` for((;;)) do mon=`top -c -b -n 1 -p $rpid | tail -2|grep -Ev "^$"|awk '{if($9 > 15) print 1;else print 0}'` if [ $mon = 1 ] then /apps/svr/redis/bin/redis-cli -h 127.0.0.1 -p $port monitor > /tmp/$port.txt & echo 'client list'|/apps/svr/redis/bin/redis-cli -h 127.0.0.1 -p $port > /tmp/$port'_client'.txt sleep 1 ps aux | grep redis | grep -v 'grep' |grep 'redis-cli' | grep 'monitor' | grep $port | awk '{print $2}' | xargs kill -s 9 exit fi sleep 0.5 done
改进:
#!/usr/bin/env bash port=$1 # get the pid of redis-server of specified port rpid=`ps aux | grep redis-server| grep -v grep | grep $port | awk '{print $2}'` #when cpu usage is greater than 50%,begin to catch the redis query log by using the redis MONITOR command mon=`top -c -b -n 1 -p $rpid | tail -2|grep -Ev "^$"|awk '{if($9 > 50) print 1;else print 0}'` if [ $mon = 1 ] then /apps/svr/redis/bin/redis-cli -h 127.0.0.1 -p $port monitor > /tmp/$port.txt &; mpid=$! sleep 3 # killing the subprocess and exit kill $mpid exit fi