MONITOR 是一个调试命令,返回服务器处理的每一个命令,它能帮助我们了解在数据库上发生了什么操作,可以通过redis-cli和telnet命令使用.
$ redis-cli monitor
1339518083.107412 [0 127.0.0.1:60866] "keys" "*"
1339518087.877697 [0 127.0.0.1:60866] "dbsize"
1339518090.420270 [0 127.0.0.1:60866] "set" "x" "6"
1339518096.506257 [0 127.0.0.1:60866] "get" "x"
1339518099.363765 [0 127.0.0.1:60866] "del" "x"
1339518100.544926 [0 127.0.0.1:60866] "get" "x"
使用SIGINT (Ctrl-C)来停止 通过redis-cli使用MONITOR命令返回的输出.
$ telnet localhost 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
MONITOR
+OK
+1339518083.107412 [0 127.0.0.1:60866] "keys" "*"
+1339518087.877697 [0 127.0.0.1:60866] "dbsize"
+1339518090.420270 [0 127.0.0.1:60866] "set" "x" "6"
+1339518096.506257 [0 127.0.0.1:60866] "get" "x"
+1339518099.363765 [0 127.0.0.1:60866] "del" "x"
+1339518100.544926 [0 127.0.0.1:60866] "get" "x"
QUIT
+OK
Connection closed by foreign host.
使用QUIT命令来停止通过telnet使用MONITOR返回的输出.
##MONITOR 性能消耗
由于MONITOR命令返回 服务器处理的所有的 命令, 所以在性能上会有一些消耗.
在不运行MONITOR命令的情况下,benchmark的测试结果:
$ src/redis-benchmark -c 10 -n 100000 -q
PING_INLINE: 101936.80 requests per second
PING_BULK: 102880.66 requests per second
SET: 95419.85 requests per second
GET: 104275.29 requests per second
INCR: 93283.58 requests per second
在运行MONITOR命令的情况下,benchmark的测试结果: (redis-cli monitor > /dev/null):
$ src/redis-benchmark -c 10 -n 100000 -q
PING_INLINE: 58479.53 requests per second
PING_BULK: 59136.61 requests per second
SET: 41823.50 requests per second
GET: 45330.91 requests per second
INCR: 41771.09 requests per second
在这种特定的情况下,运行一个MONITOR命令能够降低50%的吞吐量,运行多个MONITOR命令 降低的吞吐量更多.
##返回值
没有统一标准的返回值, 无限的返回服务器端处理的命令流.
输出分析shell脚本:
#!/bin/sh
disp_help() {
echo "Usage: $0 -f filename -t time -c comand " >&2
echo "comand means echo detail command Y|N"
}
FILENAME= TIME= COMMAND=
while getopts f:t:c: opt
do
case $opt in
f) FILENAME=$OPTARG
;;
t) TIME=$OPTARG
;;
c) COMMAND=$OPTARG
;;
?) disp_help
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ -z $FILENAME ]
then
disp_help
exit 1
fi
if [ -z $TIME ]
then
disp_help
exit 1
fi
if [ -z $COMMAND ]
then
COMMAND="N"
elif [ "$COMMAND" != "Y" ]&&[ "$COMMAND" != "N" ];then
disp_help
exit 1
fi
egrep "^$TIME" $FILENAME|egrep -v '^$' > ${FILENAME}.tmplog
echo >${FILENAME}.temp_result.log
start_line=`head -1 ${FILENAME}.tmplog`
start_time=`echo "$start_line"|awk -F' ' '{print $1}'`
start_command=`echo "$start_line"|awk -F' ' '{print $4,$5}' OFS=' '`
while read LINE
do
end_time=`echo "$LINE"|awk -F' ' '{print $1}'`
used_time=`echo "$end_time - $start_time"|bc`
echo "$used_time $start_command">>${FILENAME}.temp_result.log
start_line="$LINE"
start_time=`echo "$start_line"|awk -F' ' '{print $1}'`
start_command=`echo "$start_line"|awk -F' ' '{print $4,$5}' OFS=' '`
done < ${FILENAME}.tmplog
if [ "$COMMAND" = "Y" ];then
more ${FILENAME}.temp_result.log|egrep -v '^$'|awk -F' ' '{a[$2" "$3]+=$1;}END{for(i in a){print i" "a[i];}}'|sort -rnk 3|egrep -v 'e-05$'
else
more ${FILENAME}.temp_result.log|egrep -v '^$'|awk -F' ' '{a[$2]+=$1;}END{for(i in a){print i" "a[i];}}'|sort -rnk 2
fi