#!/bin/bash
# ============================================================
# GUARDiA SM | agent_scouter_sm.sh
# 대상: Scouter APM (Collector / Host-Agent / Java-Agent)
# 파라미터: SCOUTER_HOME=/opt/scouter
#           SC_COLLECTOR_HOST=localhost SC_COLLECTOR_PORT=6100
#           SC_WEB_PORT=6180 SC_COLLECTOR_UDP=6100
# ============================================================
set -euo pipefail
SCOUTER_HOME=${SCOUTER_HOME:-/opt/scouter}
SC_COLLECTOR_HOST=${SC_COLLECTOR_HOST:-localhost}
SC_COLLECTOR_PORT=${SC_COLLECTOR_PORT:-6100}
SC_WEB_PORT=${SC_WEB_PORT:-6180}
OK="[OK]"; WARN="[WARN]"; CRIT="[CRIT]"
SEP="─────────────────────────────────────────"
RESULT=0

echo "======================================================"
echo " GUARDiA SM 점검 | Scouter APM | $(hostname -s)"
echo " 점검 시각: $(date '+%Y-%m-%d %H:%M:%S %Z')"
echo "======================================================"

# ── 1. Scouter Collector ──────────────────────────────────
echo; echo "[$SEP] 1. Scouter Collector"
COLL_PROC=$(pgrep -f "scouter.server\|ScouterCollector\|scouter-collector" 2>/dev/null | wc -l || echo 0)
COLL_JAR=$(pgrep -f "scouter.server" 2>/dev/null | head -1 || echo "")
if [ "$COLL_PROC" -gt 0 ] || pgrep -f "scouter" &>/dev/null; then
    echo " ${OK} Scouter Collector 실행 중"
    if [ -n "$COLL_JAR" ]; then
        RSS_MB=$(awk '/VmRSS/{print $2}' /proc/${COLL_JAR}/status 2>/dev/null | \
            awk '{printf "%d", $1/1024}' || echo "N/A")
        echo " RSS 메모리: ${RSS_MB} MB"
    fi
else
    echo " ${CRIT} Scouter Collector 프로세스 없음"
    RESULT=2
fi

# 포트 리스닝 (TCP + UDP 6100)
ss -tlnp 2>/dev/null | grep -q ":${SC_COLLECTOR_PORT} " && \
    echo " ${OK} TCP 포트 ${SC_COLLECTOR_PORT} LISTEN" || \
    echo " ${WARN} TCP 포트 ${SC_COLLECTOR_PORT} LISTEN 없음"
ss -ulnp 2>/dev/null | grep -q ":${SC_COLLECTOR_PORT} " && \
    echo " ${OK} UDP 포트 ${SC_COLLECTOR_PORT} LISTEN" || \
    echo " ${WARN} UDP 포트 ${SC_COLLECTOR_PORT} LISTEN 없음"

# ── 2. Scouter Web (선택) ────────────────────────────────
echo; echo "[$SEP] 2. Scouter Web API"
ss -tlnp 2>/dev/null | grep -q ":${SC_WEB_PORT} " && \
    echo " ${OK} Web API 포트 ${SC_WEB_PORT} LISTEN" || \
    echo " ${WARN} Web API 포트 ${SC_WEB_PORT} LISTEN 없음 (별도 배포 시 무시)"
if command -v curl &>/dev/null; then
    HTTP_CODE=$(curl -sk -o /dev/null -w "%{http_code}" \
        --max-time 5 "http://${SC_COLLECTOR_HOST}:${SC_WEB_PORT}/scouter/v1/info/server" \
        2>/dev/null || echo "ERR")
    echo "$HTTP_CODE" | grep -qE "^[23]" && \
        echo " ${OK} Web API 응답: ${HTTP_CODE}" || \
        echo " ${WARN} Web API 응답: ${HTTP_CODE}"
fi

# ── 3. Host Agent ─────────────────────────────────────────
echo; echo "[$SEP] 3. Scouter Host Agent"
HOST_PROC=$(pgrep -f "scouter.agent.host\|host-agent" 2>/dev/null | wc -l || echo 0)
if [ "$HOST_PROC" -gt 0 ]; then
    echo " ${OK} Host Agent 실행 중"
else
    echo " ${WARN} Host Agent 프로세스 없음"
fi

# ── 4. Java Agent 연결된 프로세스 ────────────────────────
echo; echo "[$SEP] 4. Java Agent 연결 현황"
JAVA_AGENTS=$(pgrep -f "scouter.agent.jar\|scouter-agent.jar" 2>/dev/null | wc -l || echo 0)
echo " Scouter Java Agent 연결 수: ${JAVA_AGENTS}"
if [ "$JAVA_AGENTS" -gt 0 ]; then
    echo " ${OK} Java Agent 활성"
    # 에이전트가 붙은 프로세스 목록
    pgrep -f "scouter.agent.jar\|scouter-agent.jar" 2>/dev/null | while read PID; do
        CMD=$(ps -p "$PID" -o comm= 2>/dev/null || echo "unknown")
        echo "   PID:${PID} CMD:${CMD}"
    done | head -5
else
    echo " ${WARN} Scouter Java Agent 미연결 (WAS에 agent 미설정)"
fi

# ── 5. Collector 데이터 디렉터리 ─────────────────────────
echo; echo "[$SEP] 5. 데이터 저장 용량"
for DATADIR in "${SCOUTER_HOME}/server/database" "${SCOUTER_HOME}/database" \
               "${SCOUTER_HOME}/collector/database"; do
    if [ -d "$DATADIR" ]; then
        USED=$(du -sh "$DATADIR" 2>/dev/null | awk '{print $1}' || echo "N/A")
        echo " 데이터 경로: ${DATADIR}"
        echo " 사용 용량: ${USED}"
        break
    fi
done

# ── 6. Collector 로그 ─────────────────────────────────────
echo; echo "[$SEP] 6. Collector 로그 오류"
for LOGDIR in "${SCOUTER_HOME}/server/logs" "${SCOUTER_HOME}/logs" \
              "${SCOUTER_HOME}/collector/logs"; do
    if [ -d "$LOGDIR" ]; then
        LOGFILE=$(ls -t "${LOGDIR}"/*.log 2>/dev/null | head -1 || echo "")
        if [ -n "$LOGFILE" ] && [ -r "$LOGFILE" ]; then
            ERR=$(tail -500 "$LOGFILE" | grep -cE "ERROR|FATAL|Exception" || echo 0)
            echo " 최근 오류: ${ERR}건 (${LOGFILE})"
            [ "$ERR" -gt 0 ] && tail -500 "$LOGFILE" | grep -E "ERROR|FATAL" | tail -5 | sed 's/^/   /'
            [ "$ERR" -gt 0 ] && [ $RESULT -lt 1 ] && RESULT=1
        fi
        break
    fi
done

# ── 7. XLog 수집 확인 ─────────────────────────────────────
echo; echo "[$SEP] 7. XLog/Counter 수집 파일"
for DATADIR in "${SCOUTER_HOME}/server/database/xlog" "${SCOUTER_HOME}/database/xlog"; do
    if [ -d "$DATADIR" ]; then
        TODAY=$(date +%Y%m%d)
        TODAY_FILES=$(ls "${DATADIR}"/*${TODAY}* 2>/dev/null | wc -l || echo 0)
        echo " 오늘 XLog 파일 수: ${TODAY_FILES} (${DATADIR})"
        [ "$TODAY_FILES" -eq 0 ] && echo " ${WARN} 오늘 XLog 없음 (수집 중단 가능성)" && \
            [ $RESULT -lt 1 ] && RESULT=1 || echo " ${OK} XLog 수집 중"
        break
    fi
done

# ── 요약 ─────────────────────────────────────────────────
echo
echo "======================================================"
case $RESULT in
    0) echo " 최종 결과: ${OK}   Scouter APM 정상" ;;
    1) echo " 최종 결과: ${WARN} 주의 항목 있음" ;;
    2) echo " 최종 결과: ${CRIT} 즉시 조치 필요" ;;
esac
echo " 점검 완료: $(date '+%Y-%m-%d %H:%M:%S')"
echo "======================================================"
exit $RESULT
