二、prometheus之Alertmanager告警


一、Alertmanager簡介

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

報警設置:

prometheus --> 觸發閾值 --> 超出持續時間 --> alertmanager --> 分組|抑制|靜默 --> 媒介類型 --> 郵件|釘釘|微信等

分組(group):將類似性質的告警合並為單個通知,比如網絡通知、主機通知、服務通知。
靜默(silences):是一種簡單的特定時間靜默的機制,例如:服務器要升級維護可以先設置這個時間段告警靜默
抑制(inhibition):當告警發出后,停止重復發送由此告警引發的其他告警即合並一個故障引起多個報警時間,可以消除冗余告警

二、Alertmanager部署

# 解包
root@prometheus:~# tar xf alertmanager-0.23.0.linux-amd64.tar.gz -C /usr/local/
root@prometheus:~# cd /usr/local/
root@prometheus:/usr/local# ls
alertmanager-0.23.0.linux-amd64  bin  etc  games  include  lib  man  prometheus  prometheus-2.32.1.linux-amd64  sbin  share  src
root@prometheus:/usr/local# ln -sv alertmanager-0.23.0.linux-amd64/ alertmanager
'alertmanager' -> 'alertmanager-0.23.0.linux-amd64/'
root@prometheus:/usr/local#


# service 啟動文件
root@prometheus:/usr/local# cat > /etc/systemd/system/alertmanager.service << EOF
> [Unit]
> Description=Prometheus Server
> Documentation=https://prometheus.io/docs/introduction/overview/
> After=network.target
> 
> [Service]
> Restart=on-failure
> WorkingDirectory=/usr/local/alertmanager
> ExecStart=/usr/local/alertmanager/alertmanager
> 
> [Install]
> WantedBy=multi-user.target
> EOF
root@prometheus:/usr/local#


# 啟動
root@prometheus:/usr/local# systemctl daemon-reload
root@prometheus:/usr/local# systemctl start alertmanager.service 
root@prometheus:/usr/local# netstat -tnlp | grep 9093
tcp6       0      0 :::9093                 :::*                    LISTEN      45186/alertmanager  
root@prometheus:/usr/local#

測試訪問

 

 三、Alertmanager告警配置

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

root@prometheus:/usr/local/prometheus# vim /usr/local/prometheus/prometheus.yml
global:
  scrape_interval: 15s 
  evaluation_interval: 15s

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
           - 192.168.88.200:9093

rule_files:
   - "rules/*_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "alertmanager"
    static_configs:
      - targets: ["192.168.88.200:9093"]

prometheus web端可以看到alertmanager 端點

 配置完成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{instance="192.168.88.201:9100"} == 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.163.com:465'
  smtp_from: 'xxxxxxxxx@163.com'
  smtp_auth_username: 'xxxxxxxxx@163.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@163.com'

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

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

[root@node1 ~]# systemctl stop node_exporter

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

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

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

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

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

 

 五、自定義告警模板

上面已經實現了告警規則觸發,告警郵件的發送,但是官方的告警模板實在吝嗇難懂,為此,我們也可以進行自定義告警模板,實現最直觀的告警信息。在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.163.com:465'
  smtp_from: 'zrxzaizai@163.com'
  smtp_auth_username: 'zrxzaizai@163.com'
  smtp_auth_password: 'administrator2!' 
  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:
  - to: 'zrxzaizai@163.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]# systemctl restart alertmanager

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

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

 


免責聲明!

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



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