Initial commit
This commit is contained in:
135
metrics/pve_node_subscription_collector.go
Normal file
135
metrics/pve_node_subscription_collector.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"lostak.dev/pve-exporter/proxmox"
|
||||
)
|
||||
|
||||
// PVE subscription state collector.
|
||||
type PveSubscriptionCollector struct {
|
||||
apiClient *proxmox.PveApiClient // PVE API client instance.
|
||||
|
||||
info *prometheus.GaugeVec // Node subscription info prometheus gauge.
|
||||
status *prometheus.GaugeVec // Node subscription status prometheus gauge.
|
||||
nextDueDate *prometheus.GaugeVec // Node subscription next due date prometheus gauge.
|
||||
regDate *prometheus.GaugeVec // Node subscription registration date prometheus gauge.
|
||||
sockets *prometheus.GaugeVec // Node subscription sockets count prometheus gauge.
|
||||
}
|
||||
|
||||
// Create new instance of PVE cluster state collector.
|
||||
func NewPveSubscriptionCollector(apiClient *proxmox.PveApiClient) *PveSubscriptionCollector {
|
||||
c := PveSubscriptionCollector{apiClient: apiClient}
|
||||
|
||||
// Node subscription info.
|
||||
c.info = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_node_subscription_info",
|
||||
Help: "Node subscription info.",
|
||||
},
|
||||
[]string{"cluster", "node", "productname", "serverid"},
|
||||
)
|
||||
|
||||
// Node subscription status.
|
||||
c.status = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_node_subscription_status",
|
||||
Help: "Node subscription status.",
|
||||
},
|
||||
[]string{"cluster", "node"},
|
||||
)
|
||||
|
||||
// Node subscription registration date.
|
||||
c.regDate = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_node_subscription_regdate",
|
||||
Help: "Node subscription registration date.",
|
||||
},
|
||||
[]string{"cluster", "node"},
|
||||
)
|
||||
|
||||
// Node subscription next due date.
|
||||
c.nextDueDate = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_node_subscription_nextduedate",
|
||||
Help: "Node subscription next due date.",
|
||||
},
|
||||
[]string{"cluster", "node"},
|
||||
)
|
||||
|
||||
// Node subscription count of sockets.
|
||||
c.sockets = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "pve_node_subscription_sockets",
|
||||
Help: "Node subscription count of sockets.",
|
||||
},
|
||||
[]string{"cluster", "node"},
|
||||
)
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
// PveMetricsCollector interface implementation.
|
||||
func (c *PveSubscriptionCollector) CollectMetrics() error {
|
||||
cluster, err := c.apiClient.GetClusterStatus()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, node := range cluster.NodeStatuses {
|
||||
labels := prometheus.Labels{
|
||||
"cluster": cluster.GetClusterName(),
|
||||
"node": node.Name,
|
||||
}
|
||||
|
||||
// Node subscription.
|
||||
subscription, err := c.apiClient.GetNodeSubscription(node.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
// Subscription info.
|
||||
subsLabels := prometheus.Labels{
|
||||
"cluster": cluster.GetClusterName(),
|
||||
"node": node.Name,
|
||||
"productname": subscription.ProductName,
|
||||
"serverid": subscription.ServerID,
|
||||
}
|
||||
|
||||
c.info.With(subsLabels).Set(1)
|
||||
|
||||
// Subscription state.
|
||||
c.status.With(labels).Set(subscription.GetActiveNumeric())
|
||||
|
||||
// Subscription sockets count.
|
||||
c.sockets.With(labels).Set(float64(subscription.Sockets))
|
||||
|
||||
// Subscription registered date.
|
||||
if subscription.Registration != "" {
|
||||
registrationTime, err := time.Parse("2006-01-02 15:04:05", subscription.Registration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.regDate.With(labels).Set(float64(registrationTime.Unix()))
|
||||
}
|
||||
|
||||
// Subscription due date.
|
||||
if subscription.NextDueDate != "" {
|
||||
nextDueTime, err := time.Parse("2006-01-02", subscription.NextDueDate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.nextDueDate.With(labels).Set(float64(nextDueTime.Unix()))
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PveMetricsCollector interface implementation.
|
||||
func (c *PveSubscriptionCollector) GetName() string {
|
||||
return "Node Subscription"
|
||||
}
|
||||
Reference in New Issue
Block a user