-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
processors.go
399 lines (349 loc) · 13.2 KB
/
processors.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package vcenterreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver"
import (
"fmt"
"time"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/scraper/scrapererror"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/metadata"
)
type DatacenterStats struct {
ClusterStatusCounts map[types.ManagedEntityStatus]int64
HostStats map[string]map[types.ManagedEntityStatus]int64
VMStats map[string]map[types.ManagedEntityStatus]int64
DatastoreCount int64
DiskCapacity int64
DiskFree int64
CPULimit int64
MemoryLimit int64
}
// processDatacenterData creates all of the vCenter metrics from the stored scraped data under a single Datacenter
func (v *vcenterMetricScraper) processDatacenterData(dc *mo.Datacenter, errs *scrapererror.ScrapeErrors) {
// Init for current collection
now := pcommon.NewTimestampFromTime(time.Now())
dcStats := &DatacenterStats{
ClusterStatusCounts: make(map[types.ManagedEntityStatus]int64),
HostStats: make(map[string]map[types.ManagedEntityStatus]int64),
VMStats: make(map[string]map[types.ManagedEntityStatus]int64),
}
v.processDatastores(now, dc, dcStats)
v.processResourcePools(now, dc, errs)
vmRefToComputeRef := v.processHosts(now, dc, dcStats, errs)
vmGroupInfoByComputeRef := v.processVMs(now, dc, vmRefToComputeRef, dcStats, errs)
v.processClusters(now, dc, vmGroupInfoByComputeRef, dcStats, errs)
v.buildDatacenterMetrics(now, dc, dcStats)
}
// buildDatacenterMetrics builds a resource and metrics for a given scraped vCenter Datacenter
func (v *vcenterMetricScraper) buildDatacenterMetrics(
ts pcommon.Timestamp,
dc *mo.Datacenter,
dcStats *DatacenterStats,
) {
// Create Datacenter resource builder
rb := v.createDatacenterResourceBuilder(dc)
// Record & emit Datacenter metric data points
v.recordDatacenterStats(ts, dcStats)
v.mb.EmitForResource(metadata.WithResource(rb.Emit()))
}
// processDatastores creates the vCenter Datastore metrics and resources from the stored scraped data under a single Datacenter
func (v *vcenterMetricScraper) processDatastores(
ts pcommon.Timestamp,
dc *mo.Datacenter,
dcStats *DatacenterStats,
) {
for _, ds := range v.scrapeData.datastores {
v.buildDatastoreMetrics(ts, dc, ds)
dcStats.DatastoreCount++
dcStats.DiskCapacity += ds.Summary.Capacity
dcStats.DiskFree += ds.Summary.FreeSpace
}
}
// buildDatastoreMetrics builds a resource and metrics for a given scraped vCenter Datastore
func (v *vcenterMetricScraper) buildDatastoreMetrics(
ts pcommon.Timestamp,
dc *mo.Datacenter,
ds *mo.Datastore,
) {
// Create Datastore resource builder
rb := v.createDatastoreResourceBuilder(dc, ds)
// Record & emit Datastore metric data points
v.recordDatastoreStats(ts, ds)
v.mb.EmitForResource(metadata.WithResource(rb.Emit()))
}
// processResourcePools creates the vCenter Resource Pool metrics and resources from the stored scraped data under a single Datacenter
func (v *vcenterMetricScraper) processResourcePools(
ts pcommon.Timestamp,
dc *mo.Datacenter,
errs *scrapererror.ScrapeErrors,
) {
for _, rp := range v.scrapeData.rPoolsByRef {
// Don't make metrics for vApps
if rp.Reference().Type == "VirtualApp" {
continue
}
if err := v.buildResourcePoolMetrics(ts, dc, rp); err != nil {
errs.AddPartial(1, err)
}
}
}
// buildResourcePoolMetrics builds a resource and metrics for a given scraped vCenter Resource Pool
func (v *vcenterMetricScraper) buildResourcePoolMetrics(
ts pcommon.Timestamp,
dc *mo.Datacenter,
rp *mo.ResourcePool,
) error {
// Get related ResourcePool Compute info
crRef := rp.Owner
cr := v.scrapeData.computesByRef[crRef.Value]
if cr == nil {
return fmt.Errorf("no collected ComputeResource found for ResourcePool [%s]'s parent ref: %s", rp.Name, crRef.Value)
}
// Create ResourcePool resource builder
rb, err := v.createResourcePoolResourceBuilder(dc, cr, rp)
if err != nil {
return err
}
// Record & emit Resource Pool metric data points
v.recordResourcePoolStats(ts, rp)
v.mb.EmitForResource(metadata.WithResource(rb.Emit()))
return nil
}
// processHosts creates the vCenter HostS metrics and resources from the stored scraped data under a single Datacenter
//
// returns a map containing the ComputeResource info for each VM
func (v *vcenterMetricScraper) processHosts(
ts pcommon.Timestamp,
dc *mo.Datacenter,
dcStats *DatacenterStats,
errs *scrapererror.ScrapeErrors,
) map[string]*types.ManagedObjectReference {
vmRefToComputeRef := map[string]*types.ManagedObjectReference{}
for _, hs := range v.scrapeData.hostsByRef {
powerState := string(hs.Runtime.PowerState)
ensureInnerMapInitialized(dcStats.HostStats, powerState)
dcStats.HostStats[powerState][hs.Summary.OverallStatus]++
dcStats.CPULimit += int64(hs.Summary.Hardware.CpuMhz * int32(hs.Summary.Hardware.NumCpuCores))
dcStats.MemoryLimit += hs.Summary.Hardware.MemorySize
hsVMRefToComputeRef, err := v.buildHostMetrics(ts, dc, hs)
if err != nil {
errs.AddPartial(1, err)
continue
}
// Populate master VM to CR relationship map from
// single Host based version of it
for vmRef, csRef := range hsVMRefToComputeRef {
vmRefToComputeRef[vmRef] = csRef
}
}
return vmRefToComputeRef
}
// buildHostMetrics builds a resource and metrics for a given scraped Host
//
// returns a map containing the ComputeResource info for each VM running on the Host
func (v *vcenterMetricScraper) buildHostMetrics(
ts pcommon.Timestamp,
dc *mo.Datacenter,
hs *mo.HostSystem,
) (vmRefToComputeRef map[string]*types.ManagedObjectReference, err error) {
vmRefToComputeRef = map[string]*types.ManagedObjectReference{}
// Get related Host ComputeResource info
crRef := hs.Parent
if crRef == nil {
return vmRefToComputeRef, fmt.Errorf("no parent found for Host: %s", hs.Name)
}
cr := v.scrapeData.computesByRef[crRef.Value]
if cr == nil {
return vmRefToComputeRef, fmt.Errorf("no collected ComputeResource found for Host [%s]'s parent ref: %s", hs.Name, crRef.Value)
}
// Store VM to ComputeResource relationship for all child VMs
for _, vmRef := range hs.Vm {
vmRefToComputeRef[vmRef.Value] = crRef
}
// Create Host resource builder
rb := v.createHostResourceBuilder(dc, cr, hs)
// Record & emit Host metric data points
v.recordHostSystemStats(ts, hs)
hostPerfMetrics := v.scrapeData.hostPerfMetricsByRef[hs.Reference().Value]
if hostPerfMetrics != nil {
v.recordHostPerformanceMetrics(hostPerfMetrics)
}
if hs.Config == nil || hs.Config.VsanHostConfig == nil || hs.Config.VsanHostConfig.ClusterInfo == nil {
v.logger.Info("couldn't determine UUID necessary for vSAN metrics for host " + hs.Name)
v.mb.EmitForResource(metadata.WithResource(rb.Emit()))
return vmRefToComputeRef, nil
}
vSANMetrics := v.scrapeData.hostVSANMetricsByUUID[hs.Config.VsanHostConfig.ClusterInfo.NodeUuid]
if vSANMetrics != nil {
v.recordHostVSANMetrics(vSANMetrics)
}
v.mb.EmitForResource(metadata.WithResource(rb.Emit()))
return vmRefToComputeRef, nil
}
// processVMs creates the vCenter VM metrics and resources from the stored scraped data under a single Datacenter
//
// returns a map of all VM State counts for each ComputeResource
func (v *vcenterMetricScraper) processVMs(
ts pcommon.Timestamp,
dc *mo.Datacenter,
vmRefToComputeRef map[string]*types.ManagedObjectReference,
dcStats *DatacenterStats,
errs *scrapererror.ScrapeErrors,
) map[string]*vmGroupInfo {
vmGroupInfoByComputeRef := map[string]*vmGroupInfo{}
for _, vm := range v.scrapeData.vmsByRef {
crRef, singleVMGroupInfo, err := v.buildVMMetrics(ts, dc, vm, vmRefToComputeRef)
if err != nil {
errs.AddPartial(1, err)
}
// Update master ComputeResource VM power state counts with VM power info
if crRef != nil && singleVMGroupInfo != nil {
crVMGroupInfo := vmGroupInfoByComputeRef[crRef.Value]
if crVMGroupInfo == nil {
crVMGroupInfo = &vmGroupInfo{poweredOff: 0, poweredOn: 0, suspended: 0, templates: 0}
vmGroupInfoByComputeRef[crRef.Value] = crVMGroupInfo
}
overallStatus := vm.Summary.OverallStatus
if singleVMGroupInfo.poweredOn > 0 {
crVMGroupInfo.poweredOn++
ensureInnerMapInitialized(dcStats.VMStats, "poweredOn")
dcStats.VMStats["poweredOn"][overallStatus]++
}
if singleVMGroupInfo.poweredOff > 0 {
crVMGroupInfo.poweredOff++
ensureInnerMapInitialized(dcStats.VMStats, "poweredOff")
dcStats.VMStats["poweredOff"][overallStatus]++
}
if singleVMGroupInfo.suspended > 0 {
crVMGroupInfo.suspended++
ensureInnerMapInitialized(dcStats.VMStats, "suspended")
dcStats.VMStats["suspended"][overallStatus]++
}
if singleVMGroupInfo.templates > 0 {
crVMGroupInfo.templates++
}
}
}
return vmGroupInfoByComputeRef
}
// ensureInnerMapInitialized is a helper function that ensures maps are initialized in order to freely aggregate VM and Host stats
func ensureInnerMapInitialized(stats map[string]map[types.ManagedEntityStatus]int64, key string) {
if stats[key] == nil {
stats[key] = make(map[types.ManagedEntityStatus]int64)
}
}
// buildVMMetrics builds a resource and metrics for a given scraped VM
//
// returns the ComputeResource and power state info associated with this VM
func (v *vcenterMetricScraper) buildVMMetrics(
ts pcommon.Timestamp,
dc *mo.Datacenter,
vm *mo.VirtualMachine,
vmRefToComputeRef map[string]*types.ManagedObjectReference,
) (crRef *types.ManagedObjectReference, groupInfo *vmGroupInfo, err error) {
// Get related VM compute info
crRef = vmRefToComputeRef[vm.Reference().Value]
if crRef == nil {
return crRef, groupInfo, fmt.Errorf("no ComputeResource ref found for VM: %s", vm.Name)
}
cr := v.scrapeData.computesByRef[crRef.Value]
if cr == nil {
return crRef, groupInfo, fmt.Errorf("no collected ComputeResource for VM [%s]'s ComputeResource ref: %s", vm.Name, crRef)
}
// Get related VM host info
hsRef := vm.Summary.Runtime.Host
if hsRef == nil {
return crRef, groupInfo, fmt.Errorf("no Host ref for VM: %s", vm.Name)
}
hs := v.scrapeData.hostsByRef[hsRef.Value]
if hs == nil {
return crRef, groupInfo, fmt.Errorf("no collected Host for VM [%s]'s Host ref: %s", vm.Name, hsRef.Value)
}
// VMs may not have a ResourcePool reported (templates)
// But grab it if available
rpRef := vm.ResourcePool
var rp *mo.ResourcePool
if rpRef != nil {
rp = v.scrapeData.rPoolsByRef[rpRef.Value]
}
groupInfo = &vmGroupInfo{poweredOff: 0, poweredOn: 0, suspended: 0, templates: 0}
if vm.Config.Template {
groupInfo.templates++
} else {
switch vm.Runtime.PowerState {
case "poweredOff":
groupInfo.poweredOff++
case "poweredOn":
groupInfo.poweredOn++
default:
groupInfo.suspended++
}
}
// Create VM resource builder
rb, err := v.createVMResourceBuilder(dc, cr, hs, rp, vm)
if err != nil {
return crRef, groupInfo, err
}
// Record VM metric data points
v.recordVMStats(ts, vm, hs)
perfMetrics := v.scrapeData.vmPerfMetricsByRef[vm.Reference().Value]
if perfMetrics != nil {
v.recordVMPerformanceMetrics(perfMetrics)
}
vSANMetrics := v.scrapeData.vmVSANMetricsByUUID[vm.Config.InstanceUuid]
if vSANMetrics != nil {
v.recordVMVSANMetrics(vSANMetrics)
}
v.mb.EmitForResource(metadata.WithResource(rb.Emit()))
return crRef, groupInfo, err
}
// processClusters creates the vCenter Cluster metrics and resources from the stored scraped data under a single Datacenter
func (v *vcenterMetricScraper) processClusters(
ts pcommon.Timestamp,
dc *mo.Datacenter,
vmStatesByComputeRef map[string]*vmGroupInfo,
dcStats *DatacenterStats,
errs *scrapererror.ScrapeErrors,
) {
for crRef, cr := range v.scrapeData.computesByRef {
// Don't make metrics for anything that's not a Cluster (otherwise it should be the same as a HostSystem)
if cr.Reference().Type != "ClusterComputeResource" {
continue
}
summary := cr.Summary.GetComputeResourceSummary()
dcStats.ClusterStatusCounts[summary.OverallStatus]++
vmGroupInfo := vmStatesByComputeRef[crRef]
if err := v.buildClusterMetrics(ts, dc, cr, vmGroupInfo); err != nil {
errs.AddPartial(1, err)
}
}
}
// buildClusterMetrics builds a resource and metrics for a given scraped Cluster
func (v *vcenterMetricScraper) buildClusterMetrics(
ts pcommon.Timestamp,
dc *mo.Datacenter,
cr *mo.ComputeResource,
vmGroupInfo *vmGroupInfo,
) (err error) {
// Create Cluster resource builder
rb := v.createClusterResourceBuilder(dc, cr)
if vmGroupInfo == nil {
err = fmt.Errorf("no VM power counts found for Cluster: %s", cr.Name)
}
// Record and emit Cluster metric data points
v.recordClusterStats(ts, cr, vmGroupInfo)
vSANConfig := cr.ConfigurationEx.(*types.ClusterConfigInfoEx).VsanConfigInfo
if vSANConfig == nil || vSANConfig.Enabled == nil || !*vSANConfig.Enabled || vSANConfig.DefaultConfig == nil {
v.logger.Info("couldn't determine UUID necessary for vSAN metrics for cluster " + cr.Name)
v.mb.EmitForResource(metadata.WithResource(rb.Emit()))
return err
}
vSANMetrics := v.scrapeData.clusterVSANMetricsByUUID[vSANConfig.DefaultConfig.Uuid]
if vSANMetrics != nil {
v.recordClusterVSANMetrics(vSANMetrics)
}
v.mb.EmitForResource(metadata.WithResource(rb.Emit()))
return err
}