Merge upstream/master and resolve conflicts

This commit is contained in:
2026-04-16 11:58:30 +08:00
52 changed files with 4612 additions and 419 deletions
+1
View File
@@ -15,6 +15,7 @@ import (
const (
CtxKeyAuthorizedUser = "ckau"
CtxKeyRealIPStr = "ckri"
CtxKeyIsIPMismatch = "ckipm"
)
const (
+22
View File
@@ -70,6 +70,12 @@ type Config struct {
// HTTPS 配置
HTTPS HTTPSConf `koanf:"https" json:"https"`
// TSDB 配置
TSDB TSDBConf `koanf:"tsdb" json:"tsdb"`
// 内存配置
Memory MemoryConf `koanf:"memory" json:"memory"`
k *koanf.Koanf `json:"-"`
filePath string `json:"-"`
}
@@ -81,6 +87,22 @@ type HTTPSConf struct {
TLSKeyPath string `koanf:"tls_key_path" json:"tls_key_path,omitempty"`
}
// TSDBConf TSDB 配置
type TSDBConf struct {
DataPath string `koanf:"data_path" json:"data_path,omitempty"`
RetentionDays uint16 `koanf:"retention_days" json:"retention_days,omitempty"`
MinFreeDiskSpaceGB float64 `koanf:"min_free_disk_space_gb" json:"min_free_disk_space_gb,omitempty"`
MaxMemoryMB int64 `koanf:"max_memory_mb" json:"max_memory_mb,omitempty"`
WriteBufferSize int `koanf:"write_buffer_size" json:"write_buffer_size,omitempty"`
WriteBufferFlushInterval int `koanf:"write_buffer_flush_interval" json:"write_buffer_flush_interval,omitempty"`
}
// MemoryConf 内存配置
type MemoryConf struct {
// GoMemLimitMB Go 运行时内存限制(MB),0 表示不限制
GoMemLimitMB int64 `koanf:"go_mem_limit_mb" json:"go_mem_limit_mb,omitempty"`
}
// Read 读取配置文件并应用
func (c *Config) Read(path string, frontendTemplates []FrontendTemplate) error {
c.k = koanf.New(".")
+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,
+1
View File
@@ -53,6 +53,7 @@ type Service struct {
Target string `json:"target"`
SkipServersRaw string `json:"-"`
Duration uint64 `json:"duration"`
DisplayIndex int `json:"display_index"` // 展示排序,越大越靠前
Notify bool `json:"notify,omitempty"`
NotificationGroupID uint64 `json:"notification_group_id"` // 当前服务监控所属的通知组 ID
Cover uint8 `json:"cover"`
+2 -1
View File
@@ -7,6 +7,7 @@ type ServiceForm struct {
Target string `json:"target,omitempty"`
Type uint8 `json:"type,omitempty"`
Cover uint8 `json:"cover,omitempty"`
DisplayIndex int `json:"display_index,omitempty" default:"0"` // 展示排序,越大越靠前
Notify bool `json:"notify,omitempty" validate:"optional"`
Duration uint64 `json:"duration,omitempty"`
MinLatency float32 `json:"min_latency,omitempty" default:"0.0"`
@@ -26,7 +27,7 @@ type ServiceResponseItem struct {
CurrentDown uint64 `json:"current_down"`
TotalUp uint64 `json:"total_up"`
TotalDown uint64 `json:"total_down"`
Delay *[30]float32 `json:"delay,omitempty"`
Delay *[30]float64 `json:"delay,omitempty"`
Up *[30]uint64 `json:"up,omitempty"`
Down *[30]uint64 `json:"down,omitempty"`
}
+1 -1
View File
@@ -10,7 +10,7 @@ type ServiceHistory struct {
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at,omitempty"`
ServiceID uint64 `gorm:"index:idx_server_id_created_at_service_id_avg_delay" json:"service_id,omitempty"`
ServerID uint64 `gorm:"index:idx_server_id_created_at_service_id_avg_delay" json:"server_id,omitempty"`
AvgDelay float32 `gorm:"index:idx_server_id_created_at_service_id_avg_delay" json:"avg_delay,omitempty"` // 平均延迟,毫秒
AvgDelay float64 `gorm:"index:idx_server_id_created_at_service_id_avg_delay" json:"avg_delay,omitempty"` // 平均延迟,毫秒
Up uint64 `json:"up,omitempty"` // 检查状态良好计数
Down uint64 `json:"down,omitempty"` // 检查状态异常计数
Data string `json:"data,omitempty"`
+52 -6
View File
@@ -1,10 +1,56 @@
package model
// ServiceInfos 服务监控信息(兼容旧API)
type ServiceInfos struct {
ServiceID uint64 `json:"monitor_id"`
ServerID uint64 `json:"server_id"`
ServiceName string `json:"monitor_name"`
ServerName string `json:"server_name"`
CreatedAt []int64 `json:"created_at"`
AvgDelay []float32 `json:"avg_delay"`
ServiceID uint64 `json:"monitor_id"`
ServerID uint64 `json:"server_id"`
ServiceName string `json:"monitor_name"`
ServerName string `json:"server_name"`
DisplayIndex int `json:"display_index"` // 展示排序,越大越靠前
CreatedAt []int64 `json:"created_at"`
AvgDelay []float64 `json:"avg_delay"`
}
// DataPoint 数据点
type DataPoint struct {
Timestamp int64 `json:"ts"`
Delay float64 `json:"delay"`
Status uint8 `json:"status"` // 1=成功, 0=失败
}
// ServiceHistorySummary 服务历史统计摘要
type ServiceHistorySummary struct {
AvgDelay float64 `json:"avg_delay"`
UpPercent float32 `json:"up_percent"`
TotalUp uint64 `json:"total_up"`
TotalDown uint64 `json:"total_down"`
DataPoints []DataPoint `json:"data_points,omitempty"`
}
// ServerServiceStats 某服务器对某服务的统计
type ServerServiceStats struct {
ServerID uint64 `json:"server_id"`
ServerName string `json:"server_name,omitempty"`
Stats ServiceHistorySummary `json:"stats"`
}
// ServiceHistoryResponse 服务历史查询响应
type ServiceHistoryResponse struct {
ServiceID uint64 `json:"service_id"`
ServiceName string `json:"service_name,omitempty"`
Servers []ServerServiceStats `json:"servers"`
}
// ServerMetricsDataPoint 服务器指标数据点
type ServerMetricsDataPoint struct {
Timestamp int64 `json:"ts"`
Value float64 `json:"value"`
}
// ServerMetricsResponse 服务器指标历史查询响应
type ServerMetricsResponse struct {
ServerID uint64 `json:"server_id"`
ServerName string `json:"server_name,omitempty"`
Metric string `json:"metric"`
DataPoints []ServerMetricsDataPoint `json:"data_points"`
}
+3 -2
View File
@@ -10,8 +10,8 @@ type SettingForm struct {
InstallHost string `json:"install_host,omitempty" validate:"optional"`
CustomCode string `json:"custom_code,omitempty" validate:"optional"`
CustomCodeDashboard string `json:"custom_code_dashboard,omitempty" validate:"optional"`
WebRealIPHeader string `json:"web_real_ip_header,omitempty" validate:"optional"` // 前端真实IP
AgentRealIPHeader string `json:"agent_real_ip_header,omitempty" validate:"optional"` // Agent真实IP
WebRealIPHeader string `json:"web_real_ip_header,omitempty" validate:"optional"` // 前端真实IP
AgentRealIPHeader string `json:"agent_real_ip_header,omitempty" validate:"optional"` // Agent真实IP
UserTemplate string `json:"user_template,omitempty" validate:"optional"`
AgentTLS bool `json:"tls,omitempty" validate:"optional"`
@@ -42,4 +42,5 @@ type SettingResponse struct {
Version string `json:"version,omitempty"`
FrontendTemplates []FrontendTemplate `json:"frontend_templates,omitempty"`
TSDBEnabled bool `json:"tsdb_enabled"`
}