From 89264dea2c2ec0941429148715e07cd9f4a90952 Mon Sep 17 00:00:00 2001 From: Tesifonte Belda Date: Tue, 30 Aug 2022 09:31:17 +0200 Subject: [PATCH] [enhancement]: add number of VMs field to cluster measurement --- METRICS.md | 1 + README.md | 4 +-- pkg/vccollector/vccollector_cluster.go | 34 +++++++++++++++++--------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/METRICS.md b/METRICS.md index 0ece9c5..2aa38a1 100644 --- a/METRICS.md +++ b/METRICS.md @@ -31,6 +31,7 @@ - num_effective_hosts (int) - num_cpu_cores (int) - num_cpu_threads (int) + - num_vms (int) - total_cpu (int) in MHz - total_memory (int) in bytes - effective_cpu (int) in MHz diff --git a/README.md b/README.md index 64e6bb8..893ee6e 100644 --- a/README.md +++ b/README.md @@ -92,8 +92,8 @@ Metric timestamp precision will be set according to the polling interval, so it ```plain vcstat_vcenter,vcenter=vcenter.local name="VMware vCenter Server",num_datacenters=1i,ostype="linux-x64",version="6.5.0" 1653060681000000000 -vcstat_datacenter,dcname=MyDC,moid=datacenter-2,vcenter=vcenter.local num_catastores=51i,num_hosts=8i,num_networks=32i,num_clusters=1i 1653060681000000000 -vcstat_cluster,clustername=MyCluster-01,dcname=MyDC,moid=domain-c121,vcenter=vcenter.local num_cpu_cores=152i,total_cpu=342248i,total_memory=1648683421696i,effective_cpu=299032i,status="green",status_code=0i,num_hosts=8i,num_effective_hosts=8i,num_cpu_threads=304i,effective_memory=1502236i 1653060681000000000 +vcstat_datacenter,dcname=MyDC,moid=datacenter-2,vcenter=vcenter.local num_datastores=51i,num_hosts=8i,num_networks=32i,num_clusters=1i 1653060681000000000 +vcstat_cluster,clustername=MyCluster-01,dcname=MyDC,moid=domain-c121,vcenter=vcenter.local num_cpu_cores=152i,total_cpu=342248i,total_memory=1648683421696i,effective_cpu=299032i,status="green",status_code=0i,num_vms=26i,num_hosts=8i,num_effective_hosts=8i,num_cpu_threads=304i,effective_memory=1502236i 1653060681000000000 vcstat_host,dcname=MyDC,clustername=MyCluster-01,esxhostname=myesxi01.local,moid=host-706,vcenter=vcenter.local connection_state_code=0i,memory_size=206110695424i,num_cpus=16i,cpu_freq=2199i,status="green",status_code=0i,reboot_required=false,in_maintenance_mode=false,connection_state="connected" 1653060681000000000 vcstat_host_firewall,dcname=MyDC,clustername=MyCluster-01,esxhostname=myesxi01.local,vcenter=vcenter.local defaultaction="DROP",enabled=true,loaded=true 1653060681000000000 vcstat_host_hba,dcname=MyDC,clustername=MyCluster-01,device=vmhba0,driver=lpfc,esxhostname=myesxi01.local,vcenter=vcenter.local status="link-n/a",status_code=1i 1653060681000000000 diff --git a/pkg/vccollector/vccollector_cluster.go b/pkg/vccollector/vccollector_cluster.go index 24f16ec..dcdfc20 100644 --- a/pkg/vccollector/vccollector_cluster.go +++ b/pkg/vccollector/vccollector_cluster.go @@ -23,11 +23,12 @@ func (c *VcCollector) CollectClusterInfo( acc telegraf.Accumulator, ) error { var ( - clusters []*object.ClusterComputeResource - clMo mo.ClusterComputeResource - resourceSum *(types.ComputeResourceSummary) - clusterStatusCode int16 - err error + clusters []*object.ClusterComputeResource + clMo mo.ClusterComputeResource + resourceSum *(types.ComputeResourceSummary) + usageSum *types.ClusterUsageSummary + numVms int32 + err error ) if c.client == nil { @@ -51,7 +52,13 @@ func (c *VcCollector) CollectClusterInfo( if resourceSum = clMo.Summary.GetComputeResourceSummary(); resourceSum == nil { return fmt.Errorf("Could not get cluster resource summary") } - clusterStatusCode = entityStatusCode(resourceSum.OverallStatus) + + // get number of VMs in the cluster (tip: https://github.com/vmware/govmomi/issues/1247) + numVms = 0 + usageSum = clMo.Summary.(*types.ClusterComputeResourceSummary).UsageSummary + if usageSum != nil { + numVms = usageSum.TotalVmCount + } cltags := getClusterTags( c.client.Client.URL().Host, @@ -61,15 +68,16 @@ func (c *VcCollector) CollectClusterInfo( ) clfields := getClusterFields( string(resourceSum.OverallStatus), - clusterStatusCode, + entityStatusCode(resourceSum.OverallStatus), resourceSum.NumHosts, resourceSum.NumEffectiveHosts, resourceSum.NumCpuCores, resourceSum.NumCpuThreads, - int(resourceSum.TotalCpu), - int(resourceSum.TotalMemory), - int(resourceSum.EffectiveCpu), - int(resourceSum.EffectiveMemory), + int64(resourceSum.TotalCpu), + resourceSum.TotalMemory, + int64(resourceSum.EffectiveCpu), + resourceSum.EffectiveMemory, + numVms, ) acc.AddFields("vcstat_cluster", clfields, cltags, time.Now()) } @@ -92,7 +100,8 @@ func getClusterFields( clusterstatuscode int16, numhosts, numeffectivehosts int32, numcpucores, numcputhreads int16, - totalcpu, totalmemory, effectivecpu, effectivememory int, + totalcpu, totalmemory, effectivecpu, effectivememory int64, + numvms int32, ) map[string]interface{} { return map[string]interface{}{ "effective_cpu": effectivecpu, @@ -100,6 +109,7 @@ func getClusterFields( "num_cpu_cores": numcpucores, "num_cpu_threads": numcputhreads, "num_effective_hosts": numeffectivehosts, + "num_vms": numvms, "num_hosts": numhosts, "status": overallstatus, "status_code": clusterstatuscode,