1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
|
#! /usr/bin/env stap
/*
* Copyright (C) 2009 IBM Corp.
* This file is part of systemtap, and is free software. You can
* redistribute it and/or modify it under the terms of the GNU General
* Public License (GPL); either version 2, or (at your option) any
* later version.
*
* Version 1.0 wilder@us.ibm.com 2009-07-06
*
* Name:
* tcpipstat.stp - Per-socket tcpip statistics collection facility.
* (netstat -s on steroids)
*
* Description:
* The purpose of tcpipstat is to collect and display network statistics
* related to individual TCP sockets or groups of sockets. The statistics
* that are collected are simmer to that of the command netstat -s, only
* sorted and grouped by individual sockets. The statistics collected are
* based on the SNMP TCP and IP MIBS. As the name implies this command
* collects data relating to TCP sockets, not UDP sockets. I plan to
* support a UDP tool in a later version. The tcpipstat tool uses
* systemtap to collect data from the linux kernel; see the stap command
* manual page for instructions and options available in systemtap. For
* more information on the systemtap project see:
* http://sourceware.org/systemtap/documentation.html
*
*
* Synopsis:
* tcpstat.stp [index=laddr|raddr|lport|rport|tuple]\
* [timeout=<N sec>]\
* [nozeros=1|0]\
* [filters...]
*
* index (optional) tcpipstat collects, sorts and displays network
* statistics into buckets. The buckets are indexed based on
* one of several index types. By default the index is
* constructed from all four elements of the socket tuple,
* providing a separate bucket for each socket. However, any
* individual element of the tuple can be used as the index.
* Valid values for index are laddr (local address), raddr
* (remote address) , lport, rport or tuple. For example if
* index=lport is specified network statistics will be collected
* and reported based on the value of the local port thus
* providing statistics related to each network service provided
* by the system.
*
* timeout (optional) When a timeout value (in seconds) is specified
* tcpstat will automatically terminate it's run at the end of
* the specified time and produce a report. When timeout is
* omitted the script will run until the user terminates it by
* typing a ^c.
*
* onclose (optional) In onclose mode (onclose=1) statistics are reported
* only when a socket closes. The generated report reflect
* statistics for the entire life on the socket, assuming
* tcpipstat was running at the time the socket was opened.
* The option index=tuple must be set for onclode to work
* (index=tuple is default).
*
* nozeros (optional) When nonzero=1 is specified statistics with
* a value of zero will not be printed.
*
* filters (optional) By default statistics are counted for every socket
* on the system. Address filters can be used to limit what
* sockets data is collected for. All filters are
* pass-through filters. Multiple filters may be given separated
* by a space. When multiple filters are given an event only
* needs to be accepted by one filter to be counted. A wild-card
* (*) value can be used for any component of the filter.
*
* The format of a filter is:
* <local-address>:<local-port>-<remote address>:<remote-port>
*
* Addresses are specified as ipv4 dot notation address. Ports
* are specified as decimal numbers. A "*" character can be used
* in any field to indicate a wild-card value. If no filters are
* given the following wild-card filter is automatically used:
* *.*.*.*:*-*.*.*.*:*
*
* Known Bugs:
* Not all MIB values are currently supported.
* Ipv6 in not yet supported.
*
* Examples:
* Here are some ways that tcpipstat could be used.
*
* List per-socket statistics for every active socket for the
* next 60 seconds.
* $ tcpipstat timeout=60
*
* Who is talking to my webserver?
* $ tcpipstat.stp index=raddr *.*.*.*:80-*.*.*.*:*
*
* What services (on this system) is host 192.168.1.103 using
* $ tcpipstat.stp index=rport *.*.*.*:*-192.168.1.103:*
*
* What hosts in the 9.0.0.0 network is this system communicating
* with?
* $ tcpipstat.stp index=raddr *.*.*.*:*-9.*.*.*:*
*
* What ports are people connecting to? or which services are busy?
* $ tcpipstat.stp index=lport
*
* Show network statistics related to a single run of netperf.
* $ stap -c netperf -H <server-ip>\
* tcpipstat.stp *.*.*.*:*-<server-ip>:*
*
*/
global filter;
global number_of_filters;
global key_list;
global lastkey;
// Command arguments and default values
global index= "tuple";
global timeout = -1;
global nozeros = 1;
global onclose = 0;
probe begin {
number_of_filters=process_cmdline()
if ( index != "laddr" &&
index != "raddr" &&
index != "lport" &&
index != "rport" &&
index != "tuple" ) usage("Invalid index value!");
if ( onclose && (index!="tuple") )
usage("onclose must be used with index=tuple!");
/* The user did not supply a filter, build a wild-card filter */
if ( number_of_filters == 0 ){
number_of_filters++;
j=6;
filter[j] = ipv4_pton("*.*.*.*",0)
filter[j+1] = ipv4_pton("*.*.*.*",1)
filter[j+2] = ipv4_portton("*")
filter[j+3] = ipv4_pton("*.*.*.*",0)
filter[j+4] = ipv4_pton("*.*.*.*",1)
filter[j+5] = ipv4_portton("*")
}
if ( number_of_filters < 0 ){
usage("Bad filter format!");
}
printf("Indexing collected stats using %s values\n",index);
for (i=1; i <= number_of_filters; i++){
print_filter(i);
}
}
/* All command line arguments other than the filters are processed
* first and must be placed on the command line prior to any filters.
*/
function process_cmdline:long ()
{
filter_number=0;
for (i=1; i <= argc; i++){
argument= tokenize(argv[i], "=")
if ( argument == "index" ){
argv[i]=""
index=tokenize(argv[i], "=")
continue;
}
if ( argument == "timeout" ){
argv[i]=""
timeout=strtol(tokenize(argv[i], "="),10)
continue;
}
if ( argument == "nozeros" ){
argv[i]=""
nozeros=strtol(tokenize(argv[i], "="),10)
continue;
}
if ( argument == "onclose" ){
argv[i]=""
onclose=strtol(tokenize(argv[i], "="),10)
continue;
}
/* Example: adding more option flags
if ( argument == "stuff" ){
argv[i]=""
stuff=tokenize(argv[i], "=")
continue;
}
*/
/* Anything on the command line after this point must
* be a filter.
*/
local = tokenize(argv[i], "-")
argv[i] = ""
remote = tokenize(argv[i], "-")
local_addr = tokenize(local, ":")
local=""
local_port = tokenize(local, ":")
remote_addr = tokenize(remote, ":")
remote=""
remote_port = tokenize(remote, ":")
/* stap bug */
if ( remote_port == "fobar") i=i;
++filter_number;
j=filter_number*6;
filter[j] = ipv4_pton(local_addr,0) // Local address
filter[j+1] = ipv4_pton(local_addr,1) // Local address mask
filter[j+2] = ipv4_portton(local_port) // Local port
filter[j+3] = ipv4_pton(remote_addr,0) // Remote address
filter[j+4] = ipv4_pton(remote_addr,1) // Remote address mask
filter[j+5] = ipv4_portton(remote_port) // Remote port
if (filter[j]< -1 ||
filter[j+1] < -1 ||
filter[j+2] < -1 ||
filter[j+3] < -1 ||
filter[j+4] < -1 ||
filter[j+5] < -1 ) return -1;
}
return filter_number;
}
/*
* Convert an ascii integer values between 0 and 65534 to a u16 port number.
* "*" are treated as wildcards and will be converted to 0xffff (65535).
*/
function ipv4_portton:long (port:string)
{
if ( port == "*" ) port="65535";
pport=strtol(port,10);
if ( pport > 0xffff ){
printf("Bad port number %s\n",port)
return -22;
}
return pport
}
/*
* Convert an ipv4 dot notation address into longs.
* Supports "*" in any field treating it as a wildcard by making the byte=0.
* If make_mask is set, it creates a mask based on the "*" fields. All non='*'
* bytes are set to 0xff all * fields are set to 0x0;.
*/
function ipv4_pton:long (addr:string, make_mask:long)
{
i=32;
ip=0;
ips=addr;
while(strlen(byte = tokenize(ips, ".")) != 0) {
i-=8;
ips="";
if ( byte == "*" ){
byte = "0"
} else {
if ( make_mask ) byte = "255";
}
j=strtol(byte,10);
if ( j > 255 ){
printf("bad address %s\n",addr)
return -22;
}
ip=ip+(j<<i) // left shift the byte into the address
}
if ( i != 0 ){
printf("bad address %s\n",addr)
return -22;
}
return ip;
}
function usage (msg:string)
{
printf("\nUsage:\n");
printf("\ttcpipstat.stp\t[index=laddr|raddr|lport|rport|tuple]\n");
printf("\t\t\t[timeout=<sec>] [nozeros=0|1] [onclose=0|1]\n");
printf("\t\t\t[filter ......]\n\n");
printf("\tfilter format:\n");
printf("\t<local ip-address>:<local-port>-<remote ip-address>:<remote-port>\n\n");
error(msg);
}
/* Print filter number n. This is helpful for debugging */
function print_filter (n:long)
{
j=n*6;
printf("Processed filter #%d = %s[0x%x]:%d --> %s[0x%x]:%d \n",
n,ip_ntop(htonl(filter[j])), filter[j+1], filter[j+2],
ip_ntop(htonl(filter[j+3])), filter[j+4], filter[j+5]);
}
/*
* Returns a unique value (stored in the global key_list) based on the socket
* address tuple and the global collection index value. A new value is created
* if one does not already exist.
*/
function build_key:long (laddr:long, raddr:long, lport:long, rport:long)
{
if ( index == "laddr" ) raddr = lport = rport = 0
if ( index == "raddr" ) laddr = lport = rport = 0
if ( index == "lport" ) laddr = raddr = rport = 0
if ( index == "rport" ) laddr = raddr = lport = 0
if ( key_list[laddr, raddr, lport, rport] )
return key_list[laddr, raddr, lport, rport]
else
return key_list[laddr, raddr, lport, rport] = ++lastkey
}
/*
* This is where the real work of the probe filtering is done.
* Important: this function is run for every probe hit so
* keep it small and fast!
*
* If the probe passes through the filters a "key" value is
* returned otherwise it returns zero.
*/
function filter_key:long (laddr:long, raddr:long, lport:long, rport:long)
{
for (i=1; i <= number_of_filters; i++){
j=i*6;
/*
printf("\n %s\n",pp())
print_filter(i);
printf("local=0x%x:0x%x remote=0x%x:0x%x ",
laddr, lport, raddr, rport);
*/
// Local filter
local_filter=remote_filter=0;
if ( (laddr&filter[j+1]) == filter[j] ) {
if ( (filter[j+2] == 0xffff) || (lport == filter[j+2]))
local_filter = 1;
}
// Remote filter
if ( (raddr&filter[j+4]) == filter[j+3] ) {
if ( (filter[j+5] == 0xffff) || (rport == filter[j+5]))
remote_filter = 1;
}
// printf("local=%d remote=%d ",local_filter,remote_filter);
if(local_filter && remote_filter){
// key = build_key(laddr, raddr, lport, rport);
// printf("|Collected: key=%d\n",key);
// return key;
return build_key(laddr, raddr, lport, rport);
}
}
// printf("\n");
return 0;
}
/* This function is called by every ipmib probe handler.
* Returns a "key" value to be used as an index into the collection arrays.
*/
function ipmib_filter_key:long (skb:long, op:long, SourceIsLocal:long)
{
if ( !skb ) return 0;
// We only care about events with protocol IPPROTO_TCP(=6)
if( !(ipmib_get_proto(skb) == 6) ) return 0;
raddr = ipmib_remote_addr(skb, SourceIsLocal);
laddr = ipmib_local_addr(skb, SourceIsLocal);
rport = ipmib_tcp_remote_port(skb, SourceIsLocal);
lport = ipmib_tcp_local_port(skb, SourceIsLocal);
return filter_key(laddr, raddr, lport, rport);
}
/* This function is called by every tcpmib probe handler.
* Returns a "key" value to be used as an index into the collection arrays.
*/
function tcpmib_filter_key:long (sk:long, op:long)
{
if ( !sk ) return 0;
laddr = tcpmib_local_addr(sk);
raddr = tcpmib_remote_addr(sk);
lport = tcpmib_local_port(sk);
rport = tcpmib_remote_port(sk);
return filter_key(laddr, raddr, lport, rport);
}
/* This function is called by every linuxmib probe handler.
* Returns a "key" value to be used as an index into the collection arrays.
* For now this is just the same as the tcpmib_filter_key.
*/
function linuxmib_filter_key:long (sk:long, op:long)
{
return tcpmib_filter_key(sk,op);
}
function print_sockmib (key:long)
{
printf("Socket:\n");
if (SockSendbytes[key]||!nozeros)
printf("\tBytes Sent = %d\n", SockSendbytes[key]);
if (SockSendmsg[key]||!nozeros)
printf("\tMessages sent = %d\n", SockSendmsg[key]);
if (SockRecvbytes[key]||!nozeros)
printf("\tBytes Received = %d\n", SockRecvbytes[key]);
if (SockRecvmsg[key]||!nozeros)
printf("\tMessages Received = %d\n", SockRecvmsg[key]);
}
/*
* Prints the collected values for the IP mib.
*/
function print_ipmib (key:long)
{
printf("Ip:\n");
if (InReceives[key]||!nozeros)
printf("\tInReceives = %d\n", InReceives[key]);
if (OutRequests[key]||!nozeros)
printf("\tOutRequests = %d\n", OutRequests[key]);
if (ReasmTimeout[key]||!nozeros)
printf("\tReasmTimeout = %d\n", ReasmTimeout[key]);
if (ReasmReqds[key]||!nozeros)
printf("\tReasmReqds = %d\n", ReasmReqds[key]);
if (FragOKs[key]||!nozeros)
printf("\tFragOKs = %d\n", FragOKs[key]);
if (FragFails[key]||!nozeros)
printf("\tFragFails = %d\n", FragFails[key]);
}
/*
* Prints the collected values for the TCP mib.
*/
function print_tcpmib (key:long)
{
printf("Tcp:\n");
if (ActiveOpens[key]||!nozeros)
printf("\tActiveOpens = %d\n", ActiveOpens[key]);
%( kernel_v > "2.6.21" %?
if (AttemptFails[key]||!nozeros)
printf("\tAttemptFails = %d\n", AttemptFails[key]);
%)
%( kernel_v > "2.6.24" %?
if (CurrEstab[key]||!nozeros)
printf("\tCurrEstab = %d\n", CurrEstab[key]);
%)
%( kernel_v > "2.6.24" %?
if (EstabResets[key]||!nozeros)
printf("\tEstabResets = %d\n", EstabResets[key]);
%)
// if (InErrs[key]||!nozeros)
// printf("\tInErrs = %d\n", InErrs[key]);
if (InSegs[key]||!nozeros)
printf("\tInSegs = %d\n", InSegs[key]);
if (OutRsts[key]||!nozeros)
printf("\tOutRsts = %d\n", OutRsts[key]);
if (OutSegs[key]||!nozeros)
printf("\tOutSegs = %d\n", OutSegs[key]);
if (PassiveOpens[key]||!nozeros)
printf("\tPassiveOpens = %d\n", PassiveOpens[key]);
if (RetransSegs[key]||!nozeros)
printf("\tRetransSegs = %d\n", RetransSegs[key]);
}
/*
* Prints the collected values for the linux mib.
*/
function print_linuxmib (key:long)
{
printf("TcpExt:\n");
if (DelayedACKs[key]||!nozeros)
printf("\tdelayed acks sent = %d\n", DelayedACKs[key]);
if (ListenOverflows[key]||!nozeros)
printf("\ttimes the listen queue of a socket overflowed = %d\n", ListenOverflows[key]);
if (ListenDrops[key]||!nozeros)
printf("\tSYNs to LISTEN sockets ignored = %d\n", ListenDrops[key]);
if (TCPMemoryPressures[key]||!nozeros)
printf("\ttcp memory pressure = %d\n", TCPMemoryPressures[key]);
}
function report ()
{
number_of_keys=0
foreach ([laddr, raddr, lport, rport] in key_list) {
++number_of_keys;
_report(laddr, raddr, lport, rport);
}
if ( !number_of_keys )
printf("\nNo packets were accepted by the filters.\n");
}
function _report(laddr:long, raddr:long, lport:long, rport:long)
{
printf("\n-----------------------------------------\n");
if ( laddr && raddr && lport && rport ) // index=tuple
printf("%s:%d <-> %s:%d\n",
ip_ntop(htonl(laddr)), lport,
ip_ntop(htonl(raddr)), rport)
else {
if ( laddr )
printf("Local address: %s\n",ip_ntop(htonl(laddr)));
if ( raddr )
printf("Remote address: %s\n",ip_ntop(htonl(raddr)));
if ( lport )
printf("Local port: %d\n",lport);
if ( rport )
printf("Remote port: %d\n",rport);
}
key = key_list[laddr, raddr, lport, rport]
%( kernel_v > "2.6.24" %?
if ( onclose && (CurrEstab[key] < 0) )
printf("Stats were not collected for the entire socket life.\n")
%)
printf("-----------------------------------------\n");
print_sockmib(key)
print_ipmib(key)
print_tcpmib(key)
print_linuxmib(key)
printf("\n\n")
}
/* Terminates the run in timeout seconds, using global timeout value */
probe timer.s(1) {
if ( timeout == -1 ) next
if ( !timeout-- ) exit()
}
/* We are done, print a report and exit. */
probe end {
if ( !onclose )
report()
}
/* Enable the probes for the statictics we want to count.
*
* The impact of running this script on your system can
* be reduced by enabling only the probes corresponding to the
* statictics you are interested in. For example, if all you care about
* is counting the number of incoming connections that are established use:
* "probe tcpmib.PassiveOpens {}"
*/
/* Collect all tcpmib stats */
probe tcpmib.* {}
/* Collect all ip stats */
probe ipmib.* {}
/* Collect the extended linux stats */
probe linuxmib.* {}
/* This probe supports the onclose option. When a connection closes
* CurrEstab will return an op of -1, we use that condition to trigger
* the dump off all stats for this socket. This feature only makes sense
* when index=tuple.
*/
%( kernel_v > "2.6.24" %?
probe tcpmib.CurrEstab
{
if (!onclose) next
if (!key) next
if ( op != -1 ) next;
if ( !sk ) next;
laddr = tcpmib_local_addr(sk);
raddr = tcpmib_remote_addr(sk);
lport = tcpmib_local_port(sk);
rport = tcpmib_remote_port(sk);
_report(laddr, raddr, lport, rport)
delete key_list[laddr, raddr, lport, rport]
delete CurrEstab[key]
}
%)
/* SNMP has no counter for the number of bytes sent or received by TCP.
* The next two probes give us these raw byte counts.
*/
global SockSendbytes;
global SockSendmsg;
probe tcp.sendmsg.return {
sk = @defined(@entry($sk)) ? @entry($sk) : @entry($sock->sk);
op = size; // $return
if ( op <= 0 ) next;
if ( !sk ) next;
key = tcpmib_filter_key(sk, op);
if ( key ) {
SockSendbytes[key] += op;
++SockSendmsg[key];
}
}
global SockRecvbytes;
global SockRecvmsg;
probe tcp.recvmsg.return{
sk = @entry($sk)
op = size // $return
if ( op <= 0 ) next;
if ( !sk ) next;
key = tcpmib_filter_key(sk, op);
if ( key ) {
SockRecvbytes[key] += op;
++SockRecvmsg[key];
}
}
|