prometheus+alertmanager根據配置標簽來進行告警分組


【0】需求

我的promethues里面配置了很多項目組,可能很多項目都用的這一個監控。
我想要根據 prometheus.yml 的job_name 來分別給不同項目組的人發告警 

  

【1】prometheus.yml 中的 job_name 項目組

  

【2】altermanager.yml 管理配置

【2.1】altermanager.yml 中使用 routes 下的 match

也可以使用 match_re,進行正則表達式匹配

https://prometheus.io/docs/alerting/latest/configuration/

# A set of equality matchers an alert has to fulfill to match the node.
match:
  [ <labelname>: <labelvalue>, ... ]

# A set of regex-matchers an alert has to fulfill to match the node.
match_re:
  [ <labelname>: <regex>, ... ]

配置測試案例如下:

  

【官方案例】

# The root route with all parameters, which are inherited by the child
# routes if they are not overwritten.
route:
  receiver: 'default-receiver'
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 4h
  group_by: [cluster, alertname]
  # All alerts that do not match the following child routes
  # will remain at the root node and be dispatched to 'default-receiver'.
  routes:
  # All alerts with service=mysql or service=cassandra
  # are dispatched to the database pager.
  - receiver: 'database-pager'
    group_wait: 10s
    match_re:
      service: mysql|cassandra
  # All alerts with the team=frontend label match this sub-route.
  # They are grouped by product and environment rather than cluster
  # and alertname.
  - receiver: 'frontend-pager'
    group_by: [product, environment]
    match:
      team: frontend

 

 那們,我們這里的 match 匹配的其實是 告警信息出來的消息;

【2.2】采集的來源 prometheus.yml  中的 rule_files

  

 

 到實際的文件如下:

  

 

我們既可以使用 $labels中的標簽,也可以使用上圖中的  labels 下的標簽(這些標簽可以自己隨意加的),如

labels:
   serverity: warning

直接把 serverity: warning 寫到 altermanager.yml 的 match下即可,如:

match:
  serverity: warning

 

【2.3】 $labels 究竟包含了哪些東西?

根據不同采集指標,包含了不同的值信息,所以 ;

$lables 包含的就是所有采集指標中蘊含的這些值;

   

 

那么,問題來了,$labels 在采集指標中為什么會生成這些 tag(tag指得就是 instance/job/job_name/name 等等這種標簽變量)標簽呢?而不是其他呢?它的來源在哪里?

【$lablels 的來源】

(1)來自prometheus.yml中的配置

  

 

 系統tag:

  - job_name  =》$labels.job

  - targets       =》$labels.instalce

       - labels: 下面的所有,比如 name  =》$labels.name

(2)來自采集器中的 lab(我這里以mssql的自定義采集器舉例,其他官方提供的采集器也一樣)

  

 

我們上prometheus,用pql 試試,如下圖,db果然在里面了

  

 

 

【3】altermanager 中的 routes下的 match中可以匹配的到底有哪些?

(1)$labels 下的所有 tag,如:

  

(2)rules.yml 報警規則文件下的  labels: 下定義的 lag,如:

  

 

【4】最佳實踐,配置 altermanager.yml  routes

match、match_re 下面可以填 $labels 下面的所有 tag

【4.1】- receiver 接收者為主

(1)match用法

   

 

(2)match_re 正則用法

route:
  group_by: ['instance'] #將類似性質的報警 合並為單個通知
  group_wait: 10s  # 收到告警時 等待10s確認時間內是否有新告警 如果有則一並發送
  group_interval: 10s #下一次評估過程中,同一個組的alert生效,則會等待該時長發送告警通知,此時不會等待group_wait設置時間
  repeat_interval: 10m #告警發送間隔時間 建議10m 或者30m
  receiver: 'wechat'
  routes:
  - receiver: 'renshibu'
    group_wait: 10s
    group_interval: 10s
    repeat_interval: 10m
    match_re: job: ^aaa.*$
  - receiver: 'wechat'
    continue: true

多個值用法

match_re: 
      service:^(foo1|foo2|baz).*$

【4.2】- match/match_re 內容為主

routes:
  - match_re:
      service: ^(foo1|foo2|baz)$
    receiver: team-X-mails
    routes:
    - match:
        severity: critical
      receiver: team-X-pager
      
  - match:
      service: files
    receiver: team-Y-mails

    routes:
    - match:
        severity: critical
      receiver: team-Y-pager


  - match:
      service: database
    receiver: team-DB-pager
    # Also group alerts by affected database.
    group_by: [alertname, cluster, database]
    routes:
    - match:
        owner: team-X
      receiver: team-X-pager
      continue: true
    - match:
        owner: team-Y
      receiver: team-Y-pager

這個配置文件時從alertmanager官方的github上面找到的。

地址如下:  https://github.com/prometheus/alertmanager/blob/master/doc/examples/simple.yml 

 

 

 

 

【參考文件】

官網配置參考:https://prometheus.io/docs/alerting/latest/configuration/

大佬文檔:https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_liunx_65_prometheus_alertmanager_conf.html

 


免責聲明!

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



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