mirror of
https://gitee.com/winc-link/hummingbird.git
synced 2025-04-19 16:02:43 +00:00
完善告警规则
This commit is contained in:
parent
53b26348d6
commit
6787292051
@ -853,17 +853,14 @@ func (p alertApp) AddAlert(ctx context.Context, req map[string]interface{}) erro
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
weixinAlertClient := yiqiweixin.NewWeiXinClient(p.lc, p.dic)
|
weixinAlertClient := yiqiweixin.NewWeiXinClient(p.lc, p.dic)
|
||||||
//发送内容请用户自行完善
|
go weixinAlertClient.Send(notify.Option["webhook"], alertRule.SubRule[0], device, product, alertRule.Name)
|
||||||
text := ""
|
|
||||||
go weixinAlertClient.Send(notify.Option["webhook"], text)
|
|
||||||
case constants.DingDing:
|
case constants.DingDing:
|
||||||
if !checkEffectTime(notify.StartEffectTime, notify.EndEffectTime) {
|
if !checkEffectTime(notify.StartEffectTime, notify.EndEffectTime) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
weixinAlertClient := dingding.NewDingDingClient(p.lc, p.dic)
|
weixinAlertClient := dingding.NewDingDingClient(p.lc)
|
||||||
//发送内容请用户自行完善
|
//发送内容请用户自行完善
|
||||||
text := ""
|
go weixinAlertClient.Send(notify.Option["webhook"], alertRule.SubRule[0], device, product, alertRule.Name)
|
||||||
go weixinAlertClient.Send(notify.Option["webhook"], text)
|
|
||||||
case constants.FeiShu:
|
case constants.FeiShu:
|
||||||
if !checkEffectTime(notify.StartEffectTime, notify.EndEffectTime) {
|
if !checkEffectTime(notify.StartEffectTime, notify.EndEffectTime) {
|
||||||
continue
|
continue
|
||||||
|
@ -16,27 +16,38 @@ package dingding
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"github.com/kirinlabs/HttpRequest"
|
"github.com/kirinlabs/HttpRequest"
|
||||||
"github.com/winc-link/hummingbird/internal/pkg/di"
|
"github.com/winc-link/hummingbird/internal/models"
|
||||||
|
"github.com/winc-link/hummingbird/internal/pkg/constants"
|
||||||
"github.com/winc-link/hummingbird/internal/pkg/logger"
|
"github.com/winc-link/hummingbird/internal/pkg/logger"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DingDingClient struct {
|
type DingDingClient struct {
|
||||||
lc logger.LoggingClient
|
lc logger.LoggingClient
|
||||||
p *di.Container
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDingDingClient(lc logger.LoggingClient, p *di.Container) *DingDingClient {
|
func NewDingDingClient(lc logger.LoggingClient) *DingDingClient {
|
||||||
return &DingDingClient{
|
return &DingDingClient{
|
||||||
lc: lc,
|
lc: lc,
|
||||||
p: p,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DingDingClient) Send(webhook string, text string) {
|
func dingDingTemplate(trigger constants.Trigger, deviceName string, ruleName string) string {
|
||||||
|
// 字符串拼接
|
||||||
|
var rst strings.Builder
|
||||||
|
rst.WriteString("# 设备触发告警 \n\n")
|
||||||
|
rst.WriteString(fmt.Sprintf("设备名称:%s \n\n", deviceName))
|
||||||
|
rst.WriteString(fmt.Sprintf("告警规则名称:%s \n\n", ruleName))
|
||||||
|
rst.WriteString(fmt.Sprintf("触发方式:%s \n\n", string(trigger)))
|
||||||
|
return rst.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DingDingClient) Send(webhook string, rule models.Rule, device models.Device, product models.Product, ruleName string) {
|
||||||
req := HttpRequest.NewRequest()
|
req := HttpRequest.NewRequest()
|
||||||
req.JSON()
|
req.JSON()
|
||||||
resp, err := req.Post(webhook, bytes.NewBuffer([]byte(text)))
|
resp, err := req.Post(webhook, bytes.NewBuffer([]byte(dingDingTemplate(rule.Trigger, device.Name, ruleName))))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.lc.Errorf("dingding send alert message error:", err.Error())
|
d.lc.Errorf("dingding send alert message error:", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,14 @@ package yiqiweixin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/kirinlabs/HttpRequest"
|
"github.com/kirinlabs/HttpRequest"
|
||||||
|
"github.com/winc-link/hummingbird/internal/dtos"
|
||||||
|
"github.com/winc-link/hummingbird/internal/models"
|
||||||
|
"github.com/winc-link/hummingbird/internal/pkg/constants"
|
||||||
"github.com/winc-link/hummingbird/internal/pkg/di"
|
"github.com/winc-link/hummingbird/internal/pkg/di"
|
||||||
"github.com/winc-link/hummingbird/internal/pkg/logger"
|
"github.com/winc-link/hummingbird/internal/pkg/logger"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WeixinClient struct {
|
type WeixinClient struct {
|
||||||
@ -42,13 +47,100 @@ type Markdown struct {
|
|||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *WeixinClient) Send(webhook string, text string) {
|
func (d *WeixinClient) generateWeiXinTemplate(rule models.Rule, device models.Device, product models.Product, ruleName string) QiYeWeiXinTemplate {
|
||||||
|
var qt QiYeWeiXinTemplate
|
||||||
|
qt.Msgtype = "markdown"
|
||||||
|
switch rule.Trigger {
|
||||||
|
case constants.DeviceDataTrigger:
|
||||||
|
var code string
|
||||||
|
var propertyName string
|
||||||
|
var data interface{}
|
||||||
|
var unit string
|
||||||
|
if rule.Option["code"] != "" {
|
||||||
|
for _, property := range product.Properties {
|
||||||
|
if property.Code == rule.Option["code"] {
|
||||||
|
code = property.Code
|
||||||
|
propertyName = property.Name
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var persisterReq dtos.ThingModelPropertyDataRequest
|
||||||
|
persisterReq.ThingModelDataBaseRequest.Last = true
|
||||||
|
persisterReq.DeviceId = device.Id
|
||||||
|
persisterReq.Code = rule.Option["code"]
|
||||||
|
}
|
||||||
|
if data == nil {
|
||||||
|
qt.Markdown.Content = fmt.Sprintf(`设备已触发数据告警
|
||||||
|
>设备名称:<font color=\"comment\">%s</font>
|
||||||
|
>告警规则:<font color=\"comment\">%s</font>
|
||||||
|
>属性名称:<font color=\"comment\">%s</font>
|
||||||
|
>触发时间:<font color=\"comment\">%s</font>
|
||||||
|
`, device.Name, ruleName, fmt.Sprintf(propertyName+"[%s]", code),
|
||||||
|
time.Now().Format("2006-01-02 15:04:05"))
|
||||||
|
} else {
|
||||||
|
qt.Markdown.Content = fmt.Sprintf(`设备已触发数据告警
|
||||||
|
>设备名称:<font color=\"comment\">%s</font>
|
||||||
|
>告警规则:<font color=\"comment\">%s</font>
|
||||||
|
>属性名称:<font color=\"comment\">%s</font>
|
||||||
|
>当前属性值:<font color=\"comment\">%s</font>
|
||||||
|
>触发时间:<font color=\"comment\">%s</font>
|
||||||
|
`, device.Name, ruleName, fmt.Sprintf(propertyName+"[%s]", code),
|
||||||
|
fmt.Sprintf("%s(%s)", data, unit),
|
||||||
|
time.Now().Format("2006-01-02 15:04:05"))
|
||||||
|
}
|
||||||
|
case constants.DeviceEventTrigger:
|
||||||
|
var code string
|
||||||
|
var eventName string
|
||||||
|
var eventType string
|
||||||
|
var data interface{}
|
||||||
|
if rule.Option["code"] != "" {
|
||||||
|
for _, event := range product.Events {
|
||||||
|
if event.Code == rule.Option["code"] {
|
||||||
|
code = event.Code
|
||||||
|
eventName = event.Name
|
||||||
|
eventType = event.EventType
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var persisterReq dtos.ThingModelEventDataRequest
|
||||||
|
persisterReq.ThingModelDataBaseRequest.Last = true
|
||||||
|
persisterReq.DeviceId = device.Id
|
||||||
|
persisterReq.EventCode = rule.Option["code"]
|
||||||
|
persisterReq.EventType = eventType
|
||||||
|
}
|
||||||
|
if data == nil {
|
||||||
|
qt.Markdown.Content = fmt.Sprintf(`设备已触发事件告警
|
||||||
|
>设备名称:<font color=\"comment\">%s</font>
|
||||||
|
>告警规则:<font color=\"comment\">%s</font>
|
||||||
|
>事件名称:<font color=\"comment\">%s</font>
|
||||||
|
>触发时间:<font color=\"comment\">%s</font>
|
||||||
|
`, device.Name, ruleName, fmt.Sprintf(eventName+"[%s]", code), time.Now().Format("2006-01-02 15:04:05"))
|
||||||
|
} else {
|
||||||
|
qt.Markdown.Content = fmt.Sprintf(`设备已触发事件告警
|
||||||
|
>设备名称:<font color=\"comment\">%s</font>
|
||||||
|
>告警规则:<font color=\"comment\">%s</font>
|
||||||
|
>事件名称:<font color=\"comment\">%s</font>
|
||||||
|
>事件内容:<font color=\"comment\">%s</font>
|
||||||
|
>触发时间:<font color=\"comment\">%s</font>
|
||||||
|
`, device.Name, ruleName, fmt.Sprintf(eventName+"[%s]", code), data, time.Now().Format("2006-01-02 15:04:05"))
|
||||||
|
}
|
||||||
|
case constants.DeviceStatusTrigger:
|
||||||
|
qt.Markdown.Content = fmt.Sprintf(`设备已触发状态告警
|
||||||
|
>设备名称:<font color=\"comment\">%s</font>
|
||||||
|
>告警规则:<font color=\"comment\">%s</font>
|
||||||
|
>设备状态:<font color=\"comment\">%s</font>`, device.Name, ruleName, device.Status)
|
||||||
|
}
|
||||||
|
return qt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *WeixinClient) Send(webhook string, rule models.Rule, device models.Device, product models.Product, ruleName string) {
|
||||||
if webhook == "" {
|
if webhook == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req := HttpRequest.NewRequest()
|
req := HttpRequest.NewRequest()
|
||||||
req.JSON()
|
req.JSON()
|
||||||
context, _ := json.Marshal(text)
|
context, _ := json.Marshal(d.generateWeiXinTemplate(rule, device, product, ruleName))
|
||||||
|
d.lc.Info("weixin sent context", string(context))
|
||||||
resp, err := req.Post(webhook, context)
|
resp, err := req.Post(webhook, context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.lc.Errorf("weixin send alert message error:", err.Error())
|
d.lc.Errorf("weixin send alert message error:", err.Error())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user