Skip to content

Commit

Permalink
fix (nullpointer): in case a vm was a template the reourcePool field …
Browse files Browse the repository at this point in the history
…was null (#24)
  • Loading branch information
paologallinaharbur authored May 4, 2020
1 parent e85817d commit d8c88e1
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
6 changes: 3 additions & 3 deletions integration-test/nri-vmware-vspere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ func exectuteIntegration() ([]byte, []byte) {
panic(err)
}

output := outbuf.Bytes()
error := errbuf.Bytes()
out := outbuf.Bytes()
errOut := errbuf.Bytes()

return output, error
return out, errOut
}

func transformAndSanitizeOutput(expectedOutput string, actualOutput string) (integration.Integration, integration.Integration) {
Expand Down
2 changes: 1 addition & 1 deletion internal/process/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/newrelic/nri-vsphere/internal/load"
)

func createClusterSamples(config *load.Config, timestamp int64) {
func createClusterSamples(config *load.Config) {
for _, dc := range config.Datacenters {
for _, cluster := range dc.Clusters {
// // resolve hypervisor host
Expand Down
2 changes: 1 addition & 1 deletion internal/process/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/newrelic/nri-vsphere/internal/load"
)

func createDatacenterSamples(config *load.Config, timestamp int64) {
func createDatacenterSamples(config *load.Config) {

if !config.IsVcenterAPIType {
return
Expand Down
2 changes: 1 addition & 1 deletion internal/process/datastores.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/vmware/govmomi/vim25/types"
)

func createDatastoreSamples(config *load.Config, timestamp int64) {
func createDatastoreSamples(config *load.Config) {
for _, dc := range config.Datacenters {
for _, ds := range dc.Datastores {
datacenterName := dc.Datacenter.Name
Expand Down
2 changes: 1 addition & 1 deletion internal/process/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/newrelic/nri-vsphere/internal/load"
)

func createHostSamples(config *load.Config, timestamp int64) {
func createHostSamples(config *load.Config) {
for _, dc := range config.Datacenters {
for _, host := range dc.Hosts {
hostConfigName := host.Summary.Config.Name
Expand Down
21 changes: 9 additions & 12 deletions internal/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
package process

import (
"strings"
"sync"
"time"

"github.com/newrelic/infra-integrations-sdk/data/metric"
"github.com/newrelic/nri-vsphere/internal/load"
"strings"
"sync"
)

const (
Expand All @@ -23,34 +21,33 @@ const (

// Run process samples
func Run(config *load.Config) {
timestamp := time.Now().UnixNano() / int64(time.Millisecond)

// create samples async
var wg sync.WaitGroup
wg.Add(6)
go func() {
defer wg.Done()
createVirtualMachineSamples(config, timestamp)
createVirtualMachineSamples(config)
}()
go func() {
defer wg.Done()
createHostSamples(config, timestamp)
createHostSamples(config)
}()
go func() {
defer wg.Done()
createDatastoreSamples(config, timestamp)
createDatastoreSamples(config)
}()
go func() {
defer wg.Done()
createDatacenterSamples(config, timestamp)
createDatacenterSamples(config)
}()
go func() {
defer wg.Done()
createClusterSamples(config, timestamp)
createClusterSamples(config)
}()
go func() {
defer wg.Done()
createResourcePoolSamples(config, timestamp)
createResourcePoolSamples(config)
}()
wg.Wait()
}
Expand Down Expand Up @@ -110,7 +107,7 @@ func sanitizeEntityName(config *load.Config, entityName string, datacenterName s
}

entityName = strings.ToLower(entityName)
entityName = strings.Replace(entityName, ".", "-", 0)
entityName = strings.Replace(entityName, ".", "-", -1)
return entityName
}

Expand Down
2 changes: 1 addition & 1 deletion internal/process/resourcepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/newrelic/nri-vsphere/internal/load"
)

func createResourcePoolSamples(config *load.Config, timestamp int64) {
func createResourcePoolSamples(config *load.Config) {
for _, dc := range config.Datacenters {
for _, rp := range dc.ResourcePools {
// Skip root default ResourcePool (not created by user)
Expand Down
24 changes: 19 additions & 5 deletions internal/process/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ import (
"github.com/newrelic/nri-vsphere/internal/load"
)

func createVirtualMachineSamples(config *load.Config, timestamp int64) {
func createVirtualMachineSamples(config *load.Config) {
for _, dc := range config.Datacenters {
for _, vm := range dc.VirtualMachines {
// // resolve hypervisor host

if vm.ResourcePool == nil {
continue // resourcePool Returns null if the virtual machine is a template or the session has no access to the resource pool.
}
if vm.Summary.Runtime.Host == nil {
continue // This property is null if the virtual machine is not running and is not assigned to run on a particular host.
}
if _, ok := dc.Hosts[*vm.Summary.Runtime.Host]; !ok {
continue
}
if dc.Hosts[*vm.Summary.Runtime.Host].Parent == nil {
continue
}
vmHost := dc.Hosts[*vm.Summary.Runtime.Host]
vmHostParent := *vmHost.Parent
vmResourcePool := *vm.ResourcePool
hostConfigName := vmHost.Summary.Config.Name
vmConfigName := vm.Summary.Config.Name
datacenterName := dc.Datacenter.Name
entityName := hostConfigName + ":" + vmConfigName

if cluster, ok := dc.Clusters[*vmHost.Parent]; ok {
if cluster, ok := dc.Clusters[vmHostParent]; ok {
entityName = cluster.Name + ":" + entityName
}

Expand All @@ -37,7 +51,7 @@ func createVirtualMachineSamples(config *load.Config, timestamp int64) {
checkError(config, ms.SetMetric("datacenterLocation", config.Args.DatacenterLocation, metric.ATTRIBUTE))
}

if cluster, ok := dc.Clusters[*vmHost.Parent]; ok {
if cluster, ok := dc.Clusters[vmHostParent]; ok {
checkError(config, ms.SetMetric("clusterName", cluster.Name, metric.ATTRIBUTE))
}

Expand All @@ -46,7 +60,7 @@ func createVirtualMachineSamples(config *load.Config, timestamp int64) {
}
checkError(config, ms.SetMetric("hypervisorHostname", hostConfigName, metric.ATTRIBUTE))

resourcePoolName := dc.GetResourcePoolName(*vm.ResourcePool)
resourcePoolName := dc.GetResourcePoolName(vmResourcePool)
checkError(config, ms.SetMetric("resourcePoolName", resourcePoolName, metric.ATTRIBUTE))

datastoreList := ""
Expand Down

0 comments on commit d8c88e1

Please sign in to comment.