#!/bin/bash # 长轮询为什么跑着跑着就全挂? # # 实测:60 次长轮询,前 18 次成功,之后全失败(502)。 # 每次耗时 15.6 秒(长轮询 hold 15 秒)。 # # 查:隧道是不是在这期间死了 / 连接数掉了 / 看门狗干了什么。 set -u RELAY_DIR=/root/lanremote-relay URL=https://relay.xxmxxs.top echo "===== 1. 服务 =====" for s in lanremote-relay lanremote-tunnel lanremote-watchdog.timer; do printf " %-26s %s\n" "$s" "$(systemctl is-active "$s" 2>/dev/null)" done echo echo "===== 2. 隧道连接数 / 注册 / 断开 =====" echo -n " ha_connections = " curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}' echo " 注册 $(grep -c 'Registered tunnel connection' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) 次" echo " 断开 $(grep -c 'Connection terminated' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) 次" echo " Lost $(grep -c 'Lost connection with the edge' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) 次" echo " 502 $(grep -ci '502\|bad gateway' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) 次" echo echo "===== 3. 隧道日志最近 15 行 =====" tail -15 "$RELAY_DIR/tunnel.log" 2>/dev/null | sed 's/^/ /' echo echo "===== 4. 看门狗最近 8 条 =====" tail -8 "$RELAY_DIR/watchdog.log" 2>/dev/null | sed 's/^/ /' echo echo "===== 5. relay 收到的请求数 / 内存 =====" curl -s --max-time 8 http://127.0.0.1:8080/v1/health 2>/dev/null \ | python3 -c 'import json,sys; d=json.load(sys.stdin); print(f" totalRequests={d[\"totalRequests\"]} memory={d[\"memoryKB\"]//1024}MB uptime={d[\"uptimeSeconds\"]}s")' \ 2>/dev/null || echo " 读不到" echo echo "===== 6. relay 的线程数(长轮询会不会堆线程)=====" PID=$(systemctl show lanremote-relay -p MainPID --value 2>/dev/null) if [ -n "$PID" ] && [ "$PID" != "0" ]; then echo " PID=$PID 线程数=$(ls /proc/$PID/task 2>/dev/null | wc -l)" echo " 打开的文件数=$(ls /proc/$PID/fd 2>/dev/null | wc -l)" else echo " 拿不到 PID" fi echo echo "===== 7. 现在公网(10 次)=====" ok=0 for i in $(seq 1 10); do c=$(timeout 15 curl -4 -s -o /dev/null -w '%{http_code}' \ -H 'User-Agent: LanRemote/1.4.0' "$URL/v1/health" 2>/dev/null) printf "%s " "${c:-000}" [ "$c" = "200" ] && ok=$((ok+1)) sleep 1 done echo echo " 公网 $ok/10"