Skip to content

Commit 512ff1b

Browse files
sihenselfrittentheke
authored andcommitted
Expose storage pool metrics
1 parent 4cb1bf3 commit 512ff1b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ libvirt_domain_vcpu_maximum | "domain" | Number of maximum online vCPUs
6767
libvirt_domain_vcpu_state | "domain", "vcpu" | State of the vCPU
6868
libvirt_domain_vcpu_time_seconds_total | "domain", "vcpu" | Time spent by the virtual CPU
6969
libvirt_domain_vcpu_wait_seconds_total | "domain", "vcpu" | Time the vCPU wants to run, but the host scheduler has something else running ahead of it
70+
libvirt_storage_pool_allocation_bytes | "storage_pool" | Current allocation bytes of the storage pool
71+
libvirt_storage_pool_available_bytes | "storage_pool" | Remaining free space of the storage pool in bytes
72+
libvirt_storage_pool_capacity_bytes | "storage_pool" | Size of the storage pool in logical bytes
73+
libvirt_storage_pool_state | "storage_pool" | State of the storage pool
7074

7175

7276
## Example
@@ -105,4 +109,8 @@ libvirt_domain_vcpu_maximum{domain="instance-00000131"} 2
105109
libvirt_domain_vcpu_state{domain="instance-00000131",vcpu="0"} 1
106110
libvirt_domain_vcpu_time_seconds_total{domain="instance-00000131",vcpu="0"} 2111850000000
107111
libvirt_domain_vcpu_wait_seconds_total{domain="instance-00000131",vcpu="0"} 4103560000000
112+
libvirt_storage_pool_allocation_bytes{storage_pool="testpool"} 5309386752
113+
libvirt_storage_pool_available_bytes{storage_pool="testpool"} 7264227328
114+
libvirt_storage_pool_capacity_bytes{storage_pool="testpool"} 12573614080
115+
libvirt_storage_pool_state{storage_pool="testpool"} 2
108116
```

pkg/exporter/prometheus-libvirt-exporter.go

+78
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,28 @@ var (
193193
[]string{"domain", "vcpu"},
194194
nil)
195195

196+
// storage pool stats
197+
libvirtStoragePoolState = prometheus.NewDesc(
198+
prometheus.BuildFQName(namespace, "storage_pool", "state"),
199+
"State of the storage pool.",
200+
[]string{"storage_pool"},
201+
nil)
202+
libvirtStoragePoolCapacity = prometheus.NewDesc(
203+
prometheus.BuildFQName(namespace, "storage_pool", "capacity_bytes"),
204+
"Size of the storage pool in logical bytes.",
205+
[]string{"storage_pool"},
206+
nil)
207+
libvirtStoragePoolAllocation = prometheus.NewDesc(
208+
prometheus.BuildFQName(namespace, "storage_pool", "allocation_bytes"),
209+
"Current allocation bytes of the storage pool.",
210+
[]string{"storage_pool"},
211+
nil)
212+
libvirtStoragePoolAvailable = prometheus.NewDesc(
213+
prometheus.BuildFQName(namespace, "storage_pool", "available_bytes"),
214+
"Remaining free space of the storage pool in bytes.",
215+
[]string{"storage_pool"},
216+
nil)
217+
196218
// info metrics
197219
libvirtDomainInfoDesc = prometheus.NewDesc(
198220
prometheus.BuildFQName(namespace, "domain", "info"),
@@ -347,6 +369,21 @@ func CollectFromLibvirt(ch chan<- prometheus.Metric, uri string, driver libvirt.
347369
return err
348370
}
349371
}
372+
373+
// collect storage pool metrics
374+
// see https://libvirt.org/html/libvirt-libvirt-storage.html
375+
var pools []libvirt.StoragePool
376+
if pools, _, err = l.ConnectListAllStoragePools(1, 0); err != nil {
377+
_ = level.Error(logger).Log("err", "failed to collect storage pools", "msg", err)
378+
return err
379+
}
380+
for _, pool := range pools {
381+
if err = CollectStoragePoolInfo(ch, l, pool, logger); err != nil {
382+
_ = level.Error(logger).Log("err", "failed to collect storage pool info", "msg", err)
383+
return err
384+
}
385+
}
386+
350387
return nil
351388
}
352389

@@ -652,6 +689,41 @@ func CollectDomainVCPUInfo(ch chan<- prometheus.Metric, l *libvirt.Libvirt, doma
652689
return
653690
}
654691

692+
func CollectStoragePoolInfo(ch chan<- prometheus.Metric, l *libvirt.Libvirt, pool libvirt.StoragePool, logger log.Logger) (err error) {
693+
// Report storage pool metrics
694+
var rState uint8
695+
var rCapacity, rAllocation, rAvailable uint64
696+
697+
promLabels := []string{
698+
pool.Name,
699+
}
700+
if rState, rCapacity, rAllocation, rAvailable, err = l.StoragePoolGetInfo(pool); err != nil {
701+
_ = level.Warn(logger).Log("warn", "failed to get StoragePoolInfo for pool", "pool", pool.Name, "msg", err)
702+
return err
703+
}
704+
ch <- prometheus.MustNewConstMetric(
705+
libvirtStoragePoolState,
706+
prometheus.GaugeValue,
707+
float64(rState),
708+
promLabels...)
709+
ch <- prometheus.MustNewConstMetric(
710+
libvirtStoragePoolCapacity,
711+
prometheus.GaugeValue,
712+
float64(rCapacity),
713+
promLabels...)
714+
ch <- prometheus.MustNewConstMetric(
715+
libvirtStoragePoolAllocation,
716+
prometheus.GaugeValue,
717+
float64(rAllocation),
718+
promLabels...)
719+
ch <- prometheus.MustNewConstMetric(
720+
libvirtStoragePoolAvailable,
721+
prometheus.GaugeValue,
722+
float64(rAvailable),
723+
promLabels...)
724+
return
725+
}
726+
655727
// Describe returns metadata for all Prometheus metrics that may be exported.
656728
func (e *LibvirtExporter) Describe(ch chan<- *prometheus.Desc) {
657729
ch <- libvirtUpDesc
@@ -700,4 +772,10 @@ func (e *LibvirtExporter) Describe(ch chan<- *prometheus.Desc) {
700772
ch <- libvirtDomainVCPUStatsTime
701773
ch <- libvirtDomainVCPUStatsWait
702774
ch <- libvirtDomainVCPUStatsDelay
775+
776+
//storage pool metrics
777+
ch <- libvirtStoragePoolState
778+
ch <- libvirtStoragePoolCapacity
779+
ch <- libvirtStoragePoolAllocation
780+
ch <- libvirtStoragePoolAvailable
703781
}

0 commit comments

Comments
 (0)