check_web_status_网址状态检查.sh

check_web_status_网址状态检查.sh

#!/bin/bash
# 检测网页是否正常 状态码是否为200
# 使用Telegram发送异常主机
# Telegram 需要申请机器人 具体请参阅zabbix 文档有关telegram的讲解
# 复制到/data/shell/目录下 mkdir -p /data/shell/web_url.txt 创建域名文件
# web_url.txt 被检测的网址放到里面即可
# www.baidu.com 
# www.qq.com
# 不加参数 执行 函数aa中的命令  加任何参数则执行 函数bb中的命令
# crontab -e 里面添加 01 20 * * * /data/shell/check_web_status.sh aaaa & \\ 每天晚上8点 发送所有
# crontab -e 里面添加 */10 * * * * /data/shell/check_web_status.sh &      \\ 每十分钟检测发送异常网址

Token='1717617034:AAGLvDIL_0CVu7QyTCDpyGxUHtIMFue7Huo9'
ChatID='-10014439319089'
source /etc/profile

function aa {
while read line
do
    web_status=`curl -L -I --connect-timeout 20 -m 30 -o /dev/null -s -w %{http_code}"\n" $line`

    if [ $web_status -eq 200 ];then
	true
	#curl -X GET "https://api.telegram.org/bot$Token/sendMessage" -d "chat_id=$ChatID &text=$line 网址状态正常"	
	else
	curl -X GET "https://api.telegram.org/bot$Token/sendMessage" -d "chat_id=$ChatID &text=$line 不能正常访问"
    fi

done < /data/shell/web_url.txt

}

function bb {

while read line
do
    web_status=`curl -L -I --connect-timeout 20 -m 30 -o /dev/null -s -w %{http_code}"\n" $line`

    if [ $web_status -eq 200 ];then
	curl -X GET "https://api.telegram.org/bot$Token/sendMessage" -d "chat_id=$ChatID &text=$line 网址状态正常"	
	else
	curl -X GET "https://api.telegram.org/bot$Token/sendMessage" -d "chat_id=$ChatID &text=$line 不能正常访问"
    fi

done < /data/shell/web_url.txt

curl -X GET "https://api.telegram.org/bot$Token/sendMessage" -d "chat_id=$ChatID &text=所有域名状态检查完毕"

}

if [ -z $1 ];then
    echo "aaa"
    aa
else
    echo "bbb"
    bb
fi



Teo

You must be logged in to post a comment