Skip to content

Commit d8c88e1

Browse files
fix (nullpointer): in case a vm was a template the reourcePool field was null (#24)
1 parent e85817d commit d8c88e1

File tree

8 files changed

+36
-25
lines changed

8 files changed

+36
-25
lines changed

integration-test/nri-vmware-vspere_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ func exectuteIntegration() ([]byte, []byte) {
6868
panic(err)
6969
}
7070

71-
output := outbuf.Bytes()
72-
error := errbuf.Bytes()
71+
out := outbuf.Bytes()
72+
errOut := errbuf.Bytes()
7373

74-
return output, error
74+
return out, errOut
7575
}
7676

7777
func transformAndSanitizeOutput(expectedOutput string, actualOutput string) (integration.Integration, integration.Integration) {

internal/process/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/newrelic/nri-vsphere/internal/load"
99
)
1010

11-
func createClusterSamples(config *load.Config, timestamp int64) {
11+
func createClusterSamples(config *load.Config) {
1212
for _, dc := range config.Datacenters {
1313
for _, cluster := range dc.Clusters {
1414
// // resolve hypervisor host

internal/process/datacenter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/newrelic/nri-vsphere/internal/load"
99
)
1010

11-
func createDatacenterSamples(config *load.Config, timestamp int64) {
11+
func createDatacenterSamples(config *load.Config) {
1212

1313
if !config.IsVcenterAPIType {
1414
return

internal/process/datastores.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/vmware/govmomi/vim25/types"
1212
)
1313

14-
func createDatastoreSamples(config *load.Config, timestamp int64) {
14+
func createDatastoreSamples(config *load.Config) {
1515
for _, dc := range config.Datacenters {
1616
for _, ds := range dc.Datastores {
1717
datacenterName := dc.Datacenter.Name

internal/process/hosts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/newrelic/nri-vsphere/internal/load"
1111
)
1212

13-
func createHostSamples(config *load.Config, timestamp int64) {
13+
func createHostSamples(config *load.Config) {
1414
for _, dc := range config.Datacenters {
1515
for _, host := range dc.Hosts {
1616
hostConfigName := host.Summary.Config.Name

internal/process/process.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
package process
55

66
import (
7-
"strings"
8-
"sync"
9-
"time"
10-
117
"github.com/newrelic/infra-integrations-sdk/data/metric"
128
"github.com/newrelic/nri-vsphere/internal/load"
9+
"strings"
10+
"sync"
1311
)
1412

1513
const (
@@ -23,34 +21,33 @@ const (
2321

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

2825
// create samples async
2926
var wg sync.WaitGroup
3027
wg.Add(6)
3128
go func() {
3229
defer wg.Done()
33-
createVirtualMachineSamples(config, timestamp)
30+
createVirtualMachineSamples(config)
3431
}()
3532
go func() {
3633
defer wg.Done()
37-
createHostSamples(config, timestamp)
34+
createHostSamples(config)
3835
}()
3936
go func() {
4037
defer wg.Done()
41-
createDatastoreSamples(config, timestamp)
38+
createDatastoreSamples(config)
4239
}()
4340
go func() {
4441
defer wg.Done()
45-
createDatacenterSamples(config, timestamp)
42+
createDatacenterSamples(config)
4643
}()
4744
go func() {
4845
defer wg.Done()
49-
createClusterSamples(config, timestamp)
46+
createClusterSamples(config)
5047
}()
5148
go func() {
5249
defer wg.Done()
53-
createResourcePoolSamples(config, timestamp)
50+
createResourcePoolSamples(config)
5451
}()
5552
wg.Wait()
5653
}
@@ -110,7 +107,7 @@ func sanitizeEntityName(config *load.Config, entityName string, datacenterName s
110107
}
111108

112109
entityName = strings.ToLower(entityName)
113-
entityName = strings.Replace(entityName, ".", "-", 0)
110+
entityName = strings.Replace(entityName, ".", "-", -1)
114111
return entityName
115112
}
116113

internal/process/resourcepool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/newrelic/nri-vsphere/internal/load"
99
)
1010

11-
func createResourcePoolSamples(config *load.Config, timestamp int64) {
11+
func createResourcePoolSamples(config *load.Config) {
1212
for _, dc := range config.Datacenters {
1313
for _, rp := range dc.ResourcePools {
1414
// Skip root default ResourcePool (not created by user)

internal/process/vms.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,31 @@ import (
1010
"github.com/newrelic/nri-vsphere/internal/load"
1111
)
1212

13-
func createVirtualMachineSamples(config *load.Config, timestamp int64) {
13+
func createVirtualMachineSamples(config *load.Config) {
1414
for _, dc := range config.Datacenters {
1515
for _, vm := range dc.VirtualMachines {
16-
// // resolve hypervisor host
16+
17+
if vm.ResourcePool == nil {
18+
continue // resourcePool Returns null if the virtual machine is a template or the session has no access to the resource pool.
19+
}
20+
if vm.Summary.Runtime.Host == nil {
21+
continue // This property is null if the virtual machine is not running and is not assigned to run on a particular host.
22+
}
23+
if _, ok := dc.Hosts[*vm.Summary.Runtime.Host]; !ok {
24+
continue
25+
}
26+
if dc.Hosts[*vm.Summary.Runtime.Host].Parent == nil {
27+
continue
28+
}
1729
vmHost := dc.Hosts[*vm.Summary.Runtime.Host]
30+
vmHostParent := *vmHost.Parent
31+
vmResourcePool := *vm.ResourcePool
1832
hostConfigName := vmHost.Summary.Config.Name
1933
vmConfigName := vm.Summary.Config.Name
2034
datacenterName := dc.Datacenter.Name
2135
entityName := hostConfigName + ":" + vmConfigName
2236

23-
if cluster, ok := dc.Clusters[*vmHost.Parent]; ok {
37+
if cluster, ok := dc.Clusters[vmHostParent]; ok {
2438
entityName = cluster.Name + ":" + entityName
2539
}
2640

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

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

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

49-
resourcePoolName := dc.GetResourcePoolName(*vm.ResourcePool)
63+
resourcePoolName := dc.GetResourcePoolName(vmResourcePool)
5064
checkError(config, ms.SetMetric("resourcePoolName", resourcePoolName, metric.ATTRIBUTE))
5165

5266
datastoreList := ""

0 commit comments

Comments
 (0)