Skip to content

Commit

Permalink
[feature]: create and use govplus and tgplus packages with govmomi an…
Browse files Browse the repository at this point in the history
…d telegraf helpers
  • Loading branch information
tbelda-ems committed Aug 31, 2022
1 parent 6319f5f commit aa41402
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 173 deletions.
1 change: 1 addition & 0 deletions internal/vccollector/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
const (
strAsterisk = "*"
)

var (
findNotFoundError *find.NotFoundError
)
Expand Down
14 changes: 8 additions & 6 deletions internal/vccollector/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/influxdata/telegraf"

"github.com/tesibelda/vcstat/pkg/govplus"

"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
Expand All @@ -25,14 +27,14 @@ func (c *VcCollector) CollectClusterInfo(
var (
clusters []*object.ClusterComputeResource
clMo mo.ClusterComputeResource
resourceSum *(types.ComputeResourceSummary)
usageSum *types.ClusterUsageSummary
resourceSum *(types.ClusterComputeResourceSummary)
usageSum *(types.ClusterUsageSummary)
numVms int32
err error
)

if c.client == nil {
return fmt.Errorf("Could not get clusters info: %w", Error_NoClient)
return fmt.Errorf("Could not get clusters info: %w", govplus.ErrorNoClient)
}
if err = c.getAllDatacentersClustersAndHosts(ctx); err != nil {
return fmt.Errorf("Could not get cluster and host entity list: %w", err)
Expand All @@ -49,13 +51,13 @@ func (c *VcCollector) CollectClusterInfo(
err,
)
}
if resourceSum = clMo.Summary.GetComputeResourceSummary(); resourceSum == nil {
if resourceSum = clMo.Summary.(*types.ClusterComputeResourceSummary); resourceSum == nil {
return fmt.Errorf("Could not get cluster resource summary")
}

// get number of VMs in the cluster (tip: https://github.com/vmware/govmomi/issues/1247)
numVms = 0
usageSum = clMo.Summary.(*types.ClusterComputeResourceSummary).UsageSummary
usageSum = resourceSum.UsageSummary
if usageSum != nil {
numVms = usageSum.TotalVmCount
}
Expand Down Expand Up @@ -101,7 +103,7 @@ func getClusterFields(
numhosts, numeffectivehosts int32,
numcpucores, numcputhreads int16,
totalcpu, totalmemory, effectivecpu, effectivememory int64,
numvms int32,
numvms int32,
) map[string]interface{} {
return map[string]interface{}{
"effective_cpu": effectivecpu,
Expand Down
4 changes: 3 additions & 1 deletion internal/vccollector/dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"

"github.com/influxdata/telegraf"

"github.com/tesibelda/vcstat/pkg/govplus"
)

// CollectDatacenterInfo gathers datacenter info
Expand All @@ -21,7 +23,7 @@ func (c *VcCollector) CollectDatacenterInfo(
var err error

if c.client == nil {
return fmt.Errorf("Could not get datacenters info: %w", Error_NoClient)
return fmt.Errorf("Could not get datacenters info: %w", govplus.ErrorNoClient)
}

if err = c.getAllDatacentersEntities(ctx); err != nil {
Expand Down
30 changes: 18 additions & 12 deletions internal/vccollector/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"github.com/influxdata/telegraf"

"github.com/tesibelda/vcstat/pkg/govplus"

"github.com/vmware/govmomi/govc/host/esxcli"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
Expand All @@ -29,11 +31,15 @@ func (c *VcCollector) CollectHostInfo(
hsMo mo.HostSystem
hostSt *hostState
err error
exit bool
s *(types.HostListSummary)
r *(types.HostRuntimeInfo)
h *(types.HostHardwareSummary)
hsCode, hsConnectionCode int16
)

if c.client == nil {
return fmt.Errorf("Could not get host info: %w", Error_NoClient)
return fmt.Errorf("Could not get host info: %w", govplus.ErrorNoClient)
}
if err = c.getAllDatacentersClustersAndHosts(ctx); err != nil {
return fmt.Errorf("Could not get cluster and host entity list: %w", err)
Expand All @@ -47,7 +53,7 @@ func (c *VcCollector) CollectHostInfo(
}
err = host.Properties(ctx, host.Reference(), []string{"summary"}, &hsMo)
if err != nil {
if err, exit := govQueryError(err); exit {
if err, exit = govplus.IsHardQueryError(err); exit {
return fmt.Errorf(
"Could not get host %s summary property: %w",
host.Name(),
Expand All @@ -63,9 +69,9 @@ func (c *VcCollector) CollectHostInfo(
)
continue
}
s := hsMo.Summary
r := s.Runtime
h := s.Hardware
s = &hsMo.Summary
r = s.Runtime
h = s.Hardware
hostSt.setNotConnected(
r.ConnectionState != types.HostSystemConnectionStateConnected,
)
Expand Down Expand Up @@ -111,7 +117,7 @@ func (c *VcCollector) CollectHostHBA(
)

if c.client == nil {
return fmt.Errorf("Could not get host HBAs info: %w", Error_NoClient)
return fmt.Errorf("Could not get host HBAs info: %w", govplus.ErrorNoClient)
}
if err = c.getAllDatacentersClustersAndHosts(ctx); err != nil {
return fmt.Errorf("Could not get cluster and host entity list: %w", err)
Expand Down Expand Up @@ -140,7 +146,7 @@ func (c *VcCollector) CollectHostHBA(
res, err = x.Run([]string{"storage", "core", "adapter", "list"})
hostSt.setMeanResponseTime(time.Since(startTime))
if err != nil {
if err, exit := govQueryError(err); exit {
if err, exit := govplus.IsHardQueryError(err); exit {
return err
}
acc.AddError(
Expand Down Expand Up @@ -197,7 +203,7 @@ func (c *VcCollector) CollectHostNIC(
)

if c.client == nil {
return fmt.Errorf("Could not get host NICs info: %w", Error_NoClient)
return fmt.Errorf("Could not get host NICs info: %w", govplus.ErrorNoClient)
}
if err = c.getAllDatacentersClustersAndHosts(ctx); err != nil {
return fmt.Errorf("Could not get cluster and host entity list: %w", err)
Expand All @@ -220,7 +226,7 @@ func (c *VcCollector) CollectHostNIC(
res, err = x.Run([]string{"network", "nic", "list"})
hostSt.setMeanResponseTime(time.Since(startTime))
if err != nil {
if err, exit := govQueryError(err); exit {
if err, exit := govplus.IsHardQueryError(err); exit {
return err
}
acc.AddError(
Expand Down Expand Up @@ -279,7 +285,7 @@ func (c *VcCollector) CollectHostFw(
)

if c.client == nil {
return fmt.Errorf("Could not get host firewalls info: %w", Error_NoClient)
return fmt.Errorf("Could not get host firewalls info: %w", govplus.ErrorNoClient)
}
if err = c.getAllDatacentersClustersAndHosts(ctx); err != nil {
return fmt.Errorf("Could not get cluster and host entity list: %w", err)
Expand Down Expand Up @@ -308,7 +314,7 @@ func (c *VcCollector) CollectHostFw(
res, err = x.Run([]string{"network", "firewall", "get"})
hostSt.setMeanResponseTime(time.Since(startTime))
if err != nil {
if err, exit := govQueryError(err); exit {
if err, exit := govplus.IsHardQueryError(err); exit {
return err
}
acc.AddError(
Expand Down Expand Up @@ -375,7 +381,7 @@ func (c *VcCollector) ReportHostEsxcliResponse(
)

if c.client == nil {
return fmt.Errorf("Could not report host esxcli responses info: %w", Error_NoClient)
return fmt.Errorf("Could not report host esxcli responses info: %w", govplus.ErrorNoClient)
}

for i, dc := range c.dcs {
Expand Down
10 changes: 6 additions & 4 deletions internal/vccollector/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"github.com/influxdata/telegraf"

"github.com/tesibelda/vcstat/pkg/govplus"

"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
Expand All @@ -34,7 +36,7 @@ func (c *VcCollector) CollectNetDVS(
)

if c.client == nil {
return fmt.Errorf("Could not get network DVSs info: %w", Error_NoClient)
return fmt.Errorf("Could not get network DVSs info: %w", govplus.ErrorNoClient)
}
if err = c.getAllDatacentersNetworks(ctx); err != nil {
return fmt.Errorf("Could not get network entity list: %w", err)
Expand All @@ -55,7 +57,7 @@ func (c *VcCollector) CollectNetDVS(
&dvsMo,
)
if err != nil {
if err, exit := govQueryError(err); exit {
if err, exit := govplus.IsHardQueryError(err); exit {
return err
}
acc.AddError(fmt.Errorf("Could not get dvs config property: %w", err))
Expand Down Expand Up @@ -102,7 +104,7 @@ func (c *VcCollector) CollectNetDVP(
)

if c.client == nil {
return fmt.Errorf("Could not get network DVPs info: %w", Error_NoClient)
return fmt.Errorf("Could not get network DVPs info: %w", govplus.ErrorNoClient)
}
if err = c.getAllDatacentersNetworks(ctx); err != nil {
return fmt.Errorf("Could not get network entity list: %w", err)
Expand All @@ -122,7 +124,7 @@ func (c *VcCollector) CollectNetDVP(
&dvpMo,
)
if err != nil {
if err, exit := govQueryError(err); exit {
if err, exit := govplus.IsHardQueryError(err); exit {
return err
}
acc.AddError(fmt.Errorf("Could not get dvp config property: %w", err))
Expand Down
6 changes: 4 additions & 2 deletions internal/vccollector/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/influxdata/telegraf"

"github.com/tesibelda/vcstat/pkg/govplus"

"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
Expand All @@ -30,7 +32,7 @@ func (c *VcCollector) CollectDatastoresInfo(
)

if c.client == nil {
return fmt.Errorf("Could not get datastores info: %w", Error_NoClient)
return fmt.Errorf("Could not get datastores info: %w", govplus.ErrorNoClient)
}
if err = c.getAllDatacentersDatastores(ctx); err != nil {
return fmt.Errorf("Could not get datastore entity list: %w", err)
Expand All @@ -45,7 +47,7 @@ func (c *VcCollector) CollectDatastoresInfo(
}
err = pc.Retrieve(ctx, refs, []string{"summary"}, &dsMo)
if err != nil {
if err, exit := govQueryError(err); exit {
if err, exit := govplus.IsHardQueryError(err); exit {
return err
}
acc.AddError(fmt.Errorf("Could not retrieve summary for datastore: %w", err))
Expand Down
4 changes: 3 additions & 1 deletion internal/vccollector/vc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"

"github.com/influxdata/telegraf"

"github.com/tesibelda/vcstat/pkg/govplus"
)

// CollectVcenterInfo gathers basic vcenter info
Expand All @@ -19,7 +21,7 @@ func (c *VcCollector) CollectVcenterInfo(
acc telegraf.Accumulator,
) error {
if c.client == nil {
return fmt.Errorf("Could not get vcenter info: %w", Error_NoClient)
return fmt.Errorf("Could not get vcenter info: %w", govplus.ErrorNoClient)
}
cli := c.client.Client

Expand Down
Loading

0 comments on commit aa41402

Please sign in to comment.