127 lines
4.8 KiB
Bash
127 lines
4.8 KiB
Bash
#!/bin/bash
|
||
|
||
# 加载 .env 文件中的配置
|
||
if [[ -f .env ]]; then
|
||
source .env
|
||
fi
|
||
# .env 示例
|
||
|
||
# 最大请求时间,单位秒
|
||
#MAX_TIME=10
|
||
# 最大文件大小,单位字节(10MB)
|
||
#MAX_FILESIZE=10485760
|
||
# 连接超时时间,单位秒
|
||
#TIMEOUT=5
|
||
# 钉钉 Webhook Access Token
|
||
#ACCESS_TOKEN="your_access_token_here"
|
||
|
||
# 默认值(如果 .env 文件没有设置这些值,则使用默认值)
|
||
: "${MAX_TIME:=10}" # 默认最大请求时间为 10 秒
|
||
: "${MAX_FILESIZE:=10485760}" # 默认最大文件大小为 10MB
|
||
: "${TIMEOUT:=5}" # 默认连接超时时间为 5 秒
|
||
: "${KEYWORD:="BWH:"}" # 默认连接超时时间为 5 秒
|
||
: "${ACCESS_TOKEN:="b2a92184affebb3f26dfcab1eff9571b46c27a85380fd9ff56ca4c66cf93e1a1"}" # 默认钉钉access_token
|
||
|
||
# 构造完整的Webhook URL
|
||
WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=$ACCESS_TOKEN"
|
||
|
||
# 通用检测和报警函数
|
||
send_alert() {
|
||
local msg=$1
|
||
|
||
# 将 msg 中的花括号 {} 转换为尖括号 <>,"转换为'
|
||
msg=$(echo "$msg" | sed "s/{/</g" | sed "s/}/>/g" | sed "s/\"/'/g")
|
||
# 发送报警信息
|
||
curl -sL "$WEBHOOK" \
|
||
-H 'Content-Type: application/json' \
|
||
-d "{\"msgtype\": \"text\",\"text\": {\"content\": \"$KEYWORD$msg\"}}"
|
||
}
|
||
|
||
# 检测API的函数
|
||
check_api() {
|
||
local api_url=$1
|
||
local expected_keyword=$2
|
||
local result="success" # 默认检测成功
|
||
|
||
# 发送请求并获取响应内容
|
||
local response=$(curl -s -w "\n%{http_code}" --connect-timeout "$TIMEOUT" --max-time "$MAX_TIME" --max-filesize "$MAX_FILESIZE" "$api_url")
|
||
http_code=$(echo "$response" | tail -n1)
|
||
response=$(echo "$response" | head -n-1)
|
||
# 检查curl是否超时或出错
|
||
if [[ $? -ne 0 ]]; then
|
||
result="服务请求失败(可能是网络问题或端口未开放)"
|
||
fi
|
||
|
||
# 检查返回内容中是否包含期望的关键词
|
||
if [[ "$response" != *"$expected_keyword"* ]]; then
|
||
result="服务返回异常内容(不包含期望的关键词 '$expected_keyword'),状态码:$http_code 响应: $response"
|
||
fi
|
||
|
||
# 返回检测结果
|
||
echo "$result"
|
||
}
|
||
|
||
# 主程序
|
||
|
||
# 如果提供了命令行参数(只支持一组 URL 和期望的关键词)
|
||
if [[ $# -eq 2 ]]; then
|
||
api_url="$1"
|
||
expected_keyword="$2"
|
||
|
||
echo "命令行参数提供了 API URL 和期望返回关键词,开始执行检测..."
|
||
|
||
# 调用检测函数并传递参数
|
||
result=$(check_api "$api_url" "$expected_keyword")
|
||
|
||
# 如果检测失败,发送报警
|
||
if [[ "$result" != "success" ]]; then
|
||
send_alert "$result"
|
||
echo "检测失败:$result"
|
||
exit 1
|
||
fi
|
||
else
|
||
# 如果没有提供命令行参数,尝试从文件 api_check_list.txt 读取
|
||
if [[ -f "api_check_list.txt" ]]; then
|
||
echo "从 api_check_list.txt 文件读取检测组合..."
|
||
while IFS=' ' read -r api_url expected_keyword; do
|
||
# 跳过空行或注释行
|
||
if [[ -z "$api_url" || "$api_url" =~ ^# ]]; then
|
||
continue
|
||
fi
|
||
|
||
echo "正在检测 $api_url,期望返回关键词:$expected_keyword"
|
||
|
||
# 调用检测函数并传递参数
|
||
result=$(check_api "$api_url" "$expected_keyword")
|
||
|
||
# 如果检测失败,发送报警
|
||
if [[ "$result" != "success" ]]; then
|
||
send_alert "$result"
|
||
echo "检测失败:$result"
|
||
exit 1
|
||
fi
|
||
done < "api_check_list.txt"
|
||
else
|
||
echo "未提供命令行参数,且未找到 api_check_list.txt 文件。请提供 API 地址和期望关键词,或创建 api_check_list.txt 文件。"
|
||
exit 1
|
||
fi
|
||
fi
|
||
|
||
echo "API 检查通过!"
|
||
|
||
|
||
|
||
# 把这段shell 脚本,转换为go语言的应用。其中:
|
||
# 在go应用中设置自动执行定时器,可根据用户设置进行定时检测。
|
||
# 提供如下接口:
|
||
# 1. check_url:检测url是否正常,返回检测结果。无需发送钉钉webhook警报。
|
||
# 2. add_cron: 添加定时任务,定时检测url是否正常。需要的参数包括:任务名称、url、期望关键词、定时时间(秒)。
|
||
# 3. del_cron: 删除定时任务。需要的参数包括:定时任务ID。
|
||
# 4. get_cron_list: 获取定时任务列表。需要的参数包括:定时任务ID、任务名称、url、期望关键词、定时时间(秒)。
|
||
# 5. config_cron: 配置定时任务。包括配置钉钉的token、keyword、超时时间、最大请求时间等。
|
||
|
||
# 我需要把任务持久化存储在配置文件cron.conf中,格式为:
|
||
# task_id task_name api_url expected_keyword cron_time
|
||
1 "my task" "http://www.example.com" "success" 60
|
||
2 "my task2" "http://www.example2.com" "success" 120
|
||
每次执行add_cron del_cron get_cron_list时,需要读写配置文件cron.conf,并进行相应的操作。 |