From 06482d2f206d6662d052a0916f137639450d1658 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Wed, 3 Dec 2025 16:18:01 +0800 Subject: [PATCH] fix(monitor): filter baremetal host for no data query (#23892) --- pkg/monitor/alerting/conditions/query.go | 2 + pkg/monitor/models/monitor_resource_sync.go | 157 -------------------- 2 files changed, 2 insertions(+), 157 deletions(-) diff --git a/pkg/monitor/alerting/conditions/query.go b/pkg/monitor/alerting/conditions/query.go index 1f56745237..f8f8c6b92f 100644 --- a/pkg/monitor/alerting/conditions/query.go +++ b/pkg/monitor/alerting/conditions/query.go @@ -26,6 +26,7 @@ import ( "yunion.io/x/pkg/util/sets" "yunion.io/x/pkg/utils" + "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/apis/monitor" "yunion.io/x/onecloud/pkg/hostman/hostinfo/hostconsts" "yunion.io/x/onecloud/pkg/mcclient" @@ -633,6 +634,7 @@ func (c *QueryCondition) getOnecloudResources(s *mcclient.ClientSession, scope s case monitor.METRIC_RES_TYPE_HOST: // query.Set("host_type", jsonutils.NewString(hostconsts.TELEGRAF_TAG_KEY_HYPERVISOR)) queryStatus = append(queryStatus, "unknown") + query.Set("filter.0", jsonutils.NewString(fmt.Sprintf("host_type.notequals('%s')", compute.HOST_TYPE_BAREMETAL))) query.Set("enabled", jsonutils.NewInt(1)) manager = &mc_mds.Hosts case monitor.METRIC_RES_TYPE_GUEST: diff --git a/pkg/monitor/models/monitor_resource_sync.go b/pkg/monitor/models/monitor_resource_sync.go index 17dffff2da..72cd57af92 100644 --- a/pkg/monitor/models/monitor_resource_sync.go +++ b/pkg/monitor/models/monitor_resource_sync.go @@ -15,20 +15,11 @@ package models import ( - "context" "fmt" - "yunion.io/x/jsonutils" - "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/apis/monitor" - "yunion.io/x/onecloud/pkg/cloudcommon/db" - "yunion.io/x/onecloud/pkg/mcclient" - "yunion.io/x/onecloud/pkg/mcclient/auth" - "yunion.io/x/onecloud/pkg/mcclient/modulebase" - mc_mds "yunion.io/x/onecloud/pkg/mcclient/modules/compute" - "yunion.io/x/onecloud/pkg/mcclient/modules/identity" ) var ( @@ -72,7 +63,6 @@ type SyncObject struct { } type IResourceSync interface { - SyncResources(ctx context.Context, userCred mcclient.TokenCredential, param jsonutils.JSONObject) error SyncType() string } @@ -211,150 +201,3 @@ func (g *StorageResourceSync) SyncType() string { func newSyncObj(sync IResourceSync) SyncObject { return SyncObject{sync: sync} } - -func (self *SyncObject) SyncResources(ctx context.Context, userCred mcclient.TokenCredential, - param jsonutils.JSONObject) error { - log.Errorf("start sync %s", self.sync.SyncType()) - resources, err := GetOnecloudResources(self.sync.SyncType()) - if err != nil { - return errors.Wrapf(err, fmt.Sprintf("syncType:%s GetOnecloudResources err", self.sync.SyncType())) - } - input := monitor.MonitorResourceListInput{ - OnlyResId: true, - ResType: self.sync.SyncType(), - } - monResources, err := MonitorResourceManager.GetMonitorResources(input) - if err != nil { - return errors.Wrap(err, "GetMonitorResources err") - } - errs := make([]error, 0) -monLoop: - for i, _ := range monResources { - for index, res := range resources { - resId, _ := res.GetString("id") - if resId == monResources[i].ResId { - resource, err := MonitorResourceManager.GetMonitorResourceById(monResources[i].GetId()) - if err != nil { - errs = append(errs, err) - continue monLoop - } - _, err = db.Update(resource, func() error { - res.(*jsonutils.JSONDict).Remove("id") - res.Unmarshal(resource) - return nil - }) - if err != nil { - errs = append(errs, errors.Wrapf(err, "monitorResource:%s Update err", resource.Name)) - continue monLoop - } - resource.UpdateAlertState() - if index == len(resources)-1 { - resources = resources[0:index] - } else { - resources = append(resources[0:index], resources[index+1:]...) - } - - index-- - continue monLoop - } - } - resource, _ := MonitorResourceManager.GetMonitorResourceById(monResources[i].GetId()) - err := resource.RealDelete(ctx, userCred) - if err != nil { - errs = append(errs, errors.Wrapf(err, "delete monitorResource:%s err", resource.GetId())) - } - } - for _, res := range resources { - createData := self.newMonitorResourceCreateInput(res) - _, err = db.DoCreate(MonitorResourceManager, ctx, userCred, nil, createData, - userCred) - if err != nil { - name, _ := createData.GetString("name") - errs = append(errs, errors.Wrapf(err, "monitorResource:%s resType:%s DoCreate err", name, self.sync.SyncType())) - } - } - return errors.NewAggregate(errs) -} - -func (self *SyncObject) newMonitorResourceCreateInput(input jsonutils.JSONObject) jsonutils.JSONObject { - monitorResource := jsonutils.DeepCopy(input).(*jsonutils.JSONDict) - id, _ := monitorResource.GetString("id") - monitorResource.Add(jsonutils.NewString(id), "res_id") - monitorResource.Remove("id") - monitorResource.Add(jsonutils.NewString(self.sync.SyncType()), "res_type") - - return monitorResource -} - -func GetOnecloudResources(resTyep string) ([]jsonutils.JSONObject, error) { - var err error - allResources := make([]jsonutils.JSONObject, 0) - - query := jsonutils.NewDict() - query.Add(jsonutils.NewStringArray([]string{"running", "ready"}), "status") - query.Add(jsonutils.NewString("true"), "admin") - switch resTyep { - case monitor.METRIC_RES_TYPE_HOST: - //query.Set("host-type", jsonutils.NewString(hostconsts.TELEGRAF_TAG_KEY_HYPERVISOR)) - allResources, err = ListAllResources(&mc_mds.Hosts, query) - case monitor.METRIC_RES_TYPE_GUEST: - allResources, err = ListAllResources(&mc_mds.Servers, query) - case monitor.METRIC_RES_TYPE_AGENT: - allResources, err = ListAllResources(&mc_mds.Servers, query) - case monitor.METRIC_RES_TYPE_RDS: - allResources, err = ListAllResources(&mc_mds.DBInstance, query) - case monitor.METRIC_RES_TYPE_REDIS: - allResources, err = ListAllResources(&mc_mds.ElasticCache, query) - case monitor.METRIC_RES_TYPE_OSS: - allResources, err = ListAllResources(&mc_mds.Buckets, query) - case monitor.METRIC_RES_TYPE_CLOUDACCOUNT: - query.Remove("status") - query.Add(jsonutils.NewBool(true), "enabled") - allResources, err = ListAllResources(&mc_mds.Cloudaccounts, query) - case monitor.METRIC_RES_TYPE_TENANT: - allResources, err = ListAllResources(&identity.Projects, query) - case monitor.METRIC_RES_TYPE_DOMAIN: - allResources, err = ListAllResources(&identity.Domains, query) - case monitor.METRIC_RES_TYPE_STORAGE: - query.Remove("status") - allResources, err = ListAllResources(&mc_mds.Storages, query) - 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(&mc_mds.Hosts, query) - } - - if err != nil { - return nil, errors.Wrap(err, "NoDataQueryCondition Host list error") - } - return allResources, nil -} - -func ListAllResources(manager modulebase.Manager, params *jsonutils.JSONDict) ([]jsonutils.JSONObject, error) { - if params == nil { - params = jsonutils.NewDict() - } - params.Add(jsonutils.NewString("system"), "scope") - params.Add(jsonutils.NewInt(20), "limit") - params.Add(jsonutils.NewBool(true), "details") - var count int - session := auth.GetAdminSession(context.Background(), "") - objs := make([]jsonutils.JSONObject, 0) - for { - params.Set("offset", jsonutils.NewInt(int64(count))) - result, err := manager.List(session, params) - if err != nil { - return nil, errors.Wrapf(err, "list %s resources with params %s", manager.KeyString(), params.String()) - } - for _, data := range result.Data { - objs = append(objs, data) - } - total := result.Total - count = count + len(result.Data) - if count >= total { - break - } - } - return objs, nil -}