一、脚本编写
[root@monitor-agent-b1 ~]# cat /server/scripts/domain.sh
#!/bin/bash
array=(
https://www.cloudcrowd.com.cn/
https://www.sec-in.com
)
Wait(){
echo -n "wait 3s "
for((i=0;i<3;i++))
do
echo -n "."
sleep 1
done
echo
}
CheckUrl(){
wget -t 2 -T 5 --spider $1 &> /dev/null #-T超时时间 -t 超时重新加载次数 --spider 只加载网页不下载
if [ $? -eq 0 ];then
echo "check $1 is OK"
else
/usr/bin/domain "check $1 is FAILED" #异常引入飞书预警
fi
return $?
}
main(){
Wait
for((i=0;i<${#array[*]};i++))
do
CheckUrl ${array[i]}
done
return $?
}
main $*
增加执行权限
[root@monitor-agent-b1 ~]# chmod +x /server/scripts/domain.shpo
二、Python 调用飞书Webhook
[root@monitor-agent-b1 ~]# cat /usr/bin/domain
#!/usr/bin/python3
import requests
import json
import sys
import os
import datetime
url = "填写自己webhook地址"
def send_message(message):
payload_message = {
"msg_type": "text",
"content": {
"text": message
}
}
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload_message))
return response
if __name__ == '__main__':
text = sys.argv[1]
send_message(text)
三、预警展示
3.1执行第一个脚本
[root@monitor-agent-b1 ~]# sh /server/scripts/domain.sh
wait 3s ...
check https://www.cloudcrowd.com.cn/ is OK
check jd.com is O
3.2 飞书预警展示
3.3 加入定时任务监测
[root@monitor-agent-b1 ~]# crontab -lpo
*/1 * * * * /usr/bin/sh /server/scripts/domain.sh >/dev/null 2>&1po