Update CF-Under-Attack.sh

This commit is contained in:
科技lion 2024-06-30 14:07:53 +08:00 committed by GitHub
parent 2d650e94e6
commit f02b7b8d59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,12 +6,23 @@ API_KEY="BBBB"
ZONE_ID="CCCC" ZONE_ID="CCCC"
LOAD_THRESHOLD=5.0 # 设置高负载阈值 LOAD_THRESHOLD=5.0 # 设置高负载阈值


TELEGRAM_BOT_TOKEN="输入TG机器人API"
CHAT_ID="输入TG用户ID"



# 获取当前系统负载 # 获取当前系统负载
CURRENT_LOAD=$(uptime | awk -F'load average:' '{ print $2 }' | cut -d, -f1 | awk '{print $1}') CURRENT_LOAD=$(uptime | awk -F'load average:' '{ print $2 }' | cut -d, -f1 | awk '{print $1}')


echo "当前系统负载: $CURRENT_LOAD" echo "当前系统负载: $CURRENT_LOAD"



send_tg_notification() {
local MESSAGE=$1
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" -d "chat_id=$CHAT_ID" -d "text=$MESSAGE"
}



# 获取当前的“Under Attack”模式状态 # 获取当前的“Under Attack”模式状态
STATUS=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/settings/security_level" \ STATUS=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/settings/security_level" \
-H "X-Auth-Email: $EMAIL" \ -H "X-Auth-Email: $EMAIL" \
@ -24,6 +35,7 @@ echo "当前的Under Attack模式状态: $STATUS"
if (( $(echo "$CURRENT_LOAD > $LOAD_THRESHOLD" | bc -l) )); then if (( $(echo "$CURRENT_LOAD > $LOAD_THRESHOLD" | bc -l) )); then
if [ "$STATUS" != "under_attack" ]; then if [ "$STATUS" != "under_attack" ]; then
echo "系统负载高于阈值开启Under Attack模式" echo "系统负载高于阈值开启Under Attack模式"
# send_tg_notification "系统负载高于阈值开启Under Attack模式"
NEW_STATUS="under_attack" NEW_STATUS="under_attack"
else else
echo "系统负载高但Under Attack模式已经开启" echo "系统负载高但Under Attack模式已经开启"
@ -32,6 +44,7 @@ if (( $(echo "$CURRENT_LOAD > $LOAD_THRESHOLD" | bc -l) )); then
else else
if [ "$STATUS" == "under_attack" ]; then if [ "$STATUS" == "under_attack" ]; then
echo "系统负载低于阈值关闭Under Attack模式" echo "系统负载低于阈值关闭Under Attack模式"
# send_tg_notification "系统负载低于阈值关闭Under Attack模式"
NEW_STATUS="high" NEW_STATUS="high"
else else
echo "系统负载低Under Attack模式已经关闭" echo "系统负载低Under Attack模式已经关闭"