forked from kumina/libvirt_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibvirt_exporter.go
551 lines (499 loc) · 16.5 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
// 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.
package main
import (
"encoding/xml"
"log"
"net/http"
"os"
"github.com/libvirt/libvirt-go"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/kumina/libvirt_exporter/libvirt_schema"
)
//111111111111111111111111111111111111111111111111111111
var (
//check is libvirt ok
libvirtUpDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "", "up"),
"Whether scraping libvirt's metrics was successful.",
nil,
nil)
libvirtDomainInfoMaxMemDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_info", "maximum_memory_bytes"),
"Maximum allowed memory of the domain, in bytes.",
[]string{"domain"},
nil)
libvirtDomainInfoMemoryDesc = 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)
// domain block r/w
libvirtDomainBlockRdBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "read_bytes_total"),
"Number of bytes read from a block device, in bytes.",
[]string{"domain", "source_file", "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", "source_file", "target_device"},
nil)
libvirtDomainBlockRdTotalTimesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "read_seconds_total"),
"Amount of time spent reading from a block device, in seconds.",
[]string{"domain", "source_file", "target_device"},
nil)
libvirtDomainBlockWrBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "write_bytes_total"),
"Number of bytes written from a block device, in bytes.",
[]string{"domain", "source_file", "target_device"},
nil)
libvirtDomainBlockWrReqDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "write_requests_total"),
"Number of write requests from a block device.",
[]string{"domain", "source_file", "target_device"},
nil)
libvirtDomainBlockWrTotalTimesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "write_seconds_total"),
"Amount of time spent writing from a block device, in seconds.",
[]string{"domain", "source_file", "target_device"},
nil)
libvirtDomainBlockFlushReqDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "flush_requests_total"),
"Number of flush requests from a block device.",
[]string{"domain", "source_file", "target_device"},
nil)
libvirtDomainBlockFlushTotalTimesDesc = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_stats", "flush_seconds_total"),
"Amount of time spent flushing of a block device, in seconds.",
[]string{"domain", "source_file", "target_device"},
nil)
// BlockCapacity
libvirtDomainBlockCapacity = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_info", "capacity"),
"how much storage the guest will see, in bytes.",
[]string{"domain", "source_file", "target_device"},
nil)
//BlockAllocation
libvirtDomainBlockAllocation = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_info", "physical"),
"host storage in bytes occupied by the image, in bytes.",
[]string{"domain", "source_file", "target_device"},
nil)
//BlockPhysical
libvirtDomainBlockPhysical = prometheus.NewDesc(
prometheus.BuildFQName("libvirt", "domain_block_info", "allocation"),
"host physical size in bytes of the image container, in bytes.",
[]string{"domain", "source_file", "target_device"},
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", "source_bridge", "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", "source_bridge", "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", "source_bridge", "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", "source_bridge", "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", "source_bridge", "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", "source_bridge", "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", "source_bridge", "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", "source_bridge", "target_device"},
nil)
)
//22222222222222222222222222222222222222222222222222
// CollectDomain extracts Prometheus metrics from a libvirt domain.
func CollectDomain(ch chan<- prometheus.Metric, domain *libvirt.Domain) error {
domainName, err := domain.GetName()
if err != nil {
return err
}
// Decode XML description of domain to get block device names, etc.
xmlDesc, err := domain.GetXMLDesc(0)
if err != nil {
return err
}
var desc libvirt_schema.Domain
//Resolve the xml to libvirt_schema.Domain struct
err = xml.Unmarshal([]byte(xmlDesc), &desc)
if err != nil {
return err
}
// Report domain info.
info, err := domain.GetInfo()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
libvirtDomainInfoMaxMemDesc,
prometheus.GaugeValue,
float64(info.MaxMem)*1024,
domainName)
ch <- prometheus.MustNewConstMetric(
libvirtDomainInfoMemoryDesc,
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)/1e9,
domainName)
// Report block device statistics.
for _, disk := range desc.Devices.Disks {
if disk.Device == "cdrom" || disk.Device == "fd" {
continue
}
//Report domain block info
//flag 0 https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainGetBlockInfo
//extra flags; not used yet, so callers should always pass 0
BlockInfo, err := domain.GetBlockInfo(disk.Target.Device, 0)
if err != nil {
return err
}
//BlockInfo.Capacity
if BlockInfo.Capacity != 0 {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockCapacity,
prometheus.CounterValue,
float64(BlockInfo.Capacity),
domainName,
disk.Source.File,
disk.Target.Device)
}
//BlockInfo.Capacity
if BlockInfo.Allocation != 0 {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockAllocation,
prometheus.CounterValue,
float64(BlockInfo.Allocation),
domainName,
disk.Source.File,
disk.Target.Device)
}
//BlockInfo.Physical
if BlockInfo.Physical != 0 {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockPhysical,
prometheus.CounterValue,
float64(BlockInfo.Physical),
domainName,
disk.Source.File,
disk.Target.Device)
}
blockStats, err := domain.BlockStats(disk.Target.Device)
if err != nil {
return err
}
if blockStats.RdBytesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockRdBytesDesc,
prometheus.CounterValue,
float64(blockStats.RdBytes),
domainName,
disk.Source.File,
disk.Target.Device)
}
if blockStats.RdReqSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockRdReqDesc,
prometheus.CounterValue,
float64(blockStats.RdReq),
domainName,
disk.Source.File,
disk.Target.Device)
}
if blockStats.RdTotalTimesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockRdTotalTimesDesc,
prometheus.CounterValue,
float64(blockStats.RdTotalTimes)/1e9,
domainName,
disk.Source.File,
disk.Target.Device)
}
if blockStats.WrBytesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockWrBytesDesc,
prometheus.CounterValue,
float64(blockStats.WrBytes),
domainName,
disk.Source.File,
disk.Target.Device)
}
if blockStats.WrReqSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockWrReqDesc,
prometheus.CounterValue,
float64(blockStats.WrReq),
domainName,
disk.Source.File,
disk.Target.Device)
}
if blockStats.WrTotalTimesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockWrTotalTimesDesc,
prometheus.CounterValue,
float64(blockStats.WrTotalTimes)/1e9,
domainName,
disk.Source.File,
disk.Target.Device)
}
if blockStats.FlushReqSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockFlushReqDesc,
prometheus.CounterValue,
float64(blockStats.FlushReq),
domainName,
disk.Source.File,
disk.Target.Device)
}
if blockStats.FlushTotalTimesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainBlockFlushTotalTimesDesc,
prometheus.CounterValue,
float64(blockStats.FlushTotalTimes)/1e9,
domainName,
disk.Source.File,
disk.Target.Device)
}
// Skip "Errs", as the documentation does not clearly
// explain what this means.
}
// Report network interface statistics.
for _, iface := range desc.Devices.Interfaces {
if iface.Target.Device == "" {
continue
}
interfaceStats, err := domain.InterfaceStats(iface.Target.Device)
if err != nil {
return err
}
if interfaceStats.RxBytesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceRxBytesDesc,
prometheus.CounterValue,
float64(interfaceStats.RxBytes),
domainName,
iface.Source.Bridge,
iface.Target.Device)
}
if interfaceStats.RxPacketsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceRxPacketsDesc,
prometheus.CounterValue,
float64(interfaceStats.RxPackets),
domainName,
iface.Source.Bridge,
iface.Target.Device)
}
if interfaceStats.RxErrsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceRxErrsDesc,
prometheus.CounterValue,
float64(interfaceStats.RxErrs),
domainName,
iface.Source.Bridge,
iface.Target.Device)
}
if interfaceStats.RxDropSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceRxDropDesc,
prometheus.CounterValue,
float64(interfaceStats.RxDrop),
domainName,
iface.Source.Bridge,
iface.Target.Device)
}
if interfaceStats.TxBytesSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceTxBytesDesc,
prometheus.CounterValue,
float64(interfaceStats.TxBytes),
domainName,
iface.Source.Bridge,
iface.Target.Device)
}
if interfaceStats.TxPacketsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceTxPacketsDesc,
prometheus.CounterValue,
float64(interfaceStats.TxPackets),
domainName,
iface.Source.Bridge,
iface.Target.Device)
}
if interfaceStats.TxErrsSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceTxErrsDesc,
prometheus.CounterValue,
float64(interfaceStats.TxErrs),
domainName,
iface.Source.Bridge,
iface.Target.Device)
}
if interfaceStats.TxDropSet {
ch <- prometheus.MustNewConstMetric(
libvirtDomainInterfaceTxDropDesc,
prometheus.CounterValue,
float64(interfaceStats.TxDrop),
domainName,
iface.Source.Bridge,
iface.Target.Device)
}
}
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.NewConnect(uri)
if err != nil {
return err
}
defer conn.Close()
// Use ListDomains() as opposed to using ListAllDomains(), as
// the latter is unsupported when talking to a system using
// libvirt 0.9.12 or older.
domainIds, err := conn.ListDomains()
if err != nil {
return err
}
for _, id := range domainIds {
domain, err := conn.LookupDomainById(id)
if err == nil {
err = CollectDomain(ch, domain)
domain.Free()
if err != nil {
return err
}
}
}
return nil
}
// 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
}
//3333333333333333333333333333333333333333333333333
// Describe returns metadata for all Prometheus metrics that may be exported.
func (e *LibvirtExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- libvirtUpDesc
ch <- libvirtDomainInfoMaxMemDesc
ch <- libvirtDomainInfoMemoryDesc
ch <- libvirtDomainInfoNrVirtCpuDesc
ch <- libvirtDomainInfoCpuTimeDesc
//disk info
ch <- libvirtDomainBlockCapacity
ch <- libvirtDomainBlockAllocation
ch <- libvirtDomainBlockPhysical
ch <- libvirtDomainBlockRdBytesDesc
ch <- libvirtDomainBlockRdReqDesc
ch <- libvirtDomainBlockRdTotalTimesDesc
ch <- libvirtDomainBlockWrBytesDesc
ch <- libvirtDomainBlockWrReqDesc
ch <- libvirtDomainBlockWrTotalTimesDesc
ch <- libvirtDomainBlockFlushReqDesc
ch <- libvirtDomainBlockFlushTotalTimesDesc
}
// 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, prometheus.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))
}