监控memcache服务

监控memcache服务是否正常,模拟用户(web客户端)检测。 
使用nc命令加上set/get来模拟检测,以及监控响应时间及命中率。

#!/bin/bash

########################
#date:2018-04-20 #
#author:7777 #
#QQ:577545535          #
#E-mail:18401747334@163.com#
########################
#echo -ne "\033[0;33m"
echo -ne "\E[1;32m"
cat<<EOT
_oo0oo_
088888880
88" . "88
(| -_- |)
0\ = /0
___/'---'\___
.' \\\\| |// '.
/ \\\\||| : |||// \\
/_ ||||| -:- |||||- \\
| | \\\\\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' __/-. /
___'. .' /--.--\ '. .'___
."" '< '.___\_<|>_/___.' >' "".
| | : '- \'.;'\ _ /';.'/ - ' : | |
\ \ '_. \_ __\ /__ _/ .-' / /
====='-.____'.___ \_____/___.-'____.-'=====
'=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 iii 服务正常
EOT
echo -ne "\E[0m"
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
###variables
RETVAL=0
###color
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
####menu list
menu() {
cat <<EOF
############################
1 memcached status
2 memcached stop
3 memcached get_hits
4 quit
EOF
}
status() {
Set=$(printf "set key1 0 0 6\r\noldboy\r\n"|nc 127.0.0.1 11211|grep STORED|wc -l )
Get=$(printf "get key1\r\n"|nc 127.0.0.1 11211|grep oldboy|wc -l)
Port=$(netstat -lntup|grep memcached|wc -l)
if [ $Port -ge 4 ];then
Num=$(expr $Set + $Get)
if [ $Num -eq 2 ];then
action "Memcached server is running" /bin/true
else
action "Memcached server is not normal" /bin/false
fi
else
action "Memcached server is not running" /bin/false
read -t 10 -p "Memcached server whether open:[y/n]:" b
if [ "$b" == "y" -o "$b" == "Y" ];then
/usr/bin/memcached -m 16m -p 11211 -d -u root -c 8192
action "Memcached server is starting" /bin/true
elif [ "$b" == "n" -o "$b" == "N" ];then
echo -e "$YELLOW_COLOR Goodbye.............$RES"
fi
fi
return $RETVAL
}
stop() {
Port=$(netstat -lntup|grep memcached|wc -l)
if [ $Port -ge 4 ];then
pkill memcached &>/dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Memcached server is stopping" /bin/true
else
action "Memcached server is stopping" /bin/false
fi
else
action "Memcached server not running" /bin/false
fi
}
get_hits() {
Cmd_get=$(printf "stats\r\n"|nc 127.0.0.1 11211|grep "cmd_get"|awk '{print $3}')
Get_hits=$(printf "stats\r\n"|nc 127.0.0.1 11211|grep "get_hits"|awk '{print $3} ')
Get_misses=$(printf "stats\r\n"|nc 127.0.0.1 11211|grep "get_misses"|awk '{print $3}')
Port=$(netstat -lntup|grep memcached|wc -l)
if [ $Port -ge 4 ];then
echo "A total of cmd_get is $Cmd_get"
sleep 1
echo "A total of get_hits is $Get_hits"
sleep 1
echo "A total of get_misses is $Get_misses"
else
action "Memcached is not running" /bin/false
fi
return $RETVAL
}
main() {
while true
do
menu
read -t 10 -p "Please input number:" a
expr $a + 1 &>/dev/null
if [ $? -ne 0 ];then
echo -e "$BLUE_COLOR Please input error,not int $RES"
exit 1
fi
case "$a" in
1)
status
RETVAL=$?
;;
2)
stop
RETVAL=$?
;;
3)
get_hits
RETVAL=$?
;;
4)
clear
echo "##############################################"
echo -e "$GREEN_COLOR `figlet GoodBye` $RES"
echo "##############################################"
exit 0
;;
*)
echo -e "$RED_COLOR input error $RES"
esac
done
}
main
exit $RETVAL

posted @ 2018-04-21 16:47  地铁昌平线  阅读(222)  评论(0编辑  收藏  举报