如何通过Prometheus的alertmanager通知到Slack
综述
通过使用 Prometheus 的 Alertmanager,可以对 Prometheus 获取的数据进行通知处理。
然而,即使按照这个设置进行配置,
它能发送通知,但不会显示消息。
groups:
- name: InstanceDown
rules:
- alert: InstanceDdown
expr: up == 0
for: 30s
labels:
severity: critical
annotations:
description: '{{ $labels.instance }} of {{ $labels.description }} has been down for more than 1 minutes.'
summary: 'Instance {{ $labels.instance }} down'
global:
resolve_timeout: 5m
slack_api_url: 'https://hooks.slack.com/services/hogehogefugafuga'
route:
group_by: ['alertname','job', 'monitor']
group_wait: 10s
group_interval: 10s
# repeat_interval: 1h
routes:
- receiver: slack-notifications
match:
severity: critical
receiver: blackhole
- name: slack-notifications
slack_configs:
- channel: '#alerts'
# Alertmanager templates apply here.
text: "<!channel> \nsummary: {{ .CommonAnnotations.summary }}\ndescription: {{ .CommonAnnotations.description }}"
总结和描述均完全达到了空无一物的境界。
为什么会变成这样呢?
这里记载了对话的内容,简言之,就是说“没有传递.Common.Annotations给像Slack这样的聊天工具的通知”。
如果选择,应该怎么办?
使用这个工具来更改alertmanager的title和text的方式如下。
可以使用GoTemplate来编写,但是很复杂,所以使用这个工具。
虽然有点难理解,但本质上只是展开标签并进行各种插入而已。
不过,由于在rules中使用了.Annotations.description,所以直接使用.Annotations.description应该就可以。
然而,需要小心处理Annotations,因为它涵盖的范围更广。另外,文章中也提到了需要谨慎地观察其推移情况,因此需要注意。