1.安装mailx和jq
yum install -y mailx
vi /etc/mail.rc (追加以下内容)
set from=942429164@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=942429164
set smtp-auth-password=ttplbqthknzibedb (自己的邮箱授权码)
set smtp-auth=login
wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
chmod +x ./jq
cp jq /usr/bin
编写脚本
vi /opt/1.sh
#!/bin/bash
#网站url地址
URL="https://dzsmk.dg.cn:8001/prod-api/iface/branchesInfo/list"
#URL="https://dzsmk.dg.cn:8001/prod-api/"
#获取http响应代码
#HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
#HTTP_CODE=`curl -i -s -m 10 "${URL}" | grep -o '"code":200' | grep -o 200`
#echo $HTTP_CODE
RESULT=$(curl -k -s ${URL})
USER=$(echo ${RESULT} | jq '.code')
echo $USER
#服务器能正常响应,应该返回200的代码
if [ "$USER" != '200' ];then
#发送错误邮件
echo "警告:返回值不为200,网站存在异常" |tee /tmp/url_status.log
mail -s "$(date) ——URL告警" 2245925066@qq.com </tmp/url_status.log
else
echo "网站正常"
#echo "恢复:网站正常" |tee /tmp/url_status.log
#mail -s "$(date) ——URL恢复" 2245925066@qq.com </tmp/url_status.log
fi
3.配置定时任务
crontab -e
*/1 * * * * /usr/sh /opt/1.sh >/dev/null 2>&1
网友评论