From e13a06a31ad61b1dcad0b1ee734666c91899568e Mon Sep 17 00:00:00 2001 From: liugq Date: Thu, 2 Jun 2022 18:39:43 +0800 Subject: [PATCH] fixed alerting bug --- model/alerting/alert.go | 1 + plugin/api/alerting/message.go | 9 +++++++++ plugin/api/alerting/rule.go | 19 ++++++++++++++++--- plugin/api/index_management/indices.go | 1 + plugin/api/init.go | 10 +++++----- service/alerting/elasticsearch/engine.go | 5 +++-- service/alerting/engine.go | 2 +- service/alerting/init.go | 2 +- 8 files changed, 37 insertions(+), 12 deletions(-) diff --git a/model/alerting/alert.go b/model/alerting/alert.go index 2307b888..89a5b6b9 100644 --- a/model/alerting/alert.go +++ b/model/alerting/alert.go @@ -60,6 +60,7 @@ type AlertMessage struct { Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"` Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"` RuleID string `json:"rule_id" elastic_mapping:"rule_id: { type: keyword,copy_to:search_text }"` + ResourceID string `json:"resource_id" elastic_mapping:"resource_id: { type: keyword,copy_to:search_text }"` Title string `json:"title" elastic_mapping:"title: { type: keyword,copy_to:search_text }"` Message string `json:"message" elastic_mapping:"content: { type: keyword,copy_to:search_text }"` Status string `json:"status" elastic_mapping:"status: { type: keyword,copy_to:search_text }"` diff --git a/plugin/api/alerting/message.go b/plugin/api/alerting/message.go index f7cc0f1e..50386caf 100644 --- a/plugin/api/alerting/message.go +++ b/plugin/api/alerting/message.go @@ -153,6 +153,15 @@ func (h *AlertAPI) searchAlertMessage(w http.ResponseWriter, req *http.Request, if ruleID != "" { mustBuilder.WriteString(fmt.Sprintf(`,{"term":{"rule_id":{"value":"%s"}}}`, ruleID)) } + clusterFilter, hasAllPrivilege := h.GetClusterFilter(req, "resource_id") + if !hasAllPrivilege && clusterFilter == nil { + h.WriteJSON(w, elastic.SearchResponse{}, http.StatusOK) + return + } + if !hasAllPrivilege { + mustBuilder.WriteString(",") + mustBuilder.Write(util.MustToJSONBytes(clusterFilter)) + } if sort != "" { sortParts := strings.Split(sort, ",") diff --git a/plugin/api/alerting/rule.go b/plugin/api/alerting/rule.go index 4b5adad8..e0991463 100644 --- a/plugin/api/alerting/rule.go +++ b/plugin/api/alerting/rule.go @@ -81,7 +81,7 @@ func (alertAPI *AlertAPI) createRule(w http.ResponseWriter, req *http.Request, p ID: rule.ID, Interval: rule.Schedule.Interval, Description: rule.Metrics.Expression, - Task: eng.GenerateTask(&rule), + Task: eng.GenerateTask(rule), } task.RegisterScheduleTask(ruleTask) task.StartTask(ruleTask.ID) @@ -309,7 +309,7 @@ func (alertAPI *AlertAPI) updateRule(w http.ResponseWriter, req *http.Request, p ID: rule.ID, Interval: rule.Schedule.Interval, Description: rule.Metrics.Expression, - Task: eng.GenerateTask(rule), + Task: eng.GenerateTask(*rule), } task.RegisterScheduleTask(ruleTask) task.StartTask(ruleTask.ID) @@ -390,6 +390,15 @@ func (alertAPI *AlertAPI) searchRule(w http.ResponseWriter, req *http.Request, p mustQuery := []util.MapStr{ } + clusterFilter, hasAllPrivilege := alertAPI.GetClusterFilter(req, "resource.resource_id") + if !hasAllPrivilege && clusterFilter == nil { + alertAPI.WriteJSON(w, elastic.SearchResponse{ + }, http.StatusOK) + return + } + if !hasAllPrivilege { + mustQuery = append(mustQuery, clusterFilter) + } if keyword != "" { mustQuery = append(mustQuery, util.MapStr{ "match": util.MapStr{ @@ -568,7 +577,7 @@ func (alertAPI *AlertAPI) enableRule(w http.ResponseWriter, req *http.Request, p ID: obj.ID, Interval: obj.Schedule.Interval, Description: obj.Metrics.Expression, - Task: eng.GenerateTask(&obj), + Task: eng.GenerateTask(obj), } task.RegisterScheduleTask(ruleTask) task.StartTask(ruleTask.ID) @@ -749,6 +758,10 @@ func getRuleMetricData( rule *alerting.Rule, filterParam *alerting.FilterParam) if sampleData == nil { sampleData = targetData } + var label = strings.Join(md.GroupValues, "-") + if label == "" { + label, _ = rule.GetOrInitExpression() + } metricItem.Lines = append(metricItem.Lines, &common.MetricLine{ Data: targetData, BucketSize: filterParam.BucketSize, diff --git a/plugin/api/index_management/indices.go b/plugin/api/index_management/indices.go index 227668e3..1b2b53cc 100644 --- a/plugin/api/index_management/indices.go +++ b/plugin/api/index_management/indices.go @@ -117,6 +117,7 @@ func (handler APIHandler) HandleDeleteIndexAction(w http.ResponseWriter, req *ht } func (handler APIHandler) HandleCreateIndexAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + targetClusterID := ps.ByName("id") client := elastic.GetClient(targetClusterID) indexName := ps.ByName("index") diff --git a/plugin/api/init.go b/plugin/api/init.go index 5670da83..1c5368d5 100644 --- a/plugin/api/init.go +++ b/plugin/api/init.go @@ -36,11 +36,11 @@ func Init(cfg *config.AppConfig) { api.HandleAPIMethod(api.DELETE, path.Join(pathPrefix, "rebuild/:id"), handler.HandleDeleteRebuildAction) api.HandleAPIMethod(api.GET, path.Join(esPrefix, "_cat/indices"), handler.RequireLogin(handler.HandleGetIndicesAction)) - api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_mappings"), handler.HandleGetMappingsAction) - api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_settings"), handler.HandleGetSettingsAction) - api.HandleAPIMethod(api.PUT, path.Join(esPrefix, "index/:index/_settings"),handler.HandleUpdateSettingsAction) - api.HandleAPIMethod(api.DELETE, path.Join(esPrefix, "index/:index"), handler.HandleDeleteIndexAction) - api.HandleAPIMethod(api.POST, path.Join(esPrefix, "index/:index"), handler.HandleCreateIndexAction) + api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_mappings"), handler.IndexRequired(handler.HandleGetMappingsAction, "indices.get_mapping")) + api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_settings"), handler.IndexRequired(handler.HandleGetSettingsAction, "indices.get_settings")) + api.HandleAPIMethod(api.PUT, path.Join(esPrefix, "index/:index/_settings"), handler.IndexRequired(handler.HandleUpdateSettingsAction, "indices.put_settings")) + api.HandleAPIMethod(api.DELETE, path.Join(esPrefix, "index/:index"), handler.IndexRequired(handler.HandleDeleteIndexAction, "indices.delete")) + api.HandleAPIMethod(api.POST, path.Join(esPrefix, "index/:index"), handler.IndexRequired(handler.HandleCreateIndexAction, "indices.create")) api.HandleAPIMethod(api.POST, path.Join(pathPrefix, "elasticsearch/command"), handler.RequirePermission(handler.HandleAddCommonCommandAction, enum.PermissionCommandWrite)) api.HandleAPIMethod(api.PUT, path.Join(pathPrefix, "elasticsearch/command/:cid"), handler.RequirePermission(handler.HandleSaveCommonCommandAction, enum.PermissionCommandWrite)) diff --git a/service/alerting/elasticsearch/engine.go b/service/alerting/elasticsearch/engine.go index 0d44db3d..f2626b85 100644 --- a/service/alerting/elasticsearch/engine.go +++ b/service/alerting/elasticsearch/engine.go @@ -693,6 +693,7 @@ func (engine *Engine) Do(rule *alerting.Rule) error { Created: time.Now(), Updated: time.Now(), ID: util.GetUUID(), + ResourceID: rule.Resource.ID, Status: alerting.MessageStateAlerting, Severity: severity, Title: alertItem.Title, @@ -953,7 +954,7 @@ func performChannel(channel *alerting.Channel, ctx map[string]interface{}) ([]by executeResult, err := act.Execute() return executeResult, err, message } -func (engine *Engine) GenerateTask(rule *alerting.Rule) func(ctx context.Context) { +func (engine *Engine) GenerateTask(rule alerting.Rule) func(ctx context.Context) { return func(ctx context.Context) { defer func() { if err := recover(); err != nil { @@ -961,7 +962,7 @@ func (engine *Engine) GenerateTask(rule *alerting.Rule) func(ctx context.Context debug.PrintStack() } }() - err := engine.Do(rule) + err := engine.Do(&rule) if err != nil { log.Error(err) } diff --git a/service/alerting/engine.go b/service/alerting/engine.go index a6a4debd..17e8232a 100644 --- a/service/alerting/engine.go +++ b/service/alerting/engine.go @@ -15,7 +15,7 @@ type Engine interface { GenerateQuery(rule *alerting.Rule, filterParam *alerting.FilterParam) (interface{}, error) ExecuteQuery(rule *alerting.Rule, filterParam *alerting.FilterParam)(*alerting.QueryResult, error) CheckCondition(rule *alerting.Rule)(*alerting.ConditionResult, error) - GenerateTask(rule *alerting.Rule) func(ctx context.Context) + GenerateTask(rule alerting.Rule) func(ctx context.Context) Test(rule *alerting.Rule) ([]alerting.ActionExecutionResult, error) GetTargetMetricData(rule *alerting.Rule, isFilterNaN bool, filterParam *alerting.FilterParam)([]alerting.MetricData, *alerting.QueryResult, error) } diff --git a/service/alerting/init.go b/service/alerting/init.go index eed05844..759a3791 100644 --- a/service/alerting/init.go +++ b/service/alerting/init.go @@ -39,7 +39,7 @@ func InitTasks() error { ID: rule.ID, Interval: rule.Schedule.Interval, Description: rule.Metrics.Expression, - Task: eng.GenerateTask(rule), + Task: eng.GenerateTask(*rule), }) task.StartTask(rule.ID) }