Chapter 39. Monitoring network activity with SystemTap
You can use helpful example SystemTap scripts available in the /usr/share/systemtap/testsuite/systemtap.examples/
directory, upon installing the systemtap-testsuite
package, to monitor and investigate the network activity of your system.
39.1. Profiling network activity with SystemTap Copy linkLink copied to clipboard!
You can use the nettop.stp
example SystemTap script to profile network activity. The script tracks which processes are generating network traffic on the system, and provides the following information about each process:
- PID
- The ID of the listed process.
- UID
- User ID. A user ID of 0 refers to the root user.
- DEV
- Which ethernet device the process used to send or receive data (for example, eth0, eth1).
- XMIT_PK
- The number of packets transmitted by the process.
- RECV_PK
- The number of packets received by the process.
- XMIT_KB
- The amount of data sent by the process, in kilobytes.
- RECV_KB
- The amount of data received by the service, in kilobytes.
Prerequisites
- You have installed SystemTap as described in Installing SystemTap.
Procedure
Run the
nettop.stp
script:stap --example nettop.stp
# stap --example nettop.stp
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
nettop.stp
script provides network profile sampling every 5 seconds.Output of the
nettop.stp
script looks similar to the following:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
39.2. Tracing functions called in network socket code with SystemTap Copy linkLink copied to clipboard!
You can use the socket-trace.stp
example SystemTap script to trace functions called from the kernel’s net/socket.c file. This helps you identify, in finer detail, how each process interacts with the network at the kernel level.
Prerequisites
- You have installed SystemTap as described in Installing SystemTap.
Procedure
Run the
socket-trace.stp
script:stap --example socket-trace.stp
# stap --example socket-trace.stp
Copy to Clipboard Copied! Toggle word wrap Toggle overflow A 3-second excerpt of the output of the
socket-trace.stp
script looks similar to the following:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
39.3. Monitoring network packet drops with SystemTap Copy linkLink copied to clipboard!
The network stack in Linux can discard packets for various reasons. Some Linux kernels include a tracepoint, kernel.trace("kfree_skb")
, which tracks where packets are discarded.
The dropwatch.stp
SystemTap script uses kernel.trace("kfree_skb")
to trace packet discards; the script summarizes what locations discard packets in every 5-second interval.
Prerequisites
- You have installed SystemTap as described in Installing SystemTap.
Procedure
Run the
dropwatch.stp
script:stap --example dropwatch.stp
# stap --example dropwatch.stp
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Running the
dropwatch.stp
script for 15 seconds results in output similar to the following:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteTo make the location of packet drops more meaningful, see the
/boot/System.map-$(uname -r)
file. This file lists the starting addresses for each function, enabling you to map the addresses in the output of thedropwatch.stp
script to a specific function name. Given the following snippet of the/boot/System.map-$(uname -r)
file, the address0xffffffff8024cd0f
maps to the functionunix_stream_recvmsg
and the address0xffffffff8044b472
maps to the functionarp_rcv
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow