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/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..3e631d4 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,42 @@ func (n *Notification) Send(message string) error { return nil } -func replaceParamsInString(str string, message string, mod func(string) string) string { +// 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.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 9cd42c3..e6a007a 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,56 @@ 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: &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, + 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 +166,28 @@ func TestNotification(t *testing.T) { expectBody: `{"msg":"msg"}`, expectHeader: map[string]string{"asd": "dsa11"}, }, + { + url: "https://example.com/?m=#NEZHA#", + 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","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/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 @@
- +
diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 2bca2c5..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("[任务成功] %s ,服务器:%s,日志:\n%s", 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("[任务失败] %s ,服务器:%s,日志:\n%s", 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 a97397a..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("[主机故障] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name) - go SendNotification(alert.NotificationTag, message, true) + go SendNotification(alert.NotificationTag, message, true, &curServer) } else { if alertsPrevState[alert.ID][server.ID] == _RuleCheckFail { message := fmt.Sprintf("[主机恢复] %s(%s) 规则:%s", server.Name, IPDesensitize(server.Host.IP), alert.Name) - go SendNotification(alert.NotificationTag, message, true) + 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 54aa65e..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("[任务失败] %s,服务器 %s 离线,无法执行。", 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 48c0099..18d487a 100644 --- a/service/singleton/notification.go +++ b/service/singleton/notification.go @@ -102,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 @@ -143,7 +143,14 @@ func SendNotification(notificationTag string, desc string, mutable bool) { log.Println("尝试通知", n.Name) } for _, n := range NotificationList[notificationTag] { - if err := n.Send(desc); err != nil { + ns := model.NotificationServerBundle{ + Notification: n, + Server: nil, + } + if len(ext) > 0 { + ns.Server = ext[0] + } + if err := ns.Send(desc); err != nil { log.Println("NEZHA>> 向 ", n.Name, " 发送通知失败:", err) } else { log.Println("NEZHA>> 向 ", n.Name, " 发送通知成功:") 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