Skip to content

Commit 7df3a8b

Browse files
committed
refactor readBondingStats
Signed-off-by: JustHumanz <[email protected]>
1 parent ea687a3 commit 7df3a8b

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

collector/bonding_linux.go

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type bondingCollector struct {
3333
logger *slog.Logger
3434
}
3535

36+
type bondingStats struct {
37+
name string
38+
slaves, active, miimon int
39+
}
40+
3641
func init() {
3742
registerCollector("bonding", defaultEnabled, NewBondingCollector)
3843
}
@@ -71,27 +76,16 @@ func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error {
7176
}
7277
return err
7378
}
74-
for master, status := range bondingStats {
75-
ch <- c.slaves.mustNewConstMetric(float64(status[0]), master)
76-
ch <- c.active.mustNewConstMetric(float64(status[1]), master)
77-
}
78-
79-
bondingMiimon, err := readBondingMiimon(statusfile)
80-
if err != nil {
81-
if errors.Is(err, os.ErrNotExist) {
82-
c.logger.Debug("Not collecting bonding, file does not exist", "file", statusfile)
83-
return ErrNoData
84-
}
85-
return err
86-
}
87-
for bond, miimon := range bondingMiimon {
88-
ch <- c.miimon.mustNewConstMetric(float64(miimon), bond)
79+
for _, bond := range bondingStats {
80+
ch <- c.slaves.mustNewConstMetric(float64(bond.slaves), bond.name)
81+
ch <- c.active.mustNewConstMetric(float64(bond.active), bond.name)
82+
ch <- c.miimon.mustNewConstMetric(float64(bond.miimon), bond.name)
8983
}
9084
return nil
9185
}
9286

93-
func readBondingStats(root string) (status map[string][2]int, err error) {
94-
status = map[string][2]int{}
87+
func readBondingStats(root string) (status []bondingStats, err error) {
88+
status = []bondingStats{}
9589
masters, err := os.ReadFile(filepath.Join(root, "bonding_masters"))
9690
if err != nil {
9791
return nil, err
@@ -116,30 +110,22 @@ func readBondingStats(root string) (status map[string][2]int, err error) {
116110
sstat[1]++
117111
}
118112
}
119-
status[master] = sstat
120-
}
121-
return status, err
122-
}
123-
124-
func readBondingMiimon(root string) (status map[string]int, err error) {
125-
status = map[string]int{}
126-
masters, err := os.ReadFile(filepath.Join(root, "bonding_masters"))
127-
if err != nil {
128-
return nil, err
129-
}
130113

131-
for _, master := range strings.Fields(string(masters)) {
132114
miimon, err := os.ReadFile(filepath.Join(root, master, "bonding", "miimon"))
133115
if err != nil {
134116
return nil, err
135117
}
136-
137118
intMiimon, err := strconv.Atoi(strings.TrimSpace(string(miimon)))
138119
if err != nil {
139120
return nil, err
140121
}
141122

142-
status[master] = intMiimon
123+
status = append(status, bondingStats{
124+
name: master,
125+
slaves: sstat[0],
126+
active: sstat[1],
127+
miimon: intMiimon,
128+
})
143129
}
144130
return status, err
145131
}

collector/bonding_linux_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ func TestBonding(t *testing.T) {
2525
if err != nil {
2626
t.Fatal(err)
2727
}
28-
if bondingStats["bond0"][0] != 0 || bondingStats["bond0"][1] != 0 {
28+
if bondingStats[0].name != "bond0" || bondingStats[0].slaves != 0 || bondingStats[0].active != 0 || bondingStats[0].miimon != 100 {
2929
t.Fatal("bond0 in unexpected state")
3030
}
3131

32-
if bondingStats["int"][0] != 2 || bondingStats["int"][1] != 1 {
33-
t.Fatal("int in unexpected state")
32+
if bondingStats[1].name != "dmz" || bondingStats[1].slaves != 2 || bondingStats[1].active != 2 || bondingStats[1].miimon != 0 {
33+
t.Fatal("dmz in unexpected state")
3434
}
3535

36-
if bondingStats["dmz"][0] != 2 || bondingStats["dmz"][1] != 2 {
37-
t.Fatal("dmz in unexpected state")
36+
if bondingStats[2].name != "int" || bondingStats[2].slaves != 2 || bondingStats[2].active != 1 || bondingStats[2].miimon != 200 {
37+
t.Fatal("int in unexpected state")
3838
}
3939
}

collector/fixtures/sys.ttar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ Mode: 644
17131713
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17141714
Path: sys/class/net/bond0/bonding/miimon
17151715
Lines: 1
1716-
200
1716+
100
17171717
Mode: 644
17181718
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17191719
Path: sys/class/net/bond0/broadcast
@@ -1860,7 +1860,7 @@ Mode: 644
18601860
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18611861
Path: sys/class/net/dmz/bonding/miimon
18621862
Lines: 1
1863-
200
1863+
0
18641864
Mode: 644
18651865
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18661866
Path: sys/class/net/dmz/broadcast

0 commit comments

Comments
 (0)