shell監控腳本-監控memcache
注意:請先參考 shell監控腳本-準備工做,監控腳本在 rhel5 下測試正常,其它版本的linux 系統請自行測試
#監控memcachelinux
cat chk_memcache.sh shell
#!/bin/bash bash
# 運維
#script_name:chk_memcache.sh ide
#check memcache 測試
# spa
#last update 20130320 by dongnan .net
#bbs# http://bbs.ywwd.net/ code
#blog# http://dngood.blog.51cto.com xml
#
#(echo "stats";sleep 2) | telnet IP 11211
#Trying IP...
#Connected to IP (IP).
#Escape character is '^]'.
#STAT pid 26090
#STAT uptime 3179206
#STAT time 1363157551
#STAT version 1.2.6
#.................
#STAT pointer_size 64
#STAT threads 1
#END
#Connection closed by foreign host.
#
#printf "stats\r\n" | nc IP 11211
#
#nc -z bbs.ywwd.net 80 && echo $?
#0
#nc -z bbs.ywwd.net 81 || echo $?
#1
#variables
nc=/usr/bin/nc
usleep=/bin/usleep
echo=/bin/echo
sh_dir=/root/sh/
crondir=${sh_dir}crontab
source ${sh_dir}CONFIG
hosts="$MEMCACHE_HOSTS"
check_count=8
fault_count=4
#main
#主循環遍歷機器
for memcache in $hosts ;do
flag_file=${crondir}/log/${memcache}.memcache
log=$crondir/log/memcache_error.log
#true
if [ $(echo "stats" | nc -w 2 "$memcache" 11211 | wc -l) -gt 2 ];then
#flag
if [ -f $flag_file ];then
#sms
#for mobile in $MOBILES; do
#$echo "$memcache memache ok" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
#done
for mail in $MAILS;do
echo "$memcache memcache ok" | mail -s "$memcache memcache ok" $mail
done
#delete flag
test -e "$flag_file" && rm -f "$flag_file"
fi
#false
else
#flag存在,跳過本次循環
test -e $flag_file && continue
check_failed=0
#
for((i=1;i<="$check_count";i++));do
check_date=$(date '+ %F %T')
#
if [ ! $(echo "stats" | nc -w 2 "$memcache" 11211 | wc -l) -gt 2 ];then
#
((check_failed++))
#error.log
$echo "$(/bin/date +'%F %T') $memcache $check_failed fault" >> "$log"
#
$usleep 300000 || sleep 1 || sleep 1
fi
done
#大(等)於fault_count發送報警
if [ "$check_failed" -gt "$fault_count" ];then
#sms
#for mobile in $MOBILES;do
#$echo "$memcache memache error" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
#/bin/date +'%F %T' && $echo "$memcache memache error"
#done
for mail in $MAILS;do
echo "$memcache memcache error" | mail -s "$memcache memcache error" $MAILS
done
#log
echo "$check_date $HOST memcache error" >> "$log"
#flag
$echo "$memcache memache error" > "$flag_file"
fi
#主if結束
fi
#主循環結束
done
#
結束更多請: linux 系統運維 37275208 vmware 虛擬化 166682360