Prometheus 整合 AlertManager

簡介

Alertmanager 主要用於接收 Prometheus 發送的告警信息,它很容易作到告警信息的去重,降噪,分組,策略路由,是一款前衛的告警通知系統。它支持豐富的告警通知渠道,能夠將告警信息轉發到郵箱、企業微信、釘釘等。這一節講解利用AlertManager,把接受到的告警信息,轉發到郵箱。web

實驗

準備

啓動 http-simulator 度量模擬器:docker

docker run --name http-simulator -d -p 8080:8080 pierrevincent/prom-http-simulator:0.1
複製代碼

啓動 Prometheus,爲了方便更新配置,使用掛載配置文件的方式:shell

docker run --name prometheus -d -p 9090:9090 -v /Users/huanchu/Documents/prometheus-data:/prometheus-data \
       prom/prometheus --web.enable-lifecycle --config.file=/prometheus-data/prometheus.yml
複製代碼

啓動添加了參數 --web.enable-lifecycle,讓Prometheus支持經過web端點動態更新配置。json

訪問http://127.0.0.1:9090/targets ,Prometheus 自身的 metrics 和 http-simulator 的 metrics 處於up 狀態 ,那麼準備工做就作好了。bash

實驗

實驗1

告警配置

在prometheus-data文件夾下,建立告警配置文件 simulator_alert_rules.yml:微信

groups:
- name: simulator-alert-rule
  rules:
  - alert: HttpSimulatorDown
    expr: sum(up{job="http-simulator"}) == 0
    for: 1m
    labels:
      severity: critical
複製代碼

配置文件的意思是 http-simulator 服務up狀態爲 0 ,而且持續1分鐘時,產生告警 ,級別爲 「嚴重的」。app

修改prometheus.yml,引用simulator_alert_rules.yml文件,prometheus.yml 內容以下:curl

global:
  scrape_interval: 5s
  evaluation_interval: 5s
  scrape_timeout: 5s

rule_files:
  - "simulator_alert_rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'http-simulator'
    metrics_path: /metrics
    static_configs:
    - targets: ['192.168.43.121:8080']
複製代碼

更新Prometheus配置:lua

curl -X POST http://localhost:9090/-/reload
複製代碼

訪問http://127.0.0.1:9090/config,能夠看到已經爲更新了配置:url

訪問http://127.0.0.1:9090/rules,Rules 下出現了新添加的告警規則:

驗證

訪問http://127.0.0.1:9090/alerts ,Alerts 下 HttpSimulatorDown 爲綠色,處於INACTIVE 狀態,表示什麼都沒有發生。

關閉 http-simulator 服務:

docker stop http-simulator
複製代碼

訪問http://127.0.0.1:9090/alerts,HttpSimulatorDown 變成黃色,處於 PENDING 狀態,表示報警即將被激活。

一分鐘後,HttpSimulatorDown 變成紅色,處於 FIRING 狀態,表示報警已經被激活了。

實驗2

告警配置

在simulator_alert_rules.yml文件中增長告警配置:

- alert: ErrorRateHigh
    expr: sum(rate(http_requests_total{job="http-simulator", status="500"}[5m])) / sum(rate(http_requests_total{job="http-simulator"}[5m])) > 0.02
    for: 1m
    labels:
      severity: major
    annotations:
      summary: "High Error Rate detected"
      description: "Error Rate is above 2% (current value is: {{ $value }}"
複製代碼

配置文件的意思是 http-simulator 請求的錯誤率對2% ,而且持續1分鐘時,產生告警 ,級別爲 「很是嚴重的」

更新Prometheus配置:

curl -X POST http://localhost:9090/-/reload
複製代碼

驗證

訪問http://127.0.0.1:9090/alerts,ErrorRateHigh 爲綠色的 INACTIVE 狀態。

把 http-simulator 的錯誤率調到 10%

curl -H 'Content-Type: application/json' -X PUT -d '{"error_rate": 10}' http://localhost:8080/error_rate
複製代碼

稍等一會後,訪問http://127.0.0.1:9090/alerts, 能夠看到錯誤率已經大2%,ErrorRateHigh 爲紅色的 FIRING 狀態,報警已經被激活了。

安裝和配置AlertManager

經過docker 掛載文件的方式安裝AlertManager,在本地建立文件夾 alertmanager-data 文件夾,在其中建立 alertmanager.yml,內容以下:

global:
  smtp_smarthost: 'smtp.163.com:25'
  smtp_from: 'xxxxx@163.com'
  smtp_auth_username: 'xxxxx@163.com'
  smtp_auth_password: 'xxxxx'

route:
  group_interval: 1m   #當第一個報警發送後,等待'group_interval'時間來發送新的一組報警信息
  repeat_interval: 1m   # 若是一個報警信息已經發送成功了,等待'repeat_interval'時間來從新發送他們
  receiver: 'mail-receiver'
receivers:
- name: 'mail-receiver'
  email_configs:
    - to: 'xxxxxx@163.com' 
複製代碼

啓動 AlertManager:

docker run --name alertmanager -d -p 9093:9093 -v /Users/huanchu/Documents/alertmanager-data:/alertmanager-data \
       prom/alertmanager --config.file=/alertmanager-data/alertmanager.yml
複製代碼

在Prometheus目錄下,修改prometheus.yml配置Alertmanager地址:

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - 192.168.43.121:9093
複製代碼

更新Prometheus配置:

curl -X POST http://localhost:9090/-/reload
複製代碼

訪問http://127.0.0.1:9093,訪問Alertmanager UI界面,能夠看到接收到ErrorRateHigh告警:

郵箱會收到告警信息:

關注我

相關文章
相關標籤/搜索