From 35766e590dee5267d5064af46a57200d4d2d66b7 Mon Sep 17 00:00:00 2001 From: naiba Date: Wed, 6 Jan 2021 21:20:02 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=E6=97=A7=E9=87=87?= =?UTF-8?q?=E6=A0=B7=E7=82=B9=E6=B8=85=E7=90=86=E5=8F=8A=E6=8A=A5=E8=AD=A6?= =?UTF-8?q?=E7=AD=96=E7=95=A5=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/alertrule.go | 15 +++++++++------ service/alertmanager/alertmanager.go | 12 +++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/model/alertrule.go b/model/alertrule.go index 35ef273..d8277f1 100644 --- a/model/alertrule.go +++ b/model/alertrule.go @@ -54,12 +54,15 @@ func (u *Rule) Snapshot(server *Server) interface{} { case "transfer_all": src = server.State.NetOutTransfer + server.State.NetInTransfer case "offline": - src = uint64(server.LastActive.Unix()) - } - if u.Type == "offline" { - if uint64(time.Now().Unix())-src > 6 { - return struct{}{} + if server.LastActive.IsZero() { + src = 0 + } else { + src = uint64(server.LastActive.Unix()) } + } + + if u.Type == "offline" && uint64(time.Now().Unix())-src > 6 { + return struct{}{} } else if (u.Max > 0 && src > u.Max) || (u.Min > 0 && src < u.Min) { return struct{}{} } @@ -109,7 +112,7 @@ func (r *AlertRule) Check(points [][]interface{}) (int, string) { if len(points) < num { continue } - for j := len(points) - 1; j >= 0; j-- { + for j := len(points) - 1; j >= 0 && len(points)-num <= j; j-- { total++ if points[j][i] != nil { fail++ diff --git a/service/alertmanager/alertmanager.go b/service/alertmanager/alertmanager.go index 55788df..ad43299 100644 --- a/service/alertmanager/alertmanager.go +++ b/service/alertmanager/alertmanager.go @@ -84,6 +84,7 @@ func OnDeleteAlert(id uint64) { for i := 0; i < len(alerts); i++ { if alerts[i].ID == id { alerts = append(alerts[:i], alerts[i+1:]...) + i-- } } } @@ -109,6 +110,7 @@ func OnDeleteNotification(id uint64) { for i := 0; i < len(notifications); i++ { if notifications[i].ID == id { notifications = append(notifications[:i], notifications[i+1:]...) + i-- } } } @@ -155,17 +157,13 @@ func checkStatus() { }, firstNotificationDelay+time.Minute*10) } if flag { - message := fmt.Sprintf("逮到咯,快去看看!服务器:%s(%s),报警规则:%s,%s", server.Name, server.Host.IP, alert.Name, desc) + message := fmt.Sprintf("报警规则:%s,服务器:%s(%s),%s,逮到咯,快去看看!", alert.Name, server.Name, server.Host.IP, desc) go sendNotification(message) } } // 清理旧数据 - if max > 0 { - for k := 0; k < len(alertsStore[alert.ID][server.ID]); k++ { - if max < len(alertsStore[alert.ID][server.ID][k]) { - alertsStore[alert.ID][server.ID][k] = alertsStore[alert.ID][server.ID][k][len(alertsStore[alert.ID][server.ID][k])-max:] - } - } + if max > 0 && max < len(alertsStore[alert.ID][server.ID]) { + alertsStore[alert.ID][server.ID] = alertsStore[alert.ID][server.ID][len(alertsStore[alert.ID][server.ID])-max:] } } }