天天看點

Prometheus 整合 AlertManager

文章首發于公衆号《程式員果果》

簡介

Alertmanager 主要用于接收 Prometheus 發送的告警資訊,它很容易做到告警資訊的去重,降噪,分組,政策路由,是一款前衛的告警通知系統。它支援豐富的告警通知管道,可以将告警資訊轉發到郵箱、企業微信、釘釘等。這一節講解利用AlertManager,把接受到的告警資訊,轉發到郵箱。

實驗

準備

啟動 http-simulator 度量模拟器:

docker run --name http-simulator -d -p 8080:8080 pierrevincent/prom-http-simulator:0.1           

啟動 Prometheus,為了友善更新配置,使用挂載配置檔案的方式:

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端點動态更新配置。

通路

http://127.0.0.1:9090/targets

,Prometheus 自身的 metrics 和 http-simulator 的 metrics 處于up 狀态 ,那麼準備工作就做好了。

Prometheus 整合 AlertManager

實驗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分鐘時,産生告警 ,級别為 “嚴重的”。

修改prometheus.yml,引用simulator_alert_rules.yml檔案,prometheus.yml 内容如下:

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配置:

curl -X POST http://localhost:9090/-/reload           
http://127.0.0.1:9090/config

,可以看到已經為更新了配置:

Prometheus 整合 AlertManager
http://127.0.0.1:9090/rules

,Rules 下出現了新添加的告警規則:

Prometheus 整合 AlertManager

驗證

http://127.0.0.1:9090/alerts

,Alerts 下 HttpSimulatorDown 為綠色,處于INACTIVE 狀态,表示什麼都沒有發生。

Prometheus 整合 AlertManager

關閉 http-simulator 服務:

docker stop http-simulator           

,HttpSimulatorDown 變成黃色,處于 PENDING 狀态,表示報警即将被激活。

Prometheus 整合 AlertManager

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

Prometheus 整合 AlertManager

實驗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分鐘時,産生告警 ,級别為 “非常嚴重的”

curl -X POST http://localhost:9090/-/reload           

,ErrorRateHigh 為綠色的 INACTIVE 狀态。

Prometheus 整合 AlertManager

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

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

稍等一會後,通路

, 可以看到錯誤率已經大2%,ErrorRateHigh 為紅色的 FIRING 狀态,報警已經被激活了。

Prometheus 整合 AlertManager

安裝和配置AlertManager

通過docker 挂載檔案的方式安裝AlertManager,在本地建立檔案夾 alertmanager-data 檔案夾,在其中建立 alertmanager.yml,内容如下:

global:
  smtp_smarthost: 'smtp.163.com:25'
  smtp_from: '[email protected]'
  smtp_auth_username: '[email protected]'
  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: '[email protected]'            

啟動 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           
curl -X POST http://localhost:9090/-/reload           
http://127.0.0.1:9093

,通路Alertmanager UI界面,可以看到接收到ErrorRateHigh告警:

Prometheus 整合 AlertManager

郵箱會收到告警資訊:

Prometheus 整合 AlertManager

繼續閱讀