Debian Codegraph @

testsuite/systemtap.examples/network/socktop

raw
1 #!/bin/bash
2
3 # Socktop systemtap script
4 # Copyright (C) 2006 IBM Corp.
5 #
6 # This file is part of systemtap, and is free software. You can
7 # redistribute it and/or modify it under the terms of the GNU General
8 # Public License (GPL); either version 2, or (at your option) any
9 # later version.
10
11 ###
12 ### socktop - Combination shell/systemtap script to track reads and writes
13 ### on sockets by process. Can be filtered by process IDs and
14 ### names, protocols, protocol families, users and socket type.
15 ###
16
17 ITER_COUNT=-1 # number of iterations (no iteration limit default)
18 MOD_GEN=""; # Generate a instrumentation module option
19
20 # Filter options
21 F_PROTSTR=""; F_PROT=0 # Filter by protocol
22 F_FAMSTR=""; F_FAM=0 # Filter by protocol family
23 F_TYPESTR=""; F_TYPE=0 # Filter by socket type
24 F_PIDSTR=""; F_PID=0 # Filter by process ID
25 F_NAMESTR=""; F_NAME=0 # Filter by process name
26 F_UIDSTR=""; F_UID=0 # Filter by user
27 FILTER=0 # Any filters specified?
28
29 # Print options
30 P_INTERVAL=5 # default interval between output
31 P_DEVICES=0 # default is don't display network device traffic
32 P_NUMTOP=10 # default number of processes and network devices to print
33
34 DELIM=","
35
36 function usage {
37 echo "USAGE: socktop [-d] [-i interval] [-N num] [-P protocol]... [-f family]..."
38 echo " [-t stype]... [-n pname]... [-p pid]... [-u username]... [-h]"
39 echo " -d # print network device traffic (default: off)"
40 echo " -i interval # interval in seconds between printing (default: $P_INTERVAL)"
41 echo " -N num # number of top processes and devices to print (default: $P_NUMTOP)"
42 echo " -f family # this protocol family only (default: all)"
43 echo " -P protocol # this protocol only (default: all)"
44 echo " -t stype # this socket type only (default: all)"
45 echo " -n pname # this process name only (default: all)"
46 echo " -p pid # this process ID only (default: all)"
47 echo " -u username # this user only (default: all)"
48 echo " -c count # number of iteration"
49 echo " -D # dry run (generate instrumentation but do not run)"
50 echo " -h # print this help text"
51 echo ""
52 echo "Protocol Families:"
53 echo " LOCAL, INET, INET6, IPX, NETLINK, X25, AX25, ATMPVC, APPLETALK, PACKET"
54 echo ""
55 echo "Protocols:"
56 echo " TCP, UDP, SCTP, IP, FC, ... (see /etc/protocols for complete list)"
57 echo ""
58 echo "Socket Types:"
59 echo " STREAM, DGRAM, RAW, RDM, SEQPACKET, DCCP, PACKET"
60 }
61
62 # Process options
63 while getopts df:i:n:N:P:p:t:u:c:Dh option; do
64 case $option in
65 d) P_DEVICES=1 ;;
66 i) P_INTERVAL=$OPTARG ;;
67 N) P_NUMTOP=$OPTARG ;;
68 f) let "F_FAM++"
69 F_FAMSTR=$OPTARG$DELIM$F_FAMSTR ;;
70 n) let "F_NAME++"
71 F_NAMESTR=$OPTARG$DELIM$F_NAMESTR ;;
72 p) let "F_PID++"
73 F_PIDSTR=$OPTARG$DELIM$F_PIDSTR ;;
74 P) let "F_PROT++"
75 F_PROTSTR=$OPTARG$DELIM$F_PROTSTR ;;
76 t) let "F_TYPE++"
77 F_TYPESTR=$OPTARG$DELIM$F_TYPESTR ;;
78 u) uid=`awk -F: '$1 == name {print $3}' name=$OPTARG /etc/passwd`
79 if [[ $uid != "" ]]; then
80 let "F_UID++"
81 F_UIDSTR=$uid$DELIM$F_UIDSTR
82 else
83 echo "ERROR: Unknown user:" $OPTARG
84 let "ERROR++"
85 fi ;;
86 c) ITER_COUNT=$OPTARG ;;
87 D) MOD_GEN="-p4" ;;
88 h|?|*) usage
89 exit 1 ;;
90 esac
91 done
92
93 if [[ $ERROR > 0 ]]; then
94 exit 1
95 fi
96
97 if [[ $F_FAM > 0 || $F_NAME > 0 || $F_PID > 0 ||
98 $F_PROT > 0 || $F_TYPE > 0 || $F_UID > 0 ]]; then
99 FILTER=1
100 fi
101
102 #
103 # Pass a timezone adjustment value to the stap script
104 #
105 TZ=`date "+%z"`
106 TZ_SIGN=`echo $TZ | cut -c1`
107 TZ_HOURS=`echo $TZ | cut -c2-3`
108 TZ_MINS=`echo $TZ | cut -c4-5`
109 TZ_ADJUST=$TZ_SIGN$((10#$TZ_HOURS*60*60+10#$TZ_MINS*60))
110
111 #
112 # Start the systemtap script
113 #
114 stap $MOD_GEN -w -e '
115 global iter
116 global execname, user, if_tx, if_rx, if_dev
117 global sk_tx, sk_rx, sk_pid
118 global f_name_str, f_pid_str, f_prot_str, f_fam_str, f_type_str, f_uid_str
119 global f_name, f_pid, f_prot, f_fam, f_type, f_uid
120
121 probe never {
122 #workaround to avoid outputing filtering strings
123 log(f_name_str);
124 log(f_pid_str);
125 log(f_prot_str);
126 log(f_fam_str);
127 log(f_type_str);
128 log(f_uid_str);
129
130 #workaround to make sure that systemtap gets correct types for arrays
131 if_rx["junk"] <<< 0
132 if_tx["junk"] <<< 0
133 if_dev["junk"] ++
134 delete if_rx
135 delete if_tx
136 delete if_dev
137 }
138
139 probe begin
140 {
141 # set number of iterations
142 iter = '$ITER_COUNT'
143
144 # If no filters specified, skip filter processing
145 if ('$FILTER' == 0) next
146
147 f_name_str = "'$F_NAMESTR'"
148 f_pid_str = "'$F_PIDSTR'"
149 f_prot_str = "'$F_PROTSTR'"
150 f_fam_str = "'$F_FAMSTR'"
151 f_type_str = "'$F_TYPESTR'"
152 f_uid_str = "'$F_UIDSTR'"
153
154 delim = "'$DELIM'"
155 error = 0
156
157 # Protocols
158 if ('$F_PROT') {
159 prot = tokenize(f_prot_str, delim)
160 while (prot != "") {
161 p = sock_prot_str2num(prot)
162 if (p < 0) {
163 printf("ERROR: Unknown protocol: %s\n", prot)
164 error++
165 } else
166 f_prot[p] = 1
167 prot = tokenize("", delim)
168 }
169 }
170
171 # Protocol families
172 if ('$F_FAM') {
173 fam = tokenize(f_fam_str, delim)
174 while (fam != "") {
175 f = sock_fam_str2num(fam)
176 if (f < 0) {
177 printf("ERROR: Unknown protocol family: %s\n", fam)
178 error++
179 } else
180 f_fam[f] = 1
181 fam = tokenize("", delim)
182 }
183 }
184
185 # Process names
186 if ('$F_NAME') {
187 pname = tokenize(f_name_str, delim)
188 while (pname != "") {
189 f_name[pname] = 1
190 pname = tokenize("", delim)
191 }
192 }
193
194 # Process IDs
195 if ('$F_PID') {
196 pid = tokenize(f_pid_str, delim)
197 while (pid != "") {
198 f_pid[strtol(pid, 10)] = 1
199 pid = tokenize("", delim)
200 }
201 }
202
203 # Socket types
204 if ('$F_TYPE') {
205 stype = tokenize(f_type_str, delim)
206 while (stype != "") {
207 t = sock_type_str2num(stype)
208 if (t < 0) {
209 printf("ERROR: Unknown socket type: %s\n", stype)
210 error++
211 } else
212 f_type[t] = 1
213 stype = tokenize("", delim)
214 }
215 }
216
217 # User IDs
218 if ('$F_UID') {
219 uid = tokenize(f_uid_str, delim)
220 while (uid != "") {
221 f_uid[strtol(uid, 10)] = 1
222 uid = tokenize("", delim)
223 }
224 }
225
226 if (error) exit()
227 }
228
229 probe netdev.transmit
230 {
231 if ('$P_DEVICES') {
232 if_tx[dev_name] <<< length
233 if_dev[dev_name] ++
234 }
235 }
236
237 probe netdev.receive
238 {
239 if ('$P_DEVICES') {
240 if_rx[dev_name] <<< length
241 if_dev[dev_name] ++
242 }
243 }
244
245 probe socket.send
246 {
247 if (!success) next
248
249 # Check filters
250 if ('$FILTER') {
251 if ('$F_PROT' && !(protocol in f_prot)) next
252 if ('$F_FAM' && !(family in f_fam)) next
253 if ('$F_PID' && !(pid() in f_pid)) next
254 if ('$F_NAME' && !(execname() in f_name)) next
255 if ('$F_UID' && !(uid() in f_uid)) next
256 if ('$F_TYPE' && !(type in f_type)) next
257 }
258
259 execname[pid()] = execname()
260 user[pid()] = uid()
261 sk_tx[pid(), protocol, family] <<< size
262 sk_pid[pid(), protocol, family] += size
263 }
264
265 probe socket.receive
266 {
267 if (!success) next
268
269 # Check filters
270 if ('$FILTER') {
271 if ('$F_PROT' && !(protocol in f_prot)) next
272 if ('$F_FAM' && !(family in f_fam)) next
273 if ('$F_PID' && !(pid() in f_pid)) next
274 if ('$F_NAME' && !(execname() in f_name)) next
275 if ('$F_UID' && !(uid() in f_uid)) next
276 if ('$F_TYPE' && !(type in f_type)) next
277 }
278
279 execname[pid()] = execname()
280 user[pid()] = uid()
281 sk_rx[pid(), protocol, family] <<< size
282 sk_pid[pid(), protocol, family] += size
283 }
284
285 function print_activity()
286 {
287 # Print top processes
288 max = '$P_NUMTOP'
289 time = gettimeofday_s() + '$TZ_ADJUST'
290
291 printf("======================= %s ========================\n", ctime(time))
292 printf("------------------------------- PROCESSES -------------------------------\n")
293 printf("%-5s %-5s %7s %7s %7s %7s %-4s %-8s %-15s\n",
294 "PID", "UID", "#SEND", "#RECV", "SEND_KB",
295 "RECV_KB", "PROT", "FAMILY", "COMMAND")
296 foreach ([pid, prot, fam] in sk_pid- limit max) {
297 n_sk_tx = @count(sk_tx[pid, prot, fam])
298 n_sk_rx = @count(sk_rx[pid, prot, fam])
299 printf("%-5d %-5d %7d %7d %7d %7d %-4s %-8s %-15s\n",
300 pid, user[pid], n_sk_tx, n_sk_rx,
301 n_sk_tx ? @sum(sk_tx[pid, prot, fam])/1024 : 0,
302 n_sk_rx ? @sum(sk_rx[pid, prot, fam])/1024 : 0,
303 sock_prot_num2str(prot), sock_fam_num2str(fam),
304 execname[pid])
305 }
306
307 # Print top network devices
308 if ('$P_DEVICES') {
309 max = '$P_NUMTOP'
310 printf("-------------------------------- DEVICES --------------------------------\n")
311 printf("%-7s %13s %13s %15s %15s\n",
312 "DEV", "#XMIT", "#RECV", "XMIT_KB", "RECV_KB")
313 foreach ([dev] in if_dev- limit max) {
314 n_if_tx = @count(if_tx[dev])
315 n_if_rx = @count(if_rx[dev])
316 printf("%-7s %13d %13d %15d %15d\n", dev, n_if_tx, n_if_rx,
317 n_if_tx ? @sum(if_tx[dev])/1024 : 0,
318 n_if_rx ? @sum(if_rx[dev])/1024 : 0)
319 }
320 }
321
322 printf("=========================================================================\n\n")
323
324 delete execname
325 delete user
326 delete sk_tx
327 delete sk_rx
328 delete sk_pid
329 delete if_tx
330 delete if_rx
331 delete if_dev
332 }
333
334
335 probe timer.s('$P_INTERVAL')
336 {
337 print_activity();
338 --iter;
339 if (iter == 0) exit();
340 }
341 '
342 saved_status=$?
343
344 # Cleanup
345 test -n "$MOD_GEN" && rm -f ${MOD_NAME}.ko
346
347 # Exit with the status of 'stap', not the cleanup commands.
348 exit $saved_status