• Prometheus AlertManager 生产实践-直接根据 to_email label 发 alert 到对应邮箱


    概述

    通过之前的文章 - Prometheus Alertmanager 生产配置趟过的坑总结, 我们已经知道 AlertManager 作为告警平台,是非常强大的,可以去重 (deduplicating),分组 (grouping),并将它们路由 (routing) 到正确的接收器 (receiver) 集成,如电子邮件,微信,或钉钉。它还负责处理警报的静默/屏蔽 (silencing)、定时发送/不发送 (Mute) 和抑制 (inhibition) 问题。

    正常的 AlertManager 处理告警流程,是要经过 Alerts -> Route -> Receivers 这么一个步骤的

    1. Alerts 里带了一些标签,如 env, team, job 等
    2. 根据提前编辑好的 Route, 对 alerts 进行路由,比如 env=prod 的发给哪些 receiver, team=db 的发给哪些人。..
    3. 在 Receivers 里已经提前录入了这些需要处理 prod,处理 db 告警的 receivers 邮箱。告警这样发给对应的收件人。

    但是,假如我在 Alerts 里自带收件人信息(如邮箱),能不能直接使用?而不需要再录入所有的 receivers。

    答案当然是可以!通过模板(template)实现这个需求。Let's GO!💪💪💪

    模板(Template)简介

    AlertManager 模板最初的目的是为了对告警的消息做定制化的。 比如同样的 Alerts,我:

    • 通过 SMS 发送,期望是纯文本格式;
    • 通过 email 发送,期望是 HTML 格式;
    • 通过钉钉、企微发送,期望是 Markdown 格式;
    • 而且在这些渠道中,
      • 标题是不同的排列组合
      • 告警内容也是不同的段落格式和用词(比如通过钉钉、企微会加入更多的 emoji)

    AlertManager 模板是和 Prometheus 模板一样,使用的同样是 Go template。当然,具体的数据和函数会有细微的区别,因为在这里主要处理的是告警而非单个告警。

    示例如下:

    receivers:
      - name: emergency
        slack_configs:
        - api_url: https://hooks.slack.com/services/XXXXXXXX
          channel: '#emergency'
          title: 'Alerts in {{ .GroupLabels.cluster }} {{ .GroupLabels.env }}!'
    • 1
    • 2
    • 3
    • 4
    • 5

    AlertManager 进阶

    除了模板化 txt 字段,通知的定义(比如:发给谁)也可以被模板化。通常每个 team 都有自己的路由树,以及相对应的收件人(receivers)。如果另一个团队(不是监控团队,也不是运维团队,而是测试等团队)想要发送给自己团队告警,他们需要从头到尾设置 label、设置匹配其团队 labels 的路由树、把团队内的收件人信息配置到 AlertManager 的 receiver 里。

    那如果你是监控团队,你用 AlertManager 做了个告警平台提供给外部团队甚至客户使用,每次都得这么搞会有“亿点点”麻烦。

    该怎么办呢?🤔🤔🤔

    解决方案

    解决方案就是:

    • Label
    • AlertManager 通知模板

    首先,直接在 Label 里提供相关的接收人信息,然后通过 AlertManager 的模板,将 receiver -> to 写上对应的模板即可。

    具体演示如下:

    方案演示

    首先,是包含收件人信息 label 的 alerts,如下:

    [
      {
        "labels": {
          "alertname": "",
          "": "",
          "email_to": "foo@example.com,bar@example.com",
          ...
        },
        "annotations": {
          "": "",
        },
        "startsAt": "",
        "endsAt": "",
        "generatorURL": ""
      },
      ...
    ]
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    每个 alert 都提供 email_to 这样的 label。

    然后,在 AlertManager 中,可以设置如下 routereceiver, 如下:

    global:
      smtp_smarthost: 'localhost:25'
      smtp_from: 'smtp@example.com'
    route:
      group_by: [email_to, alertname]
      receiver: customer_email
    receivers:
      - name: customer_email
        email_configs:
          - to: '{{ .GroupLabels.email_to }}'
        headers:
          subject: 'Alert: {{ .GroupLabels.alertname }}'
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    注意,group_by 必须包括 email_to label,这样它才算 .GroupLabels. 下的一员。

    当有 alerts 来时,如 "email_to": "foo@example.com,bar@example.com", 会 route 到 customer_email, 其收件人是 {{ .GroupLabels.email_to }}, 会被模板化为: foo@example.com,bar@example.com, 告警邮件自然就会发过去。

    完成!🎉🎉🎉

    本文由东风微鸣技术博客 EWhisper.cn 编写!

  • 相关阅读:
    laravel框架解决sql注入问题
    Linux操作文档——pt-osc工具
    富文本编辑器vue-quill-editor的使用(可上传视频、上传图片及图片的放大缩小)
    openGauss数据库表的创建过程
    数据结构-线性表
    港科夜闻|"香港科大2020十大准独角兽"开思完成2亿元D2,D3轮融资,推进汽后全产业链数字化转型...
    Java泛型
    JDK安装配置
    Docker-安装部署全过程
    《Linux高性能服务器编程》--TCP/IP协议族
  • 原文地址:https://blog.csdn.net/east4ming/article/details/127994798