From f6a5362a972dd9f80a52e44ac252a2cfc6dd549f Mon Sep 17 00:00:00 2001 From: Akkia Date: Sun, 17 Apr 2022 17:38:45 +0800 Subject: [PATCH 1/6] =?UTF-8?q?WIP:=20=E6=B7=BB=E5=8A=A0=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E7=9A=84=E5=8D=A0=E4=BD=8D=E7=AC=A6=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=9F=BA=E4=BA=8E=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=8C=87=E6=A0=87=E6=9E=84=E9=80=A0=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=9A=84HTTP=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/dashboard/controller/member_api.go | 6 ++++- model/notification.go | 30 ++++++++++++++------- model/notification_test.go | 36 ++++++++++++++++++++++++-- service/singleton/notification.go | 18 ++++++++++++- 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index 88b54e4..03c3a02 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -412,7 +412,11 @@ func (ma *memberAPI) addOrEditNotification(c *gin.Context) { verifySSL := nf.VerifySSL == "on" n.VerifySSL = &verifySSL n.ID = nf.ID - err = n.Send("这是测试消息") + ns := model.NotificationServerBundle{ + Notification: &n, + Server: nil, + } + err = ns.Send("这是测试消息") } if err == nil { // 保证Tag不为空 diff --git a/model/notification.go b/model/notification.go index fcbfb56..acc20b0 100644 --- a/model/notification.go +++ b/model/notification.go @@ -25,6 +25,11 @@ const ( NotificationRequestMethodPOST ) +type NotificationServerBundle struct { + Notification *Notification + Server *Server +} + type Notification struct { Common Name string @@ -37,8 +42,9 @@ type Notification struct { VerifySSL *bool } -func (n *Notification) reqURL(message string) string { - return replaceParamsInString(n.URL, message, func(msg string) string { +func (ns *NotificationServerBundle) reqURL(message string) string { + n := ns.Notification + return replaceParamsInString(ns.Server, n.URL, message, func(msg string) string { return url.QueryEscape(msg) }) } @@ -53,13 +59,14 @@ func (n *Notification) reqMethod() (string, error) { return "", errors.New("不支持的请求方式") } -func (n *Notification) reqBody(message string) (string, error) { +func (ns *NotificationServerBundle) reqBody(message string) (string, error) { + n := ns.Notification if n.RequestMethod == NotificationRequestMethodGET || message == "" { return "", nil } switch n.RequestType { case NotificationRequestTypeJSON: - return replaceParamsInString(n.RequestBody, message, func(msg string) string { + return replaceParamsInString(ns.Server, n.RequestBody, message, func(msg string) string { msgBytes, _ := utils.Json.Marshal(msg) return string(msgBytes)[1 : len(msgBytes)-1] }), nil @@ -70,7 +77,7 @@ func (n *Notification) reqBody(message string) (string, error) { } params := url.Values{} for k, v := range data { - params.Add(k, replaceParamsInString(v, message, nil)) + params.Add(k, replaceParamsInString(ns.Server, v, message, nil)) } return params.Encode(), nil } @@ -102,9 +109,9 @@ func (n *Notification) setRequestHeader(req *http.Request) error { return nil } -func (n *Notification) Send(message string) error { +func (ns *NotificationServerBundle) Send(message string) error { var verifySSL bool - + n := ns.Notification if n.VerifySSL != nil && *n.VerifySSL { verifySSL = true } @@ -115,7 +122,7 @@ func (n *Notification) Send(message string) error { } client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10} - reqBody, err := n.reqBody(message) + reqBody, err := ns.reqBody(message) if err != nil { return err } @@ -125,7 +132,7 @@ func (n *Notification) Send(message string) error { return err } - req, err := http.NewRequest(reqMethod, n.reqURL(message), strings.NewReader(reqBody)) + req, err := http.NewRequest(reqMethod, ns.reqURL(message), strings.NewReader(reqBody)) if err != nil { return err } @@ -150,11 +157,14 @@ func (n *Notification) Send(message string) error { return nil } -func replaceParamsInString(str string, message string, mod func(string) string) string { +func replaceParamsInString(s *Server, str string, message string, mod func(string) string) string { if mod != nil { str = strings.ReplaceAll(str, "#NEZHA#", mod(message)) } else { str = strings.ReplaceAll(str, "#NEZHA#", message) } + if s != nil { + str = strings.ReplaceAll(str, "#SERVER#", s.Name) + } return str } diff --git a/model/notification_test.go b/model/notification_test.go index 9cd42c3..f36ec3b 100644 --- a/model/notification_test.go +++ b/model/notification_test.go @@ -4,6 +4,7 @@ import ( "net/http" "strings" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -35,8 +36,27 @@ func execCase(t *testing.T, item testSt) { RequestBody: item.body, RequestHeader: item.header, } - assert.Equal(t, item.expectURL, n.reqURL(msg)) - reqBody, err := n.reqBody(msg) + server := Server{ + Common: Common{}, + Name: "ServerName", + Tag: "", + Secret: "", + Note: "", + DisplayIndex: 0, + Host: nil, + State: nil, + LastActive: time.Time{}, + TaskClose: nil, + TaskStream: nil, + PrevHourlyTransferIn: 0, + PrevHourlyTransferOut: 0, + } + ns := NotificationServerBundle{ + Notification: &n, + Server: &server, + } + assert.Equal(t, item.expectURL, ns.reqURL(msg)) + reqBody, err := ns.reqBody(msg) assert.Nil(t, err) assert.Equal(t, item.expectBody, reqBody) reqMethod, err := n.reqMethod() @@ -117,6 +137,18 @@ func TestNotification(t *testing.T) { expectBody: `{"msg":"msg"}`, expectHeader: map[string]string{"asd": "dsa11"}, }, + { + url: "https://example.com/?m=#NEZHA#", + body: `{"Server":"#SERVER#"}`, + reqMethod: NotificationRequestMethodPOST, + header: `{"asd":"dsa11"}`, + reqType: NotificationRequestTypeJSON, + expectURL: "https://example.com/?m=" + msg, + expectMethod: http.MethodPost, + expectContentType: reqTypeJSON, + expectBody: `{"Server":"ServerName"}`, + expectHeader: map[string]string{"asd": "dsa11"}, + }, } for _, c := range cases { diff --git a/service/singleton/notification.go b/service/singleton/notification.go index 48c0099..ea80c49 100644 --- a/service/singleton/notification.go +++ b/service/singleton/notification.go @@ -4,6 +4,8 @@ import ( "crypto/md5" // #nosec "encoding/hex" "log" + "regexp" + "strconv" "sync" "time" @@ -143,10 +145,24 @@ func SendNotification(notificationTag string, desc string, mutable bool) { log.Println("尝试通知", n.Name) } for _, n := range NotificationList[notificationTag] { - if err := n.Send(desc); err != nil { + server := findServerInMsg(desc) + ns := model.NotificationServerBundle{ + Notification: n, + Server: server, + } + if err := ns.Send(desc); err != nil { log.Println("NEZHA>> 向 ", n.Name, " 发送通知失败:", err) } else { log.Println("NEZHA>> 向 ", n.Name, " 发送通知成功:") } } } + +// findServerInMsg 通过msg字符串中的$ServerID$ 返回Server对象 未找到会返回nil +func findServerInMsg(msg string) *model.Server { + reg1 := regexp.MustCompile(`^\$\d+`) + reg2 := regexp.MustCompile(`[^$]+`) + ServerIDStr := reg2.FindString(reg1.FindString(msg)) + ServerID, _ := strconv.ParseUint(ServerIDStr, 10, 64) + return ServerList[ServerID] +} From b5b521e8cfb9de614be86ad31fd304117e72e05c Mon Sep 17 00:00:00 2001 From: Akkia Date: Sun, 17 Apr 2022 18:08:30 +0800 Subject: [PATCH 2/6] =?UTF-8?q?optimize:=20=E5=9C=A8=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=88=B0Server=E5=AF=B9=E8=B1=A1=E5=90=8E=E7=A7=BB=E9=99=A4Ser?= =?UTF-8?q?verID=E6=A0=87=E8=AF=86=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/singleton/crontask.go | 2 +- service/singleton/notification.go | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/service/singleton/crontask.go b/service/singleton/crontask.go index 54aa65e..83f8674 100644 --- a/service/singleton/crontask.go +++ b/service/singleton/crontask.go @@ -84,7 +84,7 @@ func CronTrigger(cr model.Cron) func() { Type: model.TaskTypeCommand, }) } else { - SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s,服务器 %s 离线,无法执行。", cr.Name, s.Name), false) + SendNotification(cr.NotificationTag, fmt.Sprintf("$%d$[任务失败] %s,服务器 %s 离线,无法执行。", s.ID, cr.Name, s.Name), false) } } } diff --git a/service/singleton/notification.go b/service/singleton/notification.go index ea80c49..e734eb3 100644 --- a/service/singleton/notification.go +++ b/service/singleton/notification.go @@ -6,6 +6,7 @@ import ( "log" "regexp" "strconv" + "strings" "sync" "time" @@ -145,7 +146,7 @@ func SendNotification(notificationTag string, desc string, mutable bool) { log.Println("尝试通知", n.Name) } for _, n := range NotificationList[notificationTag] { - server := findServerInMsg(desc) + desc, server := findServerInMsg(desc) ns := model.NotificationServerBundle{ Notification: n, Server: server, @@ -158,11 +159,15 @@ func SendNotification(notificationTag string, desc string, mutable bool) { } } -// findServerInMsg 通过msg字符串中的$ServerID$ 返回Server对象 未找到会返回nil -func findServerInMsg(msg string) *model.Server { +// 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) - return ServerList[ServerID] + // 将原字符串的ServerID标识去除 + if ServerIDStr != "" { + msg = strings.Replace(msg, "$"+ServerIDStr+"$", "", 1) + } + return msg, ServerList[ServerID] } From e20b37b590400e2ea66ddf3b84cf9d6795d892d2 Mon Sep 17 00:00:00 2001 From: Akkia Date: Sun, 17 Apr 2022 21:06:30 +0800 Subject: [PATCH 3/6] =?UTF-8?q?update:=20=E5=A2=9E=E5=8A=A0=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E5=8D=A0=E4=BD=8D=E7=AC=A6=E4=B8=8E=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/notification.go | 34 +++++++++++++++-- model/notification_test.go | 59 +++++++++++++++++++++++++----- service/rpc/nezha.go | 4 +- service/singleton/alertsentinel.go | 4 +- 4 files changed, 84 insertions(+), 17 deletions(-) diff --git a/model/notification.go b/model/notification.go index acc20b0..3e631d4 100644 --- a/model/notification.go +++ b/model/notification.go @@ -157,14 +157,42 @@ func (ns *NotificationServerBundle) Send(message string) error { return nil } +// replaceParamInString 替换字符串中的占位符 func replaceParamsInString(s *Server, str string, message string, mod func(string) string) string { if mod != nil { str = strings.ReplaceAll(str, "#NEZHA#", mod(message)) + if s != nil { + str = strings.ReplaceAll(str, "#SERVER.NAME#", mod(s.Name)) + str = strings.ReplaceAll(str, "#SERVER.IP#", mod(s.Host.IP)) + str = strings.ReplaceAll(str, "#SERVER.CPU#", mod(fmt.Sprintf("%f", s.State.CPU))) + str = strings.ReplaceAll(str, "#SERVER.MEM#", mod(fmt.Sprintf("%d", s.State.MemUsed))) + str = strings.ReplaceAll(str, "#SERVER.SWAP#", mod(fmt.Sprintf("%d", s.State.SwapUsed))) + str = strings.ReplaceAll(str, "#SERVER.DISK#", mod(fmt.Sprintf("%d", s.State.DiskUsed))) + str = strings.ReplaceAll(str, "#SERVER.NETINSPEED#", mod(fmt.Sprintf("%d", s.State.NetInSpeed))) + str = strings.ReplaceAll(str, "#SERVER.NETOUTSPEED#", mod(fmt.Sprintf("%d", s.State.NetOutSpeed))) + str = strings.ReplaceAll(str, "#SERVER.TRANSFERIN#", mod(fmt.Sprintf("%d", s.State.NetInTransfer))) + str = strings.ReplaceAll(str, "#SERVER.TRANSFEROUT#", mod(fmt.Sprintf("%d", s.State.NetOutTransfer))) + str = strings.ReplaceAll(str, "#SERVER.LOAD1#", mod(fmt.Sprintf("%f", s.State.Load1))) + str = strings.ReplaceAll(str, "#SERVER.LOAD5#", mod(fmt.Sprintf("%f", s.State.Load5))) + str = strings.ReplaceAll(str, "#SERVER.LOAD15#", mod(fmt.Sprintf("%f", s.State.Load15))) + } } else { str = strings.ReplaceAll(str, "#NEZHA#", message) - } - if s != nil { - str = strings.ReplaceAll(str, "#SERVER#", s.Name) + if s != nil { + str = strings.ReplaceAll(str, "#SERVER.NAME#", s.Name) + str = strings.ReplaceAll(str, "#SERVER.IP#", s.Host.IP) + str = strings.ReplaceAll(str, "#SERVER.CPU#", fmt.Sprintf("%f", s.State.CPU)) + str = strings.ReplaceAll(str, "#SERVER.MEM#", fmt.Sprintf("%d", s.State.MemUsed)) + str = strings.ReplaceAll(str, "#SERVER.SWAP#", fmt.Sprintf("%d", s.State.SwapUsed)) + str = strings.ReplaceAll(str, "#SERVER.DISK#", fmt.Sprintf("%d", s.State.DiskUsed)) + str = strings.ReplaceAll(str, "#SERVER.NETINSPEED#", fmt.Sprintf("%d", s.State.NetInSpeed)) + str = strings.ReplaceAll(str, "#SERVER.NETOUTSPEED#", fmt.Sprintf("%d", s.State.NetOutSpeed)) + str = strings.ReplaceAll(str, "#SERVER.TRANSFERIN#", fmt.Sprintf("%d", s.State.NetInTransfer)) + str = strings.ReplaceAll(str, "#SERVER.TRANSFEROUT#", fmt.Sprintf("%d", s.State.NetOutTransfer)) + str = strings.ReplaceAll(str, "#SERVER.LOAD1#", fmt.Sprintf("%f", s.State.Load1)) + str = strings.ReplaceAll(str, "#SERVER.LOAD5#", fmt.Sprintf("%f", s.State.Load5)) + str = strings.ReplaceAll(str, "#SERVER.LOAD15#", fmt.Sprintf("%f", s.State.Load15)) + } } return str } diff --git a/model/notification_test.go b/model/notification_test.go index f36ec3b..e6a007a 100644 --- a/model/notification_test.go +++ b/model/notification_test.go @@ -37,14 +37,43 @@ func execCase(t *testing.T, item testSt) { RequestHeader: item.header, } server := Server{ - Common: Common{}, - Name: "ServerName", - Tag: "", - Secret: "", - Note: "", - DisplayIndex: 0, - Host: nil, - State: nil, + Common: Common{}, + Name: "ServerName", + Tag: "", + Secret: "", + Note: "", + DisplayIndex: 0, + Host: &Host{ + Platform: "", + PlatformVersion: "", + CPU: nil, + MemTotal: 0, + DiskTotal: 0, + SwapTotal: 0, + Arch: "", + Virtualization: "", + BootTime: 0, + IP: "1.1.1.1", + CountryCode: "", + Version: "", + }, + State: &HostState{ + CPU: 0, + MemUsed: 0, + SwapUsed: 8888, + DiskUsed: 0, + NetInTransfer: 0, + NetOutTransfer: 0, + NetInSpeed: 0, + NetOutSpeed: 0, + Uptime: 0, + Load1: 0, + Load5: 0, + Load15: 0, + TcpConnCount: 0, + UdpConnCount: 0, + ProcessCount: 0, + }, LastActive: time.Time{}, TaskClose: nil, TaskStream: nil, @@ -139,16 +168,26 @@ func TestNotification(t *testing.T) { }, { url: "https://example.com/?m=#NEZHA#", - body: `{"Server":"#SERVER#"}`, + body: `{"Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":#SERVER.SWAP#}`, reqMethod: NotificationRequestMethodPOST, header: `{"asd":"dsa11"}`, reqType: NotificationRequestTypeJSON, expectURL: "https://example.com/?m=" + msg, expectMethod: http.MethodPost, expectContentType: reqTypeJSON, - expectBody: `{"Server":"ServerName"}`, + expectBody: `{"Server":"ServerName","ServerIP":"1.1.1.1","ServerSWAP":8888}`, expectHeader: map[string]string{"asd": "dsa11"}, }, + { + url: "https://example.com/?m=#NEZHA#", + body: `{"#NEZHA#":"#NEZHA#","Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":"#SERVER.SWAP#"}`, + reqMethod: NotificationRequestMethodPOST, + reqType: NotificationRequestTypeForm, + expectURL: "https://example.com/?m=" + msg, + expectMethod: http.MethodPost, + expectContentType: reqTypeForm, + expectBody: "%23NEZHA%23=" + msg + "&Server=ServerName&ServerIP=1.1.1.1&ServerSWAP=8888", + }, } for _, c := range cases { diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 2bca2c5..441ca75 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -29,10 +29,10 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece singleton.ServerLock.RLock() defer singleton.ServerLock.RUnlock() if cr.PushSuccessful && r.GetSuccessful() { - singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[任务成功] %s ,服务器:%s,日志:\n%s", cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false) + singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("$%d$[任务成功] %s ,服务器:%s,日志:\n%s", singleton.ServerList[clientID].ID, cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false) } if !r.GetSuccessful() { - singleton.SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s ,服务器:%s,日志:\n%s", cr.Name, singleton.ServerList[clientID].Name, r.GetData()), false) + 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.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 a97397a..c300e6c 100644 --- a/service/singleton/alertsentinel.go +++ b/service/singleton/alertsentinel.go @@ -149,11 +149,11 @@ func checkStatus() { max, passed := alert.Check(alertsStore[alert.ID][server.ID]) if !passed { alertsPrevState[alert.ID][server.ID] = _RuleCheckFail - message := fmt.Sprintf("[主机故障] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name) + message := fmt.Sprintf("$%d$[主机故障] %s(%s) 规则:%s", server.ID, server.Name, IPDesensitize(server.Host.IP), alert.Name) go SendNotification(alert.NotificationTag, message, true) } else { if alertsPrevState[alert.ID][server.ID] == _RuleCheckFail { - message := fmt.Sprintf("[主机恢复] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name) + message := fmt.Sprintf("$%d$[主机恢复] %s(%s) 规则:%s", server.ID, server.Name, IPDesensitize(server.Host.IP), alert.Name) go SendNotification(alert.NotificationTag, message, true) } alertsPrevState[alert.ID][server.ID] = _RuleCheckPass From 1f08b579a4f28dca7448832a0f6442eae8d966a4 Mon Sep 17 00:00:00 2001 From: Akkia Date: Mon, 18 Apr 2022 19:59:42 +0800 Subject: [PATCH 4/6] =?UTF-8?q?update:=20=E5=8F=91=E9=80=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=89=8D=E5=AF=B9=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E8=BF=9B=E8=A1=8C=E6=8B=B7=E8=B4=9D=20=E4=BF=9D?= =?UTF-8?q?=E8=AF=81=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81=E7=9A=84=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=8C=87=E6=A0=87=E4=B8=8E=E8=A7=A6=E5=8F=91=E8=AF=A5?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E7=9A=84=E7=8A=B6=E6=80=81=E6=8C=87=E6=A0=87?= =?UTF-8?q?=E7=9B=B8=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] -} From 0418c288a0db87091863490071956e59705b1d5a Mon Sep 17 00:00:00 2001 From: Akkia Date: Mon, 18 Apr 2022 20:55:36 +0800 Subject: [PATCH 5/6] =?UTF-8?q?update:=20=E5=9C=A8=20=E6=B7=BB=E5=8A=A0/?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E9=80=9A=E7=9F=A5=E6=96=B9=E5=BC=8F=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=20=E5=A2=9E=E5=8A=A0=E5=8F=AF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E5=8D=A0=E4=BD=8D=E7=AC=A6=E6=A0=87=E8=AF=86=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/template/component/notification.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource/template/component/notification.html b/resource/template/component/notification.html index b37d910..693468e 100644 --- a/resource/template/component/notification.html +++ b/resource/template/component/notification.html @@ -36,7 +36,7 @@
- +
From 055b47e5d43cc5f8935884783cc36dc91c5aad8c Mon Sep 17 00:00:00 2001 From: naiba Date: Mon, 18 Apr 2022 21:10:07 +0800 Subject: [PATCH 6/6] dashboard v0.12.21 --- README.md | 2 +- service/singleton/singleton.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7281768..fe24c6a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
LOGO designed by 熊大 .

-    +   

:trollface: 哪吒监控 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。

diff --git a/service/singleton/singleton.go b/service/singleton/singleton.go index acd54d8..e1a1177 100644 --- a/service/singleton/singleton.go +++ b/service/singleton/singleton.go @@ -12,7 +12,7 @@ import ( "github.com/naiba/nezha/pkg/utils" ) -var Version = "v0.12.20" // !!记得修改 README 中的 badge 版本!! +var Version = "v0.12.21" // !!记得修改 README 中的 badge 版本!! var ( Conf *model.Config