第 4 章 Useful SystemTap Scripts
			This chapter enumerates several SystemTap scripts you can use to monitor and investigate different subsystems. All of these scripts are available in the 
/usr/share/systemtap/testsuite/systemtap.examples/ directory once you install the systemtap-testsuite package.
		4.1. Network
复制链接链接已复制到粘贴板!
				The following sections showcase scripts that trace network-related functions and build a profile of network activity.
			
4.1.1. Network Profiling
复制链接链接已复制到粘贴板!
					This section describes how to profile network activity. 例 4.1 “nettop.stp” provides a glimpse into how much network traffic each process is generating on a machine.
				
例 4.1. nettop.stp
					Note that the 
print_activity() function uses the following expressions:
				n_xmit ? @sum(ifxmit[pid, dev, exec, uid])/1024 : 0 n_recv ? @sum(ifrecv[pid, dev, exec, uid])/1024 : 0
n_xmit ? @sum(ifxmit[pid, dev, exec, uid])/1024 : 0
n_recv ? @sum(ifrecv[pid, dev, exec, uid])/1024 : 0
					These expressions are 
if or else conditionals. The second statement is simply a more concise way of writing the following pseudo code:
				if n_recv != 0 then @sum(ifrecv[pid, dev, exec, uid])/1024 else 0
if n_recv != 0 then
  @sum(ifrecv[pid, dev, exec, uid])/1024
else
  0
					例 4.1 “nettop.stp” 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- 0refers to the root user.
- DEV— which ethernet device the process used to send or receive data (for example, eth0, eth1)
- XMIT_PK— number of packets transmitted by the process
- RECV_PK— number of packets received by the process
- XMIT_KB— amount of data sent by the process, in kilobytes
- RECV_KB— amount of data received by the service, in kilobytes
					例 4.1 “nettop.stp” provides network profile sampling every 5 seconds. You can change this setting by editing 
probe timer.ms(5000) accordingly. 例 4.2 “例 4.1 “nettop.stp” Sample Output” contains an excerpt of the output from 例 4.1 “nettop.stp” over a 20-second period:
				例 4.2. 例 4.1 “nettop.stp” Sample Output
4.1.2. Tracing Functions Called in Network Socket Code
复制链接链接已复制到粘贴板!
					This section describes how to trace functions called from the kernel's 
net/socket.c file. This task helps you identify, in finer detail, how each process interacts with the network at the kernel level.
				例 4.3. socket-trace.stp
					例 4.3 “socket-trace.stp” is identical to 例 3.6 “thread_indent.stp”, which was earlier used in SystemTap Functions to illustrate how 
thread_indent() works.
				例 4.4. 例 4.3 “socket-trace.stp” Sample Output
					例 4.4 “例 4.3 “socket-trace.stp” Sample Output” contains a 3-second excerpt of the output for 例 4.3 “socket-trace.stp”. For more information about the output of this script as provided by 
thread_indent(), see SystemTap Functions 例 3.6 “thread_indent.stp”.
				4.1.3. Monitoring Incoming TCP Connections
复制链接链接已复制到粘贴板!
					This section illustrates how to monitor incoming TCP connections. This task is useful in identifying any unauthorized, suspicious, or otherwise unwanted network access requests in real time.
				
例 4.5. tcp_connections.stp
					While 例 4.5 “tcp_connections.stp” is running, it will print out the following information about any incoming TCP connections accepted by the system in real time:
				
- CurrentUID
- CMD- the command accepting the connection
- PIDof the command
- Port used by the connection
- IP address from which the TCP connection originated
例 4.6. 例 4.5 “tcp_connections.stp” Sample Output
UID CMD PID PORT IP_SOURCE 0 sshd 3165 22 10.64.0.227 0 sshd 3165 22 10.64.0.227
UID            CMD    PID   PORT        IP_SOURCE
0             sshd   3165     22      10.64.0.227
0             sshd   3165     22      10.64.0.2274.1.4. Monitoring Network Packets Drops in Kernel
复制链接链接已复制到粘贴板!
					The network stack in Linux can discard packets for various reasons. Some Linux kernels include a tracepoint, 
kernel.trace("kfree_skb"), which easily tracks where packets are discarded. 例 4.7 “dropwatch.stp” uses kernel.trace("kfree_skb") to trace packet discards; the script summarizes which locations discard packets every five-second interval.
				例 4.7. dropwatch.stp
					The 
kernel.trace("kfree_skb") traces which places in the kernel drop network packets. The kernel.trace("kfree_skb") has two arguments: a pointer to the buffer being freed ($skb) and the location in kernel code the buffer is being freed ($location).
				
					Running the dropwatch.stp script 15 seconds would result in output similar in 例 4.8 “例 4.7 “dropwatch.stp” Sample Output”. The output lists the number of misses for tracepoint address and the actual address.
				
例 4.8. 例 4.7 “dropwatch.stp” Sample Output
					To 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, allowing you to map the addresses in the output of 例 4.8 “例 4.7 “dropwatch.stp” Sample Output” to a specific function name. Given the following snippet of the /boot/System.map-$(uname -r) file, the address 0xffffffff8024cd0f maps to the function unix_stream_recvmsg and the address 0xffffffff8044b472 maps to the function arp_rcv: