-
Notifications
You must be signed in to change notification settings - Fork 1
/
netdev.go
267 lines (223 loc) · 4.81 KB
/
netdev.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package netdev
import (
"os"
"path"
"strconv"
"strings"
"github.com/NETWAYS/check_system_basics/internal/common/filter"
)
// types and constants
// dormant = 5
const (
Up = 0
Testing = 1
Lowerlayerdown = 2
Down = 3
Unknown = 4
)
const (
netDevicePath = "/sys/class/net"
)
func TranslateIfaceState(state uint) string {
switch state {
case Up:
return "Up"
case Testing:
return "Testing"
case Lowerlayerdown:
return "Lowerlayerdown"
case Down:
return "Down"
case Unknown:
return "Unknown"
default:
return ""
}
}
// Constants and the string array MUST be kept in sync!
const (
rx_bytes int = iota
rx_errs
rx_drop
rx_packets
/*
rx_fifo uint
rx_frame uint
rx_compressed uint
rx_multicast uint
*/
tx_bytes
tx_errs
tx_drop
tx_packets
/*
tx_fifo uint
tx_frame uint
tx_compressed uint
tx_multicast uint
*/
metricLength
)
func GetIfaceStatNames() []string {
return []string{
"rx_bytes",
"rx_errors",
"rx_dropped",
"rx_packets",
/*
"rx_fifo",
"rx_frame"
"rx_compressed"
"rx_multicast"
*/
"tx_bytes",
"tx_errors",
"tx_dropped",
"tx_packets",
/*
"tx_fifo",
"tx_frame",
"tx_compressed",
"tx_multicast",
*/
}
}
type statistics [metricLength]uint64
type IfaceData struct {
Name string
Operstate uint
Metrics statistics
}
const (
IfaceDataName = iota
)
func (iface IfaceData) GetFilterableValue(ident uint) string {
switch ident {
case IfaceDataName:
return iface.Name
default:
return ""
}
}
func GetAllInterfaces() ([]IfaceData, error) {
interfaces, err := listInterfaces()
if err != nil {
return []IfaceData{}, err
}
if len(interfaces) == 0 {
return []IfaceData{}, nil
}
result := make([]IfaceData, len(interfaces))
for i := range interfaces {
result[i].Name = interfaces[i]
err = getInterfaceState(&result[i])
if err != nil {
return result, err
}
err = getInfacesStatistics(&result[i])
if err != nil {
return result, err
}
}
return result, nil
}
func listInterfaces() ([]string, error) {
file, err := os.Open(netDevicePath)
if err != nil {
return []string{}, err
}
devices, err := file.Readdirnames(0)
if err != nil {
return []string{}, err
}
result := make([]string, 0, len(devices))
for idx := range devices {
fileInfo, err := os.Stat(path.Join(netDevicePath, devices[idx]))
if err != nil {
// Could not stat file there, not sure if this can be handled usefully. Just die for now.
return []string{}, err
}
// Filter here, to get only entries which end in a directory (the entries itself are mostly symlinks)
// Despite of what man 5 sysfs says, not all the files represent network interfaces, but might also
// be normal files (at least a "bonding_masters" file was found on a system with bonded interfaces)
if fileInfo.Mode().IsDir() {
result = append(result, devices[idx])
}
}
return result, nil
}
// getInterfaceState receives the name of an interfaces and returns
// an integer result code representing the state of the interface
// @result = 0 => Interface is up
// @result = 2 => Interface is down
// @result = 3 => Interface is unknown or state of the interface is unknown for some reason
func getInterfaceState(data *IfaceData) error {
basePath := path.Join(netDevicePath, data.Name)
bytes, err := os.ReadFile(path.Join(basePath, "operstate"))
if err != nil {
return err
}
switch strings.Trim(string(bytes), " \n") {
case "up":
data.Operstate = Up
return nil
case "testing":
data.Operstate = Testing
return nil
case "down":
data.Operstate = Down
return nil
case "lowerlayerdown":
data.Operstate = Lowerlayerdown
return nil
default:
data.Operstate = Unknown
return nil
}
}
// Get interfaces statistics
// @result: ifaceStats, err
func getInfacesStatistics(data *IfaceData) error {
basePath := path.Join(netDevicePath, data.Name, "statistics")
var val uint64
for idx, stat := range GetIfaceStatNames() {
numberBytes, err := os.ReadFile(path.Join(basePath, stat))
if err != nil {
return err
}
numberString := string(numberBytes)
val, err = strconv.ParseUint(numberString[:len(numberString)-1], 10, 64)
if err != nil {
return err
}
data.Metrics[idx] = val
}
return nil
}
func FilterInterfaces(interfaces *[]IfaceData, filters *Filter) ([]IfaceData, error) {
foo, err := filter.Filter(*interfaces,
&filters.IncludeInterfaceNames,
IfaceDataName,
filter.Options{
MatchIncludedInResult: true,
RegexpMatching: true,
EmptyFilterNoMatch: false,
},
)
if err != nil {
return []IfaceData{}, err
}
foo, err = filter.Filter(foo,
&filters.ExcludeInterfaceNames,
IfaceDataName,
filter.Options{
MatchIncludedInResult: false,
RegexpMatching: true,
EmptyFilterNoMatch: false,
},
)
if err != nil {
return []IfaceData{}, err
}
return foo, nil
}