prometheus支持多企微群机器人告警,修改告警时区
promethes alertmanager 等安装配置网上已经很多了,自行解决。
安装企微机器人告警消息转换服务
从网上找了无数多个文档都是添加企微应用,鲜有几个是添加群机器人告警。
github源码地址: alertmanager-wechatrobot-webhook-master
编译完成后直接启动就可以
[appdeploy@monitor wechatrobot-webhook]$ ls
alertmanager-wechatrobot-webhook nohup.out README.md start.sh
[appdeploy@monitor wechatrobot-webhook]$
[appdeploy@monitor wechatrobot-webhook]$ cat start.sh
#!/bin/bash
pathdir=$(dirname $0)
cd $pathdir
nohup ./alertmanager-wechatrobot-webhook &
服务启动后端口是8999。 alertmanager配置对应的地址和机器人id
# ...
# 省略其他配置
# ...
receivers:
- name: 'default-receiver'
email_configs:
- to: '***@***.***.com'
html: '{{ template "email.html" . }}'
send_resolved: true
webhook_configs:
- url: 'http://127.0.0.1:8999/webhook?key=******企微机器人群id,不同业务线可以配置不同的群id******'
# message: '{{ template "email.html" . }}'
send_resolved: true
#业务监控告警渠道
#1、***告警渠道
- name: '****'
email_configs:
- to: '***@***.***.com'
html: '{{ template "email.html" . }}'
send_resolved: true
webhook_configs:
- url: 'http://127.0.0.1:8999/webhook?key=******企微机器人群id,不同业务线可以配置不同的群id******'
send_resolved: true
#2、***告警渠道
- name: '****'
email_configs:
- to: '***@***.***.com'
send_resolved: true
webhook_configs:
- url: 'http://127.0.0.1:8999/webhook?key=******企微机器人群id,不同业务线可以配置不同的群id******'
send_resolved: true
修改告警时区
从网上找了无数文档, 没有找到一个修改时区的, 能把告警时间调好的, 无奈只能自己看源码修改时区。
从刚刚下载下来的源码找到文件alertmanager-wechatrobot-webhook-master/transformer/transformer.go修改如下:
package transformer
import (
"bytes"
"fmt"
"github.com/k8stech/alertmanager-wechatrobot-webhook/model"
"time"
)
// TransformToMarkdown transform alertmanager notification to wechat markdow message
func TransformToMarkdown(notification model.Notification) (markdown *model.WeChatMarkdown, robotURL string, err error) {
status := notification.Status
annotations := notification.CommonAnnotations
robotURL = annotations["wechatRobot"]
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("### 当前状态:%s \n", status))
// buffer.WriteString(fmt.Sprintf("#### 告警项:\n"))
// 修改这里, 加载时区
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
for _, alert := range notification.Alerts {
labels := alert.Labels
buffer.WriteString(fmt.Sprintf("\n>告警级别: %s\n", labels["severity"]))
buffer.WriteString(fmt.Sprintf("\n>告警类型: %s\n", labels["alertname"]))
buffer.WriteString(fmt.Sprintf("\n>故障主机: %s\n", labels["instance"]))
annotations := alert.Annotations
buffer.WriteString(fmt.Sprintf("\n>告警主题: %s\n", annotations["summary"]))
buffer.WriteString(fmt.Sprintf("\n>告警详情: %s\n", annotations["description"]))
// 修改触发时间的时区
buffer.WriteString(fmt.Sprintf("\n> 触发时间: %s\n", alert.StartsAt.In(loc).Format("2006-01-02 15:04:05")))
}
markdown = &model.WeChatMarkdown{
MsgType: "markdown",
Markdown: &model.Markdown{
Content: buffer.String(),
},
}
return
}