生产服务器的java进程存在,但是因为某些原因,接口访问不正常,所以需要写个shell脚本定时查看接口是否正常
vim check_url.sh
#!/bin/bash
URL_LIST=('http://127.0.0.1:8080/link/E2rS' 'http://192.168.21.21:8080//link/E2rS') #网址列表
for URL in ${URL_LIST[*]}; do
FAIL_COUNT=0 #设置一个变量来统计访问失败次数
for ((i=1;i<=3;i++)); do
HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL) #超时时间是3秒
if [ $HTTP_CODE -eq 200 ]; then
echo "$URL OK" #访问正常打印ok
break
else
echo "$URL retry $FAIL_COUNT"
let FAIL_COUNT++ #如果不ok的话就执行计数加一
fi
done
if [ $FAIL_COUNT -eq 3 ]; then #如果FAIL_COUNT=3,就发出告警
echo "Warning: $URL Access failure!"
error_msg="异常提醒error:$URL"
msg_body="{\"msgtype\":\"text\",\"text\":{\"content\":\"$error_msg\"}}"
WEBHOOK_URL="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=eae02527"
req_msg=$(curl -s -H "Content-Type: application/json" -X POST -d $msg_body $WEBHOOK_URL )
echo "触发报警:" $req_msg
fi
done
crontab -e
*/5 * * * * bash /data/sh/check-url.sh >/data/sh/logs/check-url.log