-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathvm.go
More file actions
200 lines (159 loc) · 6.82 KB
/
Copy pathvm.go
File metadata and controls
200 lines (159 loc) · 6.82 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
package vmwareCollectors
import (
"context"
"flag"
"fmt"
"log/slog"
"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 hostMOFromRuntime(runtime types.VirtualMachineRuntimeInfo) string {
if runtime.Host == nil {
return ""
}
return runtime.Host.Value
}
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"}, &vms, c.logger,
)
if err != nil {
return err
}
wg := sync.WaitGroup{}
for _, vm := range vms {
hostMO := hostMOFromRuntime(vm.Runtime)
vmLabels := map[string]string{
"vmmo": vm.Self.Value,
"vm": vm.Summary.Config.Name,
"hostmo": hostMO,
"vcenter": loginData["target"].(string),
}
vmPoweredOn := vm.Runtime.PowerState == "poweredOn"
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, vmSubsystem, "powered_on"),
"Whether the virtual machine is powered on", nil, vmLabels,
), prometheus.GaugeValue,
func(poweredOn bool) float64 {
if poweredOn {
return 1
}
return 0
}(vmPoweredOn),
)
if vmPoweredOn {
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": hostMO, "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": hostMO, "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": hostMO, "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"),
"Virtual memory configured in MB", 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),
)
}
// 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("msg", fmt.Sprintf("VM %s has snapshots", vm.Summary.Config.Name), nil)
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("msg", fmt.Sprintf("Time to process PropColletor for VM: %f\n", time.Since(begin).Seconds()), nil)
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("msg", fmt.Sprintf("Time to process PerfMan for VM: %f\n", time.Since(begin).Seconds()), nil)
return nil
}