forked from benapetr/xapi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
462 lines (399 loc) · 13.2 KB
/
exporter.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package main
import (
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"time"
xenAPI "github.com/johnprather/go-xen-api-client"
"github.com/prometheus/client_golang/prometheus"
)
type exporterClass struct {
metrics []*prometheus.GaugeVec
counter prometheus.Counter
replacer *strings.Replacer
hostname string
poolGatherers map[string]*poolGathererClass
}
type poolGathererClass struct {
poolName string
lastKnownMaster string
xenClients map[string]*xenAPI.Client
}
func newExporter() *exporterClass {
var err error
e := &exporterClass{}
e.hostname, err = os.Hostname()
if err != nil {
log.Fatalf("os.Hostname(): %s", err.Error())
}
e.poolGatherers = make(map[string]*poolGathererClass)
lastUpdatedMetric := newMetric("last_updated",
map[string]string{"host": e.hostname},
0)
e.metrics = append(e.metrics, lastUpdatedMetric)
return e
}
// Describe calls Describe(ch) for all stored metrics
func (e *exporterClass) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.metrics {
m.Describe(ch)
}
}
// Collect gathers data, then calls Collect(ch) for all stored metrics
func (e *exporterClass) Collect(ch chan<- prometheus.Metric) {
e.gatherData()
for _, m := range e.metrics {
m.Collect(ch)
}
}
func (e *exporterClass) newGatherer(pool string) {
log.Printf("Instantiating gatherer for %s\n", pool)
gatherer := &poolGathererClass{}
gatherer.poolName = pool
gatherer.xenClients = make(map[string]*xenAPI.Client)
e.poolGatherers[pool] = gatherer
}
func (e *exporterClass) gatherData() {
log.Printf("Starting gather job for all pools")
timeStarted := time.Now().Unix()
// create a channel to collect data from other routines
retCh := make(chan []*prometheus.GaugeVec)
// launch a separate go routine for each pool
for pool := range config.Pools {
var ok bool
if _, ok = e.poolGatherers[pool]; !ok {
e.newGatherer(pool)
}
go e.poolGatherers[pool].gather(retCh)
}
// create an array in which to gather results
var retLists [][]*prometheus.GaugeVec
for {
// wait for results from one of the gather routines
metricList := <-retCh
retLists = append(retLists, metricList)
// break if we have results back for all the routines
if len(retLists) == len(config.Pools) {
break
}
}
var newMetrics []*prometheus.GaugeVec
for _, metricsList := range retLists {
for _, metric := range metricsList {
newMetrics = append(newMetrics, metric)
}
}
timeFinished := time.Now().Unix()
lastUpdatedMetric := newMetric("last_updated",
map[string]string{"host": e.hostname},
float64(timeFinished))
newMetrics = append(newMetrics, lastUpdatedMetric)
gatherTimeMetric := newMetric("gather_time",
map[string]string{"host": e.hostname},
float64(timeFinished-timeStarted))
newMetrics = append(newMetrics, gatherTimeMetric)
log.Printf("Completed gather job for all pools in %d seconds\n",
timeFinished-timeStarted)
e.metrics = newMetrics
}
func (g *poolGathererClass) gather(retCh chan []*prometheus.GaugeVec) {
var metricList []*prometheus.GaugeVec
var defaultSRList []xenAPI.SRRef
defer func() { retCh <- metricList }()
timeStarted := time.Now().Unix()
xenClient, session, err := g.getXenClient()
if err != nil {
log.Printf("Error getting XAPI client for %s: %s\n", g.poolName, err.Error())
return
}
timeConnected := time.Now().Unix()
log.Printf("gatherPoolData(): %s: session established in %d seconds\n",
g.poolName, timeConnected-timeStarted)
poolRecs, err := xenClient.Pool.GetAllRecords(session)
if err != nil {
log.Printf("Error getting pool records for %s: %s", g.poolName, err.Error())
return
}
hostRecs, err := xenClient.Host.GetAllRecords(session)
if err != nil {
log.Printf("Error getting host records for %s: %s\n", g.poolName, err.Error())
return
}
vmRecs, err := xenClient.VM.GetAllRecords(session)
if err != nil {
log.Printf("Error getting vm records for %s: %s\n", g.poolName, err.Error())
return
}
vmMetricsRecs, err := xenClient.VMMetrics.GetAllRecords(session)
if err != nil {
log.Printf("Error getting vm metrics records for %s: %s\n", g.poolName, err.Error())
return
}
hostMetricsRecs, err := xenClient.HostMetrics.GetAllRecords(session)
if err != nil {
log.Printf("Error getting host metrics records for %s: %s\n", g.poolName, err.Error())
return
}
srRecs, err := xenClient.SR.GetAllRecords(session)
if err != nil {
log.Printf("Error getting sr records for %s: %s", g.poolName, err.Error())
return
}
for _, hostRec := range hostRecs {
// tally vcpus and vms
vCPUCount := 0
vmCount := 0
for _, vmRef := range hostRec.ResidentVMs {
if vmRec, ok := vmRecs[vmRef]; ok && !vmRec.IsControlDomain {
vmCount++
if vmMetricsRec, ok := vmMetricsRecs[vmRec.Metrics]; ok {
vCPUCount += vmMetricsRec.VCPUsNumber
}
}
}
cpuCount, _ := strconv.ParseFloat(hostRec.CPUInfo["cpu_count"], 64)
// set cpu_count metric for the host
if metricEnabled("cpu_count") {
cpuCountMetric := newMetric("cpu_count",
map[string]string{"host": hostRec.Hostname},
cpuCount)
metricList = append(metricList, cpuCountMetric)
}
// set cpu_allocation metric for the host
if metricEnabled("cpu_allocation") {
cpuAllocationMetric := newMetric("cpu_allocation",
map[string]string{"host": hostRec.Hostname},
float64(vCPUCount)*100/cpuCount)
metricList = append(metricList, cpuAllocationMetric)
}
// set memory_total metric for host
if metricEnabled("memory_total") {
memoryTotalMetric := newMetric("memory_total",
map[string]string{"host": hostRec.Hostname},
float64(hostMetricsRecs[hostRec.Metrics].MemoryTotal))
metricList = append(metricList, memoryTotalMetric)
}
// set memory_free metric for host
if metricEnabled("memory_free") {
memoryFreeMetric := newMetric("memory_free",
map[string]string{"host": hostRec.Hostname},
float64(hostMetricsRecs[hostRec.Metrics].MemoryFree))
metricList = append(metricList, memoryFreeMetric)
}
// set memory_allocation metric for host
if metricEnabled("memory_allocation") {
hostMetricsRec := hostMetricsRecs[hostRec.Metrics]
memoryAllocationMetric := newMetric("memory_allocation",
map[string]string{"host": hostRec.Hostname},
float64(hostMetricsRec.MemoryTotal-hostMetricsRec.MemoryFree)*100/
float64(hostMetricsRec.MemoryTotal))
metricList = append(metricList, memoryAllocationMetric)
}
// set resident_vcpu_count metric for host
if metricEnabled("resident_vcpu_count") {
residentVCPUCountMetric := newMetric("resident_vcpu_count",
map[string]string{"host": hostRec.Hostname},
float64(vCPUCount))
metricList = append(metricList, residentVCPUCountMetric)
}
// set resident_vm_count metric for host
if metricEnabled("resident_vm_count") {
residentVMCountMetric := newMetric("resident_vm_count",
map[string]string{"host": hostRec.Hostname},
float64(vmCount))
metricList = append(metricList, residentVMCountMetric)
}
}
for _, poolRec := range poolRecs {
defaultSRList = append(defaultSRList, poolRec.DefaultSR)
// set ha_allow_overcommit metric for pool
if metricEnabled("ha_allow_overcommit") {
haAllowOvercommitMetric := newMetric("ha_allow_overcommit",
map[string]string{"pool": poolRec.NameLabel},
boolFloat(poolRec.HaAllowOvercommit))
metricList = append(metricList, haAllowOvercommitMetric)
}
// set ha_enabled metric for pool
if metricEnabled("ha_enabled") {
haEnabledMetric := newMetric("ha_enabled",
map[string]string{"pool": poolRec.NameLabel},
boolFloat(poolRec.HaEnabled))
metricList = append(metricList, haEnabledMetric)
}
// set ha_host_failures_to_tolerate metric for pool
if metricEnabled("ha_host_failures_to_tolerate") {
haHostFailuresToTolerateMetric := newMetric("ha_host_failures_to_tolerate",
map[string]string{"pool": poolRec.NameLabel},
float64(poolRec.HaHostFailuresToTolerate))
metricList = append(metricList, haHostFailuresToTolerateMetric)
}
// set the ha_overcommitted metric for pool
if metricEnabled("ha_overcommitted") {
haOvercommittedMetric := newMetric("ha_overcommitted",
map[string]string{"pool": poolRec.NameLabel},
boolFloat(poolRec.HaOvercommitted))
metricList = append(metricList, haOvercommittedMetric)
}
// set the wlb_enabled metric for the pool
if metricEnabled("wlb_enabled") {
wlbEnabledMetric := newMetric("wlb_enabled",
map[string]string{"pool": poolRec.NameLabel},
boolFloat(poolRec.WlbEnabled))
metricList = append(metricList, wlbEnabledMetric)
}
}
for srRef, srRec := range srRecs {
defaultSR := false
for _, defSR := range defaultSRList {
if defSR == srRef {
defaultSR = true
}
}
// set the default_storage metric for the sr
if metricEnabled("default_storage") {
defaultStorageMetric := newMetric("default_storage",
map[string]string{
"uuid": srRec.UUID,
"pool": g.poolName,
"type": srRec.Type,
"name_label": srRec.NameLabel,
}, boolFloat(defaultSR))
metricList = append(metricList, defaultStorageMetric)
}
// set the physical_size metric for the sr
if metricEnabled("physical_size") {
physicalSizeMetric := newMetric("physical_size",
map[string]string{
"uuid": srRec.UUID,
"pool": g.poolName,
"type": srRec.Type,
"name_label": srRec.NameLabel,
}, float64(srRec.PhysicalSize))
metricList = append(metricList, physicalSizeMetric)
}
// set the physical_utilisation metric for the sr
if metricEnabled("physical_utilisation") {
physicalUtilisationMetric := newMetric("physical_utilisation",
map[string]string{
"uuid": srRec.UUID,
"pool": g.poolName,
"type": srRec.Type,
"name_label": srRec.NameLabel,
}, float64(srRec.PhysicalUtilisation))
metricList = append(metricList, physicalUtilisationMetric)
}
// set the physical_pct_allocated metric for the sr
physicalPctAllocated := float64(0)
if srRec.PhysicalSize > 0 {
physicalPctAllocated = float64(srRec.PhysicalUtilisation) * 100 /
float64(srRec.PhysicalSize)
}
if metricEnabled("physical_pct_allocated") {
physicalPctAllocatedMetric := newMetric("physical_pct_allocated",
map[string]string{
"uuid": srRec.UUID,
"pool": g.poolName,
"type": srRec.Type,
"name_label": srRec.NameLabel,
}, physicalPctAllocated)
metricList = append(metricList, physicalPctAllocatedMetric)
}
// set the virtual_allocation metric for the sr
if metricEnabled("virtual_allocation") {
virtualAllocationMetric := newMetric("virtual_allocation",
map[string]string{
"uuid": srRec.UUID,
"pool": g.poolName,
"type": srRec.Type,
"name_label": srRec.NameLabel,
}, float64(srRec.VirtualAllocation))
metricList = append(metricList, virtualAllocationMetric)
}
}
timeGenerated := time.Now().Unix()
log.Printf("gatherPoolData(): %s: gather time %d seconds\n",
g.poolName, timeGenerated-timeStarted)
}
func (g *poolGathererClass) getXenClient() (
xenClient *xenAPI.Client, session xenAPI.SessionRef, err error) {
var hostList = config.Pools[g.poolName]
// if a lastKnownMaster exists in our host list, bump it to the top
if len(g.lastKnownMaster) != 0 {
var newHostList = []string{g.lastKnownMaster}
for key, host := range hostList {
if host == g.lastKnownMaster {
hostList = append(hostList[:key], hostList[key+1:]...)
break
}
var ips []net.IP
ips, err = net.LookupIP(host)
if err != nil {
ipMatch := false
for _, ip := range ips {
if g.lastKnownMaster == ip.String() {
hostList = append(hostList[:key], hostList[key+1:]...)
ipMatch = true
break
}
}
if ipMatch {
break
}
}
}
hostList = newHostList
}
for _, host := range hostList {
xenClient, session, err = g.tryXenClient(host)
if err == nil {
return xenClient, session, nil
}
log.Printf("tryXenClient(): %s: %s\n", host, err.Error())
}
return nil, "", fmt.Errorf(
"%s: unable to authenticate into a master host", g.poolName)
}
func (g *poolGathererClass) tryXenClient(host string) (
xenClient *xenAPI.Client, session xenAPI.SessionRef, err error) {
var ok bool
if xenClient, ok = g.xenClients[host]; !ok {
log.Printf("Instantiating xenAPI.Client for %s\n", host)
// no xapi client exists for this host, create a new one
xenClient, err = xenAPI.NewClient("https://"+host, nil)
if err != nil {
return nil, "", fmt.Errorf("NewClient(): %s: %s\n", host, err.Error())
}
}
sessionCh := make(chan xenAPI.SessionRef)
errCh := make(chan error)
go func(xenClient *xenAPI.Client, sessionCh chan xenAPI.SessionRef,
errCh chan error) {
session, err = xenClient.Session.LoginWithPassword(
config.Auth.Username, config.Auth.Password,
"1.0", "xapi_exporter")
if err != nil {
errCh <- err
} else {
sessionCh <- session
}
}(xenClient, sessionCh, errCh)
select {
case err := <-errCh:
errParts := strings.Split(err.Error(), " ")
if errParts[2] == "HOST_IS_SLAVE" {
return g.tryXenClient(errParts[3])
}
return nil, "", fmt.Errorf(
"LoginWithPassword(): %s: %s\n", host, err.Error())
case <-time.After(time.Second * config.TimeoutLogin):
return nil, "", fmt.Errorf(
"LoginWithPassword(): timeout after %d seconds", config.TimeoutLogin)
case session = <-sessionCh:
}
g.xenClients[host] = xenClient
g.lastKnownMaster = host
return
}