60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package metrics
|
|
|
|
import (
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
"lostak.dev/pve-exporter/proxmox"
|
|
)
|
|
|
|
// PVE cluster state collector.
|
|
type PveClusterStateCollector struct {
|
|
apiClient *proxmox.PveApiClient // PVE API client instance.
|
|
|
|
nodes *prometheus.GaugeVec // Count of nodes prometheus gauge.
|
|
quorate *prometheus.GaugeVec // Cluster quorum state prometheus gauge.
|
|
}
|
|
|
|
// Create new instance of PVE cluster state collector.
|
|
func NewPveClusterStateCollector(apiClient *proxmox.PveApiClient) *PveClusterStateCollector {
|
|
c := PveClusterStateCollector{apiClient: apiClient}
|
|
|
|
// Cluster meta gauge.
|
|
c.nodes = promauto.NewGaugeVec(
|
|
prometheus.GaugeOpts{
|
|
Name: "pve_cluster_nodes",
|
|
Help: "Cluster nodes count.",
|
|
},
|
|
[]string{"cluster"},
|
|
)
|
|
|
|
// Cluster quorate gauge.
|
|
c.quorate = promauto.NewGaugeVec(
|
|
prometheus.GaugeOpts{
|
|
Name: "pve_cluster_quorate",
|
|
Help: "Cluster quorum state.",
|
|
},
|
|
[]string{"cluster"},
|
|
)
|
|
|
|
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"
|
|
}
|