From 2b5bf4d6a586288b65142ed0ed5a62ddb070d0da Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Fri, 6 Feb 2026 13:49:39 +0100 Subject: [PATCH] Replaced stringArrayContains by slices.Contains --- collectors/beegfsmetaMetric.go | 4 ++-- collectors/beegfsstorageMetric.go | 4 ++-- collectors/customCmdMetric.go | 7 +++---- collectors/gpfsMetric.go | 13 +++++++------ collectors/iostatMetric.go | 15 +++++++++------ collectors/loadavgMetric.go | 5 +++-- collectors/lustreMetric.go | 7 ++++--- collectors/memstatMetric.go | 6 +++--- collectors/metricCollector.go | 12 ------------ collectors/netstatMetric.go | 3 ++- collectors/nfsMetric.go | 7 ++++--- collectors/nfsiostatMetric.go | 5 +++-- collectors/nvidiaMetric.go | 5 +++-- 13 files changed, 45 insertions(+), 48 deletions(-) diff --git a/collectors/beegfsmetaMetric.go b/collectors/beegfsmetaMetric.go index 82130b4..3c1c544 100644 --- a/collectors/beegfsmetaMetric.go +++ b/collectors/beegfsmetaMetric.go @@ -17,6 +17,7 @@ import ( "os/exec" "os/user" "regexp" + "slices" "strconv" "strings" "time" @@ -80,8 +81,7 @@ func (m *BeegfsMetaCollector) Init(config json.RawMessage) error { //create map with possible variables m.matches = make(map[string]string) for _, value := range nodeMdstat_array { - _, skip := stringArrayContains(m.config.ExcludeMetrics, value) - if skip { + if slices.Contains(m.config.ExcludeMetrics, value) { m.matches["other"] = "0" } else { m.matches["beegfs_cmeta_"+value] = "0" diff --git a/collectors/beegfsstorageMetric.go b/collectors/beegfsstorageMetric.go index e4cb0c7..0c80387 100644 --- a/collectors/beegfsstorageMetric.go +++ b/collectors/beegfsstorageMetric.go @@ -17,6 +17,7 @@ import ( "os/exec" "os/user" "regexp" + "slices" "strconv" "strings" "time" @@ -73,8 +74,7 @@ func (m *BeegfsStorageCollector) Init(config json.RawMessage) error { //create map with possible variables m.matches = make(map[string]string) for _, value := range storageStat_array { - _, skip := stringArrayContains(m.config.ExcludeMetrics, value) - if skip { + if slices.Contains(m.config.ExcludeMetrics, value) { m.matches["other"] = "0" } else { m.matches["beegfs_cstorage_"+value] = "0" diff --git a/collectors/customCmdMetric.go b/collectors/customCmdMetric.go index 6df2052..52917c5 100644 --- a/collectors/customCmdMetric.go +++ b/collectors/customCmdMetric.go @@ -14,6 +14,7 @@ import ( "log" "os" "os/exec" + "slices" "strings" "time" @@ -110,8 +111,7 @@ func (m *CustomCmdCollector) Read(interval time.Duration, output chan lp.CCMessa continue } for _, c := range cmdmetrics { - _, skip := stringArrayContains(m.config.ExcludeMetrics, c.Name()) - if skip { + if slices.Contains(m.config.ExcludeMetrics, c.Name()) { continue } @@ -130,8 +130,7 @@ func (m *CustomCmdCollector) Read(interval time.Duration, output chan lp.CCMessa continue } for _, f := range fmetrics { - _, skip := stringArrayContains(m.config.ExcludeMetrics, f.Name()) - if skip { + if slices.Contains(m.config.ExcludeMetrics, f.Name()) { continue } output <- lp.FromInfluxMetric(f) diff --git a/collectors/gpfsMetric.go b/collectors/gpfsMetric.go index 34eff87..cf75432 100644 --- a/collectors/gpfsMetric.go +++ b/collectors/gpfsMetric.go @@ -17,6 +17,7 @@ import ( "log" "os/exec" "os/user" + "slices" "strconv" "strings" "syscall" @@ -386,28 +387,28 @@ func (m *GpfsCollector) Init(config json.RawMessage) error { m.definitions = []GpfsMetricDefinition{} if m.config.SendAbsoluteValues { for _, def := range GpfsAbsMetrics { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { m.definitions = append(m.definitions, def) } } } if m.config.SendDiffValues { for _, def := range GpfsDiffMetrics { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { m.definitions = append(m.definitions, def) } } } if m.config.SendDerivedValues { for _, def := range GpfsDeriveMetrics { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { m.definitions = append(m.definitions, def) } } } else if m.config.SendBandwidths { for _, def := range GpfsDeriveMetrics { if def.unit == "bytes/sec" { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { m.definitions = append(m.definitions, def) } } @@ -415,7 +416,7 @@ func (m *GpfsCollector) Init(config json.RawMessage) error { } if m.config.SendTotalValues { for _, def := range GpfsTotalMetrics { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { // only send total metrics of the types requested if (def.calc == "none" && m.config.SendAbsoluteValues) || (def.calc == "difference" && m.config.SendDiffValues) || @@ -427,7 +428,7 @@ func (m *GpfsCollector) Init(config json.RawMessage) error { } else if m.config.SendBandwidths { for _, def := range GpfsTotalMetrics { if def.unit == "bytes/sec" { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { m.definitions = append(m.definitions, def) } } diff --git a/collectors/iostatMetric.go b/collectors/iostatMetric.go index d93056b..0d645b1 100644 --- a/collectors/iostatMetric.go +++ b/collectors/iostatMetric.go @@ -13,6 +13,7 @@ import ( "errors" "fmt" "os" + "slices" "strconv" "strings" "time" @@ -78,7 +79,7 @@ func (m *IOstatCollector) Init(config json.RawMessage) error { m.devices = make(map[string]IOstatCollectorEntry) m.matches = make(map[string]int) for k, v := range matches { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, k); !skip { + if !slices.Contains(m.config.ExcludeMetrics, k) { m.matches[k] = v } } @@ -87,10 +88,8 @@ func (m *IOstatCollector) Init(config json.RawMessage) error { } file, err := os.Open(IOSTATFILE) if err != nil { - cclog.ComponentError(m.name, err.Error()) - return err + return fmt.Errorf("%s Init(): Failed to open file \"%s\": %w", m.name, IOSTATFILE, err) } - defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { @@ -104,7 +103,7 @@ func (m *IOstatCollector) Init(config json.RawMessage) error { if strings.Contains(device, "loop") { continue } - if _, skip := stringArrayContains(m.config.ExcludeDevices, device); skip { + if slices.Contains(m.config.ExcludeDevices, device) { continue } currentValues := make(map[string]int64) @@ -130,6 +129,10 @@ func (m *IOstatCollector) Init(config json.RawMessage) error { lastValues: lastValues, } } + if err := file.Close(); err != nil { + return fmt.Errorf("%s Init(): Failed to close file \"%s\": %w", m.name, IOSTATFILE, err) + } + m.init = true return err } @@ -168,7 +171,7 @@ func (m *IOstatCollector) Read(interval time.Duration, output chan lp.CCMessage) if strings.Contains(device, "loop") { continue } - if _, skip := stringArrayContains(m.config.ExcludeDevices, device); skip { + if slices.Contains(m.config.ExcludeDevices, device) { continue } if _, ok := m.devices[device]; !ok { diff --git a/collectors/loadavgMetric.go b/collectors/loadavgMetric.go index 5f43c08..7010084 100644 --- a/collectors/loadavgMetric.go +++ b/collectors/loadavgMetric.go @@ -11,6 +11,7 @@ import ( "encoding/json" "fmt" "os" + "slices" "strconv" "strings" "time" @@ -66,10 +67,10 @@ func (m *LoadavgCollector) Init(config json.RawMessage) error { m.proc_skips = make([]bool, len(m.proc_matches)) for i, name := range m.load_matches { - _, m.load_skips[i] = stringArrayContains(m.config.ExcludeMetrics, name) + m.load_skips[i] = slices.Contains(m.config.ExcludeMetrics, name) } for i, name := range m.proc_matches { - _, m.proc_skips[i] = stringArrayContains(m.config.ExcludeMetrics, name) + m.proc_skips[i] = slices.Contains(m.config.ExcludeMetrics, name) } m.init = true return nil diff --git a/collectors/lustreMetric.go b/collectors/lustreMetric.go index f67086b..964d163 100644 --- a/collectors/lustreMetric.go +++ b/collectors/lustreMetric.go @@ -13,6 +13,7 @@ import ( "fmt" "os/exec" "os/user" + "slices" "strconv" "strings" "time" @@ -341,21 +342,21 @@ func (m *LustreCollector) Init(config json.RawMessage) error { m.definitions = []LustreMetricDefinition{} if m.config.SendAbsoluteValues { for _, def := range LustreAbsMetrics { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { m.definitions = append(m.definitions, def) } } } if m.config.SendDiffValues { for _, def := range LustreDiffMetrics { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { m.definitions = append(m.definitions, def) } } } if m.config.SendDerivedValues { for _, def := range LustreDeriveMetrics { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + if !slices.Contains(m.config.ExcludeMetrics, def.name) { m.definitions = append(m.definitions, def) } } diff --git a/collectors/memstatMetric.go b/collectors/memstatMetric.go index e8a0612..b934b36 100644 --- a/collectors/memstatMetric.go +++ b/collectors/memstatMetric.go @@ -15,6 +15,7 @@ import ( "os" "path/filepath" "regexp" + "slices" "strconv" "strings" "time" @@ -119,13 +120,12 @@ func (m *MemstatCollector) Init(config json.RawMessage) error { "MemShared": "mem_shared", } for k, v := range matches { - _, skip := stringArrayContains(m.config.ExcludeMetrics, k) - if !skip { + if !slices.Contains(m.config.ExcludeMetrics, k) { m.matches[k] = v } } m.sendMemUsed = false - if _, skip := stringArrayContains(m.config.ExcludeMetrics, "mem_used"); !skip { + if !slices.Contains(m.config.ExcludeMetrics, "mem_used") { m.sendMemUsed = true } if len(m.matches) == 0 { diff --git a/collectors/metricCollector.go b/collectors/metricCollector.go index cc0cb63..3edfe0a 100644 --- a/collectors/metricCollector.go +++ b/collectors/metricCollector.go @@ -51,18 +51,6 @@ func (c *metricCollector) Initialized() bool { return c.init } -// stringArrayContains scans an array of strings if the value str is present in the array -// If the specified value is found, the corresponding array index is returned. -// The bool value is used to signal success or failure -func stringArrayContains(array []string, str string) (int, bool) { - for i, a := range array { - if a == str { - return i, true - } - } - return -1, false -} - // RemoveFromStringList removes the string r from the array of strings s // If r is not contained in the array an error is returned func RemoveFromStringList(s []string, r string) ([]string, error) { diff --git a/collectors/netstatMetric.go b/collectors/netstatMetric.go index e9dd131..e6b901f 100644 --- a/collectors/netstatMetric.go +++ b/collectors/netstatMetric.go @@ -13,6 +13,7 @@ import ( "errors" "fmt" "os" + "slices" "strconv" "strings" "time" @@ -132,7 +133,7 @@ func (m *NetstatCollector) Init(config json.RawMessage) error { canonical := getCanonicalName(raw, m.aliasToCanonical) // Check if device is a included device - if _, ok := stringArrayContains(m.config.IncludeDevices, canonical); ok { + if slices.Contains(m.config.IncludeDevices, canonical) { // Tag will contain original device name (raw). tags := map[string]string{"stype": "network", "stype-id": raw, "type": "node"} meta_unit_byte := map[string]string{"source": m.name, "group": "Network", "unit": "bytes"} diff --git a/collectors/nfsMetric.go b/collectors/nfsMetric.go index 80c2e68..7e8b57b 100644 --- a/collectors/nfsMetric.go +++ b/collectors/nfsMetric.go @@ -11,6 +11,7 @@ import ( "encoding/json" "fmt" "log" + "slices" // "os" "os/exec" @@ -53,7 +54,7 @@ func (m *nfsCollector) initStats() error { buffer, err := cmd.Output() if err == nil { - for _, line := range strings.Split(string(buffer), "\n") { + for line := range strings.Lines(string(buffer)) { lf := strings.Fields(line) if len(lf) != 5 { continue @@ -85,7 +86,7 @@ func (m *nfsCollector) updateStats() error { buffer, err := cmd.Output() if err == nil { - for _, line := range strings.Split(string(buffer), "\n") { + for line := range strings.Lines(string(buffer)) { lf := strings.Fields(line) if len(lf) != 5 { continue @@ -162,7 +163,7 @@ func (m *nfsCollector) Read(interval time.Duration, output chan lp.CCMessage) { } for name, data := range m.data { - if _, skip := stringArrayContains(m.config.ExcludeMetrics, name); skip { + if slices.Contains(m.config.ExcludeMetrics, name) { continue } value := data.current - data.last diff --git a/collectors/nfsiostatMetric.go b/collectors/nfsiostatMetric.go index 2ec89b7..e23b8b4 100644 --- a/collectors/nfsiostatMetric.go +++ b/collectors/nfsiostatMetric.go @@ -12,6 +12,7 @@ import ( "fmt" "os" "regexp" + "slices" "strconv" "strings" "time" @@ -71,7 +72,7 @@ func (m *NfsIOStatCollector) readNfsiostats() map[string]map[string]int64 { // Is this a device line with mount point, remote target and NFS version? dev := resolve_regex_fields(l, deviceRegex) if len(dev) > 0 { - if _, ok := stringArrayContains(m.config.ExcludeFilesystem, dev[m.key]); !ok { + if !slices.Contains(m.config.ExcludeFilesystem, dev[m.key]) { current = dev if len(current["version"]) == 0 { current["version"] = "3" @@ -85,7 +86,7 @@ func (m *NfsIOStatCollector) readNfsiostats() map[string]map[string]int64 { if len(bytes) > 0 { data[current[m.key]] = make(map[string]int64) for name, sval := range bytes { - if _, ok := stringArrayContains(m.config.ExcludeMetrics, name); !ok { + if !slices.Contains(m.config.ExcludeMetrics, name) { val, err := strconv.ParseInt(sval, 10, 64) if err == nil { data[current[m.key]][name] = val diff --git a/collectors/nvidiaMetric.go b/collectors/nvidiaMetric.go index 9513764..794573d 100644 --- a/collectors/nvidiaMetric.go +++ b/collectors/nvidiaMetric.go @@ -12,6 +12,7 @@ import ( "errors" "fmt" "log" + "slices" "strings" "time" @@ -111,7 +112,7 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error { // Skip excluded devices by ID str_i := fmt.Sprintf("%d", i) - if _, skip := stringArrayContains(m.config.ExcludeDevices, str_i); skip { + if slices.Contains(m.config.ExcludeDevices, str_i) { cclog.ComponentDebug(m.name, "Skipping excluded device", str_i) continue } @@ -139,7 +140,7 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error { pciInfo.Device) // Skip excluded devices specified by PCI ID - if _, skip := stringArrayContains(m.config.ExcludeDevices, pci_id); skip { + if slices.Contains(m.config.ExcludeDevices, pci_id) { cclog.ComponentDebug(m.name, "Skipping excluded device", pci_id) continue }