服务可用监控

发布时间 2023-12-13 11:39:59作者: stuka

一、通知脚本notice.sh

 1 #!/bin/bash
 2 
 3 # 定义需要检查的 URL 和关键字
 4 hostname=`awk 'NR==3 {print $1}' /etc/hosts`
 5 url="http://$hostname:login.html"
 6 keyword="记住密码"
 7 users=("1@qq.com;2@qq.com")
 8 # 如果一台服务器触发了就修改状态以免多次触发邮件:y-可以触发
 9 sendFlag=`cat /root/home/send.flag`
10 
11 # 发送邮件的函数
12 send_email() {
13 to=$users
14 subject="$hostname service unreachable"
15 body="URL: $url\n服务异常\n断言关键字: $keyword\n"
16 echo -e "$body" | mail -s "$subject" "$to"
17 }
18 
19 # 使用 curl 请求 URL 并检查返回结果是否包含关键字
20 response=$(curl -s "$url")
21 if [[ $sendFlag = y ]];then
22 if [[ $response =~ $keyword ]]; then
23 echo "`date`: 找到关键字"
24 else
25 echo "`date`: 未找到关键字"
26 echo "n" > /root/home/send.flag
27 for i in "$users"
28 do
29 send_email
30 done
31 #systemctl tomcat start
32 fi
33 else
34 echo "`date`:发送标志是$sendFlag,不再重复发送"
35 fi

 

二、在crontab中配置定时任务

1 */5 3-23,0-1 * * * sh /root/homet/notice.sh >> /root/home/notice.log 2>&1
2 0 7 * * * echo "y" > /root/home/send.flag