Skip to content

Commit 895c368

Browse files
alebsysАлександр Лебедев
authored andcommitted
Add count of UDP dropped packets
Signed-off-by: Aleksandr Lebedev <[email protected]> Signed-off-by: Александр Лебедев <[email protected]>
1 parent a65c691 commit 895c368

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

net_ip_socket.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ import (
2424
"strings"
2525
)
2626

27+
var (
28+
// isUDP is used to determine the type of network socket UDP or TCP.
29+
// Further its value will be used to parse UDP-specific fields from /proc/net/udp{,6} files.
30+
isUDP bool
31+
)
32+
2733
const (
2834
// readLimit is used by io.LimitReader while reading the content of the
2935
// /proc/net/udp{,6} files. The number of lines inside such a file is dynamic
@@ -50,6 +56,8 @@ type (
5056
// UsedSockets shows the total number of parsed lines representing the
5157
// number of used sockets.
5258
UsedSockets uint64
59+
// Drops shows the total number of dropped packets of all UPD sockets.
60+
Drops *uint64
5361
}
5462

5563
// netIPSocketLine represents the fields parsed from a single line
@@ -111,19 +119,28 @@ func newNetIPSocketSummary(file string) (*NetIPSocketSummary, error) {
111119
defer f.Close()
112120

113121
var netIPSocketSummary NetIPSocketSummary
122+
var udpPacketDrops uint64
123+
124+
if strings.Contains(file, "udp") {
125+
isUDP = true
126+
}
114127

115128
lr := io.LimitReader(f, readLimit)
116129
s := bufio.NewScanner(lr)
117130
s.Scan() // skip first line with headers
118131
for s.Scan() {
119132
fields := strings.Fields(s.Text())
120-
line, err := parseNetIPSocketLine(fields, false)
133+
line, err := parseNetIPSocketLine(fields, isUDP)
121134
if err != nil {
122135
return nil, err
123136
}
124137
netIPSocketSummary.TxQueueLength += line.TxQueue
125138
netIPSocketSummary.RxQueueLength += line.RxQueue
126139
netIPSocketSummary.UsedSockets++
140+
if isUDP {
141+
udpPacketDrops += *line.Drops
142+
netIPSocketSummary.Drops = &udpPacketDrops
143+
}
127144
}
128145
if err := s.Err(); err != nil {
129146
return nil, err

net_udp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ func Test_newNetUDPSummary(t *testing.T) {
142142
{
143143
name: "udp file found, no error should come up",
144144
file: "testdata/fixtures/proc/net/udp",
145-
want: &NetUDPSummary{TxQueueLength: 2, RxQueueLength: 2, UsedSockets: 3},
145+
want: &NetUDPSummary{TxQueueLength: 2, RxQueueLength: 2, UsedSockets: 3, Drops: intToU64(300)},
146146
wantErr: false,
147147
},
148148
{
149149
name: "udp6 file found, no error should come up",
150150
file: "testdata/fixtures/proc/net/udp6",
151-
want: &NetUDPSummary{TxQueueLength: 0, RxQueueLength: 0, UsedSockets: 2},
151+
want: &NetUDPSummary{TxQueueLength: 0, RxQueueLength: 0, UsedSockets: 2, Drops: intToU64(0)},
152152
wantErr: false,
153153
},
154154
{

0 commit comments

Comments
 (0)