diff --git a/src/bean/entity/alert_rules.go b/src/bean/entity/alert_rules.go index ef3f6f66c3e82510fe084c7459ad5c9c3a8d61a6..e4cc8eddf81a82880d7300d038b538d01ca32d2b 100644 --- a/src/bean/entity/alert_rules.go +++ b/src/bean/entity/alert_rules.go @@ -36,9 +36,9 @@ type RulesAlertRange struct { } type AlertCondition struct { - ThresholdsMin int `json:"thresholds_min" form:"thresholds_min" binding:"required,ltfield=ThresholdsMax"` - ThresholdsMax int `json:"thresholds_max" form:"thresholds_max" binding:"required"` - RiskLevel int `json:"risk_level" form:"risk_level" binding:"required,oneof=1 2 3 4"` + ThresholdsMin *float64 `json:"thresholds_min" form:"thresholds_min" binding:"omitempty,required_without=Id"` + ThresholdsMax *float64 `json:"thresholds_max" form:"thresholds_max" binding:"omitempty"` + RiskLevel int `json:"risk_level" form:"risk_level" binding:"required,oneof=1 2 3 4"` } type NotifyRecipients struct { diff --git a/src/controller/alert_rules.go b/src/controller/alert_rules.go index 9bea8b00bc34ff9311f6c063dfe59baa1475f1c9..4bb8946c546fdf5024b060fe09e0ae4f2b5b02c4 100644 --- a/src/controller/alert_rules.go +++ b/src/controller/alert_rules.go @@ -16,24 +16,8 @@ func AddAlertRules(c *gin.Context) { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return } - - /* - [ - { - "thresholds_min": 12, - "thresholds_max": 66, - "risk_level": 1 - }, - { - "thresholds_min": 66, - "thresholds_max": 80, - "risk_level": 2 - } - ] - */ - // TODO 阈值范围判断,阈值上下限是否存在交叉 sort.SliceStable(req.AlertCondition, func(i, j int) bool { - return req.AlertCondition[i].ThresholdsMin < req.AlertCondition[j].ThresholdsMin + return *req.AlertCondition[i].ThresholdsMin < *req.AlertCondition[j].ThresholdsMin }) svc := service.AlertRulesSvc{User: header.GetUser(c)} diff --git a/src/service/alert_rules.go b/src/service/alert_rules.go index 2fa275b5f019bac00ea2ca385a8c7e77e4eb3866..749f101dd76b61315c5a08e8b18e8a9bfde4cec6 100644 --- a/src/service/alert_rules.go +++ b/src/service/alert_rules.go @@ -200,9 +200,14 @@ func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) e UpdatedBy: a.User.SystemAccount, UpdatedAt: now, } + if req.IsEnabled == 2 { + // TODO 关闭状态需要删除prometheus规则 + + } session := db.NewSession() defer session.Close() _, err = session.Table(data.TableName()).Cols("is_enabled,updated_by,updated_at").Where("id = ?", req.Id).Update(&data) + return err } @@ -274,6 +279,7 @@ func (a *AlertRulesSvc) Delete(ids []string) (err error) { } if !exist { _, err = db.NewSession().Where("id = ?", id).Delete(new(entity.AlertRules)) + // TODO 删除普罗米修斯规则 } else { return errors.New("alert_rules_id already exists in opensearch") } diff --git a/src/service/prometheusrule.go b/src/service/prometheusrule.go index 1b8b8563108d72ee196d4cb1e7f65b1e2272b679..77daae572874f4e919eb404054b10b718decb0b2 100644 --- a/src/service/prometheusrule.go +++ b/src/service/prometheusrule.go @@ -52,7 +52,30 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) { "description": "", }, } - expr := fmt.Sprintf("%d <= %s <=%d", v.ThresholdsMin, data.Expr, v.ThresholdsMax) + + var ( + condition int + expr string + ) + + if v.ThresholdsMin != nil { + condition += 1 + } + if v.ThresholdsMax != nil { + condition += 2 + } + + switch condition { + default: + expr = data.Expr + case 1: + expr = fmt.Sprintf("%s <= %s", cast.ToString(v.ThresholdsMin), data.Expr) + case 2: + expr = fmt.Sprintf("%s <=%s", data.Expr, cast.ToString(v.ThresholdsMax)) + case 3: + expr = fmt.Sprintf("%s <= %s <=%s", cast.ToString(v.ThresholdsMin), data.Expr, cast.ToString(v.ThresholdsMax)) + } + rule.Expr = intstr.FromString(expr) group.Rules = append(group.Rules, rule) } @@ -69,3 +92,23 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) { } return } + +func (p *PrometheusRuleSvc) Delete(data response.AlertRulesItem) (err error) { + pr := monitoringv1.PrometheusRule{} + prometheusRuleName := k8s.GetPrometheusRuleName(data.Id) + pr.Name = prometheusRuleName + pr.Namespace = k8s.Namespace + pr.Labels = k8s.AlertDefLabels + + header := make(map[string]string) + header["Authorization"] = "Bearer " + conf.Options.KubernetesToken + prSvc := k8s.PrometheusRule{Header: header} + conf.Logger.Info("创建规则", zap.Any("pr", pr)) + err = prSvc.Delete(pr.Namespace, pr.Name) + if err != nil { + fmt.Println("删除失败" + err.Error()) + } else { + fmt.Println("删除成功") + } + return +}