Skip to content

Commit

Permalink
Add count of UDP dropped packets
Browse files Browse the repository at this point in the history
  • Loading branch information
alebsys committed Jun 18, 2023
1 parent 45365d2 commit 00b084c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion net_ip_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
"strings"
)

var (
// isUDP is used to determine the type of network socket UDP or TCP.
// Further its value will be used to parse UDP-specific fields from /proc/net/udp{,6} files.
isUDP bool
)

const (
// readLimit is used by io.LimitReader while reading the content of the
// /proc/net/udp{,6} files. The number of lines inside such a file is dynamic
Expand All @@ -50,6 +56,8 @@ type (
// UsedSockets shows the total number of parsed lines representing the
// number of used sockets.
UsedSockets uint64
// Drops shows the total number of dropped packets of all UPD sockets.
Drops *uint64
}

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

var netIPSocketSummary NetIPSocketSummary
var udpPacketDrops uint64

if strings.Contains(file, "udp") {
isUDP = true
}

lr := io.LimitReader(f, readLimit)
s := bufio.NewScanner(lr)
s.Scan() // skip first line with headers
for s.Scan() {
fields := strings.Fields(s.Text())
line, err := parseNetIPSocketLine(fields, false)
line, err := parseNetIPSocketLine(fields, isUDP)
if err != nil {
return nil, err
}
netIPSocketSummary.TxQueueLength += line.TxQueue
netIPSocketSummary.RxQueueLength += line.RxQueue
netIPSocketSummary.UsedSockets++
if isUDP {
udpPacketDrops += *line.Drops
netIPSocketSummary.Drops = &udpPacketDrops
}
}
if err := s.Err(); err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions net_udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ func Test_newNetUDPSummary(t *testing.T) {
{
name: "udp file found, no error should come up",
file: "testdata/fixtures/proc/net/udp",
want: &NetUDPSummary{TxQueueLength: 2, RxQueueLength: 2, UsedSockets: 3},
want: &NetUDPSummary{TxQueueLength: 2, RxQueueLength: 2, UsedSockets: 3, Drops: intToU64(300)},
wantErr: false,
},
{
name: "udp6 file found, no error should come up",
file: "testdata/fixtures/proc/net/udp6",
want: &NetUDPSummary{TxQueueLength: 0, RxQueueLength: 0, UsedSockets: 2},
want: &NetUDPSummary{TxQueueLength: 0, RxQueueLength: 0, UsedSockets: 2, Drops: intToU64(0)},
wantErr: false,
},
{
Expand Down

0 comments on commit 00b084c

Please sign in to comment.