diff --git a/go.mod b/go.mod index 4b54c19..a7dbc07 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/akquinet/oxidized-exporter -go 1.22 +go 1.23 require ( github.com/prometheus/client_golang v1.19.0 diff --git a/oxidized/client.go b/oxidized/client.go index 740879a..c432764 100644 --- a/oxidized/client.go +++ b/oxidized/client.go @@ -123,8 +123,15 @@ func (c *OxidizedClient) GetStatus() ([]DeviceStat, error) { return stats, nil } -func (c *OxidizedClient) GetConfigStats(group string, name string) (*ConfigStat, error) { - req, err := http.NewRequest("GET", c.Url+"/"+"node/fetch/"+group+"/"+name, nil) +func (c *OxidizedClient) GetConfigStats(group string, name string, onlyDefaulGroup bool) (*ConfigStat, error) { + var req *http.Request + var err error + + if onlyDefaulGroup { + req, err = http.NewRequest("GET", c.Url+"/"+"node/fetch/"+name, nil) + } else { + req, err = http.NewRequest("GET", c.Url+"/"+"node/fetch/"+group+"/"+name, nil) + } if err != nil { return nil, err } @@ -148,6 +155,22 @@ func (c *OxidizedClient) GetConfigStats(group string, name string) (*ConfigStat, }, nil } +// OnlyDefaultGroup returns true if the oxidized instance has only +// devices of the default group +func (o *OxidizedClient) OnlyDefaultGroup(devices []Device) bool { + groups := make(map[string]struct{}) + for _, device := range devices { + groups[device.Group] = struct{}{} + } + + if len(groups) == 1 { + if _, ok := groups["default"]; ok { + return true + } + } + return false +} + // ConvertOixidzedTimeTo8601 converts from 2019-11-19 14:00:00 CET // to UnixTimeStamp func ConvertOixidzedTimeToUnix(t string) (int64, error) { diff --git a/oxidized/collector.go b/oxidized/collector.go index 8ad6ddf..8ea8a50 100644 --- a/oxidized/collector.go +++ b/oxidized/collector.go @@ -81,6 +81,11 @@ func (c *OxidizedCollector) Collect(ch chan<- prometheus.Metric) { // oxidized reachable ch <- prometheus.MustNewConstMetric(c.oxidizedStatusMetric, prometheus.GaugeValue, 1) + onlyDefaultGroup := c.oxidizedClient.OnlyDefaultGroup(devices) + if onlyDefaultGroup { + slog.Info("Oxidized has only devices of group default") + } + semaphore := make(chan struct{}, 100) wg := sync.WaitGroup{} for _, device := range devices { @@ -150,7 +155,7 @@ func (c *OxidizedCollector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric(c.deviceLastBackupStatusMetric, prometheus.GaugeValue, deviceLastBackupStatus, device.FullName, device.Name, device.Group, device.Model) } - configStat, err := c.oxidizedClient.GetConfigStats(device.Group, device.Name) + configStat, err := c.oxidizedClient.GetConfigStats(device.Group, device.Name, onlyDefaultGroup) if err != nil { slog.Error("Could not get config stats", "error", err, "device", device.FullName) } else {