生产环境批量检查web url并通过手机邮件报警
场景:
*用于服务启动后快速检查,常用于和服务启动脚本放一起。*
#!/bin/bash
########################################
# this script is created by wanglei
# mail: 353938339@qq.com
# qq: 353938339
# version: 1.0
#######################################
# function: check service
# blog: https://blog.csdn.net/weixin_38753143
# major: Linux,Cloud computing,docker,kubernetes,ansible ...
######################################
set -x
RETVAL=0
MAIL_GROUP="353938339@qq.com"
LOG_FILE="/tmp/result.log"
get_url() {
FAILCOUNT=0
for ((i=1;i<=3;i++))
do
wget -T 5 --tries=3 --spider=http://${1} >/dev/null
[ $? -ne 0 ] && (($FAILCOUNT+=1))
done
if [ $FAILCOUNT -gt 1]
then
RETVAL=1
NOW_TIME=`date +F`
subje="${NOW_TIME} the service is error"
echo "${NOW_TIME} $subje" >> ${LOG_FILE}
for USER in ${MAIL_GROUP}
do
mail -s "$subje" $USER < ${LOG_FILE} #用mail可以发送邮件
done
else
RETVAL=0
fi
return $RETVAL
}
for URL in `cat urllist`
do
get_url $URL
done