任意时间区间(月流量)统计,无视重启~

This commit is contained in:
naiba
2021-07-14 23:53:37 +08:00
parent dff1e29c40
commit 63bb1570d2
16 changed files with 290 additions and 77 deletions

View File

@@ -27,10 +27,10 @@ func (r *AlertRule) AfterFind(tx *gorm.DB) error {
return json.Unmarshal([]byte(r.RulesRaw), &r.Rules)
}
func (r *AlertRule) Snapshot(server *Server) []interface{} {
func (r *AlertRule) Snapshot(server *Server, db *gorm.DB) []interface{} {
var point []interface{}
for i := 0; i < len(r.Rules); i++ {
point = append(point, r.Rules[i].Snapshot(server))
point = append(point, r.Rules[i].Snapshot(server, db))
}
return point
}
@@ -39,28 +39,39 @@ func (r *AlertRule) Check(points [][]interface{}) (int, bool) {
var max int
var count int
for i := 0; i < len(r.Rules); i++ {
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++
if r.Rules[i].IsTransferDurationRule() {
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++
}
}
if fail/total > 0.7 {
count++
break
}
}
if fail/total > 0.7 {
count++
break
}
}
if count == len(r.Rules) {
return max, false
}
return max, true
return max, count != len(r.Rules)
}