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 @@