1.安裝mailx和jq
yum install -y mailx
vi /etc/mail.rc (追加以下內(nèi)容)
set from=942429164@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=942429164
set smtp-auth-password=ttplbqthknzibedb (自己的郵箱授權(quán)碼)
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
#網(wǎng)站url地址
URL="https://dzsmk.dg.cn:8001/prod-api/iface/branchesInfo/list"
#URL="https://dzsmk.dg.cn:8001/prod-api/"
#獲取http響應(yīng)代碼
#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
#服務(wù)器能正常響應(yīng)肛炮,應(yīng)該返回200的代碼
if [ "$USER" != '200' ];then
#發(fā)送錯誤郵件
echo "警告:返回值不為200,網(wǎng)站存在異常" |tee /tmp/url_status.log
mail -s "$(date) ——URL告警" 2245925066@qq.com </tmp/url_status.log
else
echo "網(wǎng)站正常"
#echo "恢復(fù):網(wǎng)站正常" |tee /tmp/url_status.log
#mail -s "$(date) ——URL恢復(fù)" 2245925066@qq.com </tmp/url_status.log
fi
3.配置定時任務(wù)
crontab -e
*/1 * * * * /usr/sh /opt/1.sh >/dev/null 2>&1