# 监控栈 docker-compose（独立 profile）
# 使用方式: docker compose -f docker-compose.monitoring.yml --profile monitoring up -d
version: "3.9"

networks:
  edu-network:
    name: edu-network
    external: true

volumes:
  prometheus-data:
    name: edu-prometheus-data
  alertmanager-data:
    name: edu-alertmanager-data
  grafana-data:
    name: edu-grafana-data
  loki-data:
    name: edu-loki-data

services:
  # ============================================================
  # Prometheus - 指标采集与存储
  # ============================================================
  prometheus:
    image: prom/prometheus:v2.54.1
    container_name: edu-prometheus
    profiles: ["monitoring"]
    restart: unless-stopped
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
      - "--storage.tsdb.path=/prometheus"
      - "--storage.tsdb.retention.time=15d"
      - "--web.enable-lifecycle"
      - "--web.enable-admin-api"
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./prometheus/rules.yml:/etc/prometheus/rules.yml:ro
      - prometheus-data:/prometheus
    networks:
      - edu-network

  # ============================================================
  # Alertmanager - 告警路由与抑制
  # ============================================================
  alertmanager:
    image: prom/alertmanager:v0.27.0
    container_name: edu-alertmanager
    profiles: ["monitoring"]
    restart: unless-stopped
    command:
      - "--config.file=/etc/alertmanager/alertmanager.yml"
      - "--storage.path=/alertmanager"
    ports:
      - "9093:9093"
    volumes:
      - ./alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml:ro
      - alertmanager-data:/alertmanager
    networks:
      - edu-network
    depends_on:
      - prometheus

  # ============================================================
  # Grafana - 可视化
  # ============================================================
  grafana:
    image: grafana/grafana:11.2.2
    container_name: edu-grafana
    profiles: ["monitoring"]
    restart: unless-stopped
    environment:
      - GF_SECURITY_ADMIN_USER=${GRAFANA_ADMIN_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD:-admin}
      - GF_USERS_ALLOW_SIGN_UP=false
      - GF_AUTH_ANONYMOUS_ENABLED=false
    ports:
      - "3000:3000"
    volumes:
      - ./grafana/provisioning:/etc/grafana/provisioning:ro
      - ./grafana/dashboards:/var/lib/grafana/dashboards:ro
      - grafana-data:/var/lib/grafana
    networks:
      - edu-network
    depends_on:
      - prometheus

  # ============================================================
  # node-exporter - 主机指标采集
  # ============================================================
  node-exporter:
    image: prom/node-exporter:v1.8.2
    container_name: edu-node-exporter
    profiles: ["monitoring"]
    restart: unless-stopped
    command:
      - "--path.rootfs=/host"
    ports:
      - "9100:9100"
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/host:ro
    networks:
      - edu-network

  # ============================================================
  # Loki - 日志聚合（P6 硬化新增）
  # ============================================================
  loki:
    image: grafana/loki:3.2.1
    container_name: edu-loki
    profiles: ["monitoring"]
    restart: unless-stopped
    command: -config.file=/etc/loki/local-config.yaml
    ports:
      - "3100:3100"
    volumes:
      - loki-data:/loki
    networks:
      - edu-network

  # ============================================================
  # Promtail - 日志采集（收集 Docker 容器日志发送到 Loki）
  # ============================================================
  promtail:
    image: grafana/promtail:3.2.1
    container_name: edu-promtail
    profiles: ["monitoring"]
    restart: unless-stopped
    command: -config.file=/etc/promtail/config.yml
    volumes:
      - ./promtail/config.yml:/etc/promtail/config.yml:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks:
      - edu-network
    depends_on:
      - loki
