-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (145 loc) · 2.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright © 2022 Zlatko Čalušić
//
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"log"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
)
const (
sysClassNet = "/sys/class/net"
ethTool = "ethtool"
)
var (
reRX = regexp.MustCompile(`^RX:\s+(\d+)$`)
reTX = regexp.MustCompile(`^TX:\s+(\d+)$`)
)
func prepareCmd(app string, args []string) *exec.Cmd {
log.Printf("Running %s %s", app, strings.Join(args, " "))
return exec.Command(app, args...)
}
func main() {
log.SetFlags(0)
devices, err := os.ReadDir(sysClassNet)
if err != nil {
log.Fatal(err)
}
for _, link := range devices {
intf := link.Name()
fullpath := path.Join(sysClassNet, intf)
fi, err := os.Lstat(fullpath)
if err != nil {
log.Print(err)
continue
}
if fi.Mode()&os.ModeSymlink == 0 {
continue
}
dev, err := os.Readlink(fullpath)
if err != nil {
log.Print(err)
continue
}
// Skip virtual devices.
if strings.HasPrefix(dev, "../../devices/virtual/") {
continue
}
var stdout, stderr bytes.Buffer
cmd := prepareCmd(ethTool, []string{"-g", intf})
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if stderr.Len() > 0 {
log.Print(stderr.String())
}
if err != nil {
log.Print(err)
continue
}
var max, cur bool
var rxMax, txMax, rxCur, txCur string
s := bufio.NewScanner(&stdout)
for s.Scan() {
line := s.Text()
if line == "Pre-set maximums:" {
max = true
cur = false
} else if line == "Current hardware settings:" {
cur = true
max = false
} else if m := reRX.FindStringSubmatch(line); m != nil {
if max {
rxMax = m[1]
} else if cur {
rxCur = m[1]
}
} else if m := reTX.FindStringSubmatch(line); m != nil {
if max {
txMax = m[1]
} else if cur {
txCur = m[1]
}
}
}
if err := s.Err(); err != nil {
log.Print(err)
continue
}
var rxMaxInt, txMaxInt, rxCurInt, txCurInt int64
if rxMax != "" {
if rxMaxInt, err = strconv.ParseInt(rxMax, 10, 64); err != nil {
log.Print(err)
continue
}
}
if txMax != "" {
if txMaxInt, err = strconv.ParseInt(txMax, 10, 64); err != nil {
log.Print(err)
continue
}
}
if rxCur != "" {
if rxCurInt, err = strconv.ParseInt(rxCur, 10, 64); err != nil {
log.Print(err)
continue
}
}
if txCur != "" {
if txCurInt, err = strconv.ParseInt(txCur, 10, 64); err != nil {
log.Print(err)
continue
}
}
args := []string{"-G", intf}
initLen := len(args)
if rxCurInt < rxMaxInt {
args = append(args, "rx")
args = append(args, rxMax)
}
if txCurInt < txMaxInt {
args = append(args, "tx")
args = append(args, txMax)
}
if len(args) <= initLen {
// Nothing to do.
continue
}
cmd = prepareCmd(ethTool, args)
cmd.Stderr = &stderr
err = cmd.Run()
if stderr.Len() > 0 {
log.Print(stderr.String())
}
if err != nil {
log.Print(err)
continue
}
}
}