前言:Prometheus Alert是开源的运维告警中心消息转发系统,支持主流的监控系统Prometheus,Zabbix,日志系统Graylog和数据可视化系统Grafana发出的预警消息,支持钉钉,微信,华为云短信,腾讯云短信,腾讯云电话,阿里云短信,阿里云电话等
说明: 此次演示企业微信告警
一.安装
github:https://github.com/feiyu563/PrometheusAlert
#打开PrometheusAlert releases页面,根据需要选择需要的版本下载到本地解压并进入解压后的目录
如linux版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9.1/linux.zip)
# wget https://github.com/feiyu563/PrometheusAlert/releases/download/v4.9.1/linux.zip && unzip linux.zip &&cd linux/
#运行PrometheusAlert
# ./PrometheusAlert (#后台运行请执行 nohup ./PrometheusAlert &)
#启动后可使用浏览器打开以下地址查看:http://127.0.0.1:8080
#默认登录帐号和密码在app.conf中有配置
# 使用 supervisor 来守护启动。
# 配置参考 example/supervisor/prometheusalert.ini
# 如果需要将日志输出到控制台,请修改 app.conf 中 logtype=console
二.告警配置
1.企业微信机器人配置

地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=***
配置说明:https://developer.work.weixin.qq.com/document/path/91770
示例:
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=597f29bf-183e-419c-9c8a-7252f18dc390' \ -H 'Content-Type: application/json' \ -d ' { "msgtype": "markdown", "markdown": { "content": "实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n >类型:<font color=\"comment\">用户反馈</font> >普通用户反馈:<font color=\"comment\">117例</font> >VIP用户反馈:<font color=\"comment\">15例</font>" } }'
2.Alertmanager配置
global:
resolve_timeout: 5m
route:
group_by: ['instance']
group_wait: 10m
group_interval: 10s
repeat_interval: 10m
receiver: 'web.hook.prometheusalert'
receivers:
- name: 'web.hook.prometheusalert'
webhook_configs:
- url: 'http://[prometheusalert_url]:8080/prometheusalert?type=dd&tpl=prometheus-dd&ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxx&at=18888888888'
url参数说明:
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页面的路由和告警记录等功能将自动关闭,请谨慎。
自定义模板:
{{ $var := .externalURL}}{{ range $k,$v:=.alerts }}
{{if eq $v.status "resolved"}}
#### [Prometheus恢复信息]({{$v.generatorURL}})
> <font color="info">告警名称</font>:[{{$v.labels.alertname}}]({{$var}})
> <font color="info">告警级别</font>:{{$v.labels.severity}}
> <font color="info">开始时间</font>:{{$v.startsAt}}
> <font color="info">结束时间</font>:{{$v.endsAt}}
> <font color="info">实例地址</font>:{{$v.labels.instance}}
> <font color="info">主机名称</font>:{{$v.labels.hostname}}
**{{$v.annotations.description}}**
{{else}}
#### [Prometheus告警信息]({{$v.generatorURL}})
> <font color="#FF0000">告警名称</font>:[{{$v.labels.alertname}}]({{$var}})
> <font color="#FF0000">告警级别</font>:{{$v.labels.severity}}
> <font color="#FF0000">开始时间</font>:{{$v.startsAt}}
> <font color="#FF0000">实例地址</font>:{{$v.labels.instance}}
> <font color="#FF0000">主机名称</font>:{{$v.labels.hostname}}
**{{$v.annotations.description}}**
{{end}}
{{ end }}
