From f346179c2d64f44d95b7b0bfc8bf90746f2987d8 Mon Sep 17 00:00:00 2001 From: JustHumanz Date: Mon, 5 May 2025 16:16:02 +0000 Subject: [PATCH 1/3] Add Miimon metrics Signed-off-by: JustHumanz --- collector/bonding_linux.go | 45 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/collector/bonding_linux.go b/collector/bonding_linux.go index bfec32fd..ba6e495f 100644 --- a/collector/bonding_linux.go +++ b/collector/bonding_linux.go @@ -22,14 +22,15 @@ import ( "log/slog" "os" "path/filepath" + "strconv" "strings" "github.com/prometheus/client_golang/prometheus" ) type bondingCollector struct { - slaves, active typedDesc - logger *slog.Logger + slaves, active, miimon typedDesc + logger *slog.Logger } func init() { @@ -50,6 +51,11 @@ func NewBondingCollector(logger *slog.Logger) (Collector, error) { "Number of active slaves per bonding interface.", []string{"master"}, nil, ), prometheus.GaugeValue}, + miimon: typedDesc{prometheus.NewDesc( + prometheus.BuildFQName(namespace, "bonding", "miimon"), + "MII link monitoring frequency in milliseconds.", + []string{"master"}, nil, + ), prometheus.GaugeValue}, logger: logger, }, nil } @@ -69,6 +75,18 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error { 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) + } return nil } @@ -102,3 +120,26 @@ func readBondingStats(root string) (status map[string][2]int, err error) { } 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 + } + return status, err +} From ea687a302c0b3e09596e57aa821044569e80f346 Mon Sep 17 00:00:00 2001 From: JustHumanz Date: Mon, 5 May 2025 17:17:32 +0000 Subject: [PATCH 2/3] add miimon in sys.ttar Signed-off-by: JustHumanz --- collector/fixtures/sys.ttar | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/collector/fixtures/sys.ttar b/collector/fixtures/sys.ttar index 486dfd31..0ae89af8 100644 --- a/collector/fixtures/sys.ttar +++ b/collector/fixtures/sys.ttar @@ -1711,6 +1711,11 @@ Path: sys/class/net/bond0/bonding/slaves Lines: 0 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/class/net/bond0/bonding/miimon +Lines: 1 +200 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: sys/class/net/bond0/broadcast Lines: 1 ff:ff:ff:ff:ff:ff @@ -1853,6 +1858,11 @@ Lines: 1 eth0 eth4 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/class/net/dmz/bonding/miimon +Lines: 1 +200 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: sys/class/net/dmz/broadcast Lines: 1 ff:ff:ff:ff:ff:ff @@ -2025,6 +2035,11 @@ Lines: 1 eth5 eth1 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/class/net/int/bonding/miimon +Lines: 1 +200 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: sys/class/net/int/broadcast Lines: 1 ff:ff:ff:ff:ff:ff From 7df3a8b94c2c4ac4fe8d42f0fbfe057fb6e21784 Mon Sep 17 00:00:00 2001 From: JustHumanz Date: Wed, 7 May 2025 15:43:21 +0000 Subject: [PATCH 3/3] 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