# -*- coding: utf-8 -*- r'''看一眼 Lightsail 上中转服务器的完整状态。 为什么写成文件:用 `python -c @"..."@` 传带 `$(...)` 和引号的远端命令, PowerShell 会做变量插值、还会在本机执行 `$(systemctl ...)` (报 "无法将 systemctl 项识别为 cmdlet")。凡是超过两行的远端命令, 一律写成文件跑。 用法: python _build/check_lightsail.py python _build/check_lightsail.py --fix # 顺便修文件属主 ''' from __future__ import annotations import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "_build")) from lssh import run # noqa: E402 PORT = "13245" PUBLIC = "http://13.54.30.196:%s" % PORT RELAY_DIR = "/opt/ykxmz-relay" REMOTE = r''' R=__DIR__ P=__PORT__ echo "════ 服务 ════" for s in ykxmz-relay ykxmz-monitor.timer; do printf ' %-24s %s\n' "$s" "$(systemctl is-active $s 2>&1)" done echo echo "════ 健康 ════" printf ' %-24s %s\n' "本机 health" "$(curl -s -o /dev/null -w '%{http_code}' --max-time 8 http://127.0.0.1:$P/v1/health)" printf ' %-24s %s\n' "公网 health" "$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 __PUBLIC__/v1/health)" printf ' %-24s %s\n' "公网首页" "$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 __PUBLIC__/)" printf ' %-24s %s\n' "本机延迟" "$(curl -s -o /dev/null -w '%{time_total}s' --max-time 8 http://127.0.0.1:$P/v1/health)" printf ' %-24s %s\n' "公网延迟" "$(curl -s -o /dev/null -w '%{time_total}s' --max-time 15 __PUBLIC__/v1/health)" echo echo "════ 首页角标 ════" echo " 最新版: $(curl -s --max-time 10 http://127.0.0.1:$P/ | grep -o '最新版 [0-9.]*' | head -1)" echo echo "════ 监听 / 进程 ════" ss -tlnp 2>/dev/null | grep ":$P " | sed 's/^/ /' ps -eo pid,rss,etime,cmd --sort=-rss 2>/dev/null | grep 'relay\.py' | grep -v grep | head -2 | sed 's/^/ /' echo echo "════ 监测单元 ════" cat $R/monitor_state.txt 2>/dev/null | sed 's/^/ /' echo " --- monitor.log 尾部 ---" tail -4 $R/monitor.log 2>/dev/null | sed 's/^/ /' echo echo "════ 抢救报告 ════" echo " 共 $(ls $R/rescue_reports 2>/dev/null | wc -l) 份" ls -la $R/rescue_reports/ 2>/dev/null | tail -n +4 | awk '{print " " $6" "$7" "$8" "$9}' echo echo "════ 知识库 ════" if [ -f $R/rescue_knowledge.md ]; then echo " $(wc -l < $R/rescue_knowledge.md) 行" else echo " (还没有)" fi echo echo "════ 资源 ════" free -m 2>/dev/null | sed -n 2p | awk '{printf " 内存 %s/%s MB\n", $3, $2}' df -h / | tail -1 | awk '{printf " 磁盘 %s 用 %s\n", $2, $5}' uptime | sed 's/^/ /' echo echo "════ 定时器下次触发 ════" systemctl list-timers ykxmz-monitor.timer --no-pager 2>/dev/null | head -3 | sed 's/^/ /' echo echo "════ 文件属主(admin 要能写日志)════" ls -la $R/*.log $R/monitor_state.txt 2>/dev/null | awk '{print " " $3":"$4" "$9}' ''' def main() -> int: if "--fix" in sys.argv: print(" ── 修属主 ──") out = run("sudo chown -R admin:admin %s && sudo chmod 755 %s && echo OK" % (RELAY_DIR, RELAY_DIR), timeout=120) print(" " + out[1].strip()) cmd = (REMOTE.replace("__DIR__", RELAY_DIR) .replace("__PORT__", PORT) .replace("__PUBLIC__", PUBLIC)) out = run(cmd, timeout=300, quiet=True) print(out[1] if isinstance(out, tuple) else out) return 0 if __name__ == "__main__": sys.exit(main())