侧边栏壁纸
博主头像
福福不服博主等级

孩子会穿过大雨,去懂人间的道理。

  • 累计撰写 92 篇文章
  • 累计创建 98 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

定时检测接口是否正常脚本

Monster
2023-08-17 / 0 评论 / 5 点赞 / 68 阅读 / 4223 字 / 正在检测是否收录...
温馨提示:
请确保在评论和互动中保持礼貌和尊重。避免使用侮辱性、歧视性或攻击性语言。我们鼓励建设性的讨论和意见交流。

定时检测接口是否正常飞书告警脚本

第一版

支持多个接口地址,循环检测,定时每分钟执行一次脚本

告警效果

脚本

#!/bin/bash
URL_LIST=('https://gatewaxxxxxxxxxxxxxxxxxx' 'https://sandbox-gatexxxxxxxxxxxxxxxxxx')

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 "`date +\%Y-\%m-\%d-\%H:\%M:\%S`: $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="API异常警告:Access_failure!,ERROR异常地址:$URL"     #飞书机器人的告警内容
        msg_body="{\"msg_type\":\"text\",\"content\":{\"text\":\"$error_msg\"}}"     #告警消息体格式
        WEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/bd445c52-75xxxxxxxxxx"   #飞书机器人hook地址
        req_msg=$(curl -X POST -H "Content-Type: application/json" -d $msg_body $WEBHOOK_URL)   #post请求

        echo "触发报警:" $req_msg
    fi
done

定时任务crontab -e

*/1 * * * * sh /data/api_url.sh >> /data/apiurl.log

第二版

第一版不够美观,并且目前所有告警都在这一个群没有分散,所以基本所有人都是屏蔽群消息的,基于第一版改进美化并实现艾特人员

告警效果

脚本

#!/bin/bash
URL_LIST=('https://gateway.hkeasyspeed.com/webapi/boxxxxxxxxx' 'https://sandbox-gateway.hkeasyspeed.com/webapxxxxxxxxxxx')
 
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 "`date +\%Y-\%m-\%d-\%H:\%M:\%S`: $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="$URL"
        WEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/bd445c52-7xxxxxxxxxxxx"
        req_msg=$( \
        curl -X POST \
          $WEBHOOK_URL \
          -H 'Content-Type: application/json' \
          -d '{
                "msg_type": "post",
                "content": {
                        "post": {
                                "zh_cn": {
                                        "title": "API\t接口异常",
                                        "content": [
                                                [{
                                                        "tag": "text",
                                                        "text": "地址链接: "
                                                },
                                                {
                                                        "tag": "a",
                                                        "text": "请查看",
                                                        "href": "'$error_msg'"
                                                },
                                                {
                                                        "tag": "at",
                                                        "user_id": "ou_600253ef90xxxxxxxxxxxx"    #@所有人的话直接使用"user_id": "all"
                                                }
                                                ]
                                        ]
                                }
                        }
                }
        }') 

        echo "触发报警:" $req_msg
    fi
done
5
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin
  3. QQ打赏

    qrcode qq

评论区