Merge pull request #67 from yumusb/main

优化告警消息
This commit is contained in:
dushixiang
2026-01-29 20:27:35 +08:00
committed by GitHub
4 changed files with 39 additions and 6 deletions

View File

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

View File

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

View File

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

View File

@@ -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, " / ")
}