![見出し画像](https://assets.st-note.com/production/uploads/images/58214819/rectangle_large_type_2_967e9facdf809e5762d92c7054129071.png?width=800)
[Prometheus] ローカルでAlertmanagerを使ってみるまで
Alertmanagerとは
Alertmanagerを利用すると、Prometheusで監視しているメトリクスが一定の条件を満たしたとき、メールやSlackなどにアラートを通知することができます。
インストール
こちらのリポジトリを利用すると、Docker ComposeからPrometheusを起動可能です。→ docker-composeのインストール
$ git clone https://github.com/vegasbrianc/prometheus
$ cd prometheus
$ docker-compose up
それぞれ以下のポートで起動します。
・Prometheus: http://localhost:9090/
・Alertmanager: http://localhost:9093/
Slackの通知設定
以下を参考に、Prometheus用のAppsを作成し、Incoming Webhookを有効にして、Webhook URLを取得します。
alertmanager/config.ymlにusername, channel, api_urlを指定します。
route:
receiver: 'slack'
receivers:
- name: 'slack'
slack_configs:
- send_resolved: true
username: '<username>'
channel: '<channel name>'
api_url: '<webhook url>'
Alertmanagerの設定
アラートのルールは以下のように設定を行います。
groups:
- name: <ルール名>
rules:
- alert: <アラート名>
expr: <クエリ式>
for: <条件の期間>
severity: <深刻度>
annotations:
description: "アラートの説明"
summary: "サマリー"
・alert: アラート名を記載
・expr: クエリ式を指定 (PromQLで書く)
・for: 条件の継続時間を指定 (秒:s, 分:m, 時間:h, 日:d など)
・severity: アラートの深刻度を指定(critical, warningなど)
・annotations: アラートの説明、サマリーなど
今回のリポジトリでは、prometheus/alert.rulesにアラートのルールが記載されています。
以下のように設定を変更すると、exprが1のためアラートが発火します。
groups:
- name: rule-example
rules:
- alert: alert-example
expr: 1
for: 1m
設定の変更が終わったら、反映を行うためにDockerコンテナの再起動を行います。
再度開いてしばらく待つと、http://localhost:9090/alerts でアラートがactiveとなり発火していることがわかります。
![画像1](https://assets.st-note.com/production/uploads/images/58213515/picture_pc_0c03093658a47f236416c3d4ee9d83bc.png?width=800)
statusは、PENDING、FIRINGのどちらかとなり、FIRINGは発火を表します。
Alertmanagerの方では発火したアラートが表示されます。
![画像3](https://assets.st-note.com/production/uploads/images/58213533/picture_pc_3e387302cbd643b4a206bdf874d6b365.png?width=800)
SlackにもPrometheusから通知が行われます。
![スクリーンショット 2021-08-04 23.03.19](https://assets.st-note.com/production/uploads/images/58223223/picture_pc_4cefe28f8dad0d21b675d195ef3008e1.png?width=800)
参考
・Prometheus と Alertmanager によるモニタリングシステム入門
・Prometheusのクエリ機能とアラート機能
最後まで読んでいただきありがとうございます。 他の記事はこちらからどうぞ → https://note.com/sat0b3ee/