/
vc_vm.go
445 lines (382 loc) · 16.2 KB
/
vc_vm.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright 2021 Intrinsec
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !noesx
package collector
import (
"encoding/json"
"strconv"
"strings"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
type virtualMachineCollector struct {
vcCollector
numCPU typedDesc
numCoresPerSocket typedDesc
memoryBytes typedDesc
overallCPUUsage typedDesc
overallCPUDemand typedDesc
guestMemoryUsage typedDesc
hostMemoryUsage typedDesc
distributedCPUEntitlement typedDesc
distributedMemoryEntitlement typedDesc
staticCPUEntitlement typedDesc
staticMemoryEntitlement typedDesc
privateMemory typedDesc
sharedMemory typedDesc
swappedMemory typedDesc
balloonedMemory typedDesc
consumedOverheadMemory typedDesc
ftLogBandwidth typedDesc
ftSecondaryLatency typedDesc
compressedMemory typedDesc
uptimeSeconds typedDesc
ssdSwappedMemory typedDesc
numSnapshot typedDesc
diskCapacityBytes typedDesc
networkConnected typedDesc
ethernetDriverConnected typedDesc
}
const (
virtualMachineCollectorSubsystem = "vm"
)
func init() {
registerCollector(virtualMachineCollectorSubsystem, defaultEnabled, NewVirtualMachineCollector)
}
// NewVirtualMachineCollector returns a new Collector exposing IpTables stats.
func NewVirtualMachineCollector(logger log.Logger) (Collector, error) {
labels := []string{
"vc", "dc", "cluster", "esx", "pool",
"name", "hostname", "guestfullname",
"power_state", "overall_status",
"tools_status", "tools_version",
}
if *useIsecSpecifics {
labels = append(labels, "crit", "responsable", "service")
}
networkLabels := make([]string, len(labels))
ethernetDevLabels := make([]string, len(labels))
diskLabels := make([]string, len(labels))
copy(networkLabels, labels)
copy(ethernetDevLabels, labels)
copy(diskLabels, labels)
networkLabels = append(networkLabels, "network", "mac", "ip")
ethernetDevLabels = append(ethernetDevLabels, "driver_model", "driver_mac", "driver_status")
diskLabels = append(diskLabels, "vmdk", "thin_provisioned")
res := virtualMachineCollector{
numCPU: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "cpu_number_total"),
"vm number of cpu", labels, nil), prometheus.CounterValue},
numCoresPerSocket: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "cores_number_per_socket_total"),
"vm number of cores by socket", labels, nil), prometheus.CounterValue},
memoryBytes: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "memory_bytes"),
"vm memory in bytes", labels, nil), prometheus.GaugeValue},
overallCPUUsage: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "overall_cpu_usage_mhz"),
"vm overall CPU usage in MHz", labels, nil), prometheus.GaugeValue},
overallCPUDemand: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "overall_cpu_demand_mhz"),
"vm overall CPU demand in MHz", labels, nil), prometheus.GaugeValue},
guestMemoryUsage: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "guest_memory_usage_bytes"),
"vm guest memory usage in bytes", labels, nil), prometheus.GaugeValue},
hostMemoryUsage: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "host_memory_usage_bytes"),
"vm host memory usage in bytes", labels, nil), prometheus.GaugeValue},
distributedCPUEntitlement: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "distributed_cpu_entitlement_mhz"),
"vm distributed CPU entitlement in MHz", labels, nil), prometheus.GaugeValue},
distributedMemoryEntitlement: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "distributed_memory_entitlement_bytes"),
"vm distributed memory entitlement in bytes", labels, nil), prometheus.GaugeValue},
staticCPUEntitlement: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "static_cpu_entitlement_mhz"),
"vm static CPU entitlement in MHz", labels, nil), prometheus.GaugeValue},
staticMemoryEntitlement: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "static_memory_entitlement_bytes"),
"vm static memory entitlement in bytes", labels, nil), prometheus.GaugeValue},
privateMemory: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "private_memory_bytes"),
"vm private memory in bytes", labels, nil), prometheus.GaugeValue},
sharedMemory: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "shared_memory_bytes"),
"vm shared memory in bytes", labels, nil), prometheus.GaugeValue},
swappedMemory: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "swapped_memory_bytes"),
"vm swapped memory in bytes", labels, nil), prometheus.GaugeValue},
balloonedMemory: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "ballooned_memory_bytes"),
"vm ballooned memory in bytes", labels, nil), prometheus.GaugeValue},
consumedOverheadMemory: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "consumed_overhead_memory_bytes"),
"vm consumed overhead memory bytes", labels, nil), prometheus.GaugeValue},
ftLogBandwidth: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "ft_log_bandwidth"),
"vm ft log bandwidth", labels, nil), prometheus.GaugeValue},
ftSecondaryLatency: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "ft_secondary_latency"),
"vm ft secondary latency", labels, nil), prometheus.GaugeValue},
compressedMemory: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "compressed_memory_bytes"),
"vm compressed memory in bytes", labels, nil), prometheus.GaugeValue},
uptimeSeconds: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "uptime_seconds"),
"vm uptime in seconds", labels, nil), prometheus.CounterValue},
ssdSwappedMemory: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "ssd_swapped_memory_bytes"),
"vm ssd swapped memory in bytes", labels, nil), prometheus.GaugeValue},
numSnapshot: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "snapshot_number_total"),
"vm number of snapshot", labels, nil), prometheus.GaugeValue},
diskCapacityBytes: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "disk_capacity_bytes"),
"vm disk capacity bytes", diskLabels, nil), prometheus.GaugeValue},
networkConnected: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "network_connected"),
"vm network connected", networkLabels, nil), prometheus.GaugeValue},
ethernetDriverConnected: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, virtualMachineCollectorSubsystem, "ethernet_driver_connected"),
"vm ethernet driver connected", ethernetDevLabels, nil), prometheus.GaugeValue},
}
res.logger = logger
return &res, nil
}
func (c *virtualMachineCollector) Update(ch chan<- prometheus.Metric) (err error) {
cache.Flush()
err = c.apiConnect()
if err != nil {
level.Error(c.logger).Log("msg", "unable to connect", "err", err)
return err
}
defer c.apiDisconnect()
items, err := c.apiRetrieve()
if err != nil {
level.Error(c.logger).Log("msg", "unable retrieve vm", "err", err)
return err
}
vc := *vcURL
level.Debug(c.logger).Log("msg", "virtual machine retrieved", "num", len(items))
for _, item := range items {
var esxName string
var poolName string
var parents Parents
pool := getVMPool(c.ctx, c.logger, c.client.Client, item)
if pool == nil {
parents = getParents(c.ctx, c.logger, c.client.Client, item.ManagedEntity)
poolName = "NONE"
} else {
parents = getParents(c.ctx, c.logger, c.client.Client, *pool)
poolName = pool.Name
}
host := getVMHostSystem(c.ctx, c.logger, c.client.Client, item)
if host == nil {
esxName = "NONE"
} else {
esxName = host.Name
}
labelsValues := []string{
vc,
parents.dc,
parents.cluster,
esxName,
poolName,
item.Summary.Config.Name,
item.Summary.Guest.HostName,
item.Summary.Guest.GuestFullName,
string(item.Runtime.PowerState),
string(item.Summary.OverallStatus),
string(item.Guest.ToolsStatus),
item.Guest.ToolsVersion,
}
if *useIsecSpecifics {
annotation := GetIsecAnnotation(item)
labelsValues = append(
labelsValues,
annotation.Criticality,
annotation.Responsable,
annotation.Service,
)
}
mb := int64(1024 * 1024)
ch <- c.numCPU.mustNewConstMetric(float64(item.Config.Hardware.NumCPU), labelsValues...)
ch <- c.numCoresPerSocket.mustNewConstMetric(float64(item.Config.Hardware.NumCoresPerSocket), labelsValues...)
ch <- c.memoryBytes.mustNewConstMetric(float64(int64(item.Config.Hardware.MemoryMB)*mb), labelsValues...)
ch <- c.overallCPUUsage.mustNewConstMetric(float64(item.Summary.QuickStats.OverallCpuUsage), labelsValues...)
ch <- c.overallCPUDemand.mustNewConstMetric(float64(item.Summary.QuickStats.OverallCpuDemand), labelsValues...)
ch <- c.guestMemoryUsage.mustNewConstMetric(float64(int64(item.Summary.QuickStats.GuestMemoryUsage)*mb), labelsValues...)
ch <- c.hostMemoryUsage.mustNewConstMetric(float64(int64(item.Summary.QuickStats.HostMemoryUsage)*mb), labelsValues...)
ch <- c.distributedCPUEntitlement.mustNewConstMetric(float64(item.Summary.QuickStats.DistributedCpuEntitlement), labelsValues...)
ch <- c.distributedMemoryEntitlement.mustNewConstMetric(float64(int64(item.Summary.QuickStats.DistributedMemoryEntitlement)*mb), labelsValues...)
ch <- c.staticCPUEntitlement.mustNewConstMetric(float64(item.Summary.QuickStats.StaticCpuEntitlement), labelsValues...)
ch <- c.staticMemoryEntitlement.mustNewConstMetric(float64(int64(item.Summary.QuickStats.StaticMemoryEntitlement)*mb), labelsValues...)
ch <- c.privateMemory.mustNewConstMetric(float64(int64(item.Summary.QuickStats.PrivateMemory)*mb), labelsValues...)
ch <- c.sharedMemory.mustNewConstMetric(float64(int64(item.Summary.QuickStats.SharedMemory)*mb), labelsValues...)
ch <- c.swappedMemory.mustNewConstMetric(float64(int64(item.Summary.QuickStats.SwappedMemory)*mb), labelsValues...)
ch <- c.balloonedMemory.mustNewConstMetric(float64(int64(item.Summary.QuickStats.BalloonedMemory)*mb), labelsValues...)
ch <- c.consumedOverheadMemory.mustNewConstMetric(float64(int64(item.Summary.QuickStats.ConsumedOverheadMemory)*mb), labelsValues...)
ch <- c.ftLogBandwidth.mustNewConstMetric(float64(item.Summary.QuickStats.FtLogBandwidth), labelsValues...)
ch <- c.ftSecondaryLatency.mustNewConstMetric(float64(item.Summary.QuickStats.FtSecondaryLatency), labelsValues...)
ch <- c.compressedMemory.mustNewConstMetric(float64(int64(item.Summary.QuickStats.CompressedMemory)*mb), labelsValues...)
ch <- c.uptimeSeconds.mustNewConstMetric(float64(item.Summary.QuickStats.UptimeSeconds), labelsValues...)
ch <- c.ssdSwappedMemory.mustNewConstMetric(float64(int64(item.Summary.QuickStats.SsdSwappedMemory)*mb), labelsValues...)
if item.Snapshot != nil {
ch <- c.numSnapshot.mustNewConstMetric(float64(len(item.Snapshot.RootSnapshotList)), labelsValues...)
} else {
ch <- c.numSnapshot.mustNewConstMetric(0.0, labelsValues...)
}
edevices := GetEthernetDevices(item)
for _, edev := range edevices {
tmp := append(labelsValues, edev.typeName, edev.mac, edev.status)
ch <- c.ethernetDriverConnected.mustNewConstMetric(b2f(edev.connected), tmp...)
}
networks := GetNetworks(item)
for _, net := range networks {
for _, ip := range net.ip {
tmp := append(labelsValues, net.network, net.mac, ip)
ch <- c.networkConnected.mustNewConstMetric(b2f(net.connected), tmp...)
}
}
disks := GetDisks(item)
for _, disk := range disks {
tmp := append(labelsValues, disk.vmdk, strconv.FormatBool(disk.thinProvisioned))
ch <- c.diskCapacityBytes.mustNewConstMetric(float64(disk.capacity), tmp...)
}
}
return nil
}
type IsecAnnotation struct {
Criticality string `json:"crit"`
Responsable string `json:"resp"`
Service string `json:"svc"`
}
func GetIsecAnnotation(vm mo.VirtualMachine) IsecAnnotation {
tmp := IsecAnnotation{
Service: "not defined",
Responsable: "not defined",
Criticality: "not defined",
}
_ = json.Unmarshal([]byte(vm.Config.Annotation), &tmp)
return tmp
}
type EthernetDevice struct {
name string
typeName string
mac string
status string
connected bool
}
func GetEthernetDevices(vm mo.VirtualMachine) []EthernetDevice {
devices := object.VirtualDeviceList(vm.Config.Hardware.Device)
res := make([]EthernetDevice, 0, len(devices))
for _, dev := range devices {
switch md := dev.(type) {
case types.BaseVirtualEthernetCard:
status := "unknown"
connected := false
name := devices.Name(dev)
typeName := strings.TrimPrefix(devices.TypeName(dev), "Virtual")
d := dev.GetVirtualDevice()
mac := md.GetVirtualEthernetCard().MacAddress
if ca := d.Connectable; ca != nil {
status = ca.Status
connected = ca.Connected
}
res = append(res, EthernetDevice{
name: name,
typeName: typeName,
mac: mac,
status: status,
connected: connected,
})
}
}
return res
}
type Network struct {
network string
mac string
ip []string
connected bool
}
func GetNetworks(vm mo.VirtualMachine) []Network {
res := make([]Network, 0, len(vm.Guest.Net))
for _, net := range vm.Guest.Net {
item := Network{
network: net.Network,
mac: net.MacAddress,
ip: net.IpAddress,
connected: net.Connected,
}
res = append(res, item)
}
return res
}
type Disk struct {
vmdk string
capacity int64
thinProvisioned bool
}
func GetDisks(vm mo.VirtualMachine) []Disk {
disks := object.VirtualDeviceList(vm.Config.Hardware.Device).SelectByType((*types.VirtualDisk)(nil))
res := make([]Disk, 0, len(disks))
for _, d := range disks {
disk := d.(*types.VirtualDisk)
info := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo)
res = append(res, Disk{
vmdk: info.FileName,
capacity: disk.CapacityInBytes,
thinProvisioned: *info.ThinProvisioned,
})
}
return res
}
func (c *virtualMachineCollector) apiRetrieve() ([]mo.VirtualMachine, error) {
var items []mo.VirtualMachine
m := view.NewManager(c.client.Client)
v, err := m.CreateContainerView(
c.ctx,
c.client.ServiceContent.RootFolder,
[]string{"VirtualMachine"},
true,
)
if err != nil {
return items, err
}
defer c.destroyView(v)
err = v.Retrieve(
c.ctx,
[]string{"VirtualMachine"},
[]string{
"config",
//"datatore",
"guest",
"guestHeartbeatStatus",
"network",
"parent",
"resourceConfig",
"resourcePool",
"runtime",
"snapshot",
"summary",
},
&items,
)
return items, err
}