forked from jorisdevrede/one_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
328 lines (265 loc) · 10.8 KB
/
metrics.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
package main
import (
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/OpenNebula/one/src/oca/go/src/goca"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
hostMetrics = make(map[string]*prometheus.GaugeVec)
clusterMetrics = make(map[string]*prometheus.GaugeVec)
vMMetrics = make(map[string]*prometheus.GaugeVec)
datastoreMetrics = make(map[string]*prometheus.GaugeVec)
clusterIdToName = make(map[int]string) //name of the cluster, as name not given in vm api information
)
func initMetrics() {
/*
Host Metrics setup
*/
{
// Memory
hostMetrics["TotalMem"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_host_totalmem",
Help: "total memory available on host",
}, []string{"cluster", "host"})
hostMetrics["MaxMem"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_host_maxmem",
Help: "maximum memory available on host (total - reserved)",
}, []string{"cluster", "host"})
hostMetrics["MemUsage"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_host_memusage",
Help: "total allocated memory on host",
}, []string{"cluster", "host"})
//CPU
hostMetrics["TotalCPU"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_host_totalcpu",
Help: "total cpu available on host",
}, []string{"cluster", "host"})
hostMetrics["MaxCPU"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_host_maxcpu",
Help: "maximum cpu available on host (total - reserved)",
}, []string{"cluster", "host"})
hostMetrics["CPUUsage"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_host_cpuusage",
Help: "total allocated cpu on host",
}, []string{"cluster", "host"})
// Nr of VMs
hostMetrics["RunningVMs"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_host_runningvms",
Help: "running virtual machines on host",
}, []string{"cluster", "host"})
hostMetrics["State"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_host_state",
Help: "state of host",
}, []string{"cluster", "host", "state"})
}
/*
Cluster Metrics setup
*/
{
//Memory
clusterMetrics["TotalMem"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_cluster_totalmem",
Help: "total memory available in cluster",
}, []string{"cluster"})
clusterMetrics["MaxMem"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_cluster_maxmem",
Help: "maximum memory available in cluster (total - reserved)",
}, []string{"cluster"})
clusterMetrics["MemUsage"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_cluster_memusage",
Help: "total allocated memory in cluster",
}, []string{"cluster"})
//CPU
clusterMetrics["TotalCPU"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_cluster_totalcpu",
Help: "total cpu available in cluster",
}, []string{"cluster"})
clusterMetrics["MaxCPU"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_cluster_maxcpu",
Help: "maximum cpu available in cluster (total - reserved",
}, []string{"cluster"})
clusterMetrics["CPUUsage"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_cluster_cpuusage",
Help: "total allocated cpu in cluster",
}, []string{"cluster"})
}
/*
VM Metrics setup
*/
{
vMMetrics["State"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_vm_state",
Help: "state of the vm",
}, []string{"cluster", "host", "vm", "state"})
vMMetrics["LCMState"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_vm_lcmstate",
Help: "lcm state of the vm. Only set when vm is active (State 3)",
}, []string{"cluster", "host", "vm", "lcm_state"})
}
/*
Datastore Metrics setup
*/
{
datastoreMetrics["Available"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_datastore_available",
Help: "which datastores are available in which cluster",
}, []string{"cluster", "datastore"})
datastoreMetrics["State"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_datastore_state",
Help: "datastore state (ready, disabled)",
}, []string{"datastore", "state"})
datastoreMetrics["TotalMB"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_datastore_totalmb",
Help: "total capacity of datastore",
}, []string{"datastore"})
datastoreMetrics["UsedMB"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_datastore_usedmb",
Help: "used capacity of datastore",
}, []string{"datastore"})
datastoreMetrics["FreeMB"] = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "one_datastore_freemb",
Help: "free capacity of datastore",
}, []string{"datastore"})
}
}
func recordMetrics(config config, logger log.Logger) {
level.Info(logger).Log("msg", "recording metrics from opennebula frontend", "interval", config.interval)
client := goca.NewDefaultClient(goca.NewConfig(config.user, config.password, config.endpoint))
controller := goca.NewController(client)
for {
fillIdMaps(controller, logger)
recordHostMetrics(controller, logger)
recordVmMetrics(controller, logger)
recordDataStoreMetrics(controller, logger)
time.Sleep(time.Duration(config.interval) * time.Second)
}
}
func fillIdMaps(controller *goca.Controller, logger log.Logger) {
clusters, err := controller.Clusters().Info(true)
if err != nil {
level.Error(logger).Log("msg", "error retrieving cluster info", "error", err)
return
}
for _, cluster := range clusters.Clusters {
clusterIdToName[cluster.ID] = cluster.Name
}
}
func recordHostMetrics(controller *goca.Controller, logger log.Logger) {
pool, err := controller.Hosts().Info()
if err != nil {
level.Error(logger).Log("msg", "error retrieving hosts info", "error", err)
return
}
type metrics struct {
cluster, metric string
}
sum := make(map[metrics]int)
//reset the state metrics, as the State name is stored as label
hostMetrics["State"].Reset()
for _, host := range pool.Hosts {
state, err := host.State()
if err != nil {
level.Error(logger).Log("msg", "error retrieving state from host", "host", host.Name, "error", err)
continue
}
level.Debug(logger).Log("msg", "host metrics",
"host", host.Name,
"state", state,
"TotalMem", host.Share.TotalMem,
"MaxMem", host.Share.MaxMem,
"MemUsage", host.Share.MemUsage,
"TotalCPU", host.Share.TotalCPU,
"CPUUsage", host.Share.CPUUsage,
"RunningVMs", host.Share.RunningVMs,
)
// record host metrics
hostMetrics["TotalMem"].With(prometheus.Labels{"cluster": host.Cluster, "host": host.Name}).Set(float64(host.Share.TotalMem))
hostMetrics["MaxMem"].With(prometheus.Labels{"cluster": host.Cluster, "host": host.Name}).Set(float64(host.Share.MaxMem))
hostMetrics["MemUsage"].With(prometheus.Labels{"cluster": host.Cluster, "host": host.Name}).Set(float64(host.Share.MemUsage))
hostMetrics["TotalCPU"].With(prometheus.Labels{"cluster": host.Cluster, "host": host.Name}).Set(float64(host.Share.TotalCPU))
hostMetrics["MaxCPU"].With(prometheus.Labels{"cluster": host.Cluster, "host": host.Name}).Set(float64(host.Share.MaxCPU))
hostMetrics["CPUUsage"].With(prometheus.Labels{"cluster": host.Cluster, "host": host.Name}).Set(float64(host.Share.CPUUsage))
hostMetrics["RunningVMs"].With(prometheus.Labels{"cluster": host.Cluster, "host": host.Name}).Set(float64(host.Share.RunningVMs))
hostMetrics["State"].With(prometheus.Labels{"cluster": host.Cluster, "host": host.Name, "state": state.String()}).Set(float64(host.StateRaw))
// sum cluster metrics
sum[metrics{host.Cluster, "TotalMem"}] = sum[metrics{host.Cluster, "TotalMem"}] + host.Share.TotalMem
sum[metrics{host.Cluster, "MaxMem"}] = sum[metrics{host.Cluster, "MaxMem"}] + host.Share.MaxMem
sum[metrics{host.Cluster, "MemUsage"}] = sum[metrics{host.Cluster, "MemUsage"}] + host.Share.MemUsage
sum[metrics{host.Cluster, "TotalCPU"}] = sum[metrics{host.Cluster, "TotalCPU"}] + host.Share.TotalCPU
sum[metrics{host.Cluster, "MaxCPU"}] = sum[metrics{host.Cluster, "MaxCPU"}] + host.Share.MaxCPU
sum[metrics{host.Cluster, "CPUUsage"}] = sum[metrics{host.Cluster, "CPUUsage"}] + host.Share.CPUUsage
}
for key, value := range sum {
// record cluster metrics
clusterMetrics[key.metric].With(prometheus.Labels{"cluster": key.cluster}).Set(float64(value))
}
}
func recordVmMetrics(controller *goca.Controller, logger log.Logger) {
vms, err := controller.VMs().Info()
if err != nil {
level.Error(logger).Log("msg", "error retrieving vms info", "error", err)
return
}
//reset the state metrics, as the State name is stored as label
vMMetrics["State"].Reset()
vMMetrics["LCMState"].Reset()
for _, vm := range vms.VMs {
state, lcmState, err := vm.State()
if err != nil {
level.Error(logger).Log("msg", "error retrieving state from vm", "vm", vm.Name, "error", err)
continue
}
if len(vm.HistoryRecords) == 0 {
continue
}
host := vm.HistoryRecords[0].Hostname
cluster := clusterIdToName[vm.HistoryRecords[0].CID]
level.Debug(logger).Log("msg", "vm metrics",
"vm", vm.Name,
"StateRaw", vm.StateRaw,
"State", state,
"LCMStateRaw", vm.LCMStateRaw,
"LCMState", lcmState,
"Host", host,
"Cluster", cluster,
)
vMMetrics["State"].With(prometheus.Labels{"cluster": cluster, "host": host, "vm": vm.Name, "state": state.String()}).Set(float64(vm.StateRaw))
vMMetrics["LCMState"].With(prometheus.Labels{"cluster": cluster, "host": host, "vm": vm.Name, "lcm_state": lcmState.String()}).Set(float64(vm.LCMStateRaw))
}
}
func recordDataStoreMetrics(controller *goca.Controller, logger log.Logger) {
datastores, err := controller.Datastores().Info()
if err != nil {
level.Error(logger).Log("msg", "error retrieving datastores info", "error", err)
return
}
//reset the state metrics, as the State name is stored as label
datastoreMetrics["State"].Reset()
for _, datastore := range datastores.Datastores {
state, err := datastore.State()
if err != nil {
level.Debug(logger).Log("msg", "Failed get state from datastore", "error", err)
continue
}
level.Debug(logger).Log("msg", "datastore metrics",
"datastore", datastore.Name,
"StateRaw", datastore.StateRaw,
"State", state,
"TotalMB", datastore.TotalMB,
"UsedMB", datastore.UsedMB,
"FreeMB", datastore.FreeMB,
"DiskType", datastore.DiskType,
)
for _, clusterId := range datastore.Clusters.ID {
cluster := clusterIdToName[clusterId]
datastoreMetrics["Available"].With(prometheus.Labels{"datastore": datastore.Name, "cluster": cluster}).Set(float64(1))
}
datastoreMetrics["TotalMB"].With(prometheus.Labels{"datastore": datastore.Name}).Set(float64(datastore.TotalMB))
datastoreMetrics["UsedMB"].With(prometheus.Labels{"datastore": datastore.Name}).Set(float64(datastore.UsedMB))
datastoreMetrics["FreeMB"].With(prometheus.Labels{"datastore": datastore.Name}).Set(float64(datastore.FreeMB))
datastoreMetrics["State"].With(prometheus.Labels{"datastore": datastore.Name, "state": state.String()}).Set(float64(state))
}
}