1.参照 https://github.com/leohe666/gonivinck_copy/tree/gonivinck_copy_self 启动容器
2.在prometheus中设置警告规则 比如:https://github.com/leohe666/gonivinck_copy/blob/gonivinck_copy_self/prometheus/rules/cpu.rules 规则中的前缘即可警告推送消息的key, 另外要采集实例机器的基本信息还要安装node_exporter服务才可正常采集 https://github.com/prometheus/node_exporter/releases/ 下载并启动服务
3.启用prometheus中的alertmanagers关联: 见https://github.com/leohe666/gonivinck_copy/blob/gonivinck_copy_self/prometheus/prometheus.yml中的alerting配置
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093'] # Alertmanager 的地址和端口
4.配置alertmanager接收为webhook 见 alertmanager/alertmanager.yml中
route:
# 所有警报都将匹配到这个根路由
# 它将把警报发送到 'default-receiver'
receiver: 'web.hook'
和
receivers:
- name: 'web.hook'
webhook_configs:
# alertmanager发起web请求的地址
- url: 'http://192.168.5.4:8080/webhook'
5.代码处理,将收到的请求转换为结构体并发送到飞书webhook
入口 代码:
server.POST("/webhook", func(c *gin.Context) {
// 注意这里的结构中的参数是通过prometheus中rules中定义的,见:https://github.com/leohe666/gonivinck_copy/blob/gonivinck_copy_self/prometheus/rules/cpu.rules中的 remark字段
varreqmodel.Notification
iferr:=c.ShouldBindBodyWith(&req, binding.JSON); err!=nil {
c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
return
}