Replaced stringArrayContains by slices.Contains

This commit is contained in:
Holger Obermaier
2026-02-06 13:49:39 +01:00
parent 1f7b13349c
commit 2b5bf4d6a5
13 changed files with 45 additions and 48 deletions

View File

@@ -17,6 +17,7 @@ import (
"os/exec" "os/exec"
"os/user" "os/user"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -80,8 +81,7 @@ func (m *BeegfsMetaCollector) Init(config json.RawMessage) error {
//create map with possible variables //create map with possible variables
m.matches = make(map[string]string) m.matches = make(map[string]string)
for _, value := range nodeMdstat_array { for _, value := range nodeMdstat_array {
_, skip := stringArrayContains(m.config.ExcludeMetrics, value) if slices.Contains(m.config.ExcludeMetrics, value) {
if skip {
m.matches["other"] = "0" m.matches["other"] = "0"
} else { } else {
m.matches["beegfs_cmeta_"+value] = "0" m.matches["beegfs_cmeta_"+value] = "0"

View File

@@ -17,6 +17,7 @@ import (
"os/exec" "os/exec"
"os/user" "os/user"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -73,8 +74,7 @@ func (m *BeegfsStorageCollector) Init(config json.RawMessage) error {
//create map with possible variables //create map with possible variables
m.matches = make(map[string]string) m.matches = make(map[string]string)
for _, value := range storageStat_array { for _, value := range storageStat_array {
_, skip := stringArrayContains(m.config.ExcludeMetrics, value) if slices.Contains(m.config.ExcludeMetrics, value) {
if skip {
m.matches["other"] = "0" m.matches["other"] = "0"
} else { } else {
m.matches["beegfs_cstorage_"+value] = "0" m.matches["beegfs_cstorage_"+value] = "0"

View File

@@ -14,6 +14,7 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"slices"
"strings" "strings"
"time" "time"
@@ -110,8 +111,7 @@ func (m *CustomCmdCollector) Read(interval time.Duration, output chan lp.CCMessa
continue continue
} }
for _, c := range cmdmetrics { for _, c := range cmdmetrics {
_, skip := stringArrayContains(m.config.ExcludeMetrics, c.Name()) if slices.Contains(m.config.ExcludeMetrics, c.Name()) {
if skip {
continue continue
} }
@@ -130,8 +130,7 @@ func (m *CustomCmdCollector) Read(interval time.Duration, output chan lp.CCMessa
continue continue
} }
for _, f := range fmetrics { for _, f := range fmetrics {
_, skip := stringArrayContains(m.config.ExcludeMetrics, f.Name()) if slices.Contains(m.config.ExcludeMetrics, f.Name()) {
if skip {
continue continue
} }
output <- lp.FromInfluxMetric(f) output <- lp.FromInfluxMetric(f)

View File

@@ -17,6 +17,7 @@ import (
"log" "log"
"os/exec" "os/exec"
"os/user" "os/user"
"slices"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
@@ -386,28 +387,28 @@ func (m *GpfsCollector) Init(config json.RawMessage) error {
m.definitions = []GpfsMetricDefinition{} m.definitions = []GpfsMetricDefinition{}
if m.config.SendAbsoluteValues { if m.config.SendAbsoluteValues {
for _, def := range GpfsAbsMetrics { 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) m.definitions = append(m.definitions, def)
} }
} }
} }
if m.config.SendDiffValues { if m.config.SendDiffValues {
for _, def := range GpfsDiffMetrics { 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) m.definitions = append(m.definitions, def)
} }
} }
} }
if m.config.SendDerivedValues { if m.config.SendDerivedValues {
for _, def := range GpfsDeriveMetrics { 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) m.definitions = append(m.definitions, def)
} }
} }
} else if m.config.SendBandwidths { } else if m.config.SendBandwidths {
for _, def := range GpfsDeriveMetrics { for _, def := range GpfsDeriveMetrics {
if def.unit == "bytes/sec" { 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) m.definitions = append(m.definitions, def)
} }
} }
@@ -415,7 +416,7 @@ func (m *GpfsCollector) Init(config json.RawMessage) error {
} }
if m.config.SendTotalValues { if m.config.SendTotalValues {
for _, def := range GpfsTotalMetrics { 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 // only send total metrics of the types requested
if (def.calc == "none" && m.config.SendAbsoluteValues) || if (def.calc == "none" && m.config.SendAbsoluteValues) ||
(def.calc == "difference" && m.config.SendDiffValues) || (def.calc == "difference" && m.config.SendDiffValues) ||
@@ -427,7 +428,7 @@ func (m *GpfsCollector) Init(config json.RawMessage) error {
} else if m.config.SendBandwidths { } else if m.config.SendBandwidths {
for _, def := range GpfsTotalMetrics { for _, def := range GpfsTotalMetrics {
if def.unit == "bytes/sec" { 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) m.definitions = append(m.definitions, def)
} }
} }

View File

@@ -13,6 +13,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -78,7 +79,7 @@ func (m *IOstatCollector) Init(config json.RawMessage) error {
m.devices = make(map[string]IOstatCollectorEntry) m.devices = make(map[string]IOstatCollectorEntry)
m.matches = make(map[string]int) m.matches = make(map[string]int)
for k, v := range matches { for k, v := range matches {
if _, skip := stringArrayContains(m.config.ExcludeMetrics, k); !skip { if !slices.Contains(m.config.ExcludeMetrics, k) {
m.matches[k] = v m.matches[k] = v
} }
} }
@@ -87,10 +88,8 @@ func (m *IOstatCollector) Init(config json.RawMessage) error {
} }
file, err := os.Open(IOSTATFILE) file, err := os.Open(IOSTATFILE)
if err != nil { if err != nil {
cclog.ComponentError(m.name, err.Error()) return fmt.Errorf("%s Init(): Failed to open file \"%s\": %w", m.name, IOSTATFILE, err)
return err
} }
defer file.Close()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
@@ -104,7 +103,7 @@ func (m *IOstatCollector) Init(config json.RawMessage) error {
if strings.Contains(device, "loop") { if strings.Contains(device, "loop") {
continue continue
} }
if _, skip := stringArrayContains(m.config.ExcludeDevices, device); skip { if slices.Contains(m.config.ExcludeDevices, device) {
continue continue
} }
currentValues := make(map[string]int64) currentValues := make(map[string]int64)
@@ -130,6 +129,10 @@ func (m *IOstatCollector) Init(config json.RawMessage) error {
lastValues: lastValues, 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 m.init = true
return err return err
} }
@@ -168,7 +171,7 @@ func (m *IOstatCollector) Read(interval time.Duration, output chan lp.CCMessage)
if strings.Contains(device, "loop") { if strings.Contains(device, "loop") {
continue continue
} }
if _, skip := stringArrayContains(m.config.ExcludeDevices, device); skip { if slices.Contains(m.config.ExcludeDevices, device) {
continue continue
} }
if _, ok := m.devices[device]; !ok { if _, ok := m.devices[device]; !ok {

View File

@@ -11,6 +11,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -66,10 +67,10 @@ func (m *LoadavgCollector) Init(config json.RawMessage) error {
m.proc_skips = make([]bool, len(m.proc_matches)) m.proc_skips = make([]bool, len(m.proc_matches))
for i, name := range m.load_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 { 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 m.init = true
return nil return nil

View File

@@ -13,6 +13,7 @@ import (
"fmt" "fmt"
"os/exec" "os/exec"
"os/user" "os/user"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -341,21 +342,21 @@ func (m *LustreCollector) Init(config json.RawMessage) error {
m.definitions = []LustreMetricDefinition{} m.definitions = []LustreMetricDefinition{}
if m.config.SendAbsoluteValues { if m.config.SendAbsoluteValues {
for _, def := range LustreAbsMetrics { 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) m.definitions = append(m.definitions, def)
} }
} }
} }
if m.config.SendDiffValues { if m.config.SendDiffValues {
for _, def := range LustreDiffMetrics { 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) m.definitions = append(m.definitions, def)
} }
} }
} }
if m.config.SendDerivedValues { if m.config.SendDerivedValues {
for _, def := range LustreDeriveMetrics { 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) m.definitions = append(m.definitions, def)
} }
} }

View File

@@ -15,6 +15,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -119,13 +120,12 @@ func (m *MemstatCollector) Init(config json.RawMessage) error {
"MemShared": "mem_shared", "MemShared": "mem_shared",
} }
for k, v := range matches { for k, v := range matches {
_, skip := stringArrayContains(m.config.ExcludeMetrics, k) if !slices.Contains(m.config.ExcludeMetrics, k) {
if !skip {
m.matches[k] = v m.matches[k] = v
} }
} }
m.sendMemUsed = false m.sendMemUsed = false
if _, skip := stringArrayContains(m.config.ExcludeMetrics, "mem_used"); !skip { if !slices.Contains(m.config.ExcludeMetrics, "mem_used") {
m.sendMemUsed = true m.sendMemUsed = true
} }
if len(m.matches) == 0 { if len(m.matches) == 0 {

View File

@@ -51,18 +51,6 @@ func (c *metricCollector) Initialized() bool {
return c.init 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 // RemoveFromStringList removes the string r from the array of strings s
// If r is not contained in the array an error is returned // If r is not contained in the array an error is returned
func RemoveFromStringList(s []string, r string) ([]string, error) { func RemoveFromStringList(s []string, r string) ([]string, error) {

View File

@@ -13,6 +13,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -132,7 +133,7 @@ func (m *NetstatCollector) Init(config json.RawMessage) error {
canonical := getCanonicalName(raw, m.aliasToCanonical) canonical := getCanonicalName(raw, m.aliasToCanonical)
// Check if device is a included device // 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). // Tag will contain original device name (raw).
tags := map[string]string{"stype": "network", "stype-id": raw, "type": "node"} tags := map[string]string{"stype": "network", "stype-id": raw, "type": "node"}
meta_unit_byte := map[string]string{"source": m.name, "group": "Network", "unit": "bytes"} meta_unit_byte := map[string]string{"source": m.name, "group": "Network", "unit": "bytes"}

View File

@@ -11,6 +11,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"slices"
// "os" // "os"
"os/exec" "os/exec"
@@ -53,7 +54,7 @@ func (m *nfsCollector) initStats() error {
buffer, err := cmd.Output() buffer, err := cmd.Output()
if err == nil { if err == nil {
for _, line := range strings.Split(string(buffer), "\n") { for line := range strings.Lines(string(buffer)) {
lf := strings.Fields(line) lf := strings.Fields(line)
if len(lf) != 5 { if len(lf) != 5 {
continue continue
@@ -85,7 +86,7 @@ func (m *nfsCollector) updateStats() error {
buffer, err := cmd.Output() buffer, err := cmd.Output()
if err == nil { if err == nil {
for _, line := range strings.Split(string(buffer), "\n") { for line := range strings.Lines(string(buffer)) {
lf := strings.Fields(line) lf := strings.Fields(line)
if len(lf) != 5 { if len(lf) != 5 {
continue continue
@@ -162,7 +163,7 @@ func (m *nfsCollector) Read(interval time.Duration, output chan lp.CCMessage) {
} }
for name, data := range m.data { for name, data := range m.data {
if _, skip := stringArrayContains(m.config.ExcludeMetrics, name); skip { if slices.Contains(m.config.ExcludeMetrics, name) {
continue continue
} }
value := data.current - data.last value := data.current - data.last

View File

@@ -12,6 +12,7 @@ import (
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "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? // Is this a device line with mount point, remote target and NFS version?
dev := resolve_regex_fields(l, deviceRegex) dev := resolve_regex_fields(l, deviceRegex)
if len(dev) > 0 { 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 current = dev
if len(current["version"]) == 0 { if len(current["version"]) == 0 {
current["version"] = "3" current["version"] = "3"
@@ -85,7 +86,7 @@ func (m *NfsIOStatCollector) readNfsiostats() map[string]map[string]int64 {
if len(bytes) > 0 { if len(bytes) > 0 {
data[current[m.key]] = make(map[string]int64) data[current[m.key]] = make(map[string]int64)
for name, sval := range bytes { 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) val, err := strconv.ParseInt(sval, 10, 64)
if err == nil { if err == nil {
data[current[m.key]][name] = val data[current[m.key]][name] = val

View File

@@ -12,6 +12,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"slices"
"strings" "strings"
"time" "time"
@@ -111,7 +112,7 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error {
// Skip excluded devices by ID // Skip excluded devices by ID
str_i := fmt.Sprintf("%d", i) 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) cclog.ComponentDebug(m.name, "Skipping excluded device", str_i)
continue continue
} }
@@ -139,7 +140,7 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error {
pciInfo.Device) pciInfo.Device)
// Skip excluded devices specified by PCI ID // 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) cclog.ComponentDebug(m.name, "Skipping excluded device", pci_id)
continue continue
} }