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

原文地址:https://www.cnblogs.com/gered/p/14376868.html