一、配置Alertmanager接入PrometheusAlert¶
更新alertmanager配置,将所有的告警均指向告警分发平台
[root@master01 7]# cat allofconfigmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: alertmanager-config
namespace: monitor
data:
alertmanager.yml: |-
global:
resolve_timeout: 1m
#邮箱服务器的SMTP主机配置
smtp_smarthost: 'smtp.qq.com:465'
#发送邮件主题
smtp_from: 'zq@zq.cn'
#登录用户名
smtp_auth_username: '1904763431@qq.com'
#此处的auth password是邮箱的第三方登录授权密码,而非用户密码
smtp_auth_password: 'xdjdwczivdfpcbhj'
#有些邮箱需要开启此配置,这里使用的是企微邮箱,仅做测试,不需要开启此功能。
smtp_require_tls: false
templates:
- '/etc/alertmanager/*.tmpl'
route:
group_by: ['env','instance','type','group','job','alertname','cluster']
group_wait: 10s
group_interval: 2m
repeat_interval: 10m
receiver: 'webhook'
receivers:
- name: 'webhook'
webhook_configs:
- url: 'http://prometheus-alert-center.monitor.svc:8080/prometheusalert?type=wx&tpl=prometheus-wx&wxurqq.com/cgi-bin/webhook/send?key=9d8866d6-ab55-48f3-8336-786325667640&at=ZhangDaDan,ZHDYA'
send_resolved: true
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
wechat.tmpl: |-
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
========= 监控报警 =========
告警状态:{{ .Status }}
告警级别:{{ .Labels.severity }}
告警类型:{{ $alert.Labels.alertname }}
故障主机: {{ $alert.Labels.instance }}
告警主题: {{ $alert.Annotations.summary }}
告警详情: {{ $alert.Annotations.message }}{{ $alert.Annotations.description}};
触发阀值:{{ .Annotations.value }}
故障时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
========= = end = =========
{{- end }}
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
========= 告警恢复 =========
告警类型:{{ .Labels.alertname }}
告警状态:{{ .Status }}
告警主题: {{ $alert.Annotations.summary }}
告警详情: {{ $alert.Annotations.message }}{{ $alert.Annotations.description}};
故障时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
恢复时间: {{ ($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
{{- if gt (len $alert.Labels.instance) 0 }}
实例信息: {{ $alert.Labels.instance }}
{{- end }}
========= = end = =========
{{- end }}
{{- end }}
{{- end }}
{{- end }}
email.tmpl: |-
{{ define "email.from" }}xxx.com{{ end }}
{{ define "email.to" }}xxx.com{{ end }}
{{ define "email.to.html" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{ range .Alerts }}
========= 监控报警 =========<br>
告警程序: prometheus_alert <br>
告警级别: {{ .Labels.severity }} <br>
告警类型: {{ .Labels.alertname }} <br>
告警主机: {{ .Labels.instance }} <br>
告警主题: {{ .Annotations.summary }} <br>
告警详情: {{ .Annotations.description }} <br>
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} <br>
========= = end = =========<br>
{{ end }}{{ end -}}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{ range .Alerts }}
========= 告警恢复 =========<br>
告警程序: prometheus_alert <br>
告警级别: {{ .Labels.severity }} <br>
告警类型: {{ .Labels.alertname }} <br>
告警主机: {{ .Labels.instance }} <br>
告警主题: {{ .Annotations.summary }} <br>
告警详情: {{ .Annotations.description }} <br>
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} <br>
恢复时间: {{ .EndsAt.Format "2006-01-02 15:04:05" }} <br>
========= = end = =========<br>
{{ end }}{{ end -}}
{{- end }}
# 应用
[root@master01 7]# kaf allofconfigmap.yaml
接口说明:
receivers:
- name: 'webhook'
webhook_configs:
- url: 'http://prometheus-alert-center.monitor.svc:8080/prometheusalert?type=wx&tpl=prometheus-wx&wxurqq.com/cgi-bin/webhook/send?key=9d8866d6-ab55-48f3-8336-786325667640&at=ZhangDaDan,ZHDYA'
## 接口说明:
/prometheusalert #自定义模版接口,可通过Dashboard自定义模版后,支持任意WebHook接入
type=?:指定消息转发的目标类型,如钉钉、企业微信、飞书等;该参数为必选参数
目前支持的值:
dd 钉钉
wx 企业微信
workwechat 企业微信应用
fs 飞书
webhook WebHook
txdx 腾讯云短信
txdh 腾讯云电话
alydx 阿里云短信
alydh 阿里云电话
hwdx 华为云短信
bddx 百度云短信
rlydh 容联云电话
7moordx 七陌短信
7moordh 七陌语音电话
email Email
tg Telegram
rl 百度Hi(如流)
tpl=?: 指定消息所使用的模版,如prometheus-dd(Prometheus针对钉钉的模板);模版可以去PrometheusAlert 页面的模版管理-->自定义模板页面查看或新建;该参数为必选参数
ddurl=?:指定PrometheusAlert发送消息的钉钉机器人地址,如需要多个地址可以通过,分割,该参数需要配合type=dd的模版使用;该参数为可选参数,如未填写,则默认从app.conf中获取默认配置
wxurl=?:指定PrometheusAlert发送消息的企业微信机器人地址,如需要多个地址可以通过,分割,该参数需要配合type=wx的模版使用;该参数为可选参数,如未填写,则默认从app.conf中获取默认配置
fsurl=?:指定PrometheusAlert发送消息的飞书机器人地址,如需要多个地址可以通过,分割,该参数需要配合type=fs的模版使用;该参数为可选参数,如未填写,则默认从app.conf中获取默认配置
phone=?:指定PrometheusAlert发送消息的手机号,如需要多个号码可以通过,分割,该参数需要配合type=txdx | hwdx | bddx | alydx | txdh | alydh | rlydh | 7moordx | 7moordh的模版使用;该参数为可选参数,如未填写,则默认从app.conf中获取默认配置
email=?:指定PrometheusAlert发送消息的email地址,如需要多个email可以通过,分割,该参数需要配合type=email的模版使用;该参数为可选参数,如未填写,则默认从app.conf中获取默认配置
groupid=?:指定PrometheusAlert发送消息的groupid,该参数需要配合type=rl的模版使用;该参数为可选参数,如未填写,则默认从app.conf中获取默认配置
webhook=?:指定PrometheusAlert发送消息的webhook,该参数需要配合type=webhook的模版使用;该参数为可选参数
at=?:钉钉机器人、企业微信机器人开启@某人的功能,如需添加多个@目标,用,号分割即可。此处需注意:钉钉@使用的是手机号码,企业微信机器人@使用的是用户帐号。;该参数为可选参数
rr=?:该参数为开启随机轮询,目前仅针对ddurl,fsurl,wxurl有效,默认情况下如果上述Url配置的是多个地址,则多个地址全部发送,如开启该选项,则从多个地址中随机取一个地址发送,主要是为了避免消息发送频率过高导致触发部分机器人拦截消息。;该参数为可选参数
split=?:该参数仅针对Prometheus告警消息有效,作用是将Prometheus分组消息拆分成单条发送。默认开启,如果Prometheus一次告警附带的同分组的告警消息条数过多,可能会导致告警消息体过大。如需关闭请在url中加入split=false;该参数为可选参数
注意:此参数如设置为split=false,则PrometheusAlert web页面的路由和告警记录等功能将自动关闭,请谨慎。
热加载alertmanager服务:
[root@master01 7]# curl -XPOST http://alertmanager.zhang-qing.com/-/reload
二、测试验证¶
验证所有的告警是否发送到指定的群组:
## 默认标签走默认路由
$ curl -XPOST -H 'Content-Type: application/json' http://alertmanager.zhang-qing.com/api/v1/alerts -d '[{"labels":{"hostname":"zhdya"},"annotations":{"summary":"This is a test alert"}}]'
验证指定标签告警到指定的群组:
## 默认将告警分发到指定的告警组,并也需要在DEVOPS群组中展示
$ curl -XPOST -H 'Content-Type: application/json' http://alertmanager.zhang-qing.com/api/v1/alerts -d '[{"labels":{"hostname":"hello"},"annotations":{"summary":"This is a test alert"}}]'
$ curl -XPOST -H 'Content-Type: application/json' http://alertmanager.zhang-qing.com/api/v1/alerts -d '[{"labels":{"hostname":"asd"},"annotations":{"summary":"This is a test alert"}}]'

关闭node_exporter,模拟告警
[root@iZ2zei8khbswwoi09foupeZ ~]# docker stop flamboyant_mccarthy

恢复node_exporter,模拟告警
[root@iZ2zei8khbswwoi09foupeZ ~]# docker start flamboyant_mccarthy

三、总结¶
- 支持任意包含WebHook接口系统的消息作为消息来源,常见的如Prometheus, Zabbix,Grafana等。
- 支持多种类型的发送目标,支持钉钉,企业微信,email,飞书,公有云短信/电话 等。
- 支持按照不同级别发送消息到不同目标对象。
- 简化Prometheus分组配置,支持按照具体消息发送到单个或多个接收方。
- 可按照企业中的排班制,通过自动轮询的方式发送到多个人员且支持按照不同日期 发送到不同人员。