本章是在上一篇文档的一个续篇
看到上边默认发送的邮件模板,虽然所有核心的信息已经包含了,但是一般人看起来很费劲,那么,AlertManager 也是支持自定义邮件模板配置的使邮件格式内容可以更优雅直观一些
注意:钉钉需要部署prometheus-webhook-dingtalk,这个也是支持模板的,但是模板要写在prometheus-webhook-dingtalk里,而不是alertmanager
项目链接:
https://github.com/timonwong/prometheus-webhook-dingtalk
通过配置templates参数,指定模板位置。
1.首先新建一个模板文件 email.tmpl
[root@prometheus ~]# mkdir -p /usr/local/prometheus/alertmanager/template/
[root@prometheus ~]# vim /usr/local/prometheus/alertmanager/template/email.tmpl
##需写发送方和接受方的邮箱号
{{ define "email.from" }}1374815945@qq.com{{ end }}
{{ define "email.to" }}13521707413@163.com{{ end }}
{{ define "email.to.html" }}
{{ range .Alerts }}
=========start==========<br>
告警程序: prometheus_alert <br>
告警级别: {{ .Labels.severity }} 级 <br>
告警类型: {{ .Labels.alertname }} <br>
故障主机: {{ .Labels.instance }} <br>
告警主题: {{ .Annotations.summary }} <br>
告警详情: {{ .Annotations.description }} <br>
触发时间: {{ .StartsAt.Format "2019-08-04 16:58:15" }} <br>
=========end==========<br>
{{ end }}
{{ end }}
解释:
define: 定义模板名称
range: 循环遍历
index: 通过key取值
2.更改alertmanager主配增加模版
[root@prometheus ~]# vim /usr/local/prometheus/alertmanager/alertmanager.yml
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.qq.com:465'
smtp_from: '1344435945@qq.com'
smtp_auth_username: '1344435945@qq.com'
smtp_auth_password: 'uajwyvyxlfsoidbh'
smtp_require_tls: false
templates:
- '/usr/local/prometheus/alertmanager/template/*.tmpl'
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
receiver: 'web.hook'
receivers:
- name: 'web.hook'
email_configs:
- to: '{{ template "email.to" . }}'
html: '{{ template "email.to.html" . }}'
send_resolved: true
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
3.重启alertmanager使其生效
[root@prometheus ~]# systemctl restart alertmanager
[root@prometheus ~]# systemctl status alertmanager
查看状态是否正常,如果不正常则要仔细检查刚才修改处是否有误(yml格式比较严格多一个空格也不行)
4.触发告警查看新模版
相比于开头的告警消息这个模版还是比较清晰的,看起来一目了然