alertmanager: 配置多个接收器

一,配置多个接收器:

配置文件:

global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.163.com:465'
  smtp_from: '发件人邮箱'
  smtp_auth_username: '发件人邮箱的用户名'
  smtp_auth_password: '邮箱的密码或授权码'
  smtp_require_tls: false
route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: 'team_all'
  routes:
    - match_re:
        job: develop
      receiver: team_develop
      # continue=true 代表继续向下匹配,不然就break了
      continue: false
    - match_re:
        job: mysql
      receiver: team_dba
      continue: false
      # 默认all路由
    - match_re:
        job: .*
      receiver: team_all
      continue: false
receivers:
  - name: 'team_develop'
    email_configs:
      - to: '开发团队收件箱'
    webhook_configs:
      - url: 'http://127.0.0.1:5001/'
  - name: 'team_dba'
    email_configs:
      - to: 'dba团队收件箱'
    webhook_configs:
      - url: 'http://127.0.0.1:5002/'
  - name: 'team_all'
    email_configs:
      - to: '开发团队收件箱,dba团队收件箱'
    webhook_configs:
      - url: 'http://127.0.0.1:5003/'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']

二,用postman实际测试:

访问:你.的.I.P:9093/api/v2/alerts

如图:

发送的内容:

[
  {
    "labels": {
       "alertname": "laravel系统崩溃需处理1!",
       "dev": "sda1",
       "job": "develop",
       "instance": "实例1",
       "msgtype": "testing"
     },
     "annotations": {
        "info": "程序员小王提示您:这个系统雪崩了,快处理!",
        "summary": "请检查实例示例1"
      }
  },
  {
    "labels": {
       "alertname": "数据库管理系统损坏需处理1",
       "dev": "sda2",
       "job": "mysql",
       "instance": "实例1",
       "msgtype": "testing"
     },
     "annotations": {
        "info": "程序员小王提示您:电子商务管理系统中订单,仓库模块已经雪崩,快处理!",
        "summary": "请检查实例示例1",
        "runbook": "以下链接http://192.168.5.128:9093/api/v2/alerts应该是可点击的"
      }
  }
]

注意:labels下的job字段,用来供alertmanager判断发送邮件到哪个接收器

三,另一种解决方案:

每次发送信息时把收件人写在内容中:

配置:

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 }}'

发送内容:在labels下带上email_to字段

[
  {
    "labels": {
      "alertname": "<requiredAlertName>",
      "<labelname>": "<labelvalue>",
      "email_to": "foo@example.com,bar@example.com",
      ...
    },
    "annotations": {
      "<labelname>": "<labelvalue>",
    },
    "startsAt": "<rfc3339>",
    "endsAt": "<rfc3339>",
    "generatorURL": "<generator_url>"
  },
  ...
]

 

posted @ 2024-10-30 16:35  刘宏缔的架构森林  阅读(89)  评论(0编辑  收藏  举报