shell 监控网站运行状态
shell监控网站状态,依靠返回状态码来进行判断,返回200,302认为状态是正常的,否则认为tomcat/nginx/LB/Haproxy/apache应用服务挂掉了。
脚本实现如下:
1、编写shell脚本实现监控功能,使用curl访问网站,过滤出返回的状态码当做判断条件,如有返回状态码非200/302则发送邮件报警
# cat /root/shell/monitor_site/check_site.sh
#!/bin/bash
mysite=/root/shell/monitor_site/http_site
check_status=/root/shell/monitor_site/temp_status
historyfile=/root/shell/monitor_site/history/`date +%Y-%m-%d`/`date +%T`
failurefile=/root/shell/monitor_site/history/`date +%Y-%m-%d`/`date +%T`_failure
mkdir -pv /root/shell/monitor_site/history/`date +%Y-%m-%d` &>/dev/null
for site in `grep -v -E "^#|^$" $mysite`
do
curl -s -I --connect-timeout 3 -m 10 $site | grep "HTTP/1.1" | awk '{print $2}' > $check_status
status=`cat $check_status`
if [[ $status -eq 200 ]] || [[ $status -eq 302 ]]
then
echo "###########################" >>$historyfile
echo "http_site $site Access Successful" >>$historyfile
else
echo "###########################" >>$historyfile
echo "http_site $site Access Failure" >>$historyfile
fi
done
grep "Access Failure" $historyfile &>/dev/null
if [ $? -eq 0 ]
then
echo -e "\n\nThe following Service is not started !!!\n" >> $failurefile
echo -e "Please check the services !!!\n" >> $failurefile
echo -e "#############################################\n" >> $failurefile
grep "Access Failure" $historyfile >> $failurefile
echo -e "\n#############################################" >> $failurefile
mail -s "SiLuTong_Web_Site_Check !!!" liugh@qq.com 1368152xxxx@139.com < $failurefile
fi
2、监控网站列表,创建一个站点文件,把需要监控的地址URL都写到http_site文件里面
# cat /root/shell/monitor_site/http_site # chinau-site http://www.chinau.com.cn http://boss.chinau.com.cn # iaugold-site http://www.iaugold.com http://m.iaugold.com http://api.iaugold.com # b2b http://www.silkeroad.com http://supplier.silkeroad.com http://admin.silkeroad.com # b2c http://www.silubuy.cn # halal http://www.srphalal.com http://www.srphalal.com/adminLogin http://www.srphalal.com/webs/getWebgoLogin # Official websit http://www.silkeroad.cn http://www.silupay.com
3、配置报警发件邮箱
# cat /etc/mail.rc set from=wmliuguohui@163.com set smtp=smtp.163.com set smtp-auth-user=wm@163.com set smtp-auth-password=password set smtp-auth=login
4、添加计划任务,每5分钟执行一次
# crontab -e */5 * * * * /bin/bash /root/shell/monitor_site/check_site.sh > /dev/null 2>&1

浙公网安备 33010602011771号