refactor readBondingStats

Signed-off-by: JustHumanz <aldin.setiawan@vexxhost.com>
This commit is contained in:
JustHumanz 2025-05-07 15:43:21 +00:00 committed by JustHumanz
parent ea687a302c
commit 7df3a8b94c
3 changed files with 25 additions and 39 deletions

View file

@ -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
}

View file

@ -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")
}
}

View file

@ -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