Skip to content

Commit

Permalink
Merge pull request #2 from akquinet/fix/only-default-group
Browse files Browse the repository at this point in the history
fix: #1 - use differend endpoint if only default group
  • Loading branch information
rwxd authored Aug 27, 2024
2 parents b8f80bf + 930e1d6 commit 506c414
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 25 additions & 2 deletions oxidized/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion oxidized/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 506c414

Please sign in to comment.