# -*- coding: utf-8 -*- r'''看一眼中转服务器的完整状态。 为什么写成文件:这一轮我用 `python -c @"..."@` 传长命令失败了好几次 —— PowerShell 的双引号 heredoc 会做变量插值、还会把 `$(...)` 当子表达式 在本机执行(systemctl / iptables 跑到 Windows 上了)。凡是超过两行 的远端命令,一律写成文件跑。 用法: python _build/check_device.py ''' from __future__ import annotations import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "_build")) from devssh import run # noqa: E402 REMOTE = r''' RELAY=/root/lanremote-relay echo "════ 服务 ════" for s in lanremote-relay lanremote-tunnel lanremote-tunnel-h2 lanremote-nettune; do printf ' %-24s %s\n' "$s" "$(systemctl is-active $s 2>&1)" done printf ' %-24s %s\n' "monitor.timer" "$(systemctl is-active lanremote-monitor.timer 2>&1)" printf ' %-24s %s\n' "watchdog.timer" "$(systemctl is-active lanremote-watchdog.timer 2>&1)" echo echo "════ 网络 ════" printf ' %-24s %s\n' "wlan0 MTU" "$(cat /sys/class/net/wlan0/mtu 2>/dev/null)" printf ' %-24s %s\n' "MSS 规则" "$(iptables -t mangle -L -n 2>/dev/null | grep -ci mss) 条" printf ' %-24s %s\n' "tcp_mtu_probing" "$(cat /proc/sys/net/ipv4/tcp_mtu_probing 2>/dev/null)" printf ' %-24s %s\n' "默认路由" "$(ip route show default 2>/dev/null | head -1)" echo echo "════ 内存 / 运行时长 ════" printf ' %-24s %s\n' "运行时长" "$(uptime -p 2>/dev/null)" free -m 2>/dev/null | sed -n '2p' | awk '{printf " %-24s %s/%s MB\n", "内存 used/total", $3, $2}' echo echo "════ 隧道连接数 ════" for p in 20241 20242; do n=$(curl -s --max-time 5 "http://127.0.0.1:$p/metrics" 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}' | head -1) printf ' %-24s %s\n' "指标端口 $p" "${n:-拿不到}" done echo echo "════ 公网自测 ════" printf ' %-24s %s\n' "公网 /v1/health" "$(curl -s -o /dev/null -w '%{http_code} (%{time_total}s)' --max-time 15 https://relay.xxmxxs.top/v1/health 2>/dev/null)" printf ' %-24s %s\n' "本机 /v1/health" "$(curl -s -o /dev/null -w '%{http_code}' --max-time 6 http://127.0.0.1:8080/v1/health 2>/dev/null)" printf ' %-24s %s\n' "公网首页" "$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 https://relay.xxmxxs.top/ 2>/dev/null)" printf ' %-24s %s\n' "后台抢救报告路由" "$(curl -s -o /dev/null -w '%{http_code}' --max-time 6 http://127.0.0.1:8080/rescue/none.txt 2>/dev/null)" echo echo "════ 监测单元状态 ════" cat $RELAY/monitor_state.txt 2>/dev/null | sed 's/^/ /' || echo " (还没有状态文件)" echo " ── monitor.log 尾部 ──" tail -5 $RELAY/monitor.log 2>/dev/null | sed 's/^/ /' || echo " (还没有日志)" echo echo "════ 抢救报告 ════" n=$(ls $RELAY/rescue_reports 2>/dev/null | wc -l) echo " 共 $n 份" ls -la $RELAY/rescue_reports/ 2>/dev/null | tail -n +4 | awk '{printf " %s %s\n", $6" "$7" "$8, $9}' echo echo "════ 知识库 ════" if [ -f $RELAY/rescue_knowledge.md ]; then echo " 行数:$(wc -l < $RELAY/rescue_knowledge.md)" tail -6 $RELAY/rescue_knowledge.md | sed 's/^/ /' else echo " (没有)" fi echo echo "════ 定时器下次触发 ════" systemctl list-timers lanremote-monitor.timer lanremote-watchdog.timer --no-pager 2>/dev/null | head -4 | sed 's/^/ /' echo echo "════ 后台页有没有「抢救报告」分区 ════" printf ' 匹配次数: %s\n' "$(curl -s --max-time 8 http://127.0.0.1:8080/zhongzhuanzhan 2>/dev/null | grep -c '抢救报告')" ''' def main() -> int: out = run(REMOTE, timeout=300) text = out[1] if isinstance(out, tuple) else str(out) print(text) return 0 if __name__ == "__main__": sys.exit(main())