68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package metrics
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"lostak.dev/pve-exporter/proxmox"
|
|
)
|
|
|
|
// PVE cluster state collector.
|
|
type PveClusterStateCollector struct {
|
|
apiClient *proxmox.PveApiClient // PVE API client instance.
|
|
registry *TTLRegistry // TTL metrics registry.
|
|
|
|
nodes *TTLGaugeVec // Count of nodes prometheus gauge.
|
|
quorate *TTLGaugeVec // Cluster quorum state prometheus gauge.
|
|
}
|
|
|
|
// Create new instance of PVE cluster state collector.
|
|
func NewPveClusterStateCollector(apiClient *proxmox.PveApiClient, registry *TTLRegistry) *PveClusterStateCollector {
|
|
c := PveClusterStateCollector{apiClient: apiClient}
|
|
c.registry = registry
|
|
|
|
// Cluster meta gauge.
|
|
c.nodes = NewTTLGaugeVec(
|
|
prometheus.GaugeOpts{
|
|
Name: "pve_cluster_nodes",
|
|
Help: "Cluster nodes count.",
|
|
},
|
|
[]string{"cluster"},
|
|
1*time.Minute,
|
|
)
|
|
c.registry.Register(c.nodes)
|
|
|
|
// Cluster quorate gauge.
|
|
c.quorate = NewTTLGaugeVec(
|
|
prometheus.GaugeOpts{
|
|
Name: "pve_cluster_quorate",
|
|
Help: "Cluster quorum state.",
|
|
},
|
|
[]string{"cluster"},
|
|
1*time.Minute,
|
|
)
|
|
c.registry.Register(c.quorate)
|
|
c.registry.StartCleanupLoop(5 * time.Second)
|
|
|
|
return &c
|
|
}
|
|
|
|
// PveMetricsCollector interface implementation.
|
|
func (c *PveClusterStateCollector) CollectMetrics() error {
|
|
cluster, err := c.apiClient.GetClusterStatus()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
l := prometheus.Labels{"cluster": cluster.Name}
|
|
c.nodes.With(l).Set(float64(cluster.Nodes))
|
|
c.quorate.With(l).Set(float64(cluster.Quorate))
|
|
|
|
return nil
|
|
}
|
|
|
|
// PveMetricsCollector interface implementation.
|
|
func (c *PveClusterStateCollector) GetName() string {
|
|
return "Cluster State"
|
|
}
|