PrometheusとAlertmanagerによるメール通知設定

Prometheusでアラートを送信するには、Alertmanagerが必要です。まずAlertmanagerをインストールします。

Alertmanagerのインストール

cd /usr/local/src
wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz
tar -zxvf alertmanager-0.25.0.linux-amd64.tar.gz
mv alertmanager-0.25.0.linux-amd64 /usr/local/alertmanager

Alertmanager設定ファイルの編集

設定ファイルを作成します。

global:
  resolve_timeout: 2m
  smtp_smarthost: 'smtp.example.com:587'
  smtp_from: 'alerting@example.com'
  smtp_auth_username: 'alerting@example.com'
  smtp_auth_password: 'your-app-password'
  smtp_require_tls: true

route:
  group_by: ['cluster', 'alertname']
  group_wait: 5s
  group_interval: 15s
  repeat_interval: 3m
  receiver: 'notify-email'

receivers:
- name: 'notify-email'
  email_configs:
  - to: 'ops@example.com'
    headers:
      Subject: '[ALERT] {{ .CommonAnnotations.summary }}'
    send_resolved: true

※SMTPサーバーに応じてホスト名、ポート、認証方式を適切に設定してください。

Prometheusの設定更新

Prometheus側でAlertmanagerのエンドポイントを指定します。

# Prometheus configuration snippet
alerting:
  alertmanagers:
    - static_configs:
        - targets:
            - '127.0.0.1:9093'

rule_files:
  - '/usr/local/prometheus/rules/*.rules.yml'

Alertmanagerの起動

以下のコマンドでバックグラウンド実行します。

cd /usr/local/alertmanager
nohup ./alertmanager \
  --config.file="/usr/local/alertmanager/alertmanager.yml" \
  --storage.path="/usr/local/alertmanager/data" \
  > /var/log/alertmanager.log 2>&1 &

設定変更後に再起動する場合、以下のようにプロセスを終了してください。

pkill alertmanager
# 起動コマンドを再実行

アラートルールの定義

ノードの可用性監視

# /usr/local/prometheus/rules/availability.rules.yml
groups:
  - name: node-availability
    rules:
      - alert: TargetOutOfService
        expr: up{job=~".*"} == 0
        for: 60s
        labels:
          severity: critical
        annotations:
          summary: "{{$labels.instance}}: サービスが応答していません"
          description: "{{$labels.job}} ジョブの{{$labels.instance}}が60秒以上ダウンしています"

リソース使用率の監視

# /usr/local/prometheus/rules/resource-usage.rules.yml
groups:
  - name: node-resources
    rules:
      - alert: HighDiskUsage
        expr: (1 - (node_filesystem_avail_bytes{fstype=~"ext4|xfs"} / node_filesystem_size_bytes{fstype=~"ext4|xfs"})) * 100 > 85
        for: 3m
        labels:
          severity: warning
        annotations:
          summary: "{{$labels.instance}}:{{$labels.mountpoint}}のディスク使用率が異常です"
          description: "ディスク使用率が85%を超えています(現在値: {{ $value | humanizePercentage }})"

      - alert: HighMemoryUsage
        expr: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 > 85
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "{{$labels.instance}}: メモリ使用率が異常です"
          description: "メモリ使用率が85%を超えています(現在値: {{ $value | humanizePercentage }})"

      - alert: HighCPUUsage
        expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "{{$labels.instance}}: CPU負荷が высокです"
          description: "CPU使用率が85%を超えています(現在値: {{ $value | humanize }}%)"

確認と動作確認

設定完了後、数分以内にアラートが検出され、指定したメールアドレスに通知が届きます。 実際に障害を再現する代わりに、以下のように手動でトランザクションを模糊化してテスト可能です。

# alertmanager の /api/v2/alerts エンドポイントを使用したテスト送信例(curl)
curl -X POST http://localhost:9093/api/v2/alerts \
  -H "Content-Type: application/json" \
  -d '[
    {
      "labels": {
        "alertname": "ManualTest",
        "severity": "info"
      },
      "annotations": {
        "summary": "手動テストアラート",
        "description": "メール通知設定の動作確認です"
      }
    }
  ]'

受信トレイにメールが届けば、設定は正常に機能しています。

タグ: Alertmanager Prometheus SMTP monitoring email-alerts

7月19日 18:07 投稿