发布于2025-04-29 阅读(0)
扫一扫,手机访问
API接口监控告警是一种用于确保API接口正常运行并及时发现问题的机制。
监控API接口的性能、可用性和响应时间,及时发送告警通知,有助于快速解决问题,确保系统稳定可靠。
常见的API接口监控告警实现方式包括:
无论采用哪种方式,API接口监控告警都应该具备以下特点:
目前接口服务器为固定设备、固定IP。
首先,我们可以先创建一个接口地址检查的目录以及需要输出的日志文件。
第一步: # mkdir -p /opt/interface-check 第二步: # cd /opt/interface-check/ # touch interface.log
#!/bin/bash #1、不念出口IP查询接口地址 Egress_IP_query=http://111.230.202.64:8000/ #2、不念博客官网 Abstinence_platform=https://www.bunian.cn # 机器人地址(需要根据实际机器人地址配置)【钉钉或企业微信】 WEBHOOK_URL=https://oapi.dingtalk.com/robot/send?access_token=c0ead8f4af7fc4d98382c9d89dca92a0691c6ec57afc01d6c435b6782a60553f # 循环执行检测方法 while : do date=$(date +%Y-%m-%d-%H:%M:%S) #1、院长出口IP查询接口地址检测 Egress_IP_query_status_code=`curl -m 20 -s -o /dev/null -w %{http_code} $Egress_IP_query` if [ "$Egress_IP_query_status_code" -ne 200 ] then curl --location --request POST ${WEBHOOK_URL} --header 'Content-Type: application/json' -d '{"msgtype": "text","text": {"content": "'$date' 不念出口IP查询接口地址-接口连接异常"}}' echo "$date 不念出口IP查询接口地址-接口连接异常" >>/opt/interface-check/interface.log else echo "$date 不念出口IP查询接口地址-接口连接正常" >>/opt/interface-check/interface.log fi #2、不念博客官网检测 Abstinence_platform_status_code=`curl -m 20 -s -o /dev/null -w %{http_code} $Abstinence_platform` if [ "$Abstinence_platform_status_code" -ne 200 ] then curl --location --request POST ${WEBHOOK_URL} --header 'Content-Type: application/json' -d '{"msgtype": "text","text": {"content": "'$date' 不念博客官网-接口连接异常"}}' echo "$date 不念博客官网-接口连接异常" >>/opt/interface-check/interface.log else echo "$date 不念博客官网-接口连接正常" >>/opt/interface-check/interface.log fi exit done
1、需先建一个群,建好后,通过聊天信息找到添加群机器人。
2、新建机器人,填写好机器人名称,保存。
3、添加成功后,创建者可以在机器人详情页看到该机器人特有的 webhookurl,可以按说明 a 向这个地址发起 HTTP POST 请求,即可实现给该群组发送消息。
根据机器人配置说明,可以看到有用 curl 工具往群组推送文本消息的示例:
curl 'webhookurl地址' -H 'Content-Type: application/json' -d ' { "msgtype": "text", "text": { "content": "hello world" } }' curl 'https://oapi.dingtalk.com/robot/send?access_token=c0ead8f4af7fc4d98382c9d89dca92a0691c6ec57afc01d6c435b6782a60553f' -H 'Content-Type: application/json' -d ' { "msgtype": "text", "text": { "content": "hello world" } }'
注意要将 webhookurl 替换成你的机器人 webhook 地址,content 必须是 utf8 编码。
当前自定义机器人支持文本(text)、markdown(markdown)、图片(image)、图文(news)四种消息类型。
机器人的 text/markdown 类型消息支持在 content 中使用 <@userid> 扩展语法来 @群成员。
除此之外,还有消息类型及数据格式(文本类型、markdown类型、图片类型、图文类型、文件类型)模版卡片类型(文本通知模版卡片、图文展示模版卡片)消息发送频率限制、文件上传接口等配置说明,可根据实际需求参考进行配置。
配置无误后,可以先手动验证下脚本的语法是否无误,避免出现语法错误:未预期的文件结尾提示。 脚本中未加 &> /dev/null 相关参数配置,因此在验证脚本时,会有打印信息提示,我们可以放后台运行。
# nohup bash /opt/interface-check/jkdz-check.sh & # more nohup.out % Total% Received % XferdAverage Speed TimeTime TimeCurrent DloadUpload Total SpentLeftSpeed 100 13410027100 107 64254 --:--:-- --:--:-- --:--:-- 254 {"errcode":0,"errmsg":"ok"}% Total% Received % XferdAverage Speed TimeTime TimeCurrent DloadUpload Total SpentLeftSpeed 100 14010027100 113 63267 --:--:-- --:--:-- --:--:-- 267 {"errcode":0,"errmsg":"ok"}% Total% Received % XferdAverage Speed TimeTime TimeCurrent DloadUpload Total SpentLeftSpeed 100 13410027100 107 61244 --:--:-- --:--:-- --:--:-- 245 {"errcode":0,"errmsg":"ok"}% Total% Received % XferdAverage Speed TimeTime TimeCurrent DloadUpload Total SpentLeftSpeed 100 13410027100 107 58232 --:--:-- --:--:-- --:--:-- 232 {"errcode":0,"errmsg":"ok"}% Total% Received % XferdAverage Speed TimeTime TimeCurrent DloadUpload Total SpentLeftSpeed 100 14010027100 113 56235 --:--:-- --:--:-- --:--:-- 235 {"errcode":0,"errmsg":"ok"}% Total% Received % XferdAverage Speed TimeTime TimeCurrent DloadUpload Total SpentLeftSpeed 100 14010027100 113 58244 --:--:-- --:--:-- --:--:-- 244
写个 crontab 定时任务,每 2 小时执行该脚本。
第四步: crontab -e 0 */2 * * * sh /opt/interface-check/jkdz-check.sh
最后,我们可以通过企业微信群查看机器人发送的告警信息,效果如下:
输出的 log 日志情况如下:
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店