116 lines
3.5 KiB
Go
116 lines
3.5 KiB
Go
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||
//go:build plus
|
||
|
||
package models
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||
"github.com/iwind/TeaGo/dbs"
|
||
"github.com/iwind/TeaGo/maps"
|
||
"github.com/iwind/TeaGo/types"
|
||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
// FireNodeThreshold 触发相关阈值设置
|
||
func (this *NodeThresholdDAO) FireNodeThreshold(tx *dbs.Tx, role string, nodeId int64, item string) error {
|
||
clusterId, err := SharedNodeDAO.FindNodeClusterId(tx, nodeId)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if clusterId == 0 {
|
||
return nil
|
||
}
|
||
|
||
// 集群相关阈值
|
||
var thresholds []*NodeThreshold
|
||
{
|
||
clusterThresholds, err := this.FindAllEnabledAndOnClusterThresholds(tx, role, clusterId, item)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
thresholds = append(thresholds, clusterThresholds...)
|
||
}
|
||
|
||
// 节点相关阈值
|
||
{
|
||
nodeThresholds, err := this.FindAllEnabledAndOnNodeThresholds(tx, role, clusterId, nodeId, item)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
thresholds = append(thresholds, nodeThresholds...)
|
||
}
|
||
|
||
if len(thresholds) == 0 {
|
||
return nil
|
||
}
|
||
|
||
for _, threshold := range thresholds {
|
||
if len(threshold.Param) == 0 || threshold.Duration <= 0 {
|
||
continue
|
||
}
|
||
paramValue, err := SharedNodeValueDAO.SumNodeValues(tx, role, nodeId, item, threshold.Param, threshold.SumMethod, types.Int32(threshold.Duration), threshold.DurationUnit)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
var originValue = nodeconfigs.UnmarshalNodeValue(threshold.Value)
|
||
var thresholdValue = types.Float64(originValue)
|
||
|
||
var isPercent = nodeconfigs.CheckNodeValueItemParamIsPercent(item, threshold.Param)
|
||
if isPercent && thresholdValue >= 1 {
|
||
thresholdValue = thresholdValue / 100
|
||
}
|
||
|
||
var isMatched = nodeconfigs.CompareNodeValue(threshold.Operator, paramValue, thresholdValue)
|
||
|
||
if isMatched {
|
||
// TODO 执行其他动作
|
||
|
||
// 是否已经通知过
|
||
if threshold.NotifyDuration > 0 && threshold.NotifiedAt > 0 && time.Now().Unix()-int64(threshold.NotifiedAt) < int64(threshold.NotifyDuration*60) {
|
||
continue
|
||
}
|
||
|
||
// 创建消息
|
||
nodeName, err := SharedNodeDAO.FindNodeName(tx, nodeId)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
var itemName = nodeconfigs.FindNodeValueItemName(threshold.Item)
|
||
var paramName = nodeconfigs.FindNodeValueItemParamName(threshold.Item, threshold.Param)
|
||
var operatorName = nodeconfigs.FindNodeValueOperatorName(threshold.Operator)
|
||
|
||
var paramValueFormat = fmt.Sprintf("%.2f", paramValue)
|
||
if isPercent {
|
||
originValue += "%"
|
||
paramValueFormat = fmt.Sprintf("%.2f%%", paramValue*100)
|
||
}
|
||
|
||
var subject = "节点 \"" + nodeName + "\" " + itemName + " 达到阈值"
|
||
var body = "节点 \"" + nodeName + "\" " + itemName + " 达到阈值\n阈值设置:" + paramName + " " + operatorName + " " + originValue + "\n当前值:" + paramValueFormat + "\n触发时间:" + timeutil.Format("Y-m-d H:i:s")
|
||
if len(threshold.Message) > 0 {
|
||
body = threshold.Message
|
||
body = strings.Replace(body, "${item.name}", itemName, -1)
|
||
body = strings.Replace(body, "${value}", fmt.Sprintf("%.2f", paramValue), -1)
|
||
}
|
||
err = SharedMessageDAO.CreateNodeMessage(tx, role, clusterId, nodeId, MessageTypeThresholdSatisfied, MessageLevelWarning, subject, body, maps.Map{}.AsJSON(), true)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 设置通知时间
|
||
_, err = this.Query(tx).
|
||
Pk(threshold.Id).
|
||
Set("notifiedAt", time.Now().Unix()).
|
||
Update()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|