#!/bin/bash
# 실습 #19 ③ — Alertmanager + 알림 룰 (HighCPU / InstanceDown)
# - Prometheus 가 alert 평가 → firing → Alertmanager 로 push → 라우팅 → 수신처 (Slack/Email/PagerDuty)
# - 실습은 webhook → 로컬 nc 로 받는 단순 패턴
set -e

DIR="$(cd "$(dirname "$0")" && pwd)"
AM_VER="0.27.0"

# 1) Alertmanager 설치
if [ ! -x /usr/local/bin/alertmanager ]; then
    cd /tmp
    wget -q "https://github.com/prometheus/alertmanager/releases/download/v${AM_VER}/alertmanager-${AM_VER}.linux-amd64.tar.gz"
    tar xzf "alertmanager-${AM_VER}.linux-amd64.tar.gz"
    sudo install -m 755 "alertmanager-${AM_VER}.linux-amd64/alertmanager" /usr/local/bin/
    sudo install -m 755 "alertmanager-${AM_VER}.linux-amd64/amtool"       /usr/local/bin/
    sudo mkdir -p /etc/alertmanager /var/lib/alertmanager
    sudo chown -R prometheus:prometheus /etc/alertmanager /var/lib/alertmanager
fi

# 2) 설정 파일 + systemd unit
sudo cp "$DIR/configs/03_alertmanager.yml" /etc/alertmanager/alertmanager.yml
sudo cp "$DIR/configs/03_alertmanager.service" /etc/systemd/system/alertmanager.service
sudo chown -R prometheus:prometheus /etc/alertmanager

sudo systemctl daemon-reload
sudo systemctl enable --now alertmanager
sleep 2

# 3) Prometheus 에 알림 룰 + alertmanager 연결 추가
sudo cp "$DIR/configs/03_alerts.yml" /etc/prometheus/alerts.yml
sudo cp "$DIR/configs/03_prometheus_with_alerts.yml" /etc/prometheus/prometheus.yml
sudo chown -R prometheus:prometheus /etc/prometheus

# 4) 룰 문법 검증 (운영 표준)
echo "=== promtool check rules ==="
sudo -u prometheus promtool check rules /etc/prometheus/alerts.yml

# 5) Prometheus reload (HTTP API)
sudo systemctl reload prometheus 2>/dev/null || sudo systemctl restart prometheus
sleep 3

# 6) 검증
echo
echo "=== Alertmanager 상태 ==="
curl -s http://localhost:9093/api/v2/status | python3 -m json.tool | head -20

echo
echo "=== 등록된 룰 ==="
curl -s http://localhost:9090/api/v1/rules | python3 -m json.tool | head -40

# 7) HighCPU 강제 발화 — yes(1) × N 으로 CPU 끌어올림 (VM 안전)
CORES=$(nproc)
echo
echo "=== HighCPU 트리거 (yes × $CORES, 90초) ==="
for i in $(seq 1 "$CORES"); do
    yes >/dev/null &
done
LOAD_PIDS=$!
sleep 90
pkill yes || true

# 8) Active alerts
echo
echo "=== Active alerts (1분 뒤 다시 확인 권장) ==="
curl -s http://localhost:9090/api/v1/alerts | python3 -m json.tool

cat <<EOF

Alertmanager UI:
  http://<VM-IP>:9093

운영 수신처 연결:
  - Slack    : receivers 에 slack_configs (incoming webhook URL)
  - Email    : smtp_smarthost + email_configs
  - Pager    : pagerduty_configs (service_key)
  - 본 실습은 webhook → http://localhost:5001 (실제 수신 안 함, 동작 확인만)
EOF
