-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathnova.go
More file actions
622 lines (537 loc) · 27.8 KB
/
nova.go
File metadata and controls
622 lines (537 loc) · 27.8 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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package exporters
import (
"errors"
"fmt"
"log/slog"
"os"
"reflect"
"sort"
"strconv"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/quotasets"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/compute/apiversions"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/aggregates"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/extendedserverattributes"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/hypervisors"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/limits"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/services"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/usage"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
"github.com/prometheus/client_golang/prometheus"
)
var server_status = []string{
"ACTIVE",
"BUILD", // The server has not finished the original build process.
"BUILD(spawning)", // The server has not finished the original build process but networking works (HP Cloud specific)
"DELETED", // The server is deleted.
"ERROR", // The server is in error.
"HARD_REBOOT", // The server is hard rebooting.
"PASSWORD", // The password is being reset on the server.
"REBOOT", // The server is in a soft reboot state.
"REBUILD", // The server is currently being rebuilt from an image.
"RESCUE", // The server is in rescue mode.
"RESIZE", // Server is performing the differential copy of data that changed during its initial copy.
"SHUTOFF", // The virtual machine (VM) was powered down by the user, but not through the OpenStack Compute API.
"SUSPENDED", // The server is suspended, either by request or necessity.
"UNKNOWN", // The state of the server is unknown. Contact your cloud provider.
"VERIFY_RESIZE", // System is awaiting confirmation that the server is operational after a move or resize.
"MIGRATING", // The server is migrating. This is caused by a live migration (moving a server that is active) action.
"PAUSED", // The server is paused.
"REVERT_RESIZE", // The resize or migration of a server failed for some reason. The destination server is being cleaned up and the original source server is restarting.
"SHELVED", // The server is in shelved state. Depends on the shelve offload time, the server will be automatically shelved off loaded.
"SHELVED_OFFLOADED", // The shelved server is offloaded (removed from the compute host) and it needs unshelved action to be used again.
"SOFT_DELETED", // The server is marked as deleted but will remain in the cloud for some configurable amount of time.
}
func mapServerStatus(current string) int {
for idx, status := range server_status {
if current == status {
return idx
}
}
return -1
}
func searchFlavorIDbyName(flavorName interface{}, allFlavors []flavors.Flavor) string {
// flavor name is unique, making it suitable as the key for searching
for _, f := range allFlavors {
if f.Name == flavorName {
return f.ID
}
}
return ""
}
type NovaExporter struct {
BaseOpenStackExporter
}
var defaultNovaServerStatusLabels = []string{"id", "status", "name", "tenant_id", "user_id", "address_ipv4",
"address_ipv6", "host_id", "hypervisor_hostname", "uuid", "availability_zone", "flavor_id", "instance_libvirt"}
var defaultNovaMetrics = []Metric{
{Name: "flavors", Fn: ListFlavors},
{Name: "flavor", Labels: []string{"id", "name", "vcpus", "ram", "disk", "is_public"}},
{Name: "availability_zones", Fn: ListAZs},
{Name: "security_groups", Fn: ListComputeSecGroups},
{Name: "total_vms", Fn: ListAllServers},
{Name: "agent_state", Labels: []string{"id", "hostname", "service", "adminState", "zone", "disabledReason"}, Fn: ListNovaAgentState},
{Name: "running_vms", Labels: []string{"hostname", "availability_zone", "aggregates"}, Fn: ListHypervisors},
{Name: "current_workload", Labels: []string{"hostname", "availability_zone", "aggregates"}},
{Name: "vcpus_available", Labels: []string{"hostname", "availability_zone", "aggregates"}},
{Name: "vcpus_used", Labels: []string{"hostname", "availability_zone", "aggregates"}},
{Name: "memory_available_bytes", Labels: []string{"hostname", "availability_zone", "aggregates"}},
{Name: "memory_used_bytes", Labels: []string{"hostname", "availability_zone", "aggregates"}},
{Name: "local_storage_available_bytes", Labels: []string{"hostname", "availability_zone", "aggregates"}},
{Name: "local_storage_used_bytes", Labels: []string{"hostname", "availability_zone", "aggregates"}},
{Name: "free_disk_bytes", Labels: []string{"hostname", "availability_zone", "aggregates"}},
{Name: "server_status", Labels: defaultNovaServerStatusLabels},
{Name: "limits_vcpus_max", Labels: []string{"tenant", "tenant_id"}, Fn: ListComputeLimits, Slow: true},
{Name: "limits_vcpus_used", Labels: []string{"tenant", "tenant_id"}, Slow: true},
{Name: "limits_memory_max", Labels: []string{"tenant", "tenant_id"}, Slow: true},
{Name: "limits_memory_used", Labels: []string{"tenant", "tenant_id"}, Slow: true},
{Name: "limits_instances_used", Labels: []string{"tenant", "tenant_id"}, Slow: true},
{Name: "limits_instances_max", Labels: []string{"tenant", "tenant_id"}, Slow: true},
{Name: "server_local_gb", Labels: []string{"name", "id", "tenant_id"}, Fn: ListUsage, Slow: true},
{Name: "quota_cores", Labels: []string{"type", "tenant"}, Fn: ListQuotas},
{Name: "quota_instances", Labels: []string{"type", "tenant"}},
{Name: "quota_key_pairs", Labels: []string{"type", "tenant"}},
{Name: "quota_metadata_items", Labels: []string{"type", "tenant"}},
{Name: "quota_ram", Labels: []string{"type", "tenant"}},
{Name: "quota_server_groups", Labels: []string{"type", "tenant"}},
{Name: "quota_server_group_members", Labels: []string{"type", "tenant"}},
{Name: "quota_fixed_ips", Labels: []string{"type", "tenant"}},
{Name: "quota_floating_ips", Labels: []string{"type", "tenant"}},
{Name: "quota_security_group_rules", Labels: []string{"type", "tenant"}},
{Name: "quota_security_groups", Labels: []string{"type", "tenant"}},
{Name: "quota_injected_file_content_bytes", Labels: []string{"type", "tenant"}},
{Name: "quota_injected_file_path_bytes", Labels: []string{"type", "tenant"}},
{Name: "quota_injected_files", Labels: []string{"type", "tenant"}},
}
func NewNovaExporter(config *ExporterConfig, logger *slog.Logger) (*NovaExporter, error) {
exporter := NovaExporter{
BaseOpenStackExporter{
Name: "nova",
ExporterConfig: *config,
logger: logger,
},
}
for _, metric := range defaultNovaMetrics {
if metric.Name == "server_status" {
metric.Labels = append(defaultNovaServerStatusLabels, config.NovaMetadataMapping.Labels...)
}
if exporter.isDeprecatedMetric(&metric) {
continue
}
if !exporter.isSlowMetric(&metric) {
exporter.AddMetric(metric.Name, metric.Fn, metric.Labels, metric.DeprecatedVersion, nil)
}
}
envMicroversion, present := os.LookupEnv("OS_COMPUTE_API_VERSION")
if present {
exporter.Client.Microversion = envMicroversion
} else {
microversion, err := apiversions.Get(config.Client, "v2.1").Extract()
if err == nil {
exporter.Client.Microversion = microversion.Version
}
}
return &exporter, nil
}
func ListNovaAgentState(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
var allServices []services.Service
allPagesServices, err := services.List(exporter.Client, services.ListOpts{}).AllPages()
if err != nil {
return err
}
if allServices, err = services.ExtractServices(allPagesServices); err != nil {
return err
}
for _, service := range allServices {
var state = 0
if service.State == "up" {
state = 1
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["agent_state"].Metric,
prometheus.CounterValue, float64(state), service.ID, service.Host, service.Binary, service.Status, service.Zone, service.DisabledReason)
}
return nil
}
func ListHypervisors(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
var allHypervisors []hypervisors.Hypervisor
var allAggregates []aggregates.Aggregate
allPagesHypervisors, err := hypervisors.List(exporter.Client, nil).AllPages()
if err != nil {
return err
}
if allHypervisors, err = hypervisors.ExtractHypervisors(allPagesHypervisors); err != nil {
return err
}
allPagesAggregates, err := aggregates.List(exporter.Client).AllPages()
if err != nil {
return err
}
if allAggregates, err = aggregates.ExtractAggregates(allPagesAggregates); err != nil {
return err
}
hostToAzMap := map[string]string{} // map of hypervisors and in which AZ they are
hostToAggrMap := map[string][]string{} // map of hypervisors and of which aggregates they are part of
for _, a := range allAggregates {
isAzAggregate := isAzAggregate(a)
for _, h := range a.Hosts {
// Map the AZ of this aggregate to each host part of this aggregate
if a.AvailabilityZone != "" {
hostToAzMap[h] = a.AvailabilityZone
}
// Map the aggregate name to each host part of this aggregate
if !isAzAggregate {
hostToAggrMap[h] = append(hostToAggrMap[h], a.Name)
}
}
}
for _, hypervisor := range allHypervisors {
availabilityZone := ""
if val, ok := hostToAzMap[hypervisor.Service.Host]; ok {
availabilityZone = val
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["running_vms"].Metric,
prometheus.GaugeValue, float64(hypervisor.RunningVMs), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["current_workload"].Metric,
prometheus.GaugeValue, float64(hypervisor.CurrentWorkload), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
var vcpus int
if !reflect.ValueOf(hypervisor.CPUInfo).IsZero() {
vcpus = hypervisor.CPUInfo.Topology.Sockets * hypervisor.CPUInfo.Topology.Cores * hypervisor.CPUInfo.Topology.Threads
} else {
vcpus = hypervisor.VCPUs
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["vcpus_available"].Metric,
prometheus.GaugeValue, float64(vcpus), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["vcpus_used"].Metric,
prometheus.GaugeValue, float64(hypervisor.VCPUsUsed), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["memory_available_bytes"].Metric,
prometheus.GaugeValue, float64(hypervisor.MemoryMB*MEGABYTE), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["memory_used_bytes"].Metric,
prometheus.GaugeValue, float64(hypervisor.MemoryMBUsed*MEGABYTE), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["local_storage_available_bytes"].Metric,
prometheus.GaugeValue, float64(hypervisor.LocalGB*GIGABYTE), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["local_storage_used_bytes"].Metric,
prometheus.GaugeValue, float64(hypervisor.LocalGBUsed*GIGABYTE), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
ch <- prometheus.MustNewConstMetric(exporter.Metrics["free_disk_bytes"].Metric,
prometheus.GaugeValue, float64(hypervisor.FreeDiskGB*GIGABYTE), hypervisor.HypervisorHostname, availabilityZone, aggregatesLabel(hypervisor.Service.Host, hostToAggrMap))
}
return nil
}
func ListFlavors(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
var allFlavors []flavors.Flavor
allPagesFlavors, err := flavors.ListDetail(exporter.Client, flavors.ListOpts{AccessType: "None"}).AllPages()
if err != nil {
return err
}
allFlavors, err = flavors.ExtractFlavors(allPagesFlavors)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["flavors"].Metric,
prometheus.GaugeValue, float64(len(allFlavors)))
for _, f := range allFlavors {
ch <- prometheus.MustNewConstMetric(exporter.Metrics["flavor"].Metric,
prometheus.GaugeValue, 1, f.ID, f.Name, fmt.Sprintf("%v", f.VCPUs), fmt.Sprintf("%v", f.RAM), fmt.Sprintf("%v", f.Disk), fmt.Sprintf("%v", f.IsPublic))
}
return nil
}
func ListQuotas(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
var allProjects []projects.Project
var eo gophercloud.EndpointOpts
// We need a list of all tenants/projects. Therefore, within this nova exporter we need
// to create an openstack client for the Identity/Keystone API.
// If possible, use the EndpointOpts spefic to the identity service.
if v, ok := endpointOpts["identity"]; ok {
eo = v
} else if v, ok := endpointOpts["compute"]; ok {
eo = v
} else {
return errors.New("no EndpointOpts available to create Identity client")
}
c, err := openstack.NewIdentityV3(exporter.Client.ProviderClient, eo)
if err != nil {
return err
}
allPagesProject, err := projects.List(c, projects.ListOpts{DomainID: exporter.DomainID}).AllPages()
if err != nil {
return err
}
allProjects, err = projects.ExtractProjects(allPagesProject)
if err != nil {
return err
}
for _, p := range allProjects {
quotaSet, err := quotasets.GetDetail(exporter.Client, p.ID).Extract()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_cores"].Metric,
prometheus.GaugeValue, float64(quotaSet.Cores.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_cores"].Metric,
prometheus.GaugeValue, float64(quotaSet.Cores.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_cores"].Metric,
prometheus.GaugeValue, float64(quotaSet.Cores.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_instances"].Metric,
prometheus.GaugeValue, float64(quotaSet.Instances.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_instances"].Metric,
prometheus.GaugeValue, float64(quotaSet.Instances.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_instances"].Metric,
prometheus.GaugeValue, float64(quotaSet.Instances.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_key_pairs"].Metric,
prometheus.GaugeValue, float64(quotaSet.KeyPairs.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_key_pairs"].Metric,
prometheus.GaugeValue, float64(quotaSet.KeyPairs.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_key_pairs"].Metric,
prometheus.GaugeValue, float64(quotaSet.KeyPairs.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_metadata_items"].Metric,
prometheus.GaugeValue, float64(quotaSet.MetadataItems.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_metadata_items"].Metric,
prometheus.GaugeValue, float64(quotaSet.MetadataItems.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_metadata_items"].Metric,
prometheus.GaugeValue, float64(quotaSet.MetadataItems.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_ram"].Metric,
prometheus.GaugeValue, float64(quotaSet.RAM.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_ram"].Metric,
prometheus.GaugeValue, float64(quotaSet.RAM.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_ram"].Metric,
prometheus.GaugeValue, float64(quotaSet.RAM.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_server_groups"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroups.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_server_groups"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroups.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_server_groups"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroups.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_server_group_members"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroupMembers.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_server_group_members"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroupMembers.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_server_group_members"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroupMembers.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_fixed_ips"].Metric,
prometheus.GaugeValue, float64(quotaSet.FixedIPs.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_fixed_ips"].Metric,
prometheus.GaugeValue, float64(quotaSet.FixedIPs.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_fixed_ips"].Metric,
prometheus.GaugeValue, float64(quotaSet.FixedIPs.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_floating_ips"].Metric,
prometheus.GaugeValue, float64(quotaSet.FloatingIPs.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_floating_ips"].Metric,
prometheus.GaugeValue, float64(quotaSet.FloatingIPs.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_floating_ips"].Metric,
prometheus.GaugeValue, float64(quotaSet.FloatingIPs.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_security_group_rules"].Metric,
prometheus.GaugeValue, float64(quotaSet.SecurityGroupRules.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_security_group_rules"].Metric,
prometheus.GaugeValue, float64(quotaSet.SecurityGroupRules.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_security_group_rules"].Metric,
prometheus.GaugeValue, float64(quotaSet.SecurityGroupRules.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_security_groups"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroups.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_security_groups"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroups.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_security_groups"].Metric,
prometheus.GaugeValue, float64(quotaSet.ServerGroups.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_file_content_bytes"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFileContentBytes.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_file_content_bytes"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFileContentBytes.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_file_content_bytes"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFileContentBytes.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_file_path_bytes"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFilePathBytes.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_file_path_bytes"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFilePathBytes.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_file_path_bytes"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFilePathBytes.Limit), "limit", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_files"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFiles.InUse), "in_use", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_files"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFiles.Reserved), "reserved", p.Name)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["quota_injected_files"].Metric,
prometheus.GaugeValue, float64(quotaSet.InjectedFiles.Limit), "limit", p.Name)
}
return nil
}
func ListAZs(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
var allAZs []availabilityzones.AvailabilityZone
allPagesAZs, err := availabilityzones.List(exporter.Client).AllPages()
if err != nil {
return err
}
if allAZs, err = availabilityzones.ExtractAvailabilityZones(allPagesAZs); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["availability_zones"].Metric,
prometheus.GaugeValue, float64(len(allAZs)))
return nil
}
func ListComputeSecGroups(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
var allSecurityGroups []secgroups.SecurityGroup
allPagesSecurityGroups, err := secgroups.List(exporter.Client).AllPages()
if err != nil {
return err
}
if allSecurityGroups, err = secgroups.ExtractSecurityGroups(allPagesSecurityGroups); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["security_groups"].Metric,
prometheus.GaugeValue, float64(len(allSecurityGroups)))
return nil
}
func ListAllServers(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
type ServerWithExt struct {
servers.Server
availabilityzones.ServerAvailabilityZoneExt
extendedserverattributes.ServerAttributesExt
}
var allServers []ServerWithExt
var allFlavors []flavors.Flavor
var serverListOption servers.ListOpts
if exporter.TenantID == "" {
serverListOption = servers.ListOpts{AllTenants: true}
} else {
serverListOption = servers.ListOpts{TenantID: exporter.TenantID}
}
allPagesServers, err := servers.List(exporter.Client, serverListOption).AllPages()
if err != nil {
return err
}
err = servers.ExtractServersInto(allPagesServers, &allServers)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["total_vms"].Metric,
prometheus.GaugeValue, float64(len(allServers)))
apiMv, _ := strconv.ParseFloat(exporter.Client.Microversion, 64)
if apiMv >= 2.46 {
// https://docs.openstack.org/api-ref/compute/#list-servers-detailed
// ***
// If micro-version is greater than 2.46,
// we need to retrieve all flavors once again and search for flavor_id by name,
// as flavor_id are only available in server's detail data up to that version.
allPagesFlavors, err := flavors.ListDetail(exporter.Client, flavors.ListOpts{AccessType: "None"}).AllPages()
if err != nil {
return err
}
allFlavors, err = flavors.ExtractFlavors(allPagesFlavors)
if err != nil {
return err
}
}
// Server status metrics
if !exporter.MetricIsDisabled("server_status") {
for _, server := range allServers {
labelValues := func() []string {
if len(allFlavors) == 0 {
return []string{
server.ID, server.Status, server.Name, server.TenantID,
server.UserID, server.AccessIPv4, server.AccessIPv6, server.HostID, server.HypervisorHostname, server.ID,
server.AvailabilityZone, fmt.Sprintf("%v", server.Flavor["id"]), server.InstanceName,
}
}
return []string{
server.ID, server.Status, server.Name, server.TenantID,
server.UserID, server.AccessIPv4, server.AccessIPv6, server.HostID, server.HypervisorHostname, server.ID,
server.AvailabilityZone, searchFlavorIDbyName(server.Flavor["original_name"], allFlavors), server.InstanceName,
}
}()
metadataValues := exporter.NovaMetadataMapping.Extract(server.Metadata)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["server_status"].Metric,
prometheus.GaugeValue, float64(mapServerStatus(server.Status)), append(labelValues, metadataValues...)...)
}
}
return nil
}
func ListComputeLimits(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
var allProjects []projects.Project
var eo gophercloud.EndpointOpts
// We need a list of all tenants/projects. Therefore, within this nova exporter we need
// to create an openstack client for the Identity/Keystone API.
// If possible, use the EndpointOpts spefic to the identity service.
if v, ok := endpointOpts["identity"]; ok {
eo = v
} else if v, ok := endpointOpts["compute"]; ok {
eo = v
} else {
return errors.New("no EndpointOpts available to create Identity client")
}
c, err := openstack.NewIdentityV3(exporter.Client.ProviderClient, eo)
if err != nil {
return err
}
allPagesProject, err := projects.List(c, projects.ListOpts{DomainID: exporter.DomainID}).AllPages()
if err != nil {
return err
}
allProjects, err = projects.ExtractProjects(allPagesProject)
if err != nil {
return err
}
for _, p := range allProjects {
// Limits are obtained from the nova API, so now we can just use this exporter's client
limits, err := limits.Get(exporter.Client, limits.GetOpts{TenantID: p.ID}).Extract()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(exporter.Metrics["limits_vcpus_max"].Metric,
prometheus.GaugeValue, float64(limits.Absolute.MaxTotalCores), p.Name, p.ID)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["limits_vcpus_used"].Metric,
prometheus.GaugeValue, float64(limits.Absolute.TotalCoresUsed), p.Name, p.ID)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["limits_memory_max"].Metric,
prometheus.GaugeValue, float64(limits.Absolute.MaxTotalRAMSize), p.Name, p.ID)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["limits_memory_used"].Metric,
prometheus.GaugeValue, float64(limits.Absolute.TotalRAMUsed), p.Name, p.ID)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["limits_instances_used"].Metric,
prometheus.GaugeValue, float64(limits.Absolute.TotalInstancesUsed), p.Name, p.ID)
ch <- prometheus.MustNewConstMetric(exporter.Metrics["limits_instances_max"].Metric,
prometheus.GaugeValue, float64(limits.Absolute.MaxTotalInstances), p.Name, p.ID)
}
return nil
}
// ListUsage add metrics about usage
func ListUsage(exporter *BaseOpenStackExporter, ch chan<- prometheus.Metric) error {
allPagesUsage, err := usage.AllTenants(exporter.Client, usage.AllTenantsOpts{Detailed: true}).AllPages()
if err != nil {
return err
}
allTenantsUsage, err := usage.ExtractAllTenants(allPagesUsage)
if err != nil {
return err
}
// Server status metrics
for _, tenant := range allTenantsUsage {
for _, server := range tenant.ServerUsages {
ch <- prometheus.MustNewConstMetric(exporter.Metrics["server_local_gb"].Metric,
prometheus.GaugeValue, float64(server.LocalGB), server.Name, server.InstanceID, tenant.TenantID)
}
}
return nil
}
// Help function to determine if this aggregate has only the 'availability_zone' metadata
// attribute set. If so, the only purpose of the aggregate is to set the AZ for its member hosts.
func isAzAggregate(a aggregates.Aggregate) bool {
if len(a.Metadata) == 1 {
if _, ok := a.Metadata["availability_zone"]; ok {
return true
}
}
return false
}
func aggregatesLabel(h string, hostToAggrMap map[string][]string) string {
label := ""
if aggregates, ok := hostToAggrMap[h]; ok {
sort.Strings(aggregates)
for k, a := range aggregates {
if k == 0 {
label += a
} else {
label += "," + a
}
}
}
return label
}