diff --git a/pkg/monitor/alerting/conditions/metricquery.go b/pkg/monitor/alerting/conditions/metricquery.go index 2170a3b651..5b3a4251de 100644 --- a/pkg/monitor/alerting/conditions/metricquery.go +++ b/pkg/monitor/alerting/conditions/metricquery.go @@ -73,7 +73,7 @@ func NewMetricQueryCondition(models []*monitor.AlertCondition) (*MetricQueryCond return cond, nil } -func (query *MetricQueryCondition) ExecuteQuery(userCred mcclient.TokenCredential, skipCheckSeries bool) (*monitor.MetricsQueryResult, error) { +func (query *MetricQueryCondition) ExecuteQuery(userCred mcclient.TokenCredential, scope string, skipCheckSeries bool) (*monitor.MetricsQueryResult, error) { firstCond := query.QueryCons[0] timeRange := tsdb.NewTimeRange(firstCond.Query.From, firstCond.Query.To) ctx := gocontext.Background() @@ -137,7 +137,7 @@ func (query *MetricQueryCondition) ExecuteQuery(userCred mcclient.TokenCredentia }() s := auth.GetSession(ctx, userCred, "") - ress, regionErr = firstCond.getOnecloudResources(s, false) + ress, regionErr = firstCond.getOnecloudResources(s, scope, false) if err != nil { regionErr = errors.Wrap(regionErr, "get resources from region") return diff --git a/pkg/monitor/alerting/conditions/nodataquery.go b/pkg/monitor/alerting/conditions/nodataquery.go index cc6db4d0f1..e8d257ed9a 100644 --- a/pkg/monitor/alerting/conditions/nodataquery.go +++ b/pkg/monitor/alerting/conditions/nodataquery.go @@ -88,7 +88,7 @@ serLoop: } } } - allResources, err := c.GetQueryResources(auth.GetAdminSession(context.Ctx, ""), true) + allResources, err := c.GetQueryResources(auth.GetAdminSession(context.Ctx, ""), "system", true) if err != nil { return nil, errors.Wrap(err, "GetQueryResources err") } diff --git a/pkg/monitor/alerting/conditions/query.go b/pkg/monitor/alerting/conditions/query.go index 6a1d98d875..307ff6cfd6 100644 --- a/pkg/monitor/alerting/conditions/query.go +++ b/pkg/monitor/alerting/conditions/query.go @@ -552,8 +552,8 @@ func (c *QueryCondition) setResType() { } } -func (c *QueryCondition) GetQueryResources(s *mcclient.ClientSession, showDetails bool) ([]jsonutils.JSONObject, error) { - allRes, err := c.getOnecloudResources(s, showDetails) +func (c *QueryCondition) GetQueryResources(s *mcclient.ClientSession, scope string, showDetails bool) ([]jsonutils.JSONObject, error) { + allRes, err := c.getOnecloudResources(s, scope, showDetails) if err != nil { return nil, errors.Wrap(err, "getOnecloudResources error") } @@ -561,10 +561,7 @@ func (c *QueryCondition) GetQueryResources(s *mcclient.ClientSession, showDetail return allRes, nil } -func (c *QueryCondition) getOnecloudResources(s *mcclient.ClientSession, showDetails bool) ([]jsonutils.JSONObject, error) { - var err error - allResources := make([]jsonutils.JSONObject, 0) - +func (c *QueryCondition) getOnecloudResources(s *mcclient.ClientSession, scope string, showDetails bool) ([]jsonutils.JSONObject, error) { query := jsonutils.NewDict() query.Add(jsonutils.NewStringArray([]string{"running", "ready"}), "status") // query.Add(jsonutils.NewString("true"), "admin") @@ -574,53 +571,61 @@ func (c *QueryCondition) getOnecloudResources(s *mcclient.ClientSession, showDet // return nil, errors.Wrap(err, "NoDataQueryCondition convertTagsQuery error") // } //} + var ( + err error + manager modulebase.Manager + allResources = make([]jsonutils.JSONObject, 0) + ) 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("enabled", jsonutils.NewInt(1)) - allResources, err = ListAllResources(s, &mc_mds.Hosts, query, showDetails) + manager = &mc_mds.Hosts case monitor.METRIC_RES_TYPE_GUEST: - allResources, err = ListAllResources(s, &mc_mds.Servers, query, showDetails) + manager = &mc_mds.Servers case monitor.METRIC_RES_TYPE_AGENT: - allResources, err = ListAllResources(s, &mc_mds.Servers, query, showDetails) + manager = &mc_mds.Servers case monitor.METRIC_RES_TYPE_RDS: - allResources, err = ListAllResources(s, &mc_mds.DBInstance, query, showDetails) + manager = &mc_mds.DBInstance case monitor.METRIC_RES_TYPE_REDIS: - allResources, err = ListAllResources(s, &mc_mds.ElasticCache, query, showDetails) + manager = &mc_mds.ElasticCache case monitor.METRIC_RES_TYPE_OSS: - allResources, err = ListAllResources(s, &mc_mds.Buckets, query, showDetails) + manager = &mc_mds.Buckets case monitor.METRIC_RES_TYPE_CLOUDACCOUNT: query.Remove("status") query.Add(jsonutils.NewBool(true), "enabled") - allResources, err = ListAllResources(s, &mc_mds.Cloudaccounts, query, showDetails) + manager = &mc_mds.Cloudaccounts case monitor.METRIC_RES_TYPE_TENANT: - allResources, err = ListAllResources(s, &identity.Projects, query, showDetails) + manager = &identity.Projects case monitor.METRIC_RES_TYPE_DOMAIN: - allResources, err = ListAllResources(s, &identity.Domains, query, showDetails) + manager = &identity.Domains case monitor.METRIC_RES_TYPE_STORAGE: query.Remove("status") - allResources, err = ListAllResources(s, &mc_mds.Storages, query, showDetails) + manager = &mc_mds.Storages default: query := jsonutils.NewDict() query.Set("brand", jsonutils.NewString(hostconsts.TELEGRAF_TAG_ONECLOUD_BRAND)) query.Set("host-type", jsonutils.NewString(hostconsts.TELEGRAF_TAG_KEY_HYPERVISOR)) - allResources, err = ListAllResources(s, &mc_mds.Hosts, query, showDetails) + manager = &mc_mds.Hosts } + allResources, err = ListAllResources(s, manager, query, scope, showDetails) if err != nil { - return nil, errors.Wrap(err, "NoDataQueryCondition Host list error") + return nil, errors.Wrapf(err, "ListAllResources for %s with query %s, scope: %s, showDetails: %v", manager.GetKeyword(), query, scope, showDetails) } return allResources, nil } -func ListAllResources(s *mcclient.ClientSession, manager modulebase.Manager, params *jsonutils.JSONDict, showDetails bool) ([]jsonutils.JSONObject, error) { +func ListAllResources(s *mcclient.ClientSession, manager modulebase.Manager, params *jsonutils.JSONDict, scope string, showDetails bool) ([]jsonutils.JSONObject, error) { if params == nil { params = jsonutils.NewDict() } if s.GetToken().HasSystemAdminPrivilege() { params.Add(jsonutils.NewString("system"), "scope") } + if scope != "" { + params.Add(jsonutils.NewString(scope), "scope") + } params.Add(jsonutils.NewInt(int64(options.Options.APIListBatchSize)), "limit") params.Add(jsonutils.NewBool(showDetails), "details") var count int diff --git a/pkg/monitor/metricquery/interfaces.go b/pkg/monitor/metricquery/interfaces.go index e88388b66a..a230dec5d5 100644 --- a/pkg/monitor/metricquery/interfaces.go +++ b/pkg/monitor/metricquery/interfaces.go @@ -20,7 +20,7 @@ import ( ) type MetricQuery interface { - ExecuteQuery(userCred mcclient.TokenCredential, forceCheckSeries bool) (*monitor.MetricsQueryResult, error) + ExecuteQuery(userCred mcclient.TokenCredential, scope string, forceCheckSeries bool) (*monitor.MetricsQueryResult, error) } type QueryFactory func(model []*monitor.AlertCondition) (MetricQuery, error) diff --git a/pkg/monitor/models/unifiedmonitor.go b/pkg/monitor/models/unifiedmonitor.go index 6d2bb3e2af..2a02549e48 100644 --- a/pkg/monitor/models/unifiedmonitor.go +++ b/pkg/monitor/models/unifiedmonitor.go @@ -338,7 +338,7 @@ func doQuery(userCred mcclient.TokenCredential, query monitor.MetricQueryInput) if err != nil { return nil, errors.Wrap(err, "factory") } - metrics, err := metricQ.ExecuteQuery(userCred, query.SkipCheckSeries) + metrics, err := metricQ.ExecuteQuery(userCred, query.Scope, query.SkipCheckSeries) if err != nil { return nil, errors.Wrap(err, "ExecuteQuery") }