Prometheus學習筆記(6)Alertmanager告警


一、Alertmanager簡介

Prometheus是一個划分平台,metrics的收集和存儲與警報是分開的,警報是由Alertmanager負責,這是監控環境的獨立部分。警報的規則是在Prometheus server上進行定義的,這些規則可以觸發時間,然后將其傳到alertmanager,alertmanager隨后決定如何處理各自的警報,處理復制之類的問題,並決定在發送警報時使用什么機制:實時消息、電子郵件或者是其他釘釘、微信等工具。

二、Alertmanager部署

Alertmanager默認監聽9093端口,集群接聽端口9094。

# 下載
[root@prometheus ~]# wget https://github.com/prometheus/alertmanager/releases/download/v0.20.0-rc.0/alertmanager-0.20.0-rc.0.linux-amd64.tar.gz

# 解壓
[root@prometheus ~]# tar -zxf alertmanager-0.20.0-rc.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# mv /usr/local/alertmanager-0.20.0-rc.0.linux-amd64 /usr/local/alertmanager-0.20.0
[root@prometheus ~]# ln -sv /usr/local/alertmanager-0.20.0 /usr/local/alertmanager

# 運行
[root@prometheus ~]# ln -sv /usr/local/alertmanager/alertmanager /usr/local/bin/
[root@prometheus ~]# alertmanager &
[root@prometheus ~]# netstat -tulnp |grep alert
tcp6       0      0 :::9093                 :::*                    LISTEN      41194/alertmanager  
tcp6       0      0 :::9094                 :::*                    LISTEN      41194/alertmanager  
udp6       0      0 :::9094                 :::*                                41194/alertmanager  

訪問http:// :9093即可訪問alertmanager的web界面,如下:

三、Alertmanager配置

Alertmanager的配置有兩個地方,一個是在Prometheus server端進行配置告警節點,指定匹配告警規則文件路徑,以及監控alertmanager本身。另一個直接配置alertmanager自身的配置,在alertmanager.yml進行配置。

[root@prometheus alertmanager]# cat /usr/local/prometheus/prometheus.yml 
...

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
        - 192.168.0.143:9093    #配置alertmanager節點列表

rule_files:
   - "rules/*_rules.yml"    #指定規則文件
#   - "rules/*_alert.yml"

scrape_configs:
......

  - job_name: 'alertmanager'    #指定監控任務alertmanager
    static_configs:
    - targets: ['192.168.0.143:9093']

添加完成后,在prometheus server的web端可以查看到alertmanager的targets列表,如下:

配置完成prometheus.yml后,再來看看默認的alertmanager.yml的介紹,如下:

[root@prometheus alertmanager]# cat alertmanager.yml 
global:
  resolve_timeout: 5m    #處理超時時間,默認為5min

route:
  group_by: ['alertname']    # 報警分組依據
  group_wait: 10s    # 最初即第一次等待多久時間發送一組警報的通知
  group_interval: 10s    # 在發送新警報前的等待時間
  repeat_interval: 1h    # 發送重復警報的周期 對於email配置中,此項不可以設置過低,否則將會由於郵件發送太多頻繁,被smtp服務器拒絕
  receiver: 'web.hook'    # 發送警報的接收者的名稱,以下receivers name的名稱

receivers:
- name: 'web.hook'    # 警報
  webhook_configs:    # webhook配置
  - url: 'http://192.168.0.143:5001/'

inhibit_rules:    # 一個inhibition規則是在與另一組匹配器匹配的警報存在的條件下,使匹配一組匹配器的警報失效的規則。兩個警報必須具有一組相同的標簽。 
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']
  • global: 全局配置,包括報警解決后的超時時間、SMTP 相關配置、各種渠道通知的 API 地址等等。

  • route: 用來設置報警的分發策略,它是一個樹狀結構,按照深度優先從左向右的順序進行匹配。

  • receivers: 配置告警消息接受者信息,例如常用的 email、wechat、slack、webhook 等消息通知方式。

  • inhibit_rules: 抑制規則配置,當存在與另一組匹配的警報(源)時,抑制規則將禁用與一組匹配的警報(目標)。

四、自定義告警規則和發送

在prometheus.yml中創建規則文件,進行監控node02主機的node_exporter是否存活,並發送告警信息如下:

[root@prometheus prometheus]# pwd
/usr/local/prometheus
[root@prometheus prometheus]# mkdir rules
[root@prometheus rules]# vim node_rules.yml 
groups:
- name: node-up    # 分組名稱
  rules:           # 規則設置
  - alert: node-up  #告警名稱
    expr: up{job="node02"} == 0    # 表達式,查詢式語句查詢up的值是否等於0,如果等於則告警
    for: 15s    # 告警持續時間
    labels:
      severity: 1
      team: node
    annotations:    # 注解
      summary: "{{ $labels.instance }} 已停止運行超過 15s!"
[root@prometheus rules]# systemctl restart prometheus

設定prometheus.yml中的rules后,再配置alertmanager的告警相關信息,如下:

[root@prometheus alertmanager]# pwd
/usr/local/alertmanager

[root@prometheus alertmanager]# vim alertmanager.yml
global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.qq.com:465'
  smtp_from: 'xxxxxxxxx@qq.com'
  smtp_auth_username: 'xxxxxxxxx@qq.com'
  smtp_auth_password: 'xxxxxxxxx'    # 16位qq郵箱授權碼作為密碼
  smtp_require_tls: false

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'email'    # 選用郵箱告警發送

receivers:
- name: 'email'
  email_configs:
  - to: 'xxxxxxxxx@qq.com'

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']

配置完成后,測試一下是否可以正常出現告警信息和郵件的成功發送。

[root@node02 ~]# systemctl start node_exporter

停止node02的exporter后,可以在prometheus server的web界面上可以看到告警:http://<ip>:9090/alerts,如圖:

可以看到上面3張圖是告警的周期狀態,解析如下:

  • 1)inactive:表示當前報警信息即不是firing狀態也不是pending狀態

  • 2)pending:表示在設置的閾值時間范圍內被激活的

  • 3)firing:表示超過設置的閾值時間被激活的

當狀態變成了firing的時候,我們郵箱就會收到響應的告警郵件,如下:

響應的郵箱授權碼如何操作,請參考鏈接:http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

五、自定義告警模板

上面已經實現了告警規則觸發,告警郵件的發送,但是官方的告警模板實在吝嗇難懂,為此,我們也可以進行自定義告警模板,實現最直觀的告警信息。在alertmanager目錄下創建template目錄,增加郵件模板,然后修改alertmanager的配置。

# 創建模板文件
[root@prometheus alertmanager]# mkdir template
[root@prometheus template]# vim email.tmpl
{{ define "email.html" }}
    {{ range .Alerts }}
<pre>
    ========start==========
    告警程序: prometheus_alert 
    告警級別: {{ .Labels.severity }} 
    告警類型: {{ .Labels.alertname }} 
    故障主機: {{ .Labels.instance }} 
    告警主題: {{ .Annotations.summary }}
    告警詳情: {{ .Annotations.description }}
    觸發時間: {{ .StartsAt.Format "2019-12-14 16:01:01" }}
    ========end==========
</pre>
    {{ end }}
{{ end }}

# 修改alertmanager.yml配置
[root@prometheus alertmanager]# cat alertmanager.yml
global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.qq.com:465'
  smtp_from: 'xxxxxxxxx@qq.com'
  smtp_auth_username: 'xxxxxxxxx@qq.com'
  smtp_auth_password: 'xxxxxxxxxxxxxx'
  smtp_require_tls: false

templates:    # 指定郵件模板的路徑,可以使用相對路徑,template/*.tmpl的方式
  - '/usr/local/alertmanager/template/email.tmpl'

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'email'

receivers:
- name: 'email'
  email_configs:
  - send_resolved: true
    to: 'xxxxxxxxx@qq.com'
    html: '{{ template "email.html" . }}'    # 指定使用模板,如果不指定,還是會加載默認的模板的
    headers: { Subject: "[WARN]Prometheus告警郵件" }    # 配置郵件主題

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']

# 重啟alertmanager
root@prometheus template]# ps -ef |grep alert
root     49465 49165  0 16:03 pts/3    00:00:01 alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml
[root@prometheus template]# kill 49465
[root@prometheus template]# alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml &
[1] 49554

# 測試故障告警
[root@node02 ~]# systemctl stop node_exporter

經過一小段時間后,郵箱會收到告警郵件,是不是好看多了,如圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM