wiki/运维/操作系统/Linux/脚本/HTTTP探测脚本.md
2025-01-02 10:46:09 +08:00

1.2 KiB
Raw Permalink Blame History

title, categories, tags, date
title categories tags date
HTTTP探测脚本
Shell
脚本
2023-05-23 23:45:25

HTTTP探测脚本

#!/bin/bash

# 设置探测目标和日志文件路径
target="网址"
log_file="/var/log/ccps_probe.log"

# 设置企业微信机器人的webhook地址和异常通知消息
webhook_url="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的企微机器人KEY"
error_message="xxx网站 探测失败!!! HTTP status code is not 200 for 3 consecutive times."

# 循环探测
while true
do
    # 发送https请求并记录响应状态码
    status_code=$(curl -o /dev/null -s -w "%{http_code}\n" $target)

    # 记录探测日志
    echo "$(date +"%Y-%m-%d %H:%M:%S") - HTTP status code: $status_code" >> $log_file

    # 如果状态码不是200计数器加1
    if [ $status_code -ne 200 ]; then
        count=$((count+1))
    else
        count=0
    fi

    # 如果计数器达到3发送异常通知
    if [ $count -eq 3 ]; then
        curl -H "Content-Type: application/json" -d "{\"msgtype\": \"text\", \"text\": {\"content\": \"$error_message\"}}" $webhook_url
        count=0
    fi

    # 等待10秒后再次探测
    sleep 10
done