05 - 日常运维 SOP

出问题翻 06-troubleshooting.md. 本文是预防性日常.

每天做的事 (~5 分钟)

1. Health check

cd /home/kk/code/project/cs-26spring-final-project

# Backend 还在跑?
pgrep -af "backend/app.py" | grep -v "bash -c"
# 期望: 1 行真返 (PID + cmd)

# Frontend dev (试用阶段) 还在跑?
pgrep -af "vite" | grep -v "bash -c"
# 期望: 1 行真返 (Vite dev server)
# 生产 nginx 用 sudo systemctl status nginx 看

# Backend HTTP 响应
curl -s -o /dev/null -w "HTTP %{http_code}\n" --max-time 5 http://127.0.0.1:5000/api/v1/healthz
# 期望: 200 / 401 / 404 (都算在跑); 000 = 死了重启

# Backend log tail (有无 OPENCLAW timeout / ES 5xx)
tail -50 workspace/system_monitor/logs/backend-verify.log | grep -E "ERROR|Traceback|timeout|5xx" | tail -10
# 期望: 0 行或少量 retry warning 即可

2. 安全 grep (audit 真生产数据)

# 真生产域名 grep (Phase 68.E sanitize 修源头后应 0)
tail -500 workspace/system_monitor/logs/backend-verify.log | grep -cE "deepinstinctweb|gbu\.edu" || echo "0"
# 期望: 0 (backend log 可能含真上游 ES 数据, 累积 0-5 行不算严重, > 50 行说明 sanitize 漏)

# 真 MAC grep (RFC 1918/5737 占位 ✓ 真 MAC ❌)
tail -500 workspace/system_monitor/logs/backend-verify.log | grep -cE "[0-9a-f]{2}(:[0-9a-f]{2}){5}" || echo "0"
# 0 = 无真 MAC; > 0 需看是不是 02:00:00:* 占位 (Phase 67.A.1 sanitize 生成的, 合法)

3. 反馈表汇总 (试用阶段)

# 看运维填了多少
curl -s --max-time 5 http://127.0.0.1:5000/api/v1/log-assistant/feedback/summary \
    -H "Authorization: Bearer <admin_token>" | python3 -m json.tool

# 期望真返: {"useful": N, "needs_more": M, "false_positive": K, ...}
# 试用第 1 周末看是否 N+M+K > 0 (有真反馈)

每周做的事 (~30 分钟)

1. 跑 unittest baseline 验真

cd workspace/system_monitor
PYTHONPATH=. ./backend/.venv/bin/python -m unittest discover test_backup 2>&1 | tail -5
# 期望: Ran 1850 tests in NN.NNs OK (skipped=1)

# 真退 = 真有 phase 改源破回归, 立即:
# 1. git log --oneline 最近 5 commit 看改了啥
# 2. 还原到上一个 PASS 的 commit
# 3. 起 phase 修真凶

2. 跑 R(N) 评估轮 (验真后端真稳定)

cd workspace/system_monitor
./scripts/run_round_evaluation.sh r-week<N>

6 步 pipeline 真跑:

  1. backend health check + mtime 验
  2. capture 4 endpoint
  3. sanitize raw → 脱敏 JSON
  4. git add 真后端 fixture
  5. unittest discover
  6. logger 切片 (logs/r-week<N>_*_logger_slice.log)

末尾出评估 agent 输入清单 5 项, 可以喂给评估 agent 跑 R(N) 评估 (用 acceptance-evaluator-prompt.md 模板).

3. asset_registry 维护

业务系统变动 (新加 / 撤销 / 改名) 时:

vim workspace/system_monitor/backend/assistant/asset_registry.yaml
# 改完不要 commit 这个真值文件 (含真 IP), 改 .example 版同步
vim workspace/system_monitor/backend/assistant/asset_registry.yaml.example

# 重启 backend 让 asset_registry 真生效
# (启动时一次 build_registry, 改 yaml 不重启 = 没生效)
OLD_PID=$(pgrep -f "backend/app.py" | grep -v "bash -c" | head -1)
kill "$OLD_PID" && sleep 3
nohup ./backend/.venv/bin/python backend/app.py > logs/backend-verify.log 2>&1 &
sleep 6
curl -s http://127.0.0.1:5000/api/v1/healthz | head

每月做的事 (~1 小时)

1. backend log rotate

cd workspace/system_monitor
LOG_SIZE_MB=$(du -m logs/backend-verify.log | cut -f1)
echo "backend log: ${LOG_SIZE_MB} MB"

# > 100 MB 就 rotate
if [ "$LOG_SIZE_MB" -gt 100 ]; then
    mv logs/backend-verify.log logs/backend-verify-$(date +%Y%m%d).log.bak
    # backend 自动重新 flush 到新文件 (Flask logger reset)
    # 真要无缝, kill -HUP backend pid 让它 reopen log (Flask 默认无支持, 需要 gunicorn)
fi

2. SQLite DB 备份

cd workspace/system_monitor/backend/diagnose/database
# 真热备 (Flask 跑时也能拷, SQLite WAL 模式安全)
sqlite3 monitor.db ".backup monitor-$(date +%Y%m%d).db.bak"
# 拷到异地
scp monitor-*.db.bak backup-server:/backup/log-assistant/

3. 真后端 fixture 归档 (R 轮累积)

cd workspace/system_monitor/test_backup/fixtures_real_backend
# 看累积多少
ls -la r*.json | wc -l
# 期望: 40+ (R29-R37 35 + 试用 R-week-* 累积)

# > 200 时考虑归档老的 (R29-R30) 到 archive 目录留 git history 不丢
# 留下话给下个学生决

4. 全 git history 真生产数据 grep audit

cd /home/kk/code/project/cs-26spring-final-project
git log --all -p 2>&1 | grep -cE "deepinstinctweb|\.gbu\.edu|sk-[a-zA-Z0-9]{20,}"
# 期望: 0 (Phase 68.E + filter-repo 真清)
# 真返 > 0 = 真泄漏复发, 立即 filter-repo 洗 + 起新 phase 修源头 (类比 Phase 68.E)

每季度做的事 (~2 小时)

1. 依赖升级 audit

# Python
cd workspace/system_monitor
backend/.venv/bin/pip list --outdated | head -20

# Node
cd frontend
npm outdated
# 高危 vuln: npm audit

升级前先跑 unittest baseline, 升级后再跑, 0 退步才 commit.

2. asset_registry 真值 vs 业务真清单 cross-check

# 找 IT 部门要最新的业务系统清单
# 跟 asset_registry.yaml 对照, 有缺补
# 试用阶段反馈最大头: "我说 X 业务受影响, AI 没识别" — 通常是这个文件没更新

3. 安全事件复盘

  • 季度内有无真泄漏发现 (backend log / fixture / commit)?
  • Plugin v0.1.2 phase-prompt-guard 触发统计 (BLOCK 多少次? 救了多少个潜在 exit-patch)
  • bitter-lessons 是否要新加 (e.g. lesson 12)

启停命令清单 (粘到运维办公桌)

# ========== 启 ==========
cd /home/kk/code/project/cs-26spring-final-project/workspace/system_monitor
set -a; source .env.local; set +a
nohup ./backend/.venv/bin/python backend/app.py > logs/backend-verify.log 2>&1 &
cd frontend
nohup npm run dev > ../logs/frontend-dev.log 2>&1 &

# ========== 停 ==========
pkill -f "backend/app.py"
pkill -f "vite"

# ========== 重启 backend (改了 .py 或 .env.local) ==========
OLD_PID=$(pgrep -f "backend/app.py" | grep -v "bash -c" | head -1)
kill "$OLD_PID" && sleep 3
find backend/ -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null   # Phase 69.A 教训
set -a; source .env.local; set +a
nohup ./backend/.venv/bin/python backend/app.py > logs/backend-verify.log 2>&1 &
sleep 6
curl -s http://127.0.0.1:5000/api/v1/healthz

# ========== 看 log ==========
tail -f workspace/system_monitor/logs/backend-verify.log

# ========== 看测试 baseline ==========
cd workspace/system_monitor
PYTHONPATH=. ./backend/.venv/bin/python -m unittest discover test_backup 2>&1 | tail -3

性能基线 (参考)

操作 期望真返时间 异常阈值
Backend HTTP /healthz < 100ms > 500ms 检查 backend 是否 swap
Endpoint A (DNS resolver-chain) 3-5s > 30s 检查 ES + LLM
Endpoint B (DNS multi-round) 30-90s > 180s timeout 前端报错
Endpoint C (DHCP single) 30s > 60s 检查 ES + LLM
Endpoint D (DHCP multi-round) 30s > 90s 同上
Clue 路径 (按线索) 30-120s > 180s timeout (axios 默认)
Unittest discover (1850 test) 30-60s > 120s 真有 hang test
run_round_evaluation.sh 全 6 步 3-5 min > 10 min 真有 ES timeout
Frontend npm run build 2-5s > 30s 真依赖问题

容量基线 (参考)

指标 试用规模 中小规模生产 大规模生产
告警量 < 100/天 < 1k/天 > 1k/天
运维并发 1-5 5-20 > 20
Backend RAM 500 MB 1-2 GB 4+ GB (多 worker)
SQLite DB < 100 MB < 1 GB 换 PostgreSQL
Backend log/月 1 GB 5 GB 20 GB+ (logrotate 必)
LLM 调用/天 < 100 < 1000 > 1000 (考虑 cache)

安全审计 (必须做)

月度 grep audit

详见上文 §每月做的事 §4.

季度全 history grep

cd /home/kk/code/project/cs-26spring-final-project

# 1. API key 真泄漏
git log --all -p 2>&1 | grep -cE "sk-[a-zA-Z0-9]{20,}|Bearer [a-zA-Z0-9]{30,}|password.*=.*[a-zA-Z0-9]{8,}"
# 期望: 0

# 2. 真域名真泄漏 (Phase 68.E + filter-repo 真清)
git log --all -p 2>&1 | grep -cE "deepinstinctweb|\.gbu\.edu"
# 期望: 0

# 3. 真 MAC (除 02:00:00:* 占位)
git log --all -p 2>&1 | grep -cE "^\+.*[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}" | head -3
# 看 commit 是否含真 MAC (02:00:00:* 是合法占位, 其他需 audit)

# 4. RFC 1918 外的真 IP
git log --all -p 2>&1 | grep -oE "^\+.*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | \
    grep -vE "172\.16\.|172\.17-31|192\.168\.|10\.|127\.|0\.0\.|192\.0\.2\." | head -10
# 期望: 0 (除 fixture 内 backend ES 真返的真上游 IP — 这些已 sanitize 成 192.0.2.*)

真泄漏发现立即:

feedback_bitter_lessons_r10_r37 lesson 10 双修:

  1. git filter-repo --replace-text 洗 history (5 commit 真重写 hash)
  2. 起 source-fix phase 修脱敏源头 (类比 Phase 68.E)

详见 06-troubleshooting.md §安全 P0 真泄漏响应.

配置变更 SOP (运维改 ENV)

cd workspace/system_monitor

# 1. 备份 .env.local
cp .env.local .env.local.bak-$(date +%Y%m%d)

# 2. 改
vim .env.local

# 3. 必重启 backend
OLD_PID=$(pgrep -f "backend/app.py" | grep -v "bash -c" | head -1)
kill "$OLD_PID" && sleep 3
set -a; source .env.local; set +a
nohup ./backend/.venv/bin/python backend/app.py > logs/backend-verify.log 2>&1 &
sleep 6

# 4. 验
curl -s http://127.0.0.1:5000/api/v1/healthz
tail -20 logs/backend-verify.log | grep -E "ERROR|sys.exit"
# 期望: 0 sys.exit (真有说明缺关键 env)

# 5. 失败回滚
# kill 新 backend, cp .env.local.bak-XXX .env.local, 重起

跑评估轮 SOP

cd workspace/system_monitor
./scripts/run_round_evaluation.sh r<round_num>
# 例: r-trial-week1 / r38 / r-prod-monthly-202607

# 6 步 pipeline:
# Step 1: backend health + mtime 验 (phase 改了源必重启)
# Step 2: capture 4 endpoint (按 evaluator-query-design.md §2 钉死的固定 payload)
# Step 3: sanitize raw → 脱敏 JSON
# Step 4: 删 raw + git add 脱敏版
# Step 5: unittest discover (验 sentinel + ban list)
# Step 6: logger 切片 (留 audit trail)

# 末尾出"评估 agent 输入清单 5 项"

详见 07-extension-guide.md §加 phase 评估真凶.


下一步: 06-troubleshooting.md (真排障手册, 5 类紧急 + degraded_reason 真表)