-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathvm.go
More file actions
227 lines (183 loc) · 7.83 KB
/
Copy pathvm.go
File metadata and controls
227 lines (183 loc) · 7.83 KB
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
package vmwareCollectors
import (
"context"
"flag"
"fmt"
"log/slog"
"sort"
"strconv"
"sync"
"time"
"github.com/prezhdarov/prometheus-exporter/pkg/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/vmware/govmomi/performance"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
const (
vmSubsystem = "vm"
)
var vmCollectorFlag = flag.Bool(fmt.Sprintf("collector.%s", vmSubsystem), collector.DefaultEnabled, fmt.Sprintf("Enable the %s collector (default: %v)", vmSubsystem, collector.DefaultEnabled))
var (
cVMCounters = []string{"cpu.usagemhz.average", "cpu.demand.average", "cpu.latency.average", "cpu.entitlement.latest",
"cpu.ready.summation", "cpu.readiness.average", "cpu.costop.summation", "cpu.maxlimited.summation",
"mem.entitlement.average", "mem.active.average", "mem.shared.average", "mem.vmmemctl.average",
"mem.swapped.average", "mem.consumed.average", "sys.uptime.latest",
} //Common or generic counters that need not be instanced
iVMCounters = []string{"net.bytesRx.average", "net.bytesTx.average",
"datastore.read.average", "datastore.write.average", "datastore.numberReadAveraged.average",
"datastore.numberWriteAveraged.average", "datastore.totalReadLatency.average", "datastore.totalWriteLatency.average"} //Counters that come in multiple i
)
type vmCollector struct {
logger *slog.Logger
}
func init() {
collector.RegisterCollector(vmSubsystem, vmCollectorFlag, NewvmCollector)
}
// NewMeminfoCollector returns a new Collector exposing memory stats.
func NewvmCollector(logger *slog.Logger) (collector.Collector, error) {
return &vmCollector{logger}, nil
}
func (c *vmCollector) Update(ch chan<- prometheus.Metric, namespace string, clientAPI collector.ClientAPI, loginData map[string]interface{}, params map[string]string) error {
var (
vms []mo.VirtualMachine
vmRefs []types.ManagedObjectReference
vmNames = make(map[string]string)
)
begin := time.Now()
err := fetchProperties(
loginData["ctx"].(context.Context), loginData["view"].(*view.Manager), loginData["client"].(*vim25.Client),
[]string{"VirtualMachine"}, []string{"summary", "runtime", "storage", "snapshot", "snapshot.rootSnapshotList", "snapshot.currentSnapshot", "config.hardware.device"}, &vms, c.logger,
)
if err != nil {
return err
}
wg := sync.WaitGroup{}
for _, vm := range vms {
if vm.Runtime.PowerState == "poweredOn" {
vmRefs = append(vmRefs, vm.Self)
vmNames[vm.Self.Value] = vm.Summary.Config.Name
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, vmSubsystem, "info"),
"This is basic vm info to be used for parent reference", nil,
map[string]string{"vmmo": vm.Self.Value, "vm": vm.Summary.Config.Name, "hostmo": vm.Runtime.Host.Value, "vcenter": loginData["target"].(string)},
), prometheus.GaugeValue, 1.0,
)
//vmLabels := map[string]string{"vmmo": vm.Self.Value, "vm": vm.Summary.Config.Name, "vcenter": loginData["target"].(string)}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, vmSubsystem, "cpu_corecount"),
"Number of virtual CPUs", nil, map[string]string{"vmmo": vm.Self.Value, "vm": vm.Summary.Config.Name, "hostmo": vm.Runtime.Host.Value, "vcenter": loginData["target"].(string)},
), prometheus.GaugeValue, float64(vm.Summary.Config.NumCpu),
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, vmSubsystem, "mem_capacity"),
"Virtual memory configured in MB", nil, map[string]string{"vmmo": vm.Self.Value, "vm": vm.Summary.Config.Name, "hostmo": vm.Runtime.Host.Value, "vcenter": loginData["target"].(string)},
), prometheus.GaugeValue, float64(vm.Summary.Config.MemorySizeMB),
)
for _, datastore := range vm.Storage.PerDatastoreUsage {
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, vmSubsystem, "datastore_capacity_used"),
"Capacity used by the VM on this datastore in bytes", nil,
map[string]string{"vmmo": vm.Self.Value, "vm": vm.Summary.Config.Name,
"vcenter": loginData["target"].(string), "dsmo": datastore.Datastore.Value},
), prometheus.GaugeValue, float64(datastore.Committed),
)
}
if vm.Config != nil {
busNumbers := make(map[int32]int32)
disks := make([]*types.VirtualDisk, 0)
for _, device := range vm.Config.Hardware.Device {
if ctrl, ok := device.(types.BaseVirtualController); ok {
vc := ctrl.GetVirtualController()
busNumbers[vc.Key] = vc.BusNumber
}
if disk, ok := device.(*types.VirtualDisk); ok {
disks = append(disks, disk)
}
}
// Stable "disk 0, disk 1, ..." ordering: controller bus, then unit number, then device key
sort.Slice(disks, func(i, j int) bool {
if bi, bj := busNumbers[disks[i].ControllerKey], busNumbers[disks[j].ControllerKey]; bi != bj {
return bi < bj
}
var ui, uj int32
if disks[i].UnitNumber != nil {
ui = *disks[i].UnitNumber
}
if disks[j].UnitNumber != nil {
uj = *disks[j].UnitNumber
}
if ui != uj {
return ui < uj
}
return disks[i].Key < disks[j].Key
})
for i, disk := range disks {
capacity := disk.CapacityInBytes
if capacity == 0 {
capacity = disk.CapacityInKB * 1024
}
label := ""
if disk.DeviceInfo != nil {
label = disk.DeviceInfo.GetDescription().Label
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, vmSubsystem, "disk_capacity"),
"Configured virtual disk capacity in bytes", nil,
map[string]string{"vmmo": vm.Self.Value, "vm": vm.Summary.Config.Name,
"vcenter": loginData["target"].(string), "disk": strconv.Itoa(i), "label": label},
), prometheus.GaugeValue, float64(capacity),
)
}
}
// Check if the VM has any snapshots, set value of metric to unix timestamp of snapshot creation time
if vm.Snapshot != nil {
c.logger.Debug("vm has snapshots", "vm", vm.Summary.Config.Name, "vm_moref", vm.Self.Value)
for _, rootSnap := range vm.Snapshot.RootSnapshotList {
snapDate := rootSnap.CreateTime.Format(time.RFC3339)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, vmSubsystem, "snapshot_info"),
"Unix timestamp since snapshot creation", nil,
map[string]string{"vmmo": vm.Self.Value, "vm": vm.Summary.Config.Name,
"vcenter": loginData["target"].(string), "name": rootSnap.Name, "created": snapDate},
), prometheus.GaugeValue, float64(rootSnap.CreateTime.Unix()),
)
}
}
}
}
c.logger.Debug("time to process property collector for vm", "duration_seconds", time.Since(begin).Seconds())
begin = time.Now()
if len(vmRefs) > 0 {
wg.Add(2)
for i := 0; i < 2; i++ {
switch i {
case 0:
go func() {
scrapePerformance(loginData["ctx"].(context.Context), ch, c.logger, loginData["samples"].(int32), loginData["interval"].(int32), loginData["perf"].(*performance.Manager),
loginData["target"].(string), "VirtualMachine", namespace, vmSubsystem, "", cVMCounters,
loginData["counters"].(map[string]*types.PerfCounterInfo), vmRefs, vmNames)
wg.Done()
}()
case 1:
go func() {
scrapePerformance(loginData["ctx"].(context.Context), ch, c.logger, loginData["samples"].(int32), loginData["interval"].(int32), loginData["perf"].(*performance.Manager),
loginData["target"].(string), "VirtualMachine", namespace, vmSubsystem, "*", iVMCounters,
loginData["counters"].(map[string]*types.PerfCounterInfo), vmRefs, vmNames)
wg.Done()
}()
}
}
wg.Wait()
}
c.logger.Debug("time to process perfman for vm", "duration_seconds", time.Since(begin).Seconds())
return nil
}