Files
ykxmz/_build/diag_502_now.sh
T
xmz7891 2b204cae07 修看门狗重启死循环(我上一版改过头了)
用户报 502 Bad Gateway,Cloudflare 的图显示
「Host: relay.xxmxxs.top - Error」,也就是源站/隧道这一跳挂了。

查 watchdog 日志,发现它在**每 50 秒重启一次隧道**:
    [10:31:57] 只剩 1 条连接 -> 重启隧道
    [10:32:22] 重启完
    [10:32:24] 又只剩 1 条 -> 距上次才 27 秒,先不重启
    [10:32:47] 距上次 50 秒 -> 又重启
    ... 循环不停

根因(我上一版的错):
  隧道在**质量差的 4G 上重建 4 条连接要 30~60 秒**。
  而我上一版把 MIN_RESTART_GAP 设成 45 秒,并且
  "公网不通 + 连接数<=1"就立刻重启 ——
  结果每次隧道还没把连接建齐,就被下一次重启打断,
  **永远停在"只剩 1 条"的状态**。
  等于我自己制造了不稳定。

修法:
  1. 重启后给 GRACE=150 秒宽限期,这期间绝不重启,
     让 cloudflared 专心把连接建齐
  2. MIN_RESTART_GAP 从 45 提到 150(和宽限期一致)
  3. "立刻重启"的判定加一条:距上次重启必须超过宽限期

实测(停掉 watchdog -> 手动重启隧道 -> 观察):
    10 秒内建齐 4 条连接,公网立刻 200
    宽限期生效后公网 20/20 = 100%,延迟 ~690ms

顺带一个教训:之前测出 80%、20% 的 000,
是我自己后台还跑着一个观察任务在并发打接口造成的干扰。
**测稳定性的时候,测量工具本身不能成为干扰源。**
2026-10-07 10:37:26 +08:00

67 lines
3.3 KiB
Bash

#!/bin/bash
# 502 Bad Gateway —— Cloudflare 说源站报错。立刻查 relay。
set -u
echo "════════ 1. relay 还活着吗 ════════"
echo " 服务: $(systemctl is-active lanremote-relay)"
echo " 进程: $(pgrep -c -f 'relay.py' 2>/dev/null || echo 0) 个"
echo " 端口: $(ss -lntp 2>/dev/null | grep -c ':8080')"
echo " 本机 HTTP: $(curl -s -o /dev/null -w '%{http_code}' --max-time 8 http://127.0.0.1:8080/v1/health)"
echo " 官网本机: $(curl -s -o /dev/null -w '%{http_code}' --max-time 8 http://127.0.0.1:8080/)"
echo
echo "════════ 2. relay 日志最后的错误 ════════"
tail -60 /root/lanremote-relay/relay.log 2>/dev/null | tail -30 | sed 's/^/ /'
echo
echo "════════ 3. journal 里的 relay 错误 ════════"
journalctl -u lanremote-relay --since '20 minutes ago' --no-pager 2>/dev/null \
| grep -iE 'error|traceback|exception|killed|oom|restart' | tail -12 | sed 's/^/ /'
echo " (没输出=没有明显错误)"
echo
echo "════════ 4. relay 重启过几次 ════════"
journalctl -u lanremote-relay --since '30 minutes ago' --no-pager 2>/dev/null \
| grep -cE 'Started|Stopped' | sed 's/^/ 事件数: /'
journalctl -u lanremote-relay --since '30 minutes ago' --no-pager 2>/dev/null \
| grep -E 'Started|Stopped|Deactivated|killed' | tail -8 | sed 's/^/ /'
echo
echo "════════ 5. 内存(OOM 会把 relay 干掉)════════"
free -m | sed 's/^/ /'
echo " relay 内存: $(ps -o rss= -p $(pgrep -f 'relay.py' | head -1) 2>/dev/null | awk '{printf "%d MB", $1/1024}')"
systemctl show lanremote-relay -p MemoryCurrent -p MemoryMax --value 2>/dev/null | sed 's/^/ /'
echo
echo "════════ 6. 隧道连接数 + 落点 ════════"
echo " 连接: $(curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}')"
grep -E 'Registered tunnel connection' /root/lanremote-relay/tunnel.log 2>/dev/null | tail -4 \
| grep -oE 'location=[a-z0-9]+ protocol=[a-z0-9]+' | sed 's/^/ /'
echo
echo "════════ 7. cloudflared 有没有报回源错误 ════════"
tail -200 /root/lanremote-relay/tunnel.log 2>/dev/null \
| grep -iE 'origin|502|refused|dial tcp 127|connection refused|no route' | tail -10 | sed 's/^/ /'
echo " (没输出=cloudflared 没报回源错)"
echo
echo "════════ 8. 公网连打 12 次 ════════"
ok=0
for i in $(seq 1 12); do
c=$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 \
-H 'User-Agent: LanRemote/1.4.0' https://relay.xxmxxs.top/)
[ "$c" = "200" ] && ok=$((ok+1))
printf " %2d: %s\n" "$i" "$c"
sleep 1
done
echo " 可用 $ok/12"
echo
echo "════════ 9. 当前还在跑的长轮询(会不会占满线程)════════"
ss -tn state established '( sport = :8080 )' 2>/dev/null | wc -l | sed 's/^/ 到 8080 的连接数: /'
ps -o nlwp= -p $(pgrep -f 'relay.py' | head -1) 2>/dev/null | sed 's/^/ relay 线程数: /'
echo
echo "════════ 10. watchdog 最近 ════════"
tail -10 /root/lanremote-relay/watchdog.log 2>/dev/null | sed 's/^/ /'