Commit 26fa830f authored by 李科's avatar 李科

perf: 完善预警规则设置风险等级阈值上下限逻辑

parent fecb1d1c
...@@ -36,9 +36,9 @@ type RulesAlertRange struct { ...@@ -36,9 +36,9 @@ type RulesAlertRange struct {
} }
type AlertCondition struct { type AlertCondition struct {
ThresholdsMin int `json:"thresholds_min" form:"thresholds_min" binding:"required,ltfield=ThresholdsMax"` ThresholdsMin *float64 `json:"thresholds_min" form:"thresholds_min" binding:"omitempty,required_without=Id"`
ThresholdsMax int `json:"thresholds_max" form:"thresholds_max" binding:"required"` 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"` RiskLevel int `json:"risk_level" form:"risk_level" binding:"required,oneof=1 2 3 4"`
} }
type NotifyRecipients struct { type NotifyRecipients struct {
......
...@@ -16,24 +16,8 @@ func AddAlertRules(c *gin.Context) { ...@@ -16,24 +16,8 @@ func AddAlertRules(c *gin.Context) {
SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil)
return 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 { 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)} svc := service.AlertRulesSvc{User: header.GetUser(c)}
......
...@@ -200,9 +200,14 @@ func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) e ...@@ -200,9 +200,14 @@ func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) e
UpdatedBy: a.User.SystemAccount, UpdatedBy: a.User.SystemAccount,
UpdatedAt: now, UpdatedAt: now,
} }
if req.IsEnabled == 2 {
// TODO 关闭状态需要删除prometheus规则
}
session := db.NewSession() session := db.NewSession()
defer session.Close() defer session.Close()
_, err = session.Table(data.TableName()).Cols("is_enabled,updated_by,updated_at").Where("id = ?", req.Id).Update(&data) _, err = session.Table(data.TableName()).Cols("is_enabled,updated_by,updated_at").Where("id = ?", req.Id).Update(&data)
return err return err
} }
...@@ -274,6 +279,7 @@ func (a *AlertRulesSvc) Delete(ids []string) (err error) { ...@@ -274,6 +279,7 @@ func (a *AlertRulesSvc) Delete(ids []string) (err error) {
} }
if !exist { if !exist {
_, err = db.NewSession().Where("id = ?", id).Delete(new(entity.AlertRules)) _, err = db.NewSession().Where("id = ?", id).Delete(new(entity.AlertRules))
// TODO 删除普罗米修斯规则
} else { } else {
return errors.New("alert_rules_id already exists in opensearch") return errors.New("alert_rules_id already exists in opensearch")
} }
......
...@@ -52,7 +52,30 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) { ...@@ -52,7 +52,30 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) {
"description": "", "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) rule.Expr = intstr.FromString(expr)
group.Rules = append(group.Rules, rule) group.Rules = append(group.Rules, rule)
} }
...@@ -69,3 +92,23 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) { ...@@ -69,3 +92,23 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) {
} }
return 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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment