-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc_net_dev.go
More file actions
221 lines (197 loc) · 6.17 KB
/
proc_net_dev.go
File metadata and controls
221 lines (197 loc) · 6.17 KB
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
//go:build linux
package vitals
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
// ProcNetDev represents system statistics about network usage.
// See [https://man7.org/linux/man-pages/man5/proc_pid_net.5.html] for more details.
type ProcNetDev struct {
// Interfaces.
Interfaces []NetworkInterface
}
// NetworkInterface represents a network interface statistics.
type NetworkInterface struct {
// Interface name.
Interface string
// Receive statistics.
Receive NetworkReceiveStats
// Transmit statistics.
Transmit NetworkTransmitStats
}
// NetworkReceiveStats represents system statistics about network receive usage.
type NetworkReceiveStats struct {
// Bytes received.
Bytes uint64
// Packets received.
Packets uint64
// Errs detected.
Errs uint64
// Dropped packets.
Drop uint64
// Fifo buffer errors.
Fifo uint64
// Frames.
Frame uint64
// Compressed packets.
Compressed uint64
// Multicast frames.
Multicast uint64
}
// NetworkTransmitStats represents system statistics about network transmit usage.
type NetworkTransmitStats struct {
// Bytes transmitted.
Bytes uint64
// Packets transmitted.
Packets uint64
// Errs detected.
Errs uint64
// Dropped packets.
Drop uint64
// Fifo buffer errors.
Fifo uint64
// Collisions.
Colls uint64
// Carrier losses.
Carrier uint64
// Compressed packets.
Compressed uint64
}
// ReadProcNetDev returns system network statistics from /proc/net/dev.
func ReadProcNetDev() (ProcNetDev, error) {
procNetDevData, err := os.ReadFile("/proc/net/dev")
if err != nil {
return ProcNetDev{}, fmt.Errorf("read /proc/net/dev: %w", err)
}
procNetDev, err := ParseProcNetDev(procNetDevData)
if err != nil {
return ProcNetDev{}, fmt.Errorf("parse proc net dev: %w", err)
}
return procNetDev, nil
}
// ParseProcNetDev parses /proc/net/dev file data into [ProcNetDev].
func ParseProcNetDev(data []byte) (ProcNetDev, error) {
var err error
var stats ProcNetDev
for line := range strings.Lines(string(data)) {
values := strings.Fields(line)
if len(values) < 17 {
continue
}
ni := NetworkInterface{}
for i, value := range values {
switch i {
case 0:
ni.Interface = strings.TrimSuffix(value, ":")
case 1:
ni.Receive.Bytes, err = strconv.ParseUint(value, 10, 64)
case 2:
ni.Receive.Packets, err = strconv.ParseUint(value, 10, 64)
case 3:
ni.Receive.Errs, err = strconv.ParseUint(value, 10, 64)
case 4:
ni.Receive.Drop, err = strconv.ParseUint(value, 10, 64)
case 5:
ni.Receive.Fifo, err = strconv.ParseUint(value, 10, 64)
case 6:
ni.Receive.Frame, err = strconv.ParseUint(value, 10, 64)
case 7:
ni.Receive.Compressed, err = strconv.ParseUint(value, 10, 64)
case 8:
ni.Receive.Multicast, err = strconv.ParseUint(value, 10, 64)
case 9:
ni.Transmit.Bytes, err = strconv.ParseUint(value, 10, 64)
case 10:
ni.Transmit.Packets, err = strconv.ParseUint(value, 10, 64)
case 11:
ni.Transmit.Errs, err = strconv.ParseUint(value, 10, 64)
case 12:
ni.Transmit.Drop, err = strconv.ParseUint(value, 10, 64)
case 13:
ni.Transmit.Fifo, err = strconv.ParseUint(value, 10, 64)
case 14:
ni.Transmit.Colls, err = strconv.ParseUint(value, 10, 64)
case 15:
ni.Transmit.Carrier, err = strconv.ParseUint(value, 10, 64)
case 16:
ni.Transmit.Compressed, err = strconv.ParseUint(value, 10, 64)
default:
break
}
if err != nil {
return ProcNetDev{}, fmt.Errorf("parse proc net dev: %w", err)
}
}
stats.Interfaces = append(stats.Interfaces, ni)
}
return stats, nil
}
// CalculateProcNetDevDiff returns system network usage statistics difference collected during a time interval.
func CalculateProcNetDevDiff(interval time.Duration) (ProcNetDev, error) {
stats1, err := ReadProcNetDev()
if err != nil {
return ProcNetDev{}, fmt.Errorf("get proc net dev: %w", err)
}
time.Sleep(interval)
stats2, err := ReadProcNetDev()
if err != nil {
return ProcNetDev{}, fmt.Errorf("get proc net dev: %w", err)
}
if len(stats1.Interfaces) != len(stats2.Interfaces) {
return ProcNetDev{}, fmt.Errorf("number of network interfaces changed")
}
diff := ProcNetDev{
Interfaces: make([]NetworkInterface, len(stats1.Interfaces)),
}
for i := range len(diff.Interfaces) {
i1 := stats1.Interfaces[i]
i2 := stats2.Interfaces[i]
if i1.Interface != i2.Interface {
return ProcNetDev{}, fmt.Errorf("network interfaces changed")
}
diff.Interfaces[i] = NetworkInterface{
Interface: i1.Interface,
Receive: NetworkReceiveStats{
Bytes: i2.Receive.Bytes - i1.Receive.Bytes,
Packets: i2.Receive.Packets - i1.Receive.Packets,
Errs: i2.Receive.Errs - i1.Receive.Errs,
Drop: i2.Receive.Drop - i1.Receive.Drop,
Fifo: i2.Receive.Fifo - i1.Receive.Fifo,
Frame: i2.Receive.Frame - i1.Receive.Frame,
Compressed: i2.Receive.Compressed - i1.Receive.Compressed,
Multicast: i2.Receive.Multicast - i1.Receive.Multicast,
},
Transmit: NetworkTransmitStats{
Bytes: i2.Transmit.Bytes - i1.Transmit.Bytes,
Packets: i2.Transmit.Packets - i1.Transmit.Packets,
Errs: i2.Transmit.Errs - i1.Transmit.Errs,
Drop: i2.Transmit.Drop - i1.Transmit.Drop,
Fifo: i2.Transmit.Fifo - i1.Transmit.Fifo,
Colls: i2.Transmit.Colls - i1.Transmit.Colls,
Carrier: i2.Transmit.Carrier - i1.Transmit.Carrier,
Compressed: i2.Transmit.Compressed - i1.Transmit.Compressed,
},
}
}
return diff, nil
}
// NetworkIOUsage returns network IO (byte reads and writes) (measured over the last 100 ms) usage.
func NetworkIOUsage(networkInterface string) (readBytes uint64, writeBytes uint64, err error) {
return IOUsageOverTime(networkInterface, 100*time.Millisecond)
}
// NetworkIOUsageOverTime returns network IO (byte reads and writes) usage over time.
func NetworkIOUsageOverTime(networkInterface string, interval time.Duration) (reads uint64, writes uint64, err error) {
diff, err := CalculateProcNetDevDiff(interval)
if err != nil {
return 0, 0, fmt.Errorf("get proc net dev diff: %w", err)
}
for _, ni := range diff.Interfaces {
if ni.Interface == networkInterface {
return ni.Receive.Bytes, ni.Transmit.Bytes, nil
}
}
return 0, 0, fmt.Errorf("network interface not found")
}