forked from Tinkoff/libvirt-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvirt_exporter.go
736 lines (702 loc) · 24.6 KB
/
libvirt_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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Copyright 2017 Kumina, https://kumina.nl/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Project forked from https://github.com/kumina/libvirt_exporter
// And then forked from https://github.com/rumanzo/libvirt_exporter_improved
package main
import (
"encoding/xml"
"github.com/libvirt/libvirt-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/AlexZzz/libvirt-exporter/libvirtSchema"
"gopkg.in/alecthomas/kingpin.v2"
"log"
"net/http"
"os"
)
var (
libvirtUpDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "", "up"),
"Whether scraping libvirt's metrics was successful.",
nil,
nil)
libvirtDomainInfoMetaDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_info", "meta"),
"Domain metadata",
[]string{"domain", "uuid", "instance_name", "flavor", "user_name", "user_uuid", "project_name", "project_uuid", "root_type", "root_uuid"},
nil)
libvirtDomainInfoMaxMemBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_info", "maximum_memory_bytes"),
"Maximum allowed memory of the domain, in bytes.",
[]string{"domain"},
nil)
libvirtDomainInfoMemoryUsageBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_info", "memory_usage_bytes"),
"Memory usage of the domain, in bytes.",
[]string{"domain"},
nil)
libvirtDomainInfoNrVirtCPUDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_info", "virtual_cpus"),
"Number of virtual CPUs for the domain.",
[]string{"domain"},
nil)
libvirtDomainInfoCPUTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_info", "cpu_time_seconds_total"),
"Amount of CPU time used by the domain, in seconds.",
[]string{"domain"},
nil)
libvirtDomainInfoVirDomainState = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_info", "vstate"),
"Virtual domain state. 0: no state, 1: the domain is running, 2: the domain is blocked on resource,"+
" 3: the domain is paused by user, 4: the domain is being shut down, 5: the domain is shut off,"+
"6: the domain is crashed, 7: the domain is suspended by guest power management",
[]string{"domain"},
nil)
libvirtDomainMetaBlockDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block", "meta"),
"Block device metadata info. Device name, source file, serial.",
[]string{"domain", "target_device", "source_file", "serial", "bus", "disk_type", "driver_type", "cache", "discard"},
nil)
libvirtDomainBlockRdBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "read_bytes_total"),
"Number of bytes read from a block device, in bytes.",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockRdReqDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "read_requests_total"),
"Number of read requests from a block device.",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockRdTotalTimeSecondsDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "read_time_seconds_total"),
"Total time spent on reads from a block device, in seconds.",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockWrBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "write_bytes_total"),
"Number of bytes written to a block device, in bytes.",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockWrReqDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "write_requests_total"),
"Number of write requests to a block device.",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockWrTotalTimesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "write_time_seconds_total"),
"Total time spent on writes on a block device, in seconds",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockFlushReqDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "flush_requests_total"),
"Total flush requests from a block device.",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockFlushTotalTimeSecondsDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "flush_time_seconds_total"),
"Total time in seconds spent on cache flushing to a block device",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockAllocationDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "allocation"),
"Offset of the highest written sector on a block device.",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockCapacityBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "capacity_bytes"),
"Logical size in bytes of the block device backing image.",
[]string{"domain", "target_device"},
nil)
libvirtDomainBlockPhysicalSizeBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "physicalsize_bytes"),
"Physical size in bytes of the container of the backing image.",
[]string{"domain", "target_device"},
nil)
libvirtDomainMetaInterfacesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface", "meta"),
"Interfaces metadata. Source bridge, target device, interface uuid",
[]string{"domain", "source_bridge", "target_device", "virtual_interface"},
nil)
libvirtDomainInterfaceRxBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_bytes_total"),
"Number of bytes received on a network interface, in bytes.",
[]string{"domain", "target_device"},
nil)
libvirtDomainInterfaceRxPacketsDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_packets_total"),
"Number of packets received on a network interface.",
[]string{"domain", "target_device"},
nil)
libvirtDomainInterfaceRxErrsDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_errors_total"),
"Number of packet receive errors on a network interface.",
[]string{"domain", "target_device"},
nil)
libvirtDomainInterfaceRxDropDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface_stats", "receive_drops_total"),
"Number of packet receive drops on a network interface.",
[]string{"domain", "target_device"},
nil)
libvirtDomainInterfaceTxBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_bytes_total"),
"Number of bytes transmitted on a network interface, in bytes.",
[]string{"domain", "target_device"},
nil)
libvirtDomainInterfaceTxPacketsDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_packets_total"),
"Number of packets transmitted on a network interface.",
[]string{"domain", "target_device"},
nil)
libvirtDomainInterfaceTxErrsDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_errors_total"),
"Number of packet transmit errors on a network interface.",
[]string{"domain", "target_device"},
nil)
libvirtDomainInterfaceTxDropDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_interface_stats", "transmit_drops_total"),
"Number of packet transmit drops on a network interface.",
[]string{"domain", "target_device"},
nil)
libvirtDomainMemoryStatMajorFaultTotalDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "major_fault_total"),
"Page faults occur when a process makes a valid access to virtual memory that is not available. "+
"When servicing the page fault, if disk IO is required, it is considered a major fault.",
[]string{"domain"},
nil)
libvirtDomainMemoryStatMinorFaultTotalDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "minor_fault_total"),
"Page faults occur when a process makes a valid access to virtual memory that is not available. "+
"When servicing the page not fault, if disk IO is required, it is considered a minor fault.",
[]string{"domain"},
nil)
libvirtDomainMemoryStatUnusedBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "unused_bytes"),
"The amount of memory left completely unused by the system. Memory that is available but used for "+
"reclaimable caches should NOT be reported as free. This value is expressed in bytes.",
[]string{"domain"},
nil)
libvirtDomainMemoryStatAvailableBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "available_bytes"),
"The total amount of usable memory as seen by the domain. This value may be less than the amount of "+
"memory assigned to the domain if a balloon driver is in use or if the guest OS does not initialize all "+
"assigned pages. This value is expressed in bytes.",
[]string{"domain"},
nil)
libvirtDomainMemoryStatActualBaloonBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "actual_balloon_bytes"),
"Current balloon value (in bytes).",
[]string{"domain"},
nil)
libvirtDomainMemoryStatRssBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "rss_bytes"),
"Resident Set Size of the process running the domain. This value is in bytes",
[]string{"domain"},
nil)
libvirtDomainMemoryStatUsableBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "usable_bytes"),
"How much the balloon can be inflated without pushing the guest system to swap, corresponds "+
"to 'Available' in /proc/meminfo",
[]string{"domain"},
nil)
libvirtDomainMemoryStatDiskCachesBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "disk_cache_bytes"),
"The amount of memory, that can be quickly reclaimed without additional I/O (in bytes)."+
"Typically these pages are used for caching files from disk.",
[]string{"domain"},
nil)
libvirtDomainMemoryStatUsedPercentDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_memory_stats", "used_percent"),
"The amount of memory in percent, that used by domain.",
[]string{"domain"},
nil)
)
// CollectDomain extracts Prometheus metrics from a libvirt domain.
func CollectDomain(ch chan<- prometheus.Metric, stat libvirt.DomainStats) error {
domainName, err := stat.Domain.GetName()
if err != nil {
return err
}
domainUUID, err := stat.Domain.GetUUIDString()
if err != nil {
return err
}
// Decode XML description of domain to get block device names, etc.
xmlDesc, err := stat.Domain.GetXMLDesc(0)
if err != nil {
return err
}
var desc libvirtSchema.Domain
err = xml.Unmarshal([]byte(xmlDesc), &desc)
if err != nil {
return err
}
// Report domain info.
info, err := stat.Domain.GetInfo()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
libvirtDomainInfoMetaDesc,
prometheus.GaugeValue,
float64(1),
domainName,
domainUUID,
desc.Metadata.NovaInstance.NovaName,
desc.Metadata.NovaInstance.NovaFlavor.FlavorName,
desc.Metadata.NovaInstance.NovaOwner.NovaUser.UserName,
desc.Metadata.NovaInstance.NovaOwner.NovaUser.UserUUID,
desc.Metadata.NovaInstance.NovaOwner.NovaProject.ProjectName,
desc.Metadata.NovaInstance.NovaOwner.NovaProject.ProjectUUID,
desc.Metadata.NovaInstance.NovaRoot.RootType,
desc.Metadata.NovaInstance.NovaRoot.RootUUID)
ch <- prometheus.MustNewConstMetric(
libvirtDomainInfoMaxMemBytesDesc,
prometheus.GaugeValue,
float64(info.MaxMem)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainInfoMemoryUsageBytesDesc,
prometheus.GaugeValue,
float64(info.Memory)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainInfoNrVirtCPUDesc,
prometheus.GaugeValue,
float64(info.NrVirtCpu),
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainInfoCPUTimeDesc,
prometheus.CounterValue,
float64(info.CpuTime)/1000/1000/1000, // From nsec to sec
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainInfoVirDomainState,
prometheus.GaugeValue,
float64(info.State),
domainName)
// Report block device statistics.
for _, disk := range stat.Block {
var DiskSource string
var Device *libvirtSchema.Disk
if disk.Name == "hdc" {
continue
}
/* "block.<num>.path" - string describing the source of block device <num>,
if it is a file or block device (omitted for network
sources and drives with no media inserted). For network device (i.e. rbd) take from xml. */
for _, dev := range desc.Devices.Disks {
if dev.Target.Device == disk.Name {
if disk.PathSet {
DiskSource = disk.Path
} else {
DiskSource = dev.Source.Name
}
Device = &dev
break
}
}
ch <- prometheus.MustNewConstMetric(
libvirtDomainMetaBlockDesc,
prometheus.GaugeValue,
float64(1),
domainName,
disk.Name,
DiskSource,
Device.Serial,
Device.Target.Bus,
Device.DiskType,
Device.Driver.Type,
Device.Driver.Cache,
Device.Driver.Discard,
)
// https://libvirt.org/html/libvirt-libvirt-domain.html#virConnectGetAllDomainStats
if disk.RdBytesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockRdBytesDesc,
prometheus.CounterValue,
float64(disk.RdBytes),
domainName,
disk.Name)
}
if disk.RdReqsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockRdReqDesc,
prometheus.CounterValue,
float64(disk.RdReqs),
domainName,
disk.Name)
}
if disk.RdTimesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockRdTotalTimeSecondsDesc,
prometheus.CounterValue,
float64(disk.RdTimes)/1e9,
domainName,
disk.Name)
}
if disk.WrBytesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockWrBytesDesc,
prometheus.CounterValue,
float64(disk.WrBytes),
domainName,
disk.Name)
}
if disk.WrReqsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockWrReqDesc,
prometheus.CounterValue,
float64(disk.WrReqs),
domainName,
disk.Name)
}
if disk.WrTimesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockWrTotalTimesDesc,
prometheus.CounterValue,
float64(disk.WrTimes)/1e9,
domainName,
disk.Name)
}
if disk.FlReqsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockFlushReqDesc,
prometheus.CounterValue,
float64(disk.FlReqs),
domainName,
disk.Name)
}
if disk.FlTimesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockFlushTotalTimeSecondsDesc,
prometheus.CounterValue,
float64(disk.FlTimes)/1e9,
domainName,
disk.Name)
}
if disk.AllocationSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockAllocationDesc,
prometheus.GaugeValue,
float64(disk.Allocation),
domainName,
disk.Name)
}
if disk.CapacitySet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockCapacityBytesDesc,
prometheus.GaugeValue,
float64(disk.Capacity),
domainName,
disk.Name)
}
if disk.PhysicalSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockPhysicalSizeBytesDesc,
prometheus.GaugeValue,
float64(disk.Physical),
domainName,
disk.Name)
}
}
// Report network interface statistics.
for _, iface := range stat.Net {
var SourceBridge string
var VirtualInterface string
// Additional info for ovs network
for _, net := range desc.Devices.Interfaces {
if net.Target.Device == iface.Name {
SourceBridge = net.Source.Bridge
VirtualInterface = net.Virtualport.Parameters.InterfaceID
break
}
}
ch <- prometheus.MustNewConstMetric(
libvirtDomainMetaInterfacesDesc,
prometheus.GaugeValue,
float64(1),
domainName,
SourceBridge,
iface.Name,
VirtualInterface)
if iface.RxBytesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceRxBytesDesc,
prometheus.CounterValue,
float64(iface.RxBytes),
domainName,
iface.Name)
}
if iface.RxPktsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceRxPacketsDesc,
prometheus.CounterValue,
float64(iface.RxPkts),
domainName,
iface.Name)
}
if iface.RxErrsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceRxErrsDesc,
prometheus.CounterValue,
float64(iface.RxErrs),
domainName,
iface.Name)
}
if iface.RxDropSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceRxDropDesc,
prometheus.CounterValue,
float64(iface.RxDrop),
domainName,
iface.Name)
}
if iface.TxBytesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceTxBytesDesc,
prometheus.CounterValue,
float64(iface.TxBytes),
domainName,
iface.Name)
}
if iface.TxPktsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceTxPacketsDesc,
prometheus.CounterValue,
float64(iface.TxPkts),
domainName,
iface.Name)
}
if iface.TxErrsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceTxErrsDesc,
prometheus.CounterValue,
float64(iface.TxErrs),
domainName,
iface.Name)
}
if iface.TxDropSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceTxDropDesc,
prometheus.CounterValue,
float64(iface.TxDrop),
domainName,
iface.Name)
}
}
// Collect Memory Stats
memorystat, err := stat.Domain.MemoryStats(11, 0)
var MemoryStats libvirtSchema.VirDomainMemoryStats
var usedPercent float64
if err == nil {
MemoryStats = memoryStatCollect(&memorystat)
if (MemoryStats.Usable != 0 && MemoryStats.Available != 0) {
usedPercent = (float64(MemoryStats.Available) - float64(MemoryStats.Usable)) / (float64(MemoryStats.Available)/float64(100))
}
}
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatMajorFaultTotalDesc,
prometheus.CounterValue,
float64(MemoryStats.MajorFault),
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatMinorFaultTotalDesc,
prometheus.CounterValue,
float64(MemoryStats.MinorFault),
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatUnusedBytesDesc,
prometheus.GaugeValue,
float64(MemoryStats.Unused)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatAvailableBytesDesc,
prometheus.GaugeValue,
float64(MemoryStats.Available)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatActualBaloonBytesDesc,
prometheus.GaugeValue,
float64(MemoryStats.ActualBalloon)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatRssBytesDesc,
prometheus.GaugeValue,
float64(MemoryStats.Rss)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatUsableBytesDesc,
prometheus.GaugeValue,
float64(MemoryStats.Usable)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatDiskCachesBytesDesc,
prometheus.GaugeValue,
float64(MemoryStats.DiskCaches)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainMemoryStatUsedPercentDesc,
prometheus.GaugeValue,
float64(usedPercent),
domainName)
return nil
}
// CollectFromLibvirt obtains Prometheus metrics from all domains in a
// libvirt setup.
func CollectFromLibvirt(ch chan<- prometheus.Metric, uri string) error {
conn, err := libvirt.NewConnectReadOnly(uri)
if err != nil {
return err
}
defer conn.Close()
stats, err := conn.GetAllDomainStats([]*libvirt.Domain{}, libvirt.DOMAIN_STATS_STATE|libvirt.DOMAIN_STATS_CPU_TOTAL|
libvirt.DOMAIN_STATS_INTERFACE|libvirt.DOMAIN_STATS_BALLOON|libvirt.DOMAIN_STATS_BLOCK|
libvirt.DOMAIN_STATS_PERF|libvirt.DOMAIN_STATS_VCPU,
//libvirt.CONNECT_GET_ALL_DOMAINS_STATS_NOWAIT, // maybe in future
libvirt.CONNECT_GET_ALL_DOMAINS_STATS_RUNNING|libvirt.CONNECT_GET_ALL_DOMAINS_STATS_SHUTOFF)
defer func(stats []libvirt.DomainStats) {
for _, stat := range stats {
stat.Domain.Free()
}
}(stats)
if err != nil {
return err
}
for _, stat := range stats {
err = CollectDomain(ch, stat)
if err != nil {
log.Printf("Failed to scrape metrics: %s", err)
}
}
return nil
}
func memoryStatCollect(memorystat *[]libvirt.DomainMemoryStat) libvirtSchema.VirDomainMemoryStats {
var MemoryStats libvirtSchema.VirDomainMemoryStats
for _, domainmemorystat := range *memorystat {
switch tag := domainmemorystat.Tag; tag {
case 2:
MemoryStats.MajorFault = domainmemorystat.Val
case 3:
MemoryStats.MinorFault = domainmemorystat.Val
case 4:
MemoryStats.Unused = domainmemorystat.Val
case 5:
MemoryStats.Available = domainmemorystat.Val
case 6:
MemoryStats.ActualBalloon = domainmemorystat.Val
case 7:
MemoryStats.Rss = domainmemorystat.Val
case 8:
MemoryStats.Usable = domainmemorystat.Val
case 10:
MemoryStats.DiskCaches = domainmemorystat.Val
}
}
return MemoryStats
}
// LibvirtExporter implements a Prometheus exporter for libvirt state.
type LibvirtExporter struct {
uri string
}
// NewLibvirtExporter creates a new Prometheus exporter for libvirt.
func NewLibvirtExporter(uri string) (*LibvirtExporter, error) {
return &LibvirtExporter{
uri: uri,
}, nil
}
// Describe returns metadata for all Prometheus metrics that may be exported.
func (e *LibvirtExporter) Describe(ch chan<- *prometheus.Desc) {
// Status
ch <- libvirtUpDesc
// Domain info
ch <- libvirtDomainInfoMetaDesc
ch <- libvirtDomainInfoMaxMemBytesDesc
ch <- libvirtDomainInfoMemoryUsageBytesDesc
ch <- libvirtDomainInfoNrVirtCPUDesc
ch <- libvirtDomainInfoCPUTimeDesc
ch <- libvirtDomainInfoVirDomainState
// Domain block stats
ch <- libvirtDomainMetaBlockDesc
ch <- libvirtDomainBlockRdBytesDesc
ch <- libvirtDomainBlockRdReqDesc
ch <- libvirtDomainBlockRdTotalTimeSecondsDesc
ch <- libvirtDomainBlockWrBytesDesc
ch <- libvirtDomainBlockWrReqDesc
ch <- libvirtDomainBlockWrTotalTimesDesc
ch <- libvirtDomainBlockFlushReqDesc
ch <- libvirtDomainBlockFlushTotalTimeSecondsDesc
ch <- libvirtDomainBlockAllocationDesc
ch <- libvirtDomainBlockCapacityBytesDesc
ch <- libvirtDomainBlockPhysicalSizeBytesDesc
// Domain net interfaces stats
ch <- libvirtDomainMetaInterfacesDesc
ch <- libvirtDomainInterfaceRxBytesDesc
ch <- libvirtDomainInterfaceRxPacketsDesc
ch <- libvirtDomainInterfaceRxErrsDesc
ch <- libvirtDomainInterfaceRxDropDesc
ch <- libvirtDomainInterfaceTxBytesDesc
ch <- libvirtDomainInterfaceTxPacketsDesc
ch <- libvirtDomainInterfaceTxErrsDesc
ch <- libvirtDomainInterfaceTxDropDesc
// Domain memory stats
ch <- libvirtDomainMemoryStatMajorFaultTotalDesc
ch <- libvirtDomainMemoryStatMinorFaultTotalDesc
ch <- libvirtDomainMemoryStatUnusedBytesDesc
ch <- libvirtDomainMemoryStatAvailableBytesDesc
ch <- libvirtDomainMemoryStatActualBaloonBytesDesc
ch <- libvirtDomainMemoryStatRssBytesDesc
ch <- libvirtDomainMemoryStatUsableBytesDesc
ch <- libvirtDomainMemoryStatDiskCachesBytesDesc
}
// Collect scrapes Prometheus metrics from libvirt.
func (e *LibvirtExporter) Collect(ch chan<- prometheus.Metric) {
err := CollectFromLibvirt(ch, e.uri)
if err == nil {
ch <- prometheus.MustNewConstMetric(
libvirtUpDesc,
prometheus.GaugeValue,
1.0)
} else {
log.Printf("Failed to scrape metrics: %s", err)
ch <- prometheus.MustNewConstMetric(
libvirtUpDesc,
prometheus.GaugeValue,
0.0)
}
}
func main() {
var (
app = kingpin.New("libvirt_exporter", "Prometheus metrics exporter for libvirt")
listenAddress = app.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9177").String()
metricsPath = app.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
libvirtURI = app.Flag("libvirt.uri", "Libvirt URI from which to extract metrics.").Default("qemu:///system").String()
)
kingpin.MustParse(app.Parse(os.Args[1:]))
exporter, err := NewLibvirtExporter(*libvirtURI)
if err != nil {
panic(err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Libvirt Exporter</title></head>
<body>
<h1>Libvirt Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}