From 7f922e73822197109c7291d38314403ec2f6c0ee Mon Sep 17 00:00:00 2001 From: yumusb <43062104+yumusb@users.noreply.github.com> Date: Thu, 29 Jan 2026 19:20:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=91=8A=E8=AD=A6=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化告警消息,IP去重以及增加监控项的名称 --- internal/protocol/message.go | 1 + internal/service/alert_service.go | 22 ++++++++++++++++++++-- internal/service/monitor_service.go | 8 ++++++++ internal/service/notifier.go | 14 ++++++++++---- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/internal/protocol/message.go b/internal/protocol/message.go index e185b38..4cf6def 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -302,6 +302,7 @@ type MonitorData struct { AgentId string `json:"agentId"` // 探针 ID AgentName string `json:"agentName"` // 探针名称 MonitorId string `json:"monitorId"` // 监控项ID + MonitorName string `json:"monitorName,omitempty"` // 监控项名称 Type string `json:"type"` // 监控类型: http, tcp Target string `json:"target,omitempty"` // 监控目标 Status string `json:"status"` // 状态: up, down diff --git a/internal/service/alert_service.go b/internal/service/alert_service.go index ac0939b..5200856 100644 --- a/internal/service/alert_service.go +++ b/internal/service/alert_service.go @@ -452,16 +452,25 @@ func (s *AlertService) checkCertAlert(ctx context.Context, config *models.AlertC s.logger.Info("触发证书告警", zap.String("agentId", agent.ID), zap.String("monitorId", monitor.MonitorId), + zap.String("monitorName", monitor.MonitorName), zap.String("target", monitor.Target), zap.Float64("certDaysLeft", certDaysLeft), zap.Float64("threshold", config.Rules.CertThreshold), ) + // 构建告警消息,优先使用监控任务名称 + var message string + if monitor.MonitorName != "" { + message = fmt.Sprintf("监控项 %s (%s) 的HTTPS证书剩余天数%.0f天,低于阈值%.0f天", monitor.MonitorName, monitor.Target, certDaysLeft, config.Rules.CertThreshold) + } else { + message = fmt.Sprintf("监控项 %s 的HTTPS证书剩余天数%.0f天,低于阈值%.0f天", monitor.Target, certDaysLeft, config.Rules.CertThreshold) + } + record := &models.AlertRecord{ AgentID: agent.ID, AgentName: agent.Name, AlertType: "cert", - Message: fmt.Sprintf("监控项 %s 的HTTPS证书剩余天数%.0f天,低于阈值%.0f天", monitor.Target, certDaysLeft, config.Rules.CertThreshold), + Message: message, Threshold: config.Rules.CertThreshold, ActualValue: certDaysLeft, Level: s.calculateCertLevel(certDaysLeft), @@ -614,16 +623,25 @@ func (s *AlertService) fireServiceDownAlert(ctx context.Context, config *models. s.logger.Info("触发服务下线告警", zap.String("agentId", agent.ID), zap.String("monitorId", monitor.MonitorId), + zap.String("monitorName", monitor.MonitorName), zap.String("target", monitor.Target), zap.Int("duration", state.Duration), ) + // 构建告警消息,优先使用监控任务名称 + var message string + if monitor.MonitorName != "" { + message = fmt.Sprintf("监控项 %s (%s) 持续离线%d秒", monitor.MonitorName, monitor.Target, state.Duration) + } else { + message = fmt.Sprintf("监控项 %s 持续离线%d秒", monitor.Target, state.Duration) + } + // 创建告警记录 record := &models.AlertRecord{ AgentID: agent.ID, AgentName: agent.Name, AlertType: "service", - Message: fmt.Sprintf("监控项 %s 持续离线%d秒", monitor.Target, state.Duration), + Message: message, Threshold: 0, ActualValue: float64(state.Duration), Level: "critical", diff --git a/internal/service/monitor_service.go b/internal/service/monitor_service.go index 0e5f860..497e224 100644 --- a/internal/service/monitor_service.go +++ b/internal/service/monitor_service.go @@ -382,6 +382,10 @@ func (s *MonitorService) GetLatestMonitorMetricsByType(ctx context.Context, moni var result []protocol.MonitorData for _, task := range monitorTasks { monitorData := s.metricService.GetMonitorAgentStats(task.ID) + // 填充监控任务名称 + for i := range monitorData { + monitorData[i].MonitorName = task.Name + } result = append(result, monitorData...) } @@ -400,6 +404,10 @@ func (s *MonitorService) GetAllLatestMonitorMetrics(ctx context.Context) ([]prot var result []protocol.MonitorData for _, task := range monitorTasks { monitorData := s.metricService.GetMonitorAgentStats(task.ID) + // 填充监控任务名称 + for i := range monitorData { + monitorData[i].MonitorName = task.Name + } result = append(result, monitorData...) } return result, nil diff --git a/internal/service/notifier.go b/internal/service/notifier.go index a3f7eed..2182384 100644 --- a/internal/service/notifier.go +++ b/internal/service/notifier.go @@ -139,15 +139,21 @@ func maskIPAddress(ip string) string { } func joinAgentIPs(ip string, ipv4 string, ipv6 string) string { - parts := make([]string, 0, 2) - if ip != "" { + parts := make([]string, 0, 3) + seen := make(map[string]bool) + + // 按优先级添加,避免重复 + if ip != "" && !seen[ip] { parts = append(parts, ip) + seen[ip] = true } - if ipv4 != "" { + if ipv4 != "" && !seen[ipv4] { parts = append(parts, ipv4) + seen[ipv4] = true } - if ipv6 != "" { + if ipv6 != "" && !seen[ipv6] { parts = append(parts, ipv6) + seen[ipv6] = true } return strings.Join(parts, " / ") }