package service import ( "errors" "github.com/prometheus/alertmanager/notify/webhook" "github.com/spf13/cast" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/constant" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" "xorm.io/xorm" ) type AlertWebhookSvc struct { User entity.SystemUserInfo } // AlertWebhook Alertmanager 回调接口 func (a *AlertWebhookSvc) AlertWebhook(session *xorm.Session, req webhook.Message) (err error) { now := jsontime.Now() alertSvc := AlertSvc{User: a.User} alertRulesSvc := AlertRulesSvc{User: a.User} for _, alert := range req.Alerts { var ( alertRulesId string riskLevelStr string riskLevel int currentValueStr string currentValue float64 ok bool alertItem response.AlertItem alertRulesItem response.AlertRulesItem alertStatus int alertCondition entity.AlertCondition ) if alertRulesId, ok = alert.Labels["alert_rules_id"]; !ok { return errors.New("alert_rules_id not found in the map") } if riskLevelStr, ok = alert.Labels["risk_level"]; !ok { return errors.New("risk_level not found in the map") } riskLevel = cast.ToInt(riskLevelStr) if currentValueStr, ok = alert.Annotations["value"]; !ok { return errors.New("value not found in the map") } currentValue = cast.ToFloat64(currentValueStr) alertRulesItem, err = alertRulesSvc.GetDataById(request.DetailAlertRules{Id: alertRulesId}) if err != nil { return } alertItem, err = alertSvc.GetDataByAlertRulesIdAndRiskLevel(alertRulesId, riskLevel, 2) if err != nil { return } var newData bool if alertItem.Id == 0 { // 新数据 newData = true } switch newData { case true: // 新增数据到OpenSearch max := alertSvc.CatCount(OpenSearchIndex) if max == 0 { return } alertId := max + 1 for _, v := range alertRulesItem.AlertCondition { if v.RiskLevel == riskLevel { alertCondition = v break } } err = alertSvc.DocCreate(request.CreateAlert{Alert: entity.Alert{ Id: alertId, AlertPoint: alertRulesItem.ClassParentName + "/" + alertRulesItem.MetricName, AlertRulesId: alertRulesItem.Id, AlertRulesName: alertRulesItem.MetricName, RiskLevel: riskLevel, AlertTime: jsontime.Time(alert.StartsAt), ClassId: alertRulesItem.ClassId, ClassParentName: alertRulesItem.ClassParentName, ClassName: alertRulesItem.ClassName, MetricConfigId: alertRulesItem.MetricConfigId, MetricConfigName: alertRulesItem.MetricConfigName, AlertRuleType: alertRulesItem.AlertRuleType, AlertRuleTypeName: alertRulesItem.AlertRuleTypeName, CurrentValue: currentValue, AlertCondition: alertCondition, NotificationCount: len(alertRulesItem.NotifyRecipients), PushCount: 1, LastPushTime: now, Status: constant.AlertNotRecovered, CreatedBy: a.User.SystemAccount, CreatedAt: now, UpdatedBy: a.User.SystemAccount, UpdatedAt: now, }}) if err != nil { return } default: // 旧数据,更新或解决 switch alert.Status { case "firing": alertStatus = constant.AlertNotRecovered case "resolved": alertStatus = constant.AlertRecovered } err = alertSvc.Update(request.UpdateAlert{ Id: alertItem.Id, CurrentValue: currentValue, RiskLevel: riskLevel, Status: alertStatus, }) } } return }