From 1f08b579a4f28dca7448832a0f6442eae8d966a4 Mon Sep 17 00:00:00 2001 From: Akkia Date: Mon, 18 Apr 2022 19:59:42 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=89=8D=E5=AF=B9=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=8B=B7=E8=B4=9D=20=E4=BF=9D=E8=AF=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81=E7=9A=84=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=8C=87=E6=A0=87=E4=B8=8E=E8=A7=A6=E5=8F=91=E8=AF=A5=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9A=84=E7=8A=B6=E6=80=81=E6=8C=87=E6=A0=87=E7=9B=B8?= =?UTF-8?q?=E5=90=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/rpc/nezha.go | 8 ++++++-- service/singleton/alertsentinel.go | 12 ++++++++---- service/singleton/crontask.go | 6 +++++- service/singleton/notification.go | 24 +++++------------------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 441ca75..7cae949 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -3,6 +3,7 @@ package rpc import ( "context" "fmt" + "github.com/jinzhu/copier" "time" "github.com/naiba/nezha/model" @@ -28,11 +29,14 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece if cr != nil { singleton.ServerLock.RLock() defer singleton.ServerLock.RUnlock() + // 保存当前服务器状态信息 + curServer := model.Server{} + copier.Copy(&curServer, singleton.ServerList[clientID]) if cr.PushSuccessful && r.GetSuccessful() { - singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("$%d$[任务成功] %s ,服务器:%s,日志:\n%s", singleton.ServerList[clientID].ID, cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false) + singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[任务成功] %s ,服务器:%s,日志:\n%s", cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false, &curServer) } if !r.GetSuccessful() { - singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("$%d$[任务失败] %s ,服务器:%s,日志:\n%s", singleton.ServerList[clientID].ID, cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false) + singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s ,服务器:%s,日志:\n%s", cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false, &curServer) } singleton.DB.Model(cr).Updates(model.Cron{ LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())), diff --git a/service/singleton/alertsentinel.go b/service/singleton/alertsentinel.go index c300e6c..44af4b2 100644 --- a/service/singleton/alertsentinel.go +++ b/service/singleton/alertsentinel.go @@ -2,6 +2,7 @@ package singleton import ( "fmt" + "github.com/jinzhu/copier" "log" "sync" "time" @@ -147,14 +148,17 @@ func checkStatus() { ID][server.ID], alert.Snapshot(AlertsCycleTransferStatsStore[alert.ID], server, DB)) // 发送通知,分为触发报警和恢复通知 max, passed := alert.Check(alertsStore[alert.ID][server.ID]) + // 保存当前服务器状态信息 + curServer := model.Server{} + copier.Copy(&curServer, server) if !passed { alertsPrevState[alert.ID][server.ID] = _RuleCheckFail - message := fmt.Sprintf("$%d$[主机故障] %s(%s) 规则:%s", server.ID, server.Name, IPDesensitize(server.Host.IP), alert.Name) - go SendNotification(alert.NotificationTag, message, true) + message := fmt.Sprintf("[主机故障] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name) + go SendNotification(alert.NotificationTag, message, true, &curServer) } else { if alertsPrevState[alert.ID][server.ID] == _RuleCheckFail { - message := fmt.Sprintf("$%d$[主机恢复] %s(%s) 规则:%s", server.ID, server.Name, IPDesensitize(server.Host.IP), alert.Name) - go SendNotification(alert.NotificationTag, message, true) + message := fmt.Sprintf("[主机恢复] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name) + go SendNotification(alert.NotificationTag, message, true, &curServer) } alertsPrevState[alert.ID][server.ID] = _RuleCheckPass } diff --git a/service/singleton/crontask.go b/service/singleton/crontask.go index 83f8674..8cd26d7 100644 --- a/service/singleton/crontask.go +++ b/service/singleton/crontask.go @@ -3,6 +3,7 @@ package singleton import ( "bytes" "fmt" + "github.com/jinzhu/copier" "sync" "github.com/robfig/cron/v3" @@ -84,7 +85,10 @@ func CronTrigger(cr model.Cron) func() { Type: model.TaskTypeCommand, }) } else { - SendNotification(cr.NotificationTag, fmt.Sprintf("$%d$[任务失败] %s,服务器 %s 离线,无法执行。", s.ID, cr.Name, s.Name), false) + // 保存当前服务器状态信息 + curServer := model.Server{} + copier.Copy(&curServer, s) + SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s,服务器 %s 离线,无法执行。", cr.Name, s.Name), false, &curServer) } } } diff --git a/service/singleton/notification.go b/service/singleton/notification.go index e734eb3..18d487a 100644 --- a/service/singleton/notification.go +++ b/service/singleton/notification.go @@ -4,9 +4,6 @@ import ( "crypto/md5" // #nosec "encoding/hex" "log" - "regexp" - "strconv" - "strings" "sync" "time" @@ -105,7 +102,7 @@ func OnDeleteNotification(id uint64) { } // SendNotification 向指定的通知方式组的所有通知方式发送通知 -func SendNotification(notificationTag string, desc string, mutable bool) { +func SendNotification(notificationTag string, desc string, mutable bool, ext ...*model.Server) { if mutable { // 通知防骚扰策略 nID := hex.EncodeToString(md5.New().Sum([]byte(desc))) // #nosec @@ -146,10 +143,12 @@ func SendNotification(notificationTag string, desc string, mutable bool) { log.Println("尝试通知", n.Name) } for _, n := range NotificationList[notificationTag] { - desc, server := findServerInMsg(desc) ns := model.NotificationServerBundle{ Notification: n, - Server: server, + Server: nil, + } + if len(ext) > 0 { + ns.Server = ext[0] } if err := ns.Send(desc); err != nil { log.Println("NEZHA>> 向 ", n.Name, " 发送通知失败:", err) @@ -158,16 +157,3 @@ func SendNotification(notificationTag string, desc string, mutable bool) { } } } - -// findServerInMsg 通过msg字符串中的$ServerID$ 返回修改后的字符串与Server对象 -func findServerInMsg(msg string) (string, *model.Server) { - reg1 := regexp.MustCompile(`^\$\d+`) - reg2 := regexp.MustCompile(`[^$]+`) - ServerIDStr := reg2.FindString(reg1.FindString(msg)) - ServerID, _ := strconv.ParseUint(ServerIDStr, 10, 64) - // 将原字符串的ServerID标识去除 - if ServerIDStr != "" { - msg = strings.Replace(msg, "$"+ServerIDStr+"$", "", 1) - } - return msg, ServerList[ServerID] -}