feat(notification): add option to convert metric units in request body (#1156)

* feat(notification): add option to convert metric units in request body

* ignore gosec

* rename fields
This commit is contained in:
UUBulb
2026-01-10 17:29:10 +08:00
committed by GitHub
parent 6067a41038
commit 302d278644
5 changed files with 124 additions and 29 deletions
+39 -13
View File
@@ -33,13 +33,14 @@ type NotificationServerBundle struct {
type Notification struct {
Common
Name string `json:"name"`
URL string `json:"url"`
RequestMethod uint8 `json:"request_method"`
RequestType uint8 `json:"request_type"`
RequestHeader string `json:"request_header" gorm:"type:longtext"`
RequestBody string `json:"request_body" gorm:"type:longtext"`
VerifyTLS *bool `json:"verify_tls,omitempty"`
Name string `json:"name"`
URL string `json:"url"`
RequestMethod uint8 `json:"request_method"`
RequestType uint8 `json:"request_type"`
RequestHeader string `json:"request_header" gorm:"type:longtext"`
RequestBody string `json:"request_body" gorm:"type:longtext"`
VerifyTLS *bool `json:"verify_tls,omitempty"`
FormatMetricUnits *bool `json:"format_metric_units,omitempty"`
}
func (ns *NotificationServerBundle) reqURL(message string) string {
@@ -172,10 +173,19 @@ func (ns *NotificationServerBundle) replaceParamsInString(str string, message st
replacements = append(replacements,
"#SERVER.NAME#", mod(ns.Server.Name),
"#SERVER.ID#", mod(fmt.Sprintf("%d", ns.Server.ID)),
"#SERVER.CPU#", mod(fmt.Sprintf("%f", ns.Server.State.CPU)),
"#SERVER.MEM#", mod(fmt.Sprintf("%d", ns.Server.State.MemUsed)),
"#SERVER.SWAP#", mod(fmt.Sprintf("%d", ns.Server.State.SwapUsed)),
"#SERVER.DISK#", mod(fmt.Sprintf("%d", ns.Server.State.DiskUsed)),
// Converted metrics
"#SERVER.CPU#", mod(ns.formatUsage(false, ns.Server.State.CPU)),
"#SERVER.MEM#", mod(ns.formatUsage(true, float64(ns.Server.State.MemUsed)/float64(ns.Server.Host.MemTotal))),
"#SERVER.SWAP#", mod(ns.formatUsage(true, float64(ns.Server.State.SwapUsed)/float64(ns.Server.Host.SwapTotal))),
"#SERVER.DISK#", mod(ns.formatUsage(true, float64(ns.Server.State.DiskUsed)/float64(ns.Server.Host.DiskTotal))),
"#SERVER.SPEEDIN#", mod(fmt.Sprintf("%s/s", ns.formatSize(ns.Server.State.NetInSpeed))),
"#SERVER.SPEEDOUT#", mod(fmt.Sprintf("%s/s", ns.formatSize(ns.Server.State.NetOutSpeed))),
"#SERVER.TRANSFERIN#", mod(ns.formatSize(ns.Server.State.NetInTransfer)),
"#SERVER.TRANSFEROUT#", mod(ns.formatSize(ns.Server.State.NetOutTransfer)),
// Raw metrics
"#SERVER.CPUUSED#", mod(fmt.Sprintf("%f", ns.Server.State.CPU)),
"#SERVER.MEMUSED#", mod(fmt.Sprintf("%d", ns.Server.State.MemUsed)),
"#SERVER.SWAPUSED#", mod(fmt.Sprintf("%d", ns.Server.State.SwapUsed)),
"#SERVER.DISKUSED#", mod(fmt.Sprintf("%d", ns.Server.State.DiskUsed)),
@@ -184,8 +194,6 @@ func (ns *NotificationServerBundle) replaceParamsInString(str string, message st
"#SERVER.DISKTOTAL#", mod(fmt.Sprintf("%d", ns.Server.Host.DiskTotal)),
"#SERVER.NETINSPEED#", mod(fmt.Sprintf("%d", ns.Server.State.NetInSpeed)),
"#SERVER.NETOUTSPEED#", mod(fmt.Sprintf("%d", ns.Server.State.NetOutSpeed)),
"#SERVER.TRANSFERIN#", mod(fmt.Sprintf("%d", ns.Server.State.NetInTransfer)),
"#SERVER.TRANSFEROUT#", mod(fmt.Sprintf("%d", ns.Server.State.NetOutTransfer)),
"#SERVER.NETINTRANSFER#", mod(fmt.Sprintf("%d", ns.Server.State.NetInTransfer)),
"#SERVER.NETOUTTRANSFER#", mod(fmt.Sprintf("%d", ns.Server.State.NetOutTransfer)),
"#SERVER.LOAD1#", mod(fmt.Sprintf("%f", ns.Server.State.Load1)),
@@ -219,3 +227,21 @@ func (ns *NotificationServerBundle) replaceParamsInString(str string, message st
replacer := strings.NewReplacer(replacements...)
return replacer.Replace(str)
}
func (ns *NotificationServerBundle) formatUsage(toPercentage bool, usage float64) string {
if ns.Notification.FormatMetricUnits != nil && *ns.Notification.FormatMetricUnits {
if toPercentage {
usage = usage * 100
}
return fmt.Sprintf("%.2f %%", usage)
}
return fmt.Sprintf("%f", usage)
}
func (ns *NotificationServerBundle) formatSize(size uint64) string {
if ns.Notification.FormatMetricUnits != nil && *ns.Notification.FormatMetricUnits {
return utils.Bytes(size)
}
return fmt.Sprintf("%d", size)
}
+9 -8
View File
@@ -1,12 +1,13 @@
package model
type NotificationForm struct {
Name string `json:"name,omitempty" minLength:"1"`
URL string `json:"url,omitempty"`
RequestMethod uint8 `json:"request_method,omitempty"`
RequestType uint8 `json:"request_type,omitempty"`
RequestHeader string `json:"request_header,omitempty"`
RequestBody string `json:"request_body,omitempty"`
VerifyTLS bool `json:"verify_tls,omitempty" validate:"optional"`
SkipCheck bool `json:"skip_check,omitempty" validate:"optional"`
Name string `json:"name,omitempty" minLength:"1"`
URL string `json:"url,omitempty"`
RequestMethod uint8 `json:"request_method,omitempty"`
RequestType uint8 `json:"request_type,omitempty"`
RequestHeader string `json:"request_header,omitempty"`
RequestBody string `json:"request_body,omitempty"`
VerifyTLS bool `json:"verify_tls,omitempty" validate:"optional"`
SkipCheck bool `json:"skip_check,omitempty" validate:"optional"`
FormatMetricUnits bool `json:"format_metric_units,omitempty" validate:"optional"`
}
+32 -8
View File
@@ -27,12 +27,14 @@ type testSt struct {
}
func execCase(t *testing.T, item testSt) {
trueBool := true
n := Notification{
URL: item.url,
RequestMethod: item.reqMethod,
RequestType: item.reqType,
RequestBody: item.body,
RequestHeader: item.header,
URL: item.url,
RequestMethod: item.reqMethod,
RequestType: item.reqType,
RequestBody: item.body,
RequestHeader: item.header,
FormatMetricUnits: &trueBool,
}
server := Server{
Common: Common{},
@@ -45,7 +47,7 @@ func execCase(t *testing.T, item testSt) {
CPU: nil,
MemTotal: 0,
DiskTotal: 0,
SwapTotal: 0,
SwapTotal: 8888,
Arch: "",
Virtualization: "",
BootTime: 0,
@@ -184,7 +186,29 @@ func TestNotification(t *testing.T) {
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":#SERVER.SWAP#}`,
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":"100.00 %"}`,
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=100.00+%25",
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":#SERVER.SWAPUSED#}`,
reqMethod: NotificationRequestMethodPOST,
header: `{"asd":"dsa11"}`,
reqType: NotificationRequestTypeJSON,
@@ -196,7 +220,7 @@ func TestNotification(t *testing.T) {
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"#NEZHA#":"#NEZHA#","Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":"#SERVER.SWAP#"}`,
body: `{"#NEZHA#":"#NEZHA#","Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":"#SERVER.SWAPUSED#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeForm,
expectURL: "https://example.com/?m=" + msg,