Skip to content

Commit

Permalink
[feature] Add cluster and host filtering configuration parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Tesifonte Belda committed Feb 4, 2023
1 parent f3055d3 commit 2188a97
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ Use telegraf v1.14 or above so that execd input is available.
## Use SSL but skip chain & host verification
# insecure_skip_verify = false

## Filter clusters by name, default is no filtering.
## cluster names can be specified as glob patterns.
# clusters_include = []
# clusters_exclude = []

## Filter hosts by name, default is no filtering.
## host names can be specified as glob patterns.
# hosts_include = []
# hosts_exclude = []

#### you may enable or disable data collection per instance type ####
## collect cluster measurement (vcstat_cluster)
# cluster_instances = true
Expand Down
10 changes: 10 additions & 0 deletions etc/vcstat.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
## Use SSL but skip chain & host verification
# insecure_skip_verify = false

## Filter clusters by name, default is no filtering.
## cluster names can be specified as glob patterns.
# clusters_include = []
# clusters_exclude = []

## Filter hosts by name, default is no filtering.
## host names can be specified as glob patterns.
# hosts_include = []
# hosts_exclude = []

#### you may enable or disable data collection per instance type ####
## collect cluster measurement (vcstat_cluster)
# cluster_instances = true
Expand Down
5 changes: 4 additions & 1 deletion internal/vccollector/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ func (c *VcCollector) CollectClusterInfo(
for i, dc := range c.dcs {
// get cluster references and split the list into chunks
for _, cluster := range c.clusters[i] {
if !c.filterClusters.Match(cluster.Name()) {
continue
}
arefs = append(arefs, cluster.Reference())
}
chunks := chunckMoRefSlice(arefs, c.queryBulkSize)

for _, refs := range chunks {
err = c.coll.Retrieve(ctx, refs, []string{"name","summary"}, &clMos)
err = c.coll.Retrieve(ctx, refs, []string{"name", "summary"}, &clMos)
if err != nil {
if err, exit := govplus.IsHardQueryError(err); exit {
return err
Expand Down
28 changes: 28 additions & 0 deletions internal/vccollector/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (c *VcCollector) CollectHostInfo(
for i, dc := range c.dcs {
// get Host references and split the list into chunks
for j, host := range c.hosts[i] {
if !c.filterHostMatch(i, host) {
continue
}
if hostSt = c.getHostStateIdx(i, j); hostSt == nil {
acc.AddError(fmt.Errorf("Could not find host state for %s", host.Name()))
continue
Expand Down Expand Up @@ -138,6 +141,9 @@ func (c *VcCollector) CollectHostHBA(

for i, dc := range c.dcs {
for j, host := range c.hosts[i] {
if !c.filterHostMatch(i, host) {
continue
}
if hostSt = c.getHostStateIdx(i, j); hostSt == nil {
acc.AddError(fmt.Errorf("Could not find host state for %s", host.Name()))
continue
Expand Down Expand Up @@ -225,6 +231,9 @@ func (c *VcCollector) CollectHostNIC(

for i, dc := range c.dcs {
for j, host := range c.hosts[i] {
if !c.filterHostMatch(i, host) {
continue
}
if hostSt = c.getHostStateIdx(i, j); hostSt == nil {
acc.AddError(fmt.Errorf("Could not find host state for %s", host.Name()))
continue
Expand Down Expand Up @@ -310,6 +319,9 @@ func (c *VcCollector) CollectHostFw(

for i, dc := range c.dcs {
for j, host := range c.hosts[i] {
if !c.filterHostMatch(i, host) {
continue
}
if hostSt = c.getHostStateIdx(i, j); hostSt == nil {
acc.AddError(fmt.Errorf("Could not find host state for %s", host.Name()))
continue
Expand Down Expand Up @@ -414,6 +426,9 @@ func (c *VcCollector) CollectHostServices(
for i, dc := range c.dcs {
// get HostServiceSystem references and split the list into chunks
for j, host := range c.hosts[i] {
if !c.filterHostMatch(i, host) {
continue
}
if hostSt = c.getHostStateIdx(i, j); hostSt == nil {
acc.AddError(fmt.Errorf("Could not find host state for %s", host.Name()))
continue
Expand Down Expand Up @@ -488,6 +503,9 @@ func (c *VcCollector) ReportHostEsxcliResponse(
t = time.Now()
for i, dc := range c.dcs {
for j, host := range c.hosts[i] {
if !c.filterHostMatch(i, host) {
continue
}
if hostSt = c.getHostStateIdx(i, j); hostSt == nil {
acc.AddError(fmt.Errorf("Could not find host state for %s", host.Name()))
continue
Expand Down Expand Up @@ -527,6 +545,16 @@ func (c *VcCollector) getClusternameFromHost(dcindex int, host *object.HostSyste
return ""
}

func (c *VcCollector) filterHostMatch(i int, host *object.HostSystem) bool {
if !c.filterHosts.Match(host.Name()) {
return false
}
if !c.filterClusters.Match(c.getClusternameFromHost(i, host)) {
return false
}
return true
}

func (c *VcCollector) getHostObjectFromReference(
dcindex int,
r *types.ManagedObjectReference,
Expand Down
31 changes: 31 additions & 0 deletions internal/vccollector/vccollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/url"
"time"

"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/plugins/common/tls"

"github.com/tesibelda/vcstat/pkg/govplus"
Expand All @@ -30,6 +31,8 @@ type VcCollector struct {
url *url.URL
client *govmomi.Client
coll *property.Collector
filterClusters filter.Filter
filterHosts filter.Filter
dataDuration time.Duration
skipNotRespondigFor time.Duration
queryBulkSize int
Expand All @@ -49,6 +52,12 @@ func New(
urlString: vcenterUrl,
dataDuration: dataDuration,
}
if err = vcc.SetFilterClusters(nil, nil); err != nil {
return nil, err
}
if err = vcc.SetFilterHosts(nil, nil); err != nil {
return nil, err
}
vcc.TLSCA = clicfg.TLSCA
vcc.InsecureSkipVerify = clicfg.InsecureSkipVerify

Expand All @@ -65,6 +74,28 @@ func (c *VcCollector) SetDataDuration(du time.Duration) {
c.dataDuration = du
}

// SetFilterClusters sets clusters include and exclude filters
func (c *VcCollector) SetFilterClusters(include []string, exclude []string) error {
var err error

c.filterClusters, err = filter.NewIncludeExcludeFilter(include, exclude)
if err != nil {
return err
}
return nil
}

// SetFilterHosts sets hosts include and exclude filters
func (c *VcCollector) SetFilterHosts(include []string, exclude []string) error {
var err error

c.filterHosts, err = filter.NewIncludeExcludeFilter(include, exclude)
if err != nil {
return err
}
return nil
}

// SetQueryChunkSize sets chunk size of slice to use in sSphere property queries
func (c *VcCollector) SetQueryChunkSize(b int) {
c.queryBulkSize = b
Expand Down
27 changes: 25 additions & 2 deletions plugins/inputs/vcstat/vcstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ type VCstatConfig struct {
QueryBulkSize int `toml:"query_bulk_size"`
Log telegraf.Logger `toml:"-"`

ClustersExclude []string `toml:"clusters_exclude"`
ClustersInclude []string `toml:"clusters_include"`
HostsExclude []string `toml:"hosts_exclude"`
HostsInclude []string `toml:"hosts_include"`

ClusterInstances bool `toml:"cluster_instances"`
DatastoreInstances bool `toml:"datastore_instances"`
HostInstances bool `toml:"host_instances"`
Expand Down Expand Up @@ -71,6 +76,16 @@ var sampleConfig = `
## Use SSL but skip chain & host verification
# insecure_skip_verify = false
## Filter clusters by name, default is no filtering.
## cluster names can be specified as glob patterns.
# clusters_include = []
# clusters_exclude = []
## Filter hosts by name, default is no filtering.
## host names can be specified as glob patterns.
# hosts_include = []
# hosts_exclude = []
#### you may enable or disable data collection per instance type ####
## collect cluster measurement (vcstat_cluster)
# cluster_instances = true
Expand Down Expand Up @@ -142,13 +157,21 @@ func (vcs *VCstatConfig) Init() error {
return err
}

// Set vccollector options
// dataduration as half of the telegraf shim polling interval
/// Set vccollector options
// dataduration as half of the telegraf shim polling interval
vcs.vcc.SetDataDuration(time.Duration(vcs.pollInterval.Seconds() / 2))
vcs.vcc.SetSkipHostNotRespondingDuration(
time.Duration(vcs.pollInterval.Seconds() * float64(vcs.IntSkipNotRespondig)),
)
vcs.vcc.SetQueryChunkSize(vcs.QueryBulkSize)
err = vcs.vcc.SetFilterClusters(vcs.ClustersInclude, vcs.ClustersExclude);
if err != nil {
return fmt.Errorf("Error parsing clusters filters: %w", err)
}
err = vcs.vcc.SetFilterHosts(vcs.HostsInclude, vcs.HostsExclude)
if err != nil {
return fmt.Errorf("Error parsing hosts filters: %w", err)
}

// selfmonitoring
u, err := url.Parse(vcs.VCenter)
Expand Down

0 comments on commit 2188a97

Please sign in to comment.