2020-12-19 10:11:16 -05:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2021-11-06 04:00:08 -04:00
|
|
|
"time"
|
2020-12-19 10:11:16 -05:00
|
|
|
|
2022-03-18 11:13:22 -04:00
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
2020-12-19 10:11:16 -05:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2021-11-06 04:00:08 -04:00
|
|
|
type CycleTransferStats struct {
|
|
|
|
Name string
|
|
|
|
From time.Time
|
|
|
|
To time.Time
|
2021-11-10 20:40:10 -05:00
|
|
|
Max uint64
|
|
|
|
Min uint64
|
2021-11-06 04:00:08 -04:00
|
|
|
ServerName map[uint64]string
|
|
|
|
Transfer map[uint64]uint64
|
|
|
|
NextUpdate map[uint64]time.Time
|
|
|
|
}
|
|
|
|
|
2020-12-19 10:11:16 -05:00
|
|
|
type AlertRule struct {
|
|
|
|
Common
|
2022-04-14 15:13:53 -04:00
|
|
|
Name string
|
|
|
|
RulesRaw string
|
|
|
|
Enable *bool
|
|
|
|
NotificationTag string // 该报警规则所在的通知组
|
|
|
|
Rules []Rule `gorm:"-" json:"-"`
|
2020-12-19 10:11:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AlertRule) BeforeSave(tx *gorm.DB) error {
|
2022-03-18 11:13:22 -04:00
|
|
|
data, err := utils.Json.Marshal(r.Rules)
|
2020-12-19 10:11:16 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.RulesRaw = string(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AlertRule) AfterFind(tx *gorm.DB) error {
|
2022-03-18 11:13:22 -04:00
|
|
|
return utils.Json.Unmarshal([]byte(r.RulesRaw), &r.Rules)
|
2020-12-19 23:18:27 -05:00
|
|
|
}
|
|
|
|
|
2021-11-06 04:00:08 -04:00
|
|
|
func (r *AlertRule) Enabled() bool {
|
|
|
|
return r.Enable != nil && *r.Enable
|
|
|
|
}
|
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
// Snapshot 对传入的Server进行该报警规则下所有type的检查 返回包含每项检查结果的空接口
|
2021-11-06 04:00:08 -04:00
|
|
|
func (r *AlertRule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, db *gorm.DB) []interface{} {
|
2020-12-19 23:18:27 -05:00
|
|
|
var point []interface{}
|
|
|
|
for i := 0; i < len(r.Rules); i++ {
|
2021-11-06 04:00:08 -04:00
|
|
|
point = append(point, r.Rules[i].Snapshot(cycleTransferStats, server, db))
|
2020-12-19 23:18:27 -05:00
|
|
|
}
|
|
|
|
return point
|
|
|
|
}
|
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
// Check 传入包含当前报警规则下所有type检查结果的空接口 返回报警持续时间与是否通过报警检查(通过则返回true)
|
2021-06-21 09:30:42 -04:00
|
|
|
func (r *AlertRule) Check(points [][]interface{}) (int, bool) {
|
2022-04-11 10:51:02 -04:00
|
|
|
var max int // 报警持续时间
|
|
|
|
var count int // 检查未通过的个数
|
2020-12-19 23:18:27 -05:00
|
|
|
for i := 0; i < len(r.Rules); i++ {
|
2021-07-14 11:53:37 -04:00
|
|
|
if r.Rules[i].IsTransferDurationRule() {
|
2021-07-15 23:14:07 -04:00
|
|
|
// 循环区间流量报警
|
2021-07-14 11:53:37 -04:00
|
|
|
if max < 1 {
|
|
|
|
max = 1
|
|
|
|
}
|
|
|
|
for j := len(points[i]) - 1; j >= 0; j-- {
|
|
|
|
if points[i][j] != nil {
|
|
|
|
count++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 常规报警
|
|
|
|
total := 0.0
|
|
|
|
fail := 0.0
|
|
|
|
num := int(r.Rules[i].Duration)
|
|
|
|
if num > max {
|
|
|
|
max = num
|
|
|
|
}
|
|
|
|
if len(points) < num {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for j := len(points) - 1; j >= 0 && len(points)-num <= j; j-- {
|
|
|
|
total++
|
|
|
|
if points[j][i] != nil {
|
|
|
|
fail++
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 10:51:02 -04:00
|
|
|
// 当70%以上的采样点未通过规则判断时 才认为当前检查未通过
|
2021-07-14 11:53:37 -04:00
|
|
|
if fail/total > 0.7 {
|
|
|
|
count++
|
|
|
|
break
|
2020-12-19 23:18:27 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-21 09:51:23 -05:00
|
|
|
}
|
2022-04-11 10:51:02 -04:00
|
|
|
// 仅当所有检查均未通过时 返回false
|
2021-07-14 11:53:37 -04:00
|
|
|
return max, count != len(r.Rules)
|
2020-12-19 10:11:16 -05:00
|
|
|
}
|