溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Prometheus如何整合AlertManager

發(fā)布時(shí)間:2021-12-22 17:20:20 來(lái)源:億速云 閱讀:143 作者:小新 欄目:軟件技術(shù)

這篇文章主要介紹Prometheus如何整合AlertManager,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

簡(jiǎn)介

Alertmanager 主要用于接收 Prometheus 發(fā)送的告警信息,它很容易做到告警信息的去重,降噪,分組,策略路由,是一款前衛(wèi)的告警通知系統(tǒng)。它支持豐富的告警通知渠道,可以將告警信息轉(zhuǎn)發(fā)到郵箱、企業(yè)微信、釘釘?shù)取?/p>

實(shí)驗(yàn)

準(zhǔn)備

啟動(dòng) http-simulator 度量模擬器:

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

啟動(dòng) 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

啟動(dòng)添加了參數(shù) —web.enable-lifecycle,讓Prometheus支持通過(guò)web端點(diǎn)動(dòng)態(tài)更新配置。

訪問(wèn) http://127.0.0.1:9090/targets ,Prometheus 自身的 metrics 和 http-simulator 的 metrics 處于up 狀態(tài) ,那么準(zhǔn)備工作就做好了。

Prometheus如何整合AlertManager

實(shí)驗(yàn)

實(shí)驗(yàn)1

告警配置

在prometheus-data文件夾下,創(chuàng)建告警配置文件 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 服務(wù)up狀態(tài)為 0 ,并且持續(xù)1分鐘時(shí),產(chǎn)生告警 ,級(jí)別為 “嚴(yán)重的”。

修改prometheus.yml,引用simulator_alert_rules.yml文件,prometheus.yml 內(nèi)容如下:

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

訪問(wèn) http://127.0.0.1:9090/config,可以看到已經(jīng)為更新了配置:

Prometheus如何整合AlertManager

訪問(wèn) http://127.0.0.1:9090/rules,Rules 下出現(xiàn)了新添加的告警規(guī)則:

Prometheus如何整合AlertManager

驗(yàn)證

訪問(wèn) http://127.0.0.1:9090/alerts ,Alerts 下 HttpSimulatorDown 為綠色,處于INACTIVE 狀態(tài),表示什么都沒(méi)有發(fā)生。

Prometheus如何整合AlertManager

關(guān)閉 http-simulator 服務(wù):

docker stop http-simulator

訪問(wèn) http://127.0.0.1:9090/alerts,HttpSimulatorDown 變成黃色,處于 PENDING 狀態(tài),表示報(bào)警即將被激活。

Prometheus如何整合AlertManager

一分鐘后,HttpSimulatorDown 變成紅色,處于 FIRING 狀態(tài),表示報(bào)警已經(jīng)被激活了。

Prometheus如何整合AlertManager

實(shí)驗(yàn)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 請(qǐng)求的錯(cuò)誤率對(duì)2% ,并且持續(xù)1分鐘時(shí),產(chǎn)生告警 ,級(jí)別為 “非常嚴(yán)重的”

更新Prometheus配置:

curl -X POST http://localhost:9090/-/reload
驗(yàn)證

訪問(wèn) http://127.0.0.1:9090/alerts,ErrorRateHigh  為綠色的 INACTIVE 狀態(tài)。

Prometheus如何整合AlertManager

把 http-simulator 的錯(cuò)誤率調(diào)到 10%

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

稍等一會(huì)后,訪問(wèn) http://127.0.0.1:9090/alerts, 可以看到錯(cuò)誤率已經(jīng)大2%,ErrorRateHigh  為紅色的 FIRING 狀態(tài),報(bào)警已經(jīng)被激活了。

Prometheus如何整合AlertManager

安裝和配置AlertManager

通過(guò)docker 掛載文件的方式安裝AlertManager,在本地創(chuàng)建文件夾 alertmanager-data 文件夾,在其中創(chuàng)建 alertmanager.yml,內(nèi)容如下:

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   #當(dāng)?shù)谝粋€(gè)報(bào)警發(fā)送后,等待'group_interval'時(shí)間來(lái)發(fā)送新的一組報(bào)警信息
  repeat_interval: 1m   # 如果一個(gè)報(bào)警信息已經(jīng)發(fā)送成功了,等待'repeat_interval'時(shí)間來(lái)重新發(fā)送他們
  receiver: 'mail-receiver'
receivers:
- name: 'mail-receiver'
  email_configs:
    - to: 'xxxxxx@163.com'

啟動(dòng) 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

訪問(wèn) http://127.0.0.1:9093,訪問(wèn)Alertmanager UI界面,可以看到接收到ErrorRateHigh告警:

Prometheus如何整合AlertManager

郵箱會(huì)收到告警信息:

Prometheus如何整合AlertManager

以上是“Prometheus如何整合AlertManager”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI