From 44cc66b1698348d3b2ca4f73df0203f7978d752d Mon Sep 17 00:00:00 2001 From: cfdaily Date: Tue, 2 Jun 2026 22:04:41 +0800 Subject: [PATCH] auto-sync: 2026-06-02 22:04:41 --- scripts/gateway-watchdog.sh | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/scripts/gateway-watchdog.sh b/scripts/gateway-watchdog.sh index fe06e25..65b7379 100755 --- a/scripts/gateway-watchdog.sh +++ b/scripts/gateway-watchdog.sh @@ -139,6 +139,17 @@ with open('${LOG_FILE}') as f: " 2>/dev/null } +# 辅助函数:安全计数(从管道输入 grep 后取整数值) +_safe_count() { + # 接受 grep pattern,对 stdin 计数,返回纯整数 + local count + count=$(grep -c "$1" 2>/dev/null || true) + # 去掉所有空白和换行,确保是纯数字 + count=$(echo "$count" | tr -d '[:space:]') + [ -z "$count" ] && count=0 + echo "$count" +} + # 统计各规则命中次数 # 用全局变量返回:R1_COUNT, R2_COUNT, R3_COUNT count_rules() { @@ -152,20 +163,14 @@ count_rules() { return fi - # R1: 含 "lane task error" 且含 "FailoverError" - R1_COUNT=$(echo "$recent_lines" | grep -c "lane task error" 2>/dev/null | tr -d ' ' || echo 0) - if [ "$R1_COUNT" -gt 0 ]; then - R1_COUNT=$(echo "$recent_lines" | grep "lane task error" | grep -c "FailoverError" 2>/dev/null | tr -d ' ' || echo 0) - fi + # R1: 含 "lane task error" 且含 "FailoverError"(两步过滤) + R1_COUNT=$(echo "$recent_lines" | grep "lane task error" | _safe_count "FailoverError") - # R2: 含 "stalled session" 且含 "recovery=none" - R2_COUNT=$(echo "$recent_lines" | grep -c "stalled session" 2>/dev/null | tr -d ' ' || echo 0) - if [ "$R2_COUNT" -gt 0 ]; then - R2_COUNT=$(echo "$recent_lines" | grep "stalled session" | grep -c "recovery=none" 2>/dev/null | tr -d ' ' || echo 0) - fi + # R2: 含 "stalled session" 且含 "recovery=none"(两步过滤) + R2_COUNT=$(echo "$recent_lines" | grep "stalled session" | _safe_count "recovery=none") - # R3: 含 "rate_limit" 或含 "429" - R3_COUNT=$(echo "$recent_lines" | grep -c "rate_limit\|\"429\"" 2>/dev/null | tr -d ' ' || echo 0) + # R3: 含 "rate_limit" 或含 "429"(匹配 JSON 中 rate_limit 关键字或 429 错误码) + R3_COUNT=$(echo "$recent_lines" | _safe_count 'rate_limit|429') } # === 主逻辑 ===