Grafana 企业微信告警方式
现在新版本的Grafana联络方式支持企业微信WeCom,可以直接配置企业微信告警;低版本Grafana可以使用G2WW服务中转,在Grafana配置Webhook类型的告警方式,通过G2WW发送到企业微信。
文章包括:
一.企业微信群机器人
参考:
使用示例
1.在企业微信群组中添加机器人
2.记录Webhookurl地址
webhook地址示例:
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa3.使用curl工具推送文本消息示例
注意要将url替换成你的机器人webhook地址,content必须是utf8编码
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "hello world"
}
}'4.可以根据接口说明开发对应的服务进行推送消息
消息类型
文本类型
请求示例:
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "hello world"
}
}'markdown格式
markdown格式数据上传示例
{
"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>"
}
}字体颜色(只支持3种内置颜色)
<font color="info">绿色</font>
<font color="comment">灰色</font>
<font color="warning">橙红色</font>上传示例
企业微信地址
693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
内容
echo "${test//$'\n'/\\n}"
2023-03-22T00:00:00 -> 2023-03-22T23:59:59 告警次数统计\n> 环境:xxxxxxxx 告警次数:52上传数据
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"text": {
"content": "2023-03-22T00:00:00 -> 2023-03-22T23:59:59 告警次数统计\n> 环境:xxxxxxxx 告警次数:52"
}
}'error
{"errcode":44004,"errmsg":"empty content, hint: [1679553093531332339122552], from ip: 113.142.58.158, more info at https://open.work.weixin.qq.com/devtool/query?e=44004"}原因
msgtype写的是markdown,下面的text没有改成markdown,所以报错
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"markdown": {
"content": "2023-03-22T00:00:00 -> 2023-03-22T23:59:59 告警次数统计\n> 环境:xxxxxxxx 告警次数:52"
}
}'成功推送
{"errcode":0,"errmsg":"ok"}二.老版本Grafana配置企业微信告警
低版本Grafana可以使用G2WW服务中转,将告警信息发送到企业微信,Grafana配置Webhook类型的告警方式发送到G2WW服务即可。
1.启动G2WW服务
grafana官方版本不支持企业微信,可以利用写的webhook进行转发
https://github.com/imuchen/g2ww
编译
go build -o wechathook *.go得到一个可执行的wechathook文件
启动,默认端口2408
也可以通过nginx转发
2.企业微信群聊添加机器人
企业微信群聊里面添加机器人,得到webhook
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
3.Grafana添加告警方式
Grafana配置告警方式,将key添加到webhook地址后面即可
http://172.16.0.7:2408/693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
4.扩展:编译G2WW
需要先安装go语言编译环境,然后编译,运行即可。
端口监听可以在main.go文件里面配置
安装go语言编译环境
wget https://golang.org/dl/go1.16.3.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.14.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
go versiongoproxy下载的地址无法访问
# 通过go env可以查看环境变量
# 更改goproxy地址
# Go 1.13 及以上(推荐)
# 打开你的终端并执行
$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct编译
go build -o whchathook *.go运行
# 运行
./wechathook配置开机启动crontab
@reboot nohup /home/lgl/src/g2ww-master/wechathook > /dev/null 2>&1 &5.扩展:G2WW 发送逻辑
从消息结构体中获取指定内容,构造消息体发送到企业微信
url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + c.Params("key")
msgStr := fmt.Sprintf(`
{
"msgtype": "news",
"news": {
"articles": [
{
"title": "%s",
"description": "%s",
"url": "%s",
"picurl": "%s"
}
]
}
}
`, h.Title, h.Message+processMatches(h.EvalMatches), strings.Replace(h.RuleUrl, "editPanel=", "viewPanel=", -1), h.ImageUrl)三.新版本v10.1.2配置企业微信告警
新版本v10.1.2 支持企业微信
参考:
添加 WeCom 联系方式步骤
Alerting -> Contact points -> create ->
Integration 选择 Wecom
WeCom是企业微信海外版英文名称。
1.企业微信
1.1新建群
1.2新建机器人
- 群设置
- 添加群机器人
- 新创建一个机器人
- 名称:告警
- 配置:可以通过配置IP,限制可发送请求IP,默认不配置,任何IP可用
获取到Webhook地址
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa2.grafana配置企业微信告警
2.1配置联络点
- Alerting
- Contact points
Add contact points
输入Name: 企业微信
Integration: 选择 WeCom
删除默认的 Secret,不删除,无法粘贴Webhook URL到 Webhook URL
复制Webhook URL地址,粘贴到Webhook URL
Save Contact points 保存可选设置:
Message Type(消息类型):
Text 或 Markdown,默认Markdown
注意:Text类型的链接能点击,Markdown类型的链接,无法在企业微信直接点击Message: 存在默认,可以自定义消息模板
Title: 消息的模板标题,也可以自定义
To User:@all @全部用户
Notification settings(通知设置):
Disable resolved message(禁用已解决的消息)
Text和Markdown结果复制对比,实际效果存在差异,没有截图展示。
Markdown
[FIRING:1] (TestAlert Grafana)
Firing
Value: [no value]
Labels:
- alertname = TestAlert
- instance = Grafana
Annotations:
- summary = Notification test
Silence: https://grafana.xxxxxx.cn/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DTestAlert&matcher=instance%3DGrafanaText
[FIRING:1] (TestAlert Grafana)
**Firing**
Value: [no value]
Labels:
- alertname = TestAlert
- instance = Grafana
Annotations:
- summary = Notification test
Silence: https://grafana.xxxxxx.cn/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DTestAlert&matcher=instance%3DGrafana
注意
Grafana只支持企业微信的Text和Markdown格式消息,如果需要 图文类型 信息,需要自己开发webhook进行转发
2.2配置通知策略
这里之前已经创建过,进行调整。如果没有创建,直接按照自己的需求创建即可。
- 将原有的wechat,调整为 企业微信
- 现有的策略,匹配
__contacts__ =~ .*"wechat".*,匹配的alert进行发送 - 选择发送到wechat的该Notification policies
- 编辑 Edit
- 调整Contact point,wechat -> 企业微信
- Update policy
2.3自定义消息模板
参考官方文档:
判断
比较两个字符串是否相等:
{{ if eq .Status "Approved" }}
...Do something
{{ else }}
...Do something else
{{ end }}比较两个字符串是否不相等:
{{ if ne .Status "" }} // If status is not empty
...Do something
{{ else }}
...Do something else
{{ end }}2.3.1调试模板
Contact points -> Notification templates -> Add template
- Template name 模板名称
- Content 模板内容
Payload data 测试告警信息
- Select alert instances 可以选择指定的告警信息作为告警信息,测试经过 Content 处理后的内容
- Preview 可以查看经过Content处理后的内容,即发送到目标的具体告警信息
2.3.2基于邮件模板修改
基于 创建通知模板中的 邮件模板修改
subject
{{ define "email.subject" }}
{{ len .Alerts.Firing }} firing alert(s), {{ len .Alerts.Resolved }} resolved alert(s)
{{ end }}联络点的title消息模板配置以下引用模板
{{ template "email.subject" . }}效果
1 firing alert(s), 0 resolved alerts(s)message
{{- define "email.message_alert" -}}
{{- range .Labels.SortedPairs }}{{ .Name }}={{ .Value }} {{ end }} has value(s)
{{- range $k, $v := .Values }} {{ $k }}={{ $v }}{{ end }}
{{- end -}}
{{ define "email.message" }}
There are {{ len .Alerts.Firing }} firing alert(s), and {{ len .Alerts.Resolved }} resolved alert(s)
{{ if .Alerts.Firing -}}
Firing alerts:
{{- range .Alerts.Firing }}
- {{ template "email.message_alert" . }}
{{- end }}
{{- end }}
{{ if .Alerts.Resolved -}}
Resolved alerts:
{{- range .Alerts.Resolved }}
- {{ template "email.message_alert" . }}
{{- end }}
{{- end }}
{{ end }}联络点的title消息模板配置以下引用模板
{{ template "email.message" . }}效果
There are 2 firing alert(s), and 1 resolved alert(s)
Firing alerts:
- alertname=Test 1 grafana_folder=GrafanaCloud has value(s) B=1
- alertname=Test 2 grafana_folder=GrafanaCloud has value(s) B=2
Resolved alerts:
- alertname=Test 3 grafana_folder=GrafanaCloud has value(s) B=02.3.3调整后的模板
企业微信告警方式中选择 markdown类型发送
title
创建模板 wecom.subject
{{ define "wecom.subject" }}{{ if .Alerts.Firing -}}{{ len .Alerts.Firing }}个异常 {{ range .Alerts.Firing }}{{- range .Labels.SortedPairs }}{{ if eq .Name "alertname" }}**{{ .Value }}** {{end}}{{ end }}{{- end }}{{end}}{{ if .Alerts.Resolved -}}{{ len .Alerts.Resolved }}个恢复 {{ range .Alerts.Resolved }}{{- range .Labels.SortedPairs }}{{ if eq .Name "alertname" }}**{{ .Value }}** {{end}}{{ end }}{{- end }}{{ end }}{{end}}效果
==========================>1个异常**alert title** <==========================联络点引用
{{ template "wecom.subject" . }}message
创建模板 wecom.message
{{- define "wecom.message_alert" -}}
{{- range .Labels.SortedPairs }}{{ if eq .Name "rulename" }}**{{ .Value }}**{{end}} {{ if eq .Name "alertname" }}**{{ .Value }}**{{end}}{{ end }}
{{- end -}}
{{ define "wecom.message" }}
{{ len .Alerts.Firing }}个异常 {{ len .Alerts.Resolved }}个恢复
{{ if .Alerts.Firing -}}
异常警报:
{{- range .Alerts.Firing }}
- {{ template "wecom.message_alert" . }}
{{- end }}
{{- end }}
{{ if .Alerts.Resolved -}}
恢复警报:
{{- range .Alerts.Resolved }}
- {{ template "wecom.message_alert" . }}
{{- end }}
{{- end }}
ExternalURL: [告警规则访问地址](https://grafana.xxxxxx.cn/alerting/list?search=state:firing)
{{- end }}输出结果示例
5个异常 DatasourceNoData test环境 HTTP状态异常 DatasourceNoData prod环境 PG从库状态异常 DatasourceNoData prod环境 HTTP状态异常 DatasourceNoData 4个恢复 DatasourceNoData XXX环境 PG数据库最老事务年龄监控 DatasourceNoData XXX环境 HTTP状态异常 DatasourceNoData XXX环境 PG连接数占比超过阈值 DatasourceNoData XXX环境 系统平均负载过高>5 严重
5个异常 4个恢复
异常警报:
- DatasourceNoData test环境 HTTP状态异常
- DatasourceNoData prod环境 PG从库状态异常
- DatasourceNoData prod环境 HTTP状态异常
恢复警报:
- DatasourceNoData XXX环境 PG数据库最老事务年龄监控
- DatasourceNoData XXX环境 HTTP状态异常
- DatasourceNoData XXX环境 PG连接数占比超过阈值
- DatasourceNoData XXX环境 系统平均负载过高>5 严重
ExternalURL: 告警规则访问地址联络点引用
{{ template "wecom.message" . }}