stylesheet

2018-06-07

Alertmanager (prometheus) の Webhookレシーバーを調べる

AlertmanagerPrometheusでのアラート管理を行うコンポーネント。
現状で以下のレシーバー(通知先)をサポートしている。

  • SMTPメール
  • Hipchat
  • PagerDuty
  • Pushover
  • Slack
  • OpsGenie
  • VictorOps
  • WeChat
  • Webhook

今後はレシーバーの追加は積極的に行わないそうで、Webhookを使って自身で作成して欲しいとのこと。

Webhookレシーバー

設定で指定したエンドポイントに対して、JSONでPOSTリクエストを送出するレシーバー。
POSTリスエストを送るだけなので、その後の処理は利用者側で用意する必要がある。

なんでもできるけど、なんにもできない、そんな感じ。

使用するにはAlertmanagerの設定にwebhookレシーバーを追加してやる。

receivers:
- name: "myreceiver"
  webhook_configs:
  - url: 'http://127.0.0.1:3001/alerts'
    send_resolved: true

Prometheus側のアラートルールでlabelsとannotationsとして受け取るプロパティを追加出来る。

- alert: InstanceDown
    expr: up == 0
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has bennd down for more than 5 minutes"

Prometheusのアラートはpendingfiringresolvedと状態が遷移する。
この内、webhookで送出されるイベントはfiringresolvedの2つ。

firing状態では次のような構造になる。
同じグループのアラートは配列としてまとめて送られてくる。

{ receiver: 'duino-klutch-webhook-receiver',
  status: 'firing',
  alerts:
   [ { status: 'firing',
       labels:
        { alertname: 'InstanceDown',
          instance: 'localhost:9100',
          job: 'node_exporter',
          severity: 'critical' },
       annotations:
        { description: 'localhost:9100 of job node_exporter has bennd down for more than 5 minutes.',
          summary: 'Instance localhost:9100 down' },
       startsAt: '2018-06-06T18:03:58.765899704+09:00',
       endsAt: '0001-01-01T00:00:00Z',
       generatorURL: 'http://orange:9090/graph?g0.expr=up+%3D%3D+0&g0.tab=1' } ],
  groupLabels: { alertname: 'InstanceDown' },
  commonLabels:
   { alertname: 'InstanceDown',
     instance: 'localhost:9100',
     job: 'node_exporter',
     severity: 'critical' },
  commonAnnotations:
   { description: 'localhost:9100 of job node_exporter has bennd down for more than 5 minutes.',
     summary: 'Instance localhost:9100 down' },
  externalURL: 'http://orange:9093',
  version: '4',
  groupKey: '{}:{alertname="InstanceDown"}' }

設定でsend_resolvedを無効にしていない場合はresolved状態でもPOSTリクエストが送出される。
スキーマはfiringの場合と違いはない。

{ receiver: 'duino-klutch-webhook-receiver',
  status: 'resolved',
  alerts:
   [ { status: 'resolved',
       labels:
        { alertname: 'InstanceDown',
          instance: 'localhost:9100',
          job: 'node_exporter',
          severity: 'critical' },
       annotations:
        { description: 'localhost:9100 of job node_exporter has bennd down for more than 5 minutes.',
          summary: 'Instance localhost:9100 down' },
       startsAt: '2018-06-06T18:03:58.765899704+09:00',
       endsAt: '2018-06-06T18:07:58.765932428+09:00',
       generatorURL: 'http://orange:9090/graph?g0.expr=up+%3D%3D+0&g0.tab=1' } ],
  groupLabels: { alertname: 'InstanceDown' },
  commonLabels:
   { alertname: 'InstanceDown',
     instance: 'localhost:9100',
     job: 'node_exporter',
     severity: 'critical' },
  commonAnnotations:
   { description: 'localhost:9100 of job node_exporter has bennd down for more than 5 minutes.',
     summary: 'Instance localhost:9100 down' },
  externalURL: 'http://orange:9093',
  version: '4',
  groupKey: '{}:{alertname="InstanceDown"}' }

アラートの状態がpendingからfiringに変化しても、実際にリクエストが送出されるまでは多少時間が掛かる。
即応性が必要なものは不向き。

実装例

上記を踏まえて手抜き実装してみたwebhookがこちら

arduinoに繋いだ8x8マトリクスLEDへサマリーをメッセージ表示するもの。
firing状態のアラートを保持してresolvedで破棄するようにしてみた。