forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
286 lines (251 loc) · 8.47 KB
/
client.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package vcenterreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver"
import (
"context"
"errors"
"fmt"
"net/url"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/performance"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
vt "github.com/vmware/govmomi/vim25/types"
)
// vcenterClient is a client that collects data from a vCenter endpoint.
type vcenterClient struct {
moClient *govmomi.Client
vimDriver *vim25.Client
finder *find.Finder
pc *property.Collector
pm *performance.Manager
vm *view.Manager
cfg *Config
}
var newVcenterClient = defaultNewVcenterClient
func defaultNewVcenterClient(c *Config) *vcenterClient {
return &vcenterClient{
cfg: c,
}
}
// EnsureConnection will establish a connection to the vSphere SDK if not already established
func (vc *vcenterClient) EnsureConnection(ctx context.Context) error {
if vc.moClient != nil {
sessionActive, _ := vc.moClient.SessionManager.SessionIsActive(ctx)
if sessionActive {
return nil
}
}
sdkURL, err := vc.cfg.SDKUrl()
if err != nil {
return err
}
client, err := govmomi.NewClient(ctx, sdkURL, vc.cfg.Insecure)
if err != nil {
return fmt.Errorf("unable to connect to vSphere SDK on listed endpoint: %w", err)
}
tlsCfg, err := vc.cfg.LoadTLSConfig(ctx)
if err != nil {
return err
}
if tlsCfg != nil {
client.DefaultTransport().TLSClientConfig = tlsCfg
}
user := url.UserPassword(vc.cfg.Username, string(vc.cfg.Password))
err = client.Login(ctx, user)
if err != nil {
return fmt.Errorf("unable to login to vcenter sdk: %w", err)
}
vc.moClient = client
vc.vimDriver = client.Client
vc.pc = property.DefaultCollector(vc.vimDriver)
vc.finder = find.NewFinder(vc.vimDriver)
vc.pm = performance.NewManager(vc.vimDriver)
vc.vm = view.NewManager(vc.vimDriver)
return nil
}
// Disconnect will logout of the autenticated session
func (vc *vcenterClient) Disconnect(ctx context.Context) error {
if vc.moClient != nil {
return vc.moClient.Logout(ctx)
}
return nil
}
// Datacenters returns the Datacenters of the vSphere SDK
func (vc *vcenterClient) Datacenters(ctx context.Context) ([]mo.Datacenter, error) {
v, err := vc.vm.CreateContainerView(ctx, vc.vimDriver.ServiceContent.RootFolder, []string{"Datacenter"}, true)
if err != nil {
return nil, fmt.Errorf("unable to retrieve Datacenters: %w", err)
}
var datacenters []mo.Datacenter
err = v.Retrieve(ctx, []string{"Datacenter"}, []string{
"name",
}, &datacenters)
if err != nil {
return nil, fmt.Errorf("unable to retrieve Datacenters: %w", err)
}
return datacenters, nil
}
// Datastores returns the Datastores of the vSphere SDK
func (vc *vcenterClient) Datastores(ctx context.Context, containerMoRef vt.ManagedObjectReference) ([]mo.Datastore, error) {
v, err := vc.vm.CreateContainerView(ctx, containerMoRef, []string{"Datastore"}, true)
if err != nil {
return nil, fmt.Errorf("unable to retrieve Datastores: %w", err)
}
var datastores []mo.Datastore
err = v.Retrieve(ctx, []string{"Datastore"}, []string{
"name",
"summary.capacity",
"summary.freeSpace",
}, &datastores)
if err != nil {
return nil, fmt.Errorf("unable to retrieve Datastores: %w", err)
}
return datastores, nil
}
// ComputeResources returns the ComputeResources (& ClusterComputeResources) of the vSphere SDK
func (vc *vcenterClient) ComputeResources(ctx context.Context, containerMoRef vt.ManagedObjectReference) ([]mo.ComputeResource, error) {
v, err := vc.vm.CreateContainerView(ctx, containerMoRef, []string{"ComputeResource"}, true)
if err != nil {
return nil, fmt.Errorf("unable to retrieve ComputeResources (& ClusterComputeResources): %w", err)
}
var computes []mo.ComputeResource
err = v.Retrieve(ctx, []string{"ComputeResource"}, []string{
"name",
"datastore",
"host",
"summary",
}, &computes)
if err != nil {
return nil, fmt.Errorf("unable to retrieve ComputeResources (& ClusterComputeResources): %w", err)
}
return computes, nil
}
// HostSystems returns the HostSystems of the vSphere SDK
func (vc *vcenterClient) HostSystems(ctx context.Context, containerMoRef vt.ManagedObjectReference) ([]mo.HostSystem, error) {
v, err := vc.vm.CreateContainerView(ctx, containerMoRef, []string{"HostSystem"}, true)
if err != nil {
return nil, fmt.Errorf("unable to retrieve HostSystems: %w", err)
}
var hosts []mo.HostSystem
err = v.Retrieve(ctx, []string{"HostSystem"}, []string{
"name",
"summary.hardware.memorySize",
"summary.hardware.numCpuCores",
"summary.hardware.cpuMhz",
"summary.quickStats.overallMemoryUsage",
"summary.quickStats.overallCpuUsage",
"vm",
"parent",
}, &hosts)
if err != nil {
return nil, fmt.Errorf("unable to retrieve HostSystems: %w", err)
}
return hosts, nil
}
// ResourcePools returns the ResourcePools (&VirtualApps) of the vSphere SDK
func (vc *vcenterClient) ResourcePools(ctx context.Context, containerMoRef vt.ManagedObjectReference) ([]mo.ResourcePool, error) {
v, err := vc.vm.CreateContainerView(ctx, containerMoRef, []string{"ResourcePool"}, true)
if err != nil {
return nil, fmt.Errorf("unable to retrieve ResourcePools (&VirtualApps): %w", err)
}
var rps []mo.ResourcePool
err = v.Retrieve(ctx, []string{"ResourcePool"}, []string{
"summary",
"name",
"owner",
"vm",
}, &rps)
if err != nil {
return nil, fmt.Errorf("unable to retrieve ResourcePools (&VirtualApps): %w", err)
}
return rps, nil
}
// VMS returns the VirtualMachines of the vSphere SDK
func (vc *vcenterClient) VMs(ctx context.Context, containerMoRef vt.ManagedObjectReference) ([]mo.VirtualMachine, error) {
v, err := vc.vm.CreateContainerView(ctx, containerMoRef, []string{"VirtualMachine"}, true)
if err != nil {
return nil, fmt.Errorf("unable to retrieve VMs: %w", err)
}
var vms []mo.VirtualMachine
err = v.Retrieve(ctx, []string{"VirtualMachine"}, []string{
"name",
"config.hardware.numCPU",
"config.instanceUuid",
"config.template",
"runtime.powerState",
"runtime.maxCpuUsage",
"summary.quickStats.guestMemoryUsage",
"summary.quickStats.balloonedMemory",
"summary.quickStats.swappedMemory",
"summary.quickStats.ssdSwappedMemory",
"summary.quickStats.overallCpuUsage",
"summary.config.memorySizeMB",
"summary.storage.committed",
"summary.storage.uncommitted",
"summary.runtime.host",
"resourcePool",
"parentVApp",
}, &vms)
if err != nil {
return nil, fmt.Errorf("unable to retrieve VMs: %w", err)
}
return vms, nil
}
// ResourcePoolInventoryListObjects returns the ResourcePools (with populated InventoryLists) of the vSphere SDK
func (vc *vcenterClient) ResourcePoolInventoryListObjects(ctx context.Context) ([]*object.ResourcePool, error) {
rps, err := vc.finder.ResourcePoolList(ctx, "*")
if err != nil {
return nil, fmt.Errorf("unable to retrieve ResourcePools with InventoryLists: %w", err)
}
return rps, nil
}
// VAppInventoryListObjects returns the vApps (with populated InventoryLists) of the vSphere SDK
func (vc *vcenterClient) VAppInventoryListObjects(ctx context.Context) ([]*object.VirtualApp, error) {
vApps, err := vc.finder.VirtualAppList(ctx, "*")
if err == nil {
return vApps, nil
}
var notFoundErr *find.NotFoundError
if errors.As(err, ¬FoundErr) {
return []*object.VirtualApp{}, nil
}
return nil, fmt.Errorf("unable to retrieve vApps with InventoryLists: %w", err)
}
// PerfMetricsQueryResult contains performance metric related data
type PerfMetricsQueryResult struct {
// Contains performance metrics keyed by MoRef string
resultsByRef map[string]*performance.EntityMetric
}
// PerfMetricsQuery returns the requested performance metrics for the requested resources
// over a given sample interval and sample count
func (vc *vcenterClient) PerfMetricsQuery(
ctx context.Context,
spec vt.PerfQuerySpec,
names []string,
objs []vt.ManagedObjectReference,
) (*PerfMetricsQueryResult, error) {
if vc.pm == nil {
return &PerfMetricsQueryResult{}, nil
}
vc.pm.Sort = true
sample, err := vc.pm.SampleByName(ctx, spec, names, objs)
if err != nil {
return nil, err
}
result, err := vc.pm.ToMetricSeries(ctx, sample)
if err != nil {
return nil, err
}
resultsByRef := map[string]*performance.EntityMetric{}
for i := range result {
resultsByRef[result[i].Entity.Value] = &result[i]
}
return &PerfMetricsQueryResult{
resultsByRef: resultsByRef,
}, nil
}