PrometheusとGrafanaでサーバー監視環境を構築する

PrometheusとGrafanaをDocker Composeで立ち上げ、Node Exporterでサーバーメトリクスを収集・可視化するまでの手順を解説します。

構成の概要

[監視対象サーバー]
  └── node_exporter (メトリクス公開 :9100)

[監視サーバー]
  ├── prometheus  (メトリクス収集・保存 :9090)
  └── grafana     (ダッシュボード表示 :3000)

Prometheusが各サーバーの node_exporter からメトリクスをスクレイプ(Pull型)し、Grafanaで可視化します。


docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
version: "3.9"

services:
  prometheus:
    image: prom/prometheus:v2.51.0
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.retention.time=30d'
    ports:
      - "9090:9090"

  grafana:
    image: grafana/grafana:10.4.0
    environment:
      GF_SECURITY_ADMIN_PASSWORD: your_secure_password
    volumes:
      - grafana_data:/var/lib/grafana
    ports:
      - "3000:3000"
    depends_on:
      - prometheus

  node_exporter:
    image: prom/node-exporter:v1.7.0
    pid: "host"
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    command:
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
    ports:
      - "9100:9100"

volumes:
  prometheus_data:
  grafana_data:

Prometheusの設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# prometheus/prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node'
    static_configs:
      - targets: ['node_exporter:9100']

  # 複数サーバーを監視する場合
  - job_name: 'servers'
    static_configs:
      - targets:
          - '192.168.1.10:9100'
          - '192.168.1.11:9100'
        labels:
          env: production

起動と確認

1
2
3
4
docker compose up -d

# Prometheus UI: http://localhost:9090
# Grafana:       http://localhost:3000 (admin / your_secure_password)

Prometheus UIで up というクエリを実行すると、スクレイプ対象のステータスが確認できます。


GrafanaへのPrometheusデータソース追加

  1. Grafanaにログイン(admin / your_secure_password)
  2. Configuration → Data sources → Add data source
  3. Prometheus を選択
  4. URL に http://prometheus:9090 を入力(Docker Composeのサービス名で解決できます)
  5. Save & Test をクリック

PromQLの基本

Prometheusのクエリ言語PromQLを使いこなすことでGrafanaのグラフを自由に作れます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# CPU使用率(%)
100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

# メモリ使用率(%)
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100

# ディスク使用率(%)
(1 - node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100

# ネットワーク受信レート(bytes/s)
rate(node_network_receive_bytes_total{device="eth0"}[5m])

アラートの設定

Prometheusのアラートルールを定義します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# prometheus/rules/node_alerts.yml
groups:
  - name: node
    rules:
      - alert: HighCPUUsage
        expr: >
          100 - (avg by (instance)
            (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85          
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "CPU使用率が高い ({{ $labels.instance }})"
          description: "CPU使用率が {{ $value | printf \"%.1f\" }}% です"

      - alert: DiskSpaceLow
        expr: >
          (1 - node_filesystem_avail_bytes{mountpoint="/"}
               / node_filesystem_size_bytes{mountpoint="/"}) * 100 > 80          
        for: 10m
        labels:
          severity: critical
        annotations:
          summary: "ディスク残量が少ない ({{ $labels.instance }})"

prometheus.yml にルールファイルを読み込む設定を追加します。

1
2
rule_files:
  - "rules/*.yml"

アラートの通知にはAlertmanagerを組み合わせてSlackやメールへ飛ばせます。


Grafanaダッシュボードの利用

Grafanaにはコミュニティ作成のダッシュボードをIDで簡単にインポートできます。

Dashboards → Import → IDを入力 → Load でインポートできます。


まとめ

コンポーネント役割
node_exporterOSメトリクスの公開
Prometheusメトリクスの収集・保存・アラート評価
Grafana可視化・ダッシュボード

PrometheusとGrafanaの組み合わせはOSSの監視スタックとして最も広く使われています。Docker Composeで数分で立ち上げられるため、まずローカルで試してから本番環境に適用するのが確実です。