需求

grafana+elk+alertmanager实现微信报警到不同部门或不同告警人。

简介

grafana+alertmanager实现微信报警 这篇文章中详述了grafana+alertmanager的微信报警实现过程。

alertmanager默认情况下告警接收人为wechat,也就是默认对test1发送微信告警,没有将报警分类。

vim grafana.yml
global:
  resolve_timeout: 5m

templates:
- '/usr/local/alertmanager/wechat.tmpl'

route:
  group_by: ['alertname']
  group_wait: 5m
  #group_wait: 1m
  #同一组内警报,等待group_interval时间后,再继续等待repeat_interval时间
  group_interval: 10m
  #group_interval: 1m
  #当group_interval时间到后,再等待repeat_interval时间后,才进行报警
  repeat_interval: 1h
  #repeat_interval: 1m
  receiver: 'wechat'
receivers:
- name: 'wechat'
  wechat_configs:
  - corp_id: 'wwbba179b20d372e'
    agent_id: '1000005'
    api_secret: '-CJ9QLEFxLzxt-NWYOLuy-RuX3I'
    to_user: 'test1'
    send_resolved: true

问题

此时,如果我们对elk的index分组,并发给不同的接收人,如:web、api,怎么实现?

1.先来看下grafana发送到alertmanager告警信息

alertname='xxx流量告警'
description='xxx每分钟访问量超过15000'
summary='xxx流量告警'
metric='Count'

我们可以通过访问alertmanager的web页面查看,如http://10.10.10.22:9093。

在alertmanager中,对alertname进行分组;description和summary是具体报警内容;而metric是标签,即label。

要想实现分组,alertmanager需要根据label进行设置子路由,通过匹配label发送给不同的接收人。

2.设置子路由

通过routes设置子路由,子路由中设置不同的接收人。

vim grafana.yml
global:
  resolve_timeout: 5m

templates:
- '/usr/local/alertmanager/wechat.tmpl'

route:
  group_by: ['alertname']
  group_wait: 5m
  #group_wait: 1m
  #同一组内警报,等待group_interval时间后,再继续等待repeat_interval时间
  group_interval: 10m
  #group_interval: 1m
  #当group_interval时间到后,再等待repeat_interval时间后,才进行报警
  repeat_interval: 1h
  #repeat_interval: 1m
  receiver: 'wechat'
  #子路由
  routes:
  - receiver: 'wechat_web'
    match_re:
      department: 'web'
  - receiver: 'wechat_api'
    match_re:
      department: 'api'
receivers:
- name: 'wechat'
  wechat_configs:
  - corp_id: 'wwbba179b20d372e'
    agent_id: '1000005'
    api_secret: '-CJ9QLEFxLzxt-NWYOLuy-RuX3I'
    to_user: 'test1'
    send_resolved: true
- name: 'wechat_web'
  wechat_configs:
  - corp_id: 'wwbba179b20d372e'
    agent_id: '1000005'
    api_secret: '-CJ9QLEFxLzxt-NWYOLuy-RuX3I'
    to_user: 'test2'
    send_resolved: true
- name: 'wechat_api'
  wechat_configs:
  - corp_id: 'wwbba179b20d372e'
    agent_id: '1000005'
    api_secret: '-CJ9QLEFxLzxt-NWYOLuy-RuX3I'
    to_user: 'test3'
    send_resolved: true

3.amtool 调试

#触发web报警
./amtool --alertmanager.url=http://10.10.10.22:9093 alert add department=web alertname="xxx流量告警"        --annotation=description='xxx每分钟访问量超过15000'  --annotation=summary = "xxx流量告警"
#触发api报警
./amtool --alertmanager.url=http://10.10.10.22:9093 alert add department=api alertname="xxx流量告警"        --annotation=description='xxx每分钟访问量超过15000'  --annotation=summary = "xxx流量告警"

通过这种方式,我们可以在排除grafana的情况下,模拟测试alertmanager触发报警。

4.grafana设置label

由于我们使用的是grafana5.4 ,查看官方文档All supported notifiers

grafana告警配置发送邮件 grafana微信告警_grafana告警配置发送邮件

从上图看出报警通道为prometheus+alertmanager 是支持发送图片和报警标签(对应alertmanager中的label),但是是从6.3+开始。

也就是说grafana5.4 是不支持报警标签,因此无法设置label;因此我们需要将grafana升级到6.3以上。
升级过程需要测试,以免丢失数据。

两个版本的对比如下:

grafana-5.4 告警信息配置:

grafana告警配置发送邮件 grafana微信告警_微信_02


grafana-6.5 告警信息配置:

grafana告警配置发送邮件 grafana微信告警_微信_03


通过上图区别,可明显看出6.5 比 5.4 的多出了一个alert tag的功能,即对应alertmanger的label。如下:

grafana告警配置发送邮件 grafana微信告警_grafana告警配置发送邮件_04


使用grafana-5.4 默认展示一个label:metric

使用grafana-6.5 可以展示两个label:department和metric,而metric是用于elk的统计量;department是我们用于区分发送给不同部门和报警人的,alertmanger正式通过这个手动添加的label来匹配不同的子路由,发给不同的报警人。