-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathresources.go
149 lines (127 loc) · 4.36 KB
/
resources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package vcenterreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver"
import (
"fmt"
"github.com/vmware/govmomi/vim25/mo"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/metadata"
)
// createDatastoreResourceBuilder returns a ResourceBuilder with
// attributes set for a vSphere Datastore
func (v *vcenterMetricScraper) createDatastoreResourceBuilder(
dc *mo.Datacenter,
ds *mo.Datastore,
) *metadata.ResourceBuilder {
rb := v.mb.NewResourceBuilder()
rb.SetVcenterDatacenterName(dc.Name)
rb.SetVcenterDatastoreName(ds.Name)
return rb
}
// createDatacenterResourceBuilder returns a ResourceBuilder with
// attributes set for a vSphere datacenter
func (v *vcenterMetricScraper) createDatacenterResourceBuilder(
dc *mo.Datacenter,
) *metadata.ResourceBuilder {
rb := v.mb.NewResourceBuilder()
rb.SetVcenterDatacenterName(dc.Name)
return rb
}
// createClusterResourceBuilder returns a ResourceBuilder with
// attributes set for a vSphere Cluster
func (v *vcenterMetricScraper) createClusterResourceBuilder(
dc *mo.Datacenter,
cr *mo.ComputeResource,
) *metadata.ResourceBuilder {
rb := v.mb.NewResourceBuilder()
rb.SetVcenterDatacenterName(dc.Name)
rb.SetVcenterClusterName(cr.Name)
return rb
}
// createResourcePoolResourceBuilder returns a ResourceBuilder with
// attributes set for a vSphere Resource Pool
func (v *vcenterMetricScraper) createResourcePoolResourceBuilder(
dc *mo.Datacenter,
cr *mo.ComputeResource,
rp *mo.ResourcePool,
) (*metadata.ResourceBuilder, error) {
rb := v.mb.NewResourceBuilder()
rb.SetVcenterDatacenterName(dc.Name)
if cr.Reference().Type == "ClusterComputeResource" {
rb.SetVcenterClusterName(cr.Name)
}
if cr.Reference().Type == "ComputeResource" {
if len(cr.Host) == 0 {
return nil, fmt.Errorf("no Hosts found for Resource Pool [%s]'s owner ref: %s", rp.Name, cr.Reference().Value)
}
hsRef := cr.Host[0]
hs := v.scrapeData.hostsByRef[hsRef.Value]
if hs == nil {
return nil, fmt.Errorf("no Hosts found for Resource Pool [%s]'s owner ref: %s", rp.Name, cr.Reference().Value)
}
rb.SetVcenterHostName(hs.Name)
}
rb.SetVcenterResourcePoolName(rp.Name)
iPath := v.scrapeData.rPoolIPathsByRef[rp.Reference().Value]
if iPath == nil {
return nil, fmt.Errorf("no inventory path found for collected ResourcePool: %s", rp.Name)
}
rb.SetVcenterResourcePoolInventoryPath(*iPath)
return rb, nil
}
// createHostResourceBuilder returns a ResourceBuilder with
// attributes set for a vSphere Host
func (v *vcenterMetricScraper) createHostResourceBuilder(
dc *mo.Datacenter,
cr *mo.ComputeResource,
hs *mo.HostSystem,
) *metadata.ResourceBuilder {
rb := v.mb.NewResourceBuilder()
rb.SetVcenterDatacenterName(dc.Name)
if cr.Reference().Type == "ClusterComputeResource" {
rb.SetVcenterClusterName(cr.Name)
}
rb.SetVcenterHostName(hs.Name)
return rb
}
// createVMResourceBuilder returns a ResourceBuilder with
// attributes set for a vSphere Virtual Machine
func (v *vcenterMetricScraper) createVMResourceBuilder(
dc *mo.Datacenter,
cr *mo.ComputeResource,
hs *mo.HostSystem,
rp *mo.ResourcePool,
vm *mo.VirtualMachine,
) (*metadata.ResourceBuilder, error) {
rb := v.mb.NewResourceBuilder()
rb.SetVcenterDatacenterName(dc.Name)
if cr.Reference().Type == "ClusterComputeResource" {
rb.SetVcenterClusterName(cr.Name)
}
rb.SetVcenterHostName(hs.Name)
if vm.Config.Template {
rb.SetVcenterVMTemplateName(vm.Name)
rb.SetVcenterVMTemplateID(vm.Config.InstanceUuid)
return rb, nil
}
rb.SetVcenterVMName(vm.Name)
rb.SetVcenterVMID(vm.Config.InstanceUuid)
if rp == nil {
return nil, fmt.Errorf("no Resource Pool found for VM: %s", vm.Name)
}
if rp.Reference().Type == "VirtualApp" {
rb.SetVcenterVirtualAppName(rp.Name)
iPath := v.scrapeData.vAppIPathsByRef[rp.Reference().Value]
if iPath == nil {
return nil, fmt.Errorf("no inventory path found for VM [%s]'s collected vApp: %s", vm.Name, rp.Name)
}
rb.SetVcenterVirtualAppInventoryPath(*iPath)
} else {
rb.SetVcenterResourcePoolName(rp.Name)
iPath := v.scrapeData.rPoolIPathsByRef[rp.Reference().Value]
if iPath == nil {
return nil, fmt.Errorf("no inventory path found for VM [%s]'s collected ResourcePool: %s", vm.Name, rp.Name)
}
rb.SetVcenterResourcePoolInventoryPath(*iPath)
}
return rb, nil
}