diff --git a/docs/audit/2026-06-13-ssh-watch-probe-quote-fix.md b/docs/audit/2026-06-13-ssh-watch-probe-quote-fix.md new file mode 100644 index 00000000..0d7e10e5 --- /dev/null +++ b/docs/audit/2026-06-13-ssh-watch-probe-quote-fix.md @@ -0,0 +1,28 @@ +# 审计 — SSH 监测探针引号修复 + +**Changelog**: `docs/changelog/2026-06-13-ssh-watch-probe-quote-fix.md` + +## 审计范围 + +| 文件 | 变更 | 状态 | +|------|------|------| +| `server/infrastructure/ssh/remote_probe.py` | `strip(chr(34))` | ☑ | +| `tests/test_watch_metrics.py` | 探针脚本回归 | ☑ | + +## Step 3 规则扫描 + +| H | 规则 | 结论 | +|---|------|------| +| H1 | 无新攻击面 | SAFE | +| H2 | 恢复监测可用性 | SAFE — 热修复 | + +## Closure + +| H | 判定 | 依据 | +|---|------|------| +| H1–H2 | SAFE | 本地 bash 执行探针返回 status ok | + +## DoD + +- [x] `test_ssh_watch_metrics_cmd_runs_locally` passed +- [x] changelog / audit diff --git a/docs/changelog/2026-06-13-ssh-watch-probe-quote-fix.md b/docs/changelog/2026-06-13-ssh-watch-probe-quote-fix.md new file mode 100644 index 00000000..2ac29294 --- /dev/null +++ b/docs/changelog/2026-06-13-ssh-watch-probe-quote-fix.md @@ -0,0 +1,22 @@ +# 修复 SSH 监测探针脚本语法错误 + +**日期**:2026-06-13 + +## 变更摘要 + +修复 `SSH_WATCH_METRICS_CMD` 中 `.strip('"')` 破坏 bash 双引号包裹导致探针全部 `ssh_probe_failed`、实时监测不可用的问题。 + +## 动机 + +硬件详情上线后监测插槽无数据;根因为嵌入脚本的 `"` 提前结束 `python3 -c "..."` 字符串。 + +## 涉及文件 + +- `server/infrastructure/ssh/remote_probe.py` — 改用 `strip(chr(34))` +- `tests/test_watch_metrics.py` — 本地执行探针脚本回归 + +## 验证 + +```bash +.venv/bin/pytest tests/test_watch_metrics.py::test_ssh_watch_metrics_cmd_runs_locally -q +``` diff --git a/server/infrastructure/ssh/remote_probe.py b/server/infrastructure/ssh/remote_probe.py index d05e29d0..f7c37ea2 100644 --- a/server/infrastructure/ssh/remote_probe.py +++ b/server/infrastructure/ssh/remote_probe.py @@ -99,7 +99,7 @@ try: try: for _line in open('/etc/os-release'): if _line.startswith('PRETTY_NAME='): - os_release = _line.split('=', 1)[1].strip().strip('"')[:120] + os_release = _line.split('=', 1)[1].strip().strip(chr(34))[:120] break except Exception: pass @@ -189,7 +189,7 @@ try: try: for _line in open('/etc/os-release'): if _line.startswith('PRETTY_NAME='): - os_release = _line.split('=', 1)[1].strip().strip('"')[:120] + os_release = _line.split('=', 1)[1].strip().strip(chr(34))[:120] break except Exception: pass diff --git a/tests/test_watch_metrics.py b/tests/test_watch_metrics.py index cf28e074..e3055659 100644 --- a/tests/test_watch_metrics.py +++ b/tests/test_watch_metrics.py @@ -202,6 +202,23 @@ def test_merge_redis_and_ssh_prefers_ssh_io(): assert merged.net_bytes_sent == 99 +def test_ssh_watch_metrics_cmd_runs_locally(): + import subprocess + + from server.infrastructure.ssh.remote_probe import SSH_WATCH_METRICS_CMD, _parse_json_stdout + + result = subprocess.run( + ["bash", "-c", SSH_WATCH_METRICS_CMD], + capture_output=True, + text=True, + timeout=20, + ) + parsed = _parse_json_stdout(result.stdout) + assert parsed is not None + assert parsed.get("status") == "ok" + assert parsed.get("cpu_cores") is not None + + def test_normalize_watch_ttl_minutes(): assert normalize_watch_ttl_minutes(30) == 30 assert normalize_watch_ttl_minutes(60) == 60