#!/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/^/ /'