监控服务,这是放在crontab的任务,个人不喜欢用死循环放在后台运行。
所以每次会执行3次循环,连续3次循环,服务有任何一项是down的,则会报警。
普及小知识,注意重定向的3种写法,其实效果是一样的。
#!/bin/bash nmap -p 6065 10.31.6.6 | egrep "6065/tcp open" &>/dev/null code1=$? nmap -p 80 10.31.6.6 | grep "80/tcp open http" >/dev/null 2>&1 code2=$? curl http://10.31.6.6/test.html/ >&/dev/null code3=$? my_mail(){ mail_list=("test@hotmail.com.cn" "test2@hotmail.com.cn" ) now_date=`date "+%Y-%m-%d %T"` for i in ${mail_list[@]} do echo -e "webchat is down,please check;\nip:42.62.5.100\n$now_date" | mail -s "webchat down" $i done } n=1 count=1 while ((n<4)) do if [ $code1 = 0 ] && [ $code2 = 0 ] && [ $code3 = 0 ] then n=$(($n+1)) sleep 2 else count=$(($count+1)) n=$(($n+1)) sleep 2 if [ $count -eq 3 ];then my_mail fi fi done
本文出自 “wolf_ribble@163.com” 博客,请务必保留此出处http://ribble.blog.51cto.com/3863110/1649583
原文地址:http://ribble.blog.51cto.com/3863110/1649583