From 53161cb0f972c76dfbf318603f4f764b90f5a440 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Mon, 7 Nov 2022 02:17:59 +0800 Subject: [PATCH] fix(monitor): only query enabled host for alerting (#15301) --- cmd/climc/shell/monitor/monitor_resource.go | 2 +- pkg/apis/monitor/alert.go | 19 ++++++++++++------- .../options/monitor/monitor_resource.go | 2 +- pkg/monitor/alerting/conditions/query.go | 3 ++- pkg/monitor/alerting/rule.go | 19 ++++++++++++------- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/cmd/climc/shell/monitor/monitor_resource.go b/cmd/climc/shell/monitor/monitor_resource.go index 0dbbd09743..ff216ffc02 100644 --- a/cmd/climc/shell/monitor/monitor_resource.go +++ b/cmd/climc/shell/monitor/monitor_resource.go @@ -21,5 +21,5 @@ import ( func init() { cmd := NewResourceCmd(modules.MonitorResourceManager) - cmd.Get("", new(options.MonitorResourceJointAlertOptions)) + cmd.GetProperty(new(options.MonitorResourceJointAlertOptions)) } diff --git a/pkg/apis/monitor/alert.go b/pkg/apis/monitor/alert.go index b794d093b8..502bdc3c3f 100644 --- a/pkg/apis/monitor/alert.go +++ b/pkg/apis/monitor/alert.go @@ -190,13 +190,18 @@ type EvalMatch struct { type AlertTestRunOutput struct { apis.Meta - Firing bool `json:"firing"` - EvalMatches []*EvalMatch `json:"eval_matches"` - Logs []*ResultLogEntry `json:"logs"` - Error error `json:"error"` - ConditionEvals string `json:"condition_evals"` - StartTime time.Time `json:"start_time"` - EndTime time.Time `json:"end_time"` + Firing bool `json:"firing"` + IsTestRun bool `json:"is_test_run"` + IsDebug bool `json:"is_debug"` + EvalMatches []*EvalMatch `json:"eval_matches"` + AlertOKEvalMatches []*EvalMatch `json:"alert_ok_eval_matches"` + Logs []*ResultLogEntry `json:"logs"` + Error error `json:"error"` + ConditionEvals string `json:"condition_evals"` + StartTime time.Time `json:"start_time"` + EndTime time.Time `json:"end_time"` + NoDataFound bool `json:"no_data_found"` + PrevAlertState string `json:"prev_alert_state"` } type AlertPauseInput struct { diff --git a/pkg/mcclient/options/monitor/monitor_resource.go b/pkg/mcclient/options/monitor/monitor_resource.go index 974699230c..41c704c883 100644 --- a/pkg/mcclient/options/monitor/monitor_resource.go +++ b/pkg/mcclient/options/monitor/monitor_resource.go @@ -27,6 +27,6 @@ func (o *MonitorResourceJointAlertOptions) Params() (jsonutils.JSONObject, error return options.ListStructToParams(o) } -func (o *MonitorResourceJointAlertOptions) GetId() string { +func (o *MonitorResourceJointAlertOptions) Property() string { return "alert" } diff --git a/pkg/monitor/alerting/conditions/query.go b/pkg/monitor/alerting/conditions/query.go index ee580d66c3..42f035fee1 100644 --- a/pkg/monitor/alerting/conditions/query.go +++ b/pkg/monitor/alerting/conditions/query.go @@ -566,7 +566,8 @@ func (c *QueryCondition) getOnecloudResources() ([]jsonutils.JSONObject, error) switch c.ResType { case monitor.METRIC_RES_TYPE_HOST: query := jsonutils.NewDict() - query.Set("host-type", jsonutils.NewString(hostconsts.TELEGRAF_TAG_KEY_HYPERVISOR)) + query.Set("host_type", jsonutils.NewString(hostconsts.TELEGRAF_TAG_KEY_HYPERVISOR)) + query.Set("enabled", jsonutils.NewInt(1)) allResources, err = ListAllResources(&mc_mds.Hosts, query) case monitor.METRIC_RES_TYPE_GUEST: allResources, err = ListAllResources(&mc_mds.Servers, query) diff --git a/pkg/monitor/alerting/rule.go b/pkg/monitor/alerting/rule.go index 3859a47865..258be0fca7 100644 --- a/pkg/monitor/alerting/rule.go +++ b/pkg/monitor/alerting/rule.go @@ -254,13 +254,18 @@ func (_ AlertRuleTester) DoTest(ruleDef *models.SAlert, userCred mcclient.TokenC func (ctx *EvalContext) ToTestRunResult() *monitor.AlertTestRunOutput { return &monitor.AlertTestRunOutput{ - Firing: ctx.Firing, - EvalMatches: ctx.EvalMatches, - Logs: ctx.Logs, - Error: ctx.Error, - ConditionEvals: ctx.ConditionEvals, - StartTime: ctx.StartTime, - EndTime: ctx.EndTime, + Firing: ctx.Firing, + IsTestRun: ctx.IsTestRun, + IsDebug: ctx.IsDebug, + EvalMatches: ctx.EvalMatches, + AlertOKEvalMatches: ctx.AlertOkEvalMatches, + Logs: ctx.Logs, + Error: ctx.Error, + ConditionEvals: ctx.ConditionEvals, + StartTime: ctx.StartTime, + EndTime: ctx.EndTime, + NoDataFound: ctx.NoDataFound, + PrevAlertState: string(ctx.PrevAlertState), } }