16 張圖實戰 Prometheus 自定義告警規則

作者:liugp

出處:https://goo.gs/8g4je

一、概述

通過創建 Prometheus 監控告警規則,您可以制定針對特定 Prometheus 實例的告警規則。當告警規則設置的條件滿足後,系統會產生對應的告警事件。如果想要收到通知,需要進一步配置對應的通知策略以生成告警並且以短信、郵件、電話、釘羣機器人、企業微信機器人或者 Webhook 等方式發送通知。

從 Prometheus server 端接收到 alerts 後,會基於 PromQL 的告警規則 分析數據,如果滿足 PromQL 定義的規則,則會產生一條告警,併發送告警信息到 Alertmanager,Alertmanager 則是根據配置處理告警信息併發送。所以 Prometheus 的告警配置依賴於PromQLAlertManager,關於這兩個介紹可以參考以下文章:

二、告警實現流程


設置警報和通知的主要步驟是:

  1. 在 Prometheus 中配置告警規則。

  2. 配置 Prometheus 與 AlertManager 關聯。

  3. 配置 AlertManager 告警通道。

三、告警規則

官方文檔:https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/

1)告警規則配置

在 Prometheus 配置(prometheus.yml)中添加報警規則配置,配置文件中 rule_files 就是用來指定報警規則文件的,如下配置即指定存放報警規則的目錄爲 / etc/prometheus,規則文件爲 rules.yml:

rule_files:
- /etc/prometheus/rules.yml

設置報警規則:

警報規則允許基於 Prometheus 表達式語言的表達式來定義報警報條件的,並在觸發警報時發送通知給外部的接收者(Alertmanager),一條警報規則主要由以下幾部分組成:

rules.yml 示例如下:

groups:
- name: example
  rules:
  - alert: high_memory
    # 當內存佔有率超過10%,持續1min,則觸發告警
    expr: 100 - ((node_memory_MemAvailable_bytes{instance="192.168.182.110:9100",job="node_exporter"} * 100) / node_memory_MemTotal_bytes{instance="192.168.182.110:9100",job="node_exporter"}) > 90
    for: 1m
    labels:
      severity: page
    annotations:
      summary: spike memeory

1)監控服務器是否在線

對於被 Prometheus 監控的服務器,我們都有一個 up 指標,可以知道該服務是否在線。

up == 0  #服務下線了。
up == 1 #服務在線。

【示例】

groups:
- name: Test-Group-001 # 組的名字,在這個文件中必須要唯一
  rules:
  - alert: InstanceDown # 告警的名字,在組中需要唯一
    expr: up == 0 # 表達式, 執行結果爲true: 表示需要告警
    for: 1m # 超過多少時間才認爲需要告警(即up==0需要持續的時間)
    labels:
      severity: warning # 定義標籤
    annotations:
      summary: "服務 {{ $labels.instance }} 下線了"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."

注意:

3)告警數據的狀態

【溫馨提示】經測試發現,如果同一個告警數據達到了 Firing,那麼不會再次產生一個告警數據,除非該告警解決了。

四、實戰操作

1)下載 node_exporter

node-exporter 用於採集 node 的運行指標,包括 node 的 cpu、load、filesystem、meminfo、network 等基礎監控指標,類似於 zabbix 監控系統的的 zabbix-agent。

下載地址:https://github.com/prometheus/node_exporter/releases/

wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
tar -xzf node_exporter-1.5.0.linux-amd64.tar.gz

2)啓動 node_exporter

ln -s /opt/prometheus/exporter/node_exporter/node_exporter-1.5.0.linux-amd64/node_exporter  /usr/local/bin/node_exporter
# 指定端口啓動,默認端口:9100
node_exporter --web.listen-address=":9100"

配置node_exporter.service啓動

# 默認端口9100
cat >/usr/lib/systemd/system/node_exporter.service<<EOF
[Unit]
Description=node_exporter
After=network.target
 #可以創建相應的用戶和組 啓動
#User=prometheus
#Group=prometheus

[Service]
ExecStart=/opt/prometheus/exporter/node_exporter/node_exporter-1.5.0.linux-amd64/node_exporter --web.listen-address=:9100
[Install]
WantedBy=multi-user.target
EOF

啓動服務

systemctl daemon-reload
systemctl start node_exporter
systemctl status node_exporter
systemctl enable node_exporter

檢查

curl http://localhost:9100/metrics

3)配置 Prometheus 加載 node_exporter

添加或修改配置 prometheus.yml

重啓加載配置

systemctl restart prometheus
# 1、 kill方式
#kill -HUP pid
# 2、curl方式(推薦)
#curl -X POST http://IP/-/reload
# 【注意】需要在啓動的命令行增加參數: --web.enable-lifecycle
curl -X POST http://192.168.182.110:9090/-/reload
# 3、重啓(不推薦,重啓會導致所有的連接短暫性中斷)
systemctl restart prometheus

檢查
web:http://ip:9090/targets

4)告警規則配置

在 Prometheus 配置文件rometheus.yml 中配置如下:

/etc/prometheus/rule.yml配置如下:

groups:
- name: Test-Group-001 # 組的名字,在這個文件中必須要唯一
  rules:
  - alert: InstanceDown # 告警的名字,在組中需要唯一
    expr: up == 0 # 表達式, 執行結果爲true: 表示需要告警
    for: 1m # 超過多少時間才認爲需要告警(即up==0需要持續的時間)
    labels:
      severity: warning # 定義標籤
    annotations:
      summary: "服務 {{ $labels.instance }} 下線了"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."

重新加載

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

在 web 上就可以看到一個告警規則。

5)模擬告警

手動關機

sudo shutdown -h now

過了一段時間告警狀態就變成Pending

再過一段時間告警就變成了Firing

6)配置告警通道

這裏以有郵件告警爲示例,其它的也差不多。修改配置之前最好先備份一下之前的配置

cp alertmanager.yml alertmanager.bak

【1】配置 alertmanager.yml

global:
  resolve_timeout: 5m
  ## 這裏爲qq郵箱 SMTP 服務地址,官方地址爲 smtp.qq.com 端口爲 465 或 587,同時要設置開啓 POP3/SMTP 服務。
  smtp_smarthost: 'smtp.qq.com:465'
  smtp_from: 'xxxxxxxx@qq.com'
  smtp_auth_username: 'xxxxxxxx@qq.com'
  #授權碼,不是密碼,在 QQ 郵箱服務端設置開啓 POP3/SMTP 服務時會提示
  smtp_auth_password: 'xxxxxxxx'
  smtp_require_tls: false

#1、模板
templates:
  - '/opt/prometheus/alertmanager/alertmanager-0.24.0.linux-amd64/templates/email.tmpl'

#2、路由
route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  #郵箱
  receiver: 'email'

receivers:
- name: 'email'
  email_configs:
  ## 接收警報的email(這裏是引用模板文件中定義的變量)
  - to: '{{ template "email.to"}}'
    ## 發送郵件的內容(調用模板文件中的)
    html: '{{ template "email.to.html" .}}'
    send_resolved: true

# 抑制器配置
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    #確保這個配置下的標籤內容相同纔會抑制,也就是說警報中必須有這三個標籤值纔會被抑制。
    equal: ['alertname''dev''instance']

【2】模板 alert.tmpl

模板文件配置了email.fromemail.toemail.to.html 三種模板變量,可以在 alertmanager.yml 文件中直接配置引用。這裏 email.to.html 就是要發送的郵件內容,支持 Html 和 Text 格式,這裏爲了顯示好看,採用 Html 格式簡單顯示信息。下邊 {{range .Alerts}} 是個循環語法,用於循環獲取匹配的 Alerts 的信息。

{{ define "email.from" }}xxxxxxxx@qq.com{{ end }}
{{ define "email.to" }}xxxxxxxx@163.com{{ end }}
{{ define "email.to.html" }}
{{ range .Alerts }}
=========start==========<br>
告警程序: prometheus_alert <br>
告警級別: {{ .Labels.severity }} 級 <br>
告警類型: {{ .Labels.alertname }} <br>
故障主機: {{ .Labels.instance }} <br>
告警主題: {{ .Annotations.summary }} <br>
告警詳情: {{ .Annotations.description }} <br>
觸發時間: {{ .StartsAt.Format "2019-08-04 16:58:15" }} <br>
=========end==========<br>
{{ end }}
{{ end }}

【溫馨提示】這裏記得換成自己的郵箱地址!!!

重啓 alertmanager

systemctl restart alertmanager

在 web 上就可以看到對應的告警信息了。

接下來就靜待告警了。

一整套流程到這裏就全部跑通了,告警規則、告警指標、告警通道根據自己的場景來定

參考資料

[1]

Prometheus AlertManager 實戰: https://www.cnblogs.com/liugp/p/16974615.html

[2]

Prometheus PromQL 實戰: https://www.cnblogs.com/liugp/p/16977340.html

[3]

Prometheus Pushgetway 實戰: https://www.cnblogs.com/liugp/p/16973756.html

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/DnDC6OeLKNUfcJASWZQFCw