diff options
Diffstat (limited to 'src/go/collectors/go.d.plugin/modules/vsphere/discover/filter.go')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/vsphere/discover/filter.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/vsphere/discover/filter.go b/src/go/collectors/go.d.plugin/modules/vsphere/discover/filter.go new file mode 100644 index 000000000..73c1481e3 --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/vsphere/discover/filter.go @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package discover + +import ( + "time" + + rs "github.com/netdata/netdata/go/go.d.plugin/modules/vsphere/resources" +) + +func (d Discoverer) matchHost(host *rs.Host) bool { + if d.HostMatcher == nil { + return true + } + return d.HostMatcher.Match(host) +} + +func (d Discoverer) matchVM(vm *rs.VM) bool { + if d.VMMatcher == nil { + return true + } + return d.VMMatcher.Match(vm) +} + +func (d Discoverer) removeUnmatched(res *rs.Resources) (removed int) { + d.Debug("discovering : filtering : starting filtering resources process") + t := time.Now() + numH, numV := len(res.Hosts), len(res.VMs) + removed += d.removeUnmatchedHosts(res.Hosts) + removed += d.removeUnmatchedVMs(res.VMs) + d.Infof("discovering : filtering : filtered %d/%d hosts, %d/%d vms, process took %s", + numH-len(res.Hosts), + numH, + numV-len(res.VMs), + numV, + time.Since(t)) + return +} + +func (d Discoverer) removeUnmatchedHosts(hosts rs.Hosts) (removed int) { + for _, v := range hosts { + if !d.matchHost(v) { + removed++ + hosts.Remove(v.ID) + } + } + d.Debugf("discovering : filtering : removed %d unmatched hosts", removed) + return removed +} + +func (d Discoverer) removeUnmatchedVMs(vms rs.VMs) (removed int) { + for _, v := range vms { + if !d.matchVM(v) { + removed++ + vms.Remove(v.ID) + } + } + d.Debugf("discovering : filtering : removed %d unmatched vms", removed) + return removed +} |