Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package collector
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"strconv"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
func init() {
|
|
registerCollector("backup", func(logger *slog.Logger) Collector {
|
|
return newBackupCollector(logger)
|
|
})
|
|
}
|
|
|
|
type backupCollector struct {
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func newBackupCollector(logger *slog.Logger) *backupCollector {
|
|
return &backupCollector{logger: logger}
|
|
}
|
|
|
|
type backupNotBackedUpResponse struct {
|
|
Data []backupNotBackedUpEntry `json:"data"`
|
|
}
|
|
|
|
type backupNotBackedUpEntry struct {
|
|
VMID int `json:"vmid"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
}
|
|
|
|
var (
|
|
notBackedUpTotalDesc = prometheus.NewDesc(
|
|
prometheus.BuildFQName(namespace, "", "not_backed_up_total"),
|
|
"Whether a guest is not backed up (1 = not backed up).",
|
|
[]string{"id"},
|
|
nil,
|
|
)
|
|
notBackedUpInfoDesc = prometheus.NewDesc(
|
|
prometheus.BuildFQName(namespace, "", "not_backed_up_info"),
|
|
"Information about a guest that is not backed up.",
|
|
[]string{"id"},
|
|
nil,
|
|
)
|
|
)
|
|
|
|
func (c *backupCollector) Update(client *Client, ch chan<- prometheus.Metric) error {
|
|
body, err := client.Get("/cluster/backup-info/not-backed-up")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get /cluster/backup-info/not-backed-up: %w", err)
|
|
}
|
|
|
|
var resp backupNotBackedUpResponse
|
|
if err := json.Unmarshal(body, &resp); err != nil {
|
|
return fmt.Errorf("failed to parse backup-info response: %w", err)
|
|
}
|
|
|
|
for _, entry := range resp.Data {
|
|
id := entry.Type + "/" + strconv.Itoa(entry.VMID)
|
|
ch <- prometheus.MustNewConstMetric(notBackedUpTotalDesc, prometheus.GaugeValue, 1, id)
|
|
ch <- prometheus.MustNewConstMetric(notBackedUpInfoDesc, prometheus.GaugeValue, 1, id)
|
|
}
|
|
|
|
return nil
|
|
}
|