mirror of
https://github.com/dushixiang/pika.git
synced 2026-06-08 18:02:53 +08:00
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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, " / ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user