-
Notifications
You must be signed in to change notification settings - Fork 1
/
netinfo.go
122 lines (109 loc) · 3.17 KB
/
netinfo.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
package util
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
// NetInfo contains information about the system's networking interfaces
type NetInfo struct {
Interface string
RXBytes int64
RXPackets int64
RXErrors int64
RXDropped int64
RXFIFO int64
RXFrame int64
RXCompressed int64
RXMulticast int64
TXBytes int64
TXPackets int64
TXErrors int64
TXDropped int64
TXFIFO int64
TXCollisions int64
TXCarrier int64
TXCompressed int64
}
// GetNetInfo reads and parses Linux's /proc/net/dev file
func GetNetInfo(iface string) (inf NetInfo, err error) {
file, err := os.Open("/proc/net/dev")
if err != nil {
return inf, fmt.Errorf("could not open netdev file: %w", err)
}
defer file.Close()
var scanner = bufio.NewScanner(file)
var split []string
for linenr := 0; scanner.Scan(); linenr++ {
if linenr < 2 {
continue // Skip the first two header lines
}
// Split interface and stats
if split = strings.SplitN(scanner.Text(), ": ", 2); len(split) != 2 {
return inf, fmt.Errorf("invalid number of interface columns in netdev file. %d instead of 2", len(split))
}
// Save the interface name. If it's not the right interface we go to the
// next one
if inf.Interface = strings.TrimSpace(split[0]); inf.Interface != iface {
continue
}
break // We found our interface, start parsing
}
if inf.Interface != iface {
return inf, fmt.Errorf("network interface %s not found in netdev file", iface)
}
// Split the fields
if split = strings.Fields(split[1]); len(split) != 16 {
return inf, fmt.Errorf("invalid number of stat columns in netdev file. %d instead of 16", len(split))
}
if inf.RXBytes, err = strconv.ParseInt(split[0], 10, 64); err != nil {
return inf, err
}
if inf.RXPackets, err = strconv.ParseInt(split[1], 10, 64); err != nil {
return inf, err
}
if inf.RXErrors, err = strconv.ParseInt(split[2], 10, 64); err != nil {
return inf, err
}
if inf.RXDropped, err = strconv.ParseInt(split[3], 10, 64); err != nil {
return inf, err
}
if inf.RXFIFO, err = strconv.ParseInt(split[4], 10, 64); err != nil {
return inf, err
}
if inf.RXFrame, err = strconv.ParseInt(split[5], 10, 64); err != nil {
return inf, err
}
if inf.RXCompressed, err = strconv.ParseInt(split[6], 10, 64); err != nil {
return inf, err
}
if inf.RXMulticast, err = strconv.ParseInt(split[7], 10, 64); err != nil {
return inf, err
}
if inf.TXBytes, err = strconv.ParseInt(split[8], 10, 64); err != nil {
return inf, err
}
if inf.TXPackets, err = strconv.ParseInt(split[9], 10, 64); err != nil {
return inf, err
}
if inf.TXErrors, err = strconv.ParseInt(split[10], 10, 64); err != nil {
return inf, err
}
if inf.TXDropped, err = strconv.ParseInt(split[11], 10, 64); err != nil {
return inf, err
}
if inf.TXFIFO, err = strconv.ParseInt(split[12], 10, 64); err != nil {
return inf, err
}
if inf.TXCollisions, err = strconv.ParseInt(split[13], 10, 64); err != nil {
return inf, err
}
if inf.TXCarrier, err = strconv.ParseInt(split[14], 10, 64); err != nil {
return inf, err
}
if inf.TXCompressed, err = strconv.ParseInt(split[15], 10, 64); err != nil {
return inf, err
}
return inf, scanner.Err()
}