#!/usr/bin/env bash # 운영 스모크 — GET 전용, API 키 없이 호출. KIS/주문 경로 호출 없음. # - PUBLIC : 키 없이 200 + 유효 JSON 이어야 한다 (공개 시장 데이터) # - AUTH : 키 없이 401 이어야 한다 (계좌·손익·리스크 상태). 200 이면 **노출 회귀**로 FAIL, # 5xx/000 이면 기동 실패로 FAIL. (2026-10-02 H-1: journals/analysis 가 키 없이 응답하던 결함) # 사용: .claude/scripts/prod_smoke.sh (환경변수 PROD_BASE_URL 로 대상 변경 가능) set -u BASE="${PROD_BASE_URL:-https://stock.shium.net}" PUBLIC_ENDPOINTS=( "/api/v1/market/indices" ) AUTH_ENDPOINTS=( "/api/v1/journals/stats" "/api/v1/analysis/performance" "/api/v1/market-intelligence/dashboard" ) fail=0 echo "== 운영 스모크 ($BASE) $(TZ=Asia/Seoul date '+%Y-%m-%d %H:%M:%S KST') ==" probe() { # $1=kind(PUBLIC|AUTH) $2=endpoint local kind="$1" ep="$2" tmp out code secs bytes valid mark tmp="$(mktemp)" out="$(curl -s -o "$tmp" -w '%{http_code} %{time_total}' --max-time 15 "$BASE$ep" 2>/dev/null)" || out="000 0" code="${out%% *}"; secs="${out##* }" bytes="$(wc -c < "$tmp" | tr -d ' ')" valid="-" if [ "$kind" = "PUBLIC" ]; then if command -v python3 >/dev/null 2>&1 && [ "$code" = "200" ]; then python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$tmp" 2>/dev/null && valid="json-ok" || valid="json-INVALID" fi if [ "$code" = "200" ] && [ "$valid" != "json-INVALID" ]; then mark="OK "; else mark="FAIL"; fail=1; fi else if [ "$code" = "401" ]; then mark="OK "; valid="auth-enforced" elif [ "$code" = "200" ]; then mark="FAIL"; valid="LEAK(키 없이 200)"; fail=1 else mark="FAIL"; fail=1; fi fi printf '%s %-6s %s http=%s %ss %sB %s\n' "$mark" "$kind" "$ep" "$code" "$secs" "$bytes" "$valid" if [ "$mark" = "FAIL" ] && [ "$code" != "200" ]; then head -c 200 "$tmp" | sed 's/^/ /'; echo; fi rm -f "$tmp" } for ep in "${PUBLIC_ENDPOINTS[@]}"; do probe PUBLIC "$ep"; done for ep in "${AUTH_ENDPOINTS[@]}"; do probe AUTH "$ep"; done # /health 심층 확인 — 컨테이너 healthcheck 는 HTTP 200 만 보므로 스케줄러·db_worker 사망을 못 잡는다. # 비JSON(프록시 미노출 추정)은 WARN. 배포 회귀 신호(스케줄러 사망·stale, db_worker 사망, db/redis error)만 FAIL. # status=degraded 와 degraded_reasons(KRX 미수집·MDD blind·국면 차단·KIS 스트림·DLQ manual 등 운영성 사유)는 # 배포와 무관할 수 있으므로 WARN 으로만 출력한다. hbody="$(curl -s --max-time 15 "$BASE/health" 2>/dev/null || true)" if command -v python3 >/dev/null 2>&1; then hres="$(printf '%s' "$hbody" | python3 -c ' import json,sys try: h=json.load(sys.stdin) except Exception: print("NOJSON"); sys.exit(0) if not isinstance(h,dict) or "status" not in h: print("NOJSON"); sys.exit(0) p=[]; w=[] if h.get("status") not in ("healthy","degraded"): p.append("status="+str(h.get("status"))) for k in ("db","redis"): if k in h and h[k]!="ok": p.append(k+" "+str(h[k])) for n,v in (h.get("schedulers") or {}).items(): if not v.get("alive"): p.append("scheduler "+n+" 사망") elif v.get("stale"): p.append("scheduler "+n+" stale") if "alive" in (h.get("db_worker") or {}) and not h["db_worker"]["alive"]: p.append("db_worker 사망") rs=h.get("degraded_reasons") or [] if h.get("status")=="degraded" or rs: w.append("degraded: "+",".join(str(r) for r in rs)) print(("BAD "+"; ".join(p)) if p else "OK") if w: print("WARNDEG "+"; ".join(w)) ')" case "$hres" in NOJSON) echo "WARN HEALTH $BASE/health JSON 아님(프록시 미노출 추정) — 내부에서 scripts/health_watch.py 로 확인" ;; BAD*) echo "FAIL HEALTH ${hres%%$'\n'*}"; fail=1 ;; *) echo "OK HEALTH $BASE/health 회귀 신호 없음(schedulers alive, db/redis ok)" ;; esac case "$hres" in *WARNDEG*) echo "WARN HEALTH 운영성 degraded(배포 회귀 아님 가능) — ${hres##*WARNDEG }" ;; esac fi # health-watch 사이드카 확인 — 경고만(배포 실패로 만들지 않는다). 이 스크립트는 HTTP 만 쓰므로 # NAS docker 접근이 가능할 때만 동작한다: PROD_SSH_HOST(ssh 대상, 예 "nas") 를 주면 원격에서, # 없으면 로컬 docker 에 컨테이너가 있을 때만 확인하고 아니면 SKIP. docker 경로/sudo 는 PROD_DOCKER_CMD 로 지정. DOCKER_CMD="${PROD_DOCKER_CMD:-docker}" hw_run() { # 인자 그대로 docker 에 전달 if [ -n "${PROD_SSH_HOST:-}" ]; then ssh -o BatchMode=yes -o ConnectTimeout=10 "$PROD_SSH_HOST" "$DOCKER_CMD $*" 2>/dev/null else $DOCKER_CMD "$@" 2>/dev/null; fi } hw_ps="$(hw_run ps --filter name=autostock-health-watch --format '{{.Status}}' || true)" if [ -z "$hw_ps" ]; then echo "SKIP HEALTH-WATCH 컨테이너 확인 불가(PROD_SSH_HOST 미설정이거나 컨테이너 없음)" else case "$hw_ps" in Up*) echo "OK HEALTH-WATCH 컨테이너 $hw_ps" ;; *) echo "WARN HEALTH-WATCH 컨테이너 비정상: $hw_ps (scripts/health_watch.py 가 디렉터리로 마운트됐는지 확인)" ;; esac hw_last="$(hw_run exec autostock-health-watch cat /state/last_run || true)" if [ -n "$hw_last" ]; then echo "OK HEALTH-WATCH last_run=$hw_last" else echo "WARN HEALTH-WATCH /state/last_run 없음(장외 시간이면 정상, 장중이면 사이드카 루프 확인)"; fi fi [ "$fail" = 0 ] && echo "== 스모크 통과 ==" || echo "== 스모크 실패 — Jenkins 로그/컨테이너 상태 확인 (LEAK 이면 인증 회귀) ==" exit "$fail"