Initial commit
This commit is contained in:
104
metrics/pve_node_storage_collector.go
Normal file
104
metrics/pve_node_storage_collector.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"lostak.dev/pve-exporter/proxmox"
|
||||
)
|
||||
|
||||
// PVE Storage state collector.
|
||||
type PveStorageCollector struct {
|
||||
apiClient *proxmox.PveApiClient // PVE API client instance.
|
||||
|
||||
state *prometheus.GaugeVec // Storage state prometheus gauge.
|
||||
total *prometheus.GaugeVec // Storage total bytes prometheus gauge.
|
||||
avail *prometheus.GaugeVec // Storage available bytes prometheus gauge.
|
||||
used *prometheus.GaugeVec // Storage used bytes prometheus gauge.
|
||||
}
|
||||
|
||||
// Create new instance of PVE SDN collector.
|
||||
func NewPveStorageCollector(apiClient *proxmox.PveApiClient) *PveStorageCollector {
|
||||
c := PveStorageCollector{apiClient: apiClient}
|
||||
|
||||
// Storage state.
|
||||
c.state = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_storage_up",
|
||||
Help: "Node storage UP state.",
|
||||
},
|
||||
[]string{"cluster", "node", "storage", "type", "content", "shared"},
|
||||
)
|
||||
|
||||
// Storage total bytes.
|
||||
c.total = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_storage_total_bytes",
|
||||
Help: "Node storage total capacity in bytes.",
|
||||
},
|
||||
[]string{"cluster", "node", "storage", "type", "content", "shared"},
|
||||
)
|
||||
|
||||
// Storage available bytes.
|
||||
c.avail = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_storage_avail_bytes",
|
||||
Help: "Node storage available capacity in bytes.",
|
||||
},
|
||||
[]string{"cluster", "node", "storage", "type", "content", "shared"},
|
||||
)
|
||||
|
||||
// Storage used bytes.
|
||||
c.used = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_storage_used_bytes",
|
||||
Help: "Node storage used capacity in bytes.",
|
||||
},
|
||||
[]string{"cluster", "node", "storage", "type", "content", "shared"},
|
||||
)
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
// PveMetricsCollector interface implementation.
|
||||
func (c *PveStorageCollector) CollectMetrics() error {
|
||||
cluster, err := c.apiClient.GetClusterStatus()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, node := range cluster.NodeStatuses {
|
||||
storages, err := c.apiClient.GetNodeStorages(node.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, storage := range *storages {
|
||||
// Skip disabled storages.
|
||||
if storage.Enabled == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
labels := prometheus.Labels{
|
||||
"cluster": cluster.GetClusterName(),
|
||||
"node": node.Name,
|
||||
"storage": storage.Storage,
|
||||
"type": storage.Type,
|
||||
"content": storage.Content,
|
||||
"shared": strconv.Itoa(storage.Shared),
|
||||
}
|
||||
|
||||
c.state.With(labels).Set(float64(storage.Active))
|
||||
c.total.With(labels).Set(float64(storage.Total))
|
||||
c.avail.With(labels).Set(float64(storage.Avail))
|
||||
c.used.With(labels).Set(float64(storage.Used))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PveMetricsCollector interface implementation.
|
||||
func (c *PveStorageCollector) GetName() string {
|
||||
return "Storage"
|
||||
}
|
||||
Reference in New Issue
Block a user