From 7df3a8b94c2c4ac4fe8d42f0fbfe057fb6e21784 Mon Sep 17 00:00:00 2001 From: JustHumanz Date: Wed, 7 May 2025 15:43:21 +0000 Subject: [PATCH] refactor `readBondingStats` Signed-off-by: JustHumanz --- collector/bonding_linux.go | 48 ++++++++++++--------------------- collector/bonding_linux_test.go | 12 ++++----- collector/fixtures/sys.ttar | 4 +-- 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/collector/bonding_linux.go b/collector/bonding_linux.go index ba6e495f..79e3af9b 100644 --- a/collector/bonding_linux.go +++ b/collector/bonding_linux.go @@ -33,6 +33,11 @@ type bondingCollector struct { logger *slog.Logger } +type bondingStats struct { + name string + slaves, active, miimon int +} + func init() { registerCollector("bonding", defaultEnabled, NewBondingCollector) } @@ -71,27 +76,16 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error { } return err } - for master, status := range bondingStats { - ch <- c.slaves.mustNewConstMetric(float64(status[0]), master) - ch <- c.active.mustNewConstMetric(float64(status[1]), master) - } - - bondingMiimon, err := readBondingMiimon(statusfile) - if err != nil { - if errors.Is(err, os.ErrNotExist) { - c.logger.Debug("Not collecting bonding, file does not exist", "file", statusfile) - return ErrNoData - } - return err - } - for bond, miimon := range bondingMiimon { - ch <- c.miimon.mustNewConstMetric(float64(miimon), bond) + for _, bond := range bondingStats { + ch <- c.slaves.mustNewConstMetric(float64(bond.slaves), bond.name) + ch <- c.active.mustNewConstMetric(float64(bond.active), bond.name) + ch <- c.miimon.mustNewConstMetric(float64(bond.miimon), bond.name) } return nil } -func readBondingStats(root string) (status map[string][2]int, err error) { - status = map[string][2]int{} +func readBondingStats(root string) (status []bondingStats, err error) { + status = []bondingStats{} masters, err := os.ReadFile(filepath.Join(root, "bonding_masters")) if err != nil { return nil, err @@ -116,30 +110,22 @@ func readBondingStats(root string) (status map[string][2]int, err error) { sstat[1]++ } } - status[master] = sstat - } - return status, err -} -func readBondingMiimon(root string) (status map[string]int, err error) { - status = map[string]int{} - masters, err := os.ReadFile(filepath.Join(root, "bonding_masters")) - if err != nil { - return nil, err - } - - for _, master := range strings.Fields(string(masters)) { miimon, err := os.ReadFile(filepath.Join(root, master, "bonding", "miimon")) if err != nil { return nil, err } - intMiimon, err := strconv.Atoi(strings.TrimSpace(string(miimon))) if err != nil { return nil, err } - status[master] = intMiimon + status = append(status, bondingStats{ + name: master, + slaves: sstat[0], + active: sstat[1], + miimon: intMiimon, + }) } return status, err } diff --git a/collector/bonding_linux_test.go b/collector/bonding_linux_test.go index 98af73fa..c416c0eb 100644 --- a/collector/bonding_linux_test.go +++ b/collector/bonding_linux_test.go @@ -25,15 +25,15 @@ func TestBonding(t *testing.T) { if err != nil { t.Fatal(err) } - if bondingStats["bond0"][0] != 0 || bondingStats["bond0"][1] != 0 { + if bondingStats[0].name != "bond0" || bondingStats[0].slaves != 0 || bondingStats[0].active != 0 || bondingStats[0].miimon != 100 { t.Fatal("bond0 in unexpected state") } - if bondingStats["int"][0] != 2 || bondingStats["int"][1] != 1 { - t.Fatal("int in unexpected state") - } - - if bondingStats["dmz"][0] != 2 || bondingStats["dmz"][1] != 2 { + if bondingStats[1].name != "dmz" || bondingStats[1].slaves != 2 || bondingStats[1].active != 2 || bondingStats[1].miimon != 0 { t.Fatal("dmz in unexpected state") } + + if bondingStats[2].name != "int" || bondingStats[2].slaves != 2 || bondingStats[2].active != 1 || bondingStats[2].miimon != 200 { + t.Fatal("int in unexpected state") + } } diff --git a/collector/fixtures/sys.ttar b/collector/fixtures/sys.ttar index 0ae89af8..81bb5c36 100644 --- a/collector/fixtures/sys.ttar +++ b/collector/fixtures/sys.ttar @@ -1713,7 +1713,7 @@ Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: sys/class/net/bond0/bonding/miimon Lines: 1 -200 +100 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: sys/class/net/bond0/broadcast @@ -1860,7 +1860,7 @@ Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: sys/class/net/dmz/bonding/miimon Lines: 1 -200 +0 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: sys/class/net/dmz/broadcast