switch_traffic.in 11.3 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2008-2017 University of Utah and the Flux Group.
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
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Data::Dumper;
use bigint;

#
# Report network traffic crossing trunk links
#
sub usage()
{
    print STDERR "Usage: switch_traffic [-rs] [-i seconds] [switch[:switch] ...]\n";
    print STDERR "Reports traffic across trunk links and interconnects\n";
    print STDERR "-h          This message\n";
#   print STDERR "-r          Show only traffic received by each switch\n";
#   print STDERR "-s          Show only traffic sent by each node\n";
40
    print STDERR "-l          No sorting\n";
41 42 43 44 45 46 47 48 49 50 51 52
    print STDERR "-i seconds  Show stats over a <seconds>-period interval\n";
#   print STDERR "-C          Show control net traffic\n";
    exit(-1);
}

# Protos.
sub Gather($);
sub Aggregate($$);
sub Display($);
sub fatal($);
sub diffResults($$);

53
my $optlist  = "Ci:rsdl";
54 55 56 57 58
my $debug    = 0;
my $interval = 0;
my $send     = 1;
my $recv     = 1;
my $docnet   = 0;
59
my $nosort   = 0;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

#
# Configure variables
#
my $TB          = "@prefix@";
my $PORTSTATS	= "$TB/bin/portstats";
my $MAINSITE    = @TBMAINSITE@;

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use Port;
use Node;
use Interface;

# XXX Needs to be fixed. 
78
use lib '@prefix@/lib/snmpit';
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
use snmpit_lib;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Locals
#
my %trunks        = ();
my %alltrunks     = getTrunks();
my %connects      = ();
my $rawresults    = {};
my $results       = {};
my @interconnects = ();

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"h"})) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"r"})) {
    $recv = 1;
    $send = 0;
}
if (defined($options{"s"})) {
    $recv = 0;
    $send = 1;
}
if (defined($options{"C"})) {
    $docnet = 1;
}
127 128 129
if (defined($options{"l"})) {
    $nosort = 1;
}
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
if (defined($options{"i"})) {
    $interval = int($options{"i"});
    if ($interval < 0) {
	die("Bogus interval $interval\n");
    }
}

if (@ARGV) {
    #
    # Go through and confirm that each arg is a switch. We could make this
    # more complicated, allowing sw1:sw2,sw3 syntax, but I am not sure that
    # useful (yet). 
    #
    foreach my $arg (@ARGV) {
	my ($sw1,$sw2) = split(":", $arg);

	my $switch1 = Node->Lookup($sw1);
	if (!defined($switch1)) {
	    fatal("No such node $sw1");
	}
	# We want to support interconnects, which are "fake" nodes.
	fatal("$sw1 is not a switch or an interconnect!")
	    if (! ($switch1->isswitch() || $switch1->isfakenode()));

	# Confirm a trunk if a switch
	fatal("$sw1 has no trunk links!")
	    if ($switch1->isswitch() && !exists($alltrunks{$sw1}));

	if (defined($sw2)) {
	    my $switch2 = Node->Lookup($sw2);
	    if (!defined($switch2)) {
		fatal("No such node $sw2");
	    }
	    # While sw1 can be a fake node, sw2 *must* be a switch.
	    fatal("$sw2 is not a switch!")
		if (! ($switch2->isswitch()));

	    if ($switch1->isswitch()) {
		# Confirm a trunk since both nodes are switches
		fatal("$arg are not connected by a trunk link!")
		    if (! exists($alltrunks{$sw1}->{$sw2}));
		
		$trunks{$sw1}->{$sw2} = $alltrunks{$sw1}->{$sw2};
	    }
	    else {
		# An interconnect.
		push(@interconnects, $sw1);
	    }
	}
	else {
	    if ($switch1->isswitch()) {
		#
		# Find all the trunks we have recorded in the DB that are
		# connected to this switch
		#
		foreach $sw2 (keys(%{ $alltrunks{$sw1} })) {
		    $trunks{$sw1}->{$sw2} = $alltrunks{$sw1}->{$sw2};
		}
	    }
	    else {
		# An interconnect, see below.
		push(@interconnects, $switch1);
	    }
	}
    }
}
else {
    foreach my $sw1 (keys(%alltrunks)) {
	foreach my $sw2 (keys(%{ $alltrunks{$sw1} })) {
	    $trunks{$sw1}->{$sw2} = $alltrunks{$sw1}->{$sw2};
	}
    }

    #
    # We also want the interconnects (uplinks) out of the fabric. These
    # are now of type='interconnect', but in Utah they can also be other
    # types. I'm going to hardwire the list for now. We put these into
    # a different list since we do not want to aggregate them like trunks.
    #
    @interconnects = Node->LookupByType("interconnect");
210
    if (0 && $MAINSITE) {
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	foreach my $node_id ("of-losa", "of-atla", "ion") {
	    my $node = Node->Lookup($node_id);
	    push(@interconnects, $node)
		if (defined($node));
	}
    }
}
#
# Do the interconnects we found.
#
foreach my $node (@interconnects) {
    my @interfaces;
    if ($node->AllInterfaces(\@interfaces)) {
	fatal("Could not get interfaces for $node");
    }
    foreach my $iface (@interfaces) {
	next
	    if ($iface->current_speed() == 0);
229 230 231

	next
	    if (!$iface->wiredup() || $iface->wire_type() eq "Unused");
232 233 234 235 236 237
		
	# To be consistent use a Port object since getTrunks() does too.
	my $port = Port->LookupByIface($node->node_id, $iface->iface());
	if (!defined($port)) {
	    fatal("Could not get Port for $iface");
	}
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
	$connects{$node->node_id()}->{$port->switch_node_id()} = $port;
    }
}

Gather($rawresults);
Aggregate($rawresults, $results);

if (!$interval) {
    Display($results);
    exit(0);
}

#
# Wait for the interval and then get another set of numbers.
#
my $post_results = {};
sleep($interval);
Gather($rawresults);
Aggregate($rawresults, $post_results);

#
# Subtract ... Modifies the post_results. 
#
diffResults($results, $post_results);
Display($post_results);

exit(0);

#
# Gather up numbers from portstats. These are raw numbers, the numbers have to
# be aggregated.
#
sub Gather($)
{
    my ($resref) = @_;
    my $portstr  = "";
275
    my $result   = "";
276 277 278 279 280 281

    foreach my $sw1 (keys(%trunks)) {
	foreach my $sw2 (keys(%{ $trunks{$sw1} })) {
	    my @ports = @{ $trunks{$sw1}->{$sw2} };

	    foreach my $port (@ports) {
282
		$portstr .= " ${sw1}:" . $port->switch_iface();
283 284 285 286 287 288 289 290 291 292
	    }
	}
    }
    #
    # Add in the interconnects to the list. We want to use use one call to
    # portstats. 
    #
    foreach my $id (keys(%connects)) {
	foreach my $switch (keys(%{ $connects{$id} })) {
	    my $port = $connects{$id}->{$switch};
293
	    my $str  = " ${switch}:" . $port->switch_iface();
294 295 296 297 298 299
	    $portstr .= $str;
	}
    }
    if ($debug) {
	print $portstr . "\n";
    }
300
    open(PS, "$PORTSTATS -i -r -s -c $portstr 2>&1 |") or
301 302
	fatal("Could not get portstats");
    while (<PS>) {
303 304 305 306 307 308 309 310 311 312 313
	$result .= $_;
    }
    close(PS);
    if ($?) {
	print STDERR $result;
	exit(-1);
    }
    while ($result =~ /^(.*)$/gm) {
	my $line = $1;
	
	if ($line !~ /^[-\w]+:\w+/) {
314 315 316 317
	    print $_
		if ($debug);
	    next;
	}
318
	chomp($line);
319
	
320
	my ($swport, @counts) = split(",", $line);
321 322 323 324 325 326 327 328 329 330
	if (@counts != 6) {
	    print STDERR "*** $swport: invalid portstats!?\n"; 
	    next;
	}

	#
	# XXX portstats always returns nodes in the format: pcXXX:N.N
	#
	my ($switch,$port);
	
331
	if ($swport =~ /^(.*):([\/\.\w]+)$/) {
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
	    $switch = $1;
	    $port   = $2;
	}
	else {
	    fatal("Cannot parse $swport");
	}
	# Implied ordering, bad.
	my $counts = {"inoctets"  => $counts[0],
		      "inupkts"   => $counts[1],
		      "innpkts"   => $counts[2],
		      "outoctets" => $counts[3],
		      "outupkts"  => $counts[4],
		      "outnpkts"  => $counts[5]};
	    
	$resref->{$switch}->{"$port"} = $counts;
    }
}

#
# Aggregate the results by adding up the individual counts on each set
# of ports in a trunk.
#
sub Aggregate($$)
{
    my ($results, $aggref) = @_;

    foreach my $sw1 (keys(%trunks)) {
	foreach my $sw2 (keys(%{ $trunks{$sw1} })) {
	    my @ports = @{ $trunks{$sw1}->{$sw2} };
	    my %sum = ();

	    foreach my $swport (@ports) {
364 365
		my $iface = $swport->switch_iface();
		my $counts = $results->{$sw1}->{$iface};
366 367 368 369 370 371 372 373

		foreach my $key (keys(%{$counts})) {
		    if (!exists($sum{$key})) {
			$sum{$key} = 0;
		    }
		    $sum{$key} += $counts->{$key};
		}
	    }
374 375 376
	    if (keys(%sum) > 0) {
		$aggref->{$sw1}->{$sw2} = \%sum;
	    }
377 378 379 380 381 382 383 384 385 386 387 388 389 390
	}
    }
    #
    # The interconnects are in a different place, and do not need to
    # be aggregated, but put them into the new results so that other
    # phases work okay.
    #
    foreach my $id (keys(%connects)) {
	foreach my $switch (keys(%{ $connects{$id} })) {
	    my $swport = $connects{$id}->{$switch};
	    #
	    # These ports objects refer to the node side, not the switch side,
	    # so we have to explicity get the switch side in index the array.
	    #
391
	    my $port = $swport->switch_iface();
392 393 394 395 396 397 398 399 400 401 402 403 404
	    my $counts = $results->{$switch}->{"$port"};
	    
	    $aggref->{$switch}->{$id} = $counts;
	}
    }
}

#
# Display. Simple.
#
sub Display($)
{
    my ($results) = @_;
405 406 407 408 409 410 411 412 413 414 415 416
    my @list  = ();
    my %shown = ();

    if ($nosort && @ARGV) {
	foreach my $arg (@ARGV) {
	    push(@list, $arg)
		if (exists($results->{$arg}));
	}
    }
    else {
	@list = sort {$a cmp $b} keys(%{$results});
    }
417 418 419 420 421 422

    printf("%-20s %12s %12s %10s %12s %12s %10s\n", "Trunk", "InOctets",
	   "InUpkts", "InNUpkts", "OutOctets", "OutUpkts", "OutNUpkts");
    print "-------------------------------------------------------------";
    print "---------------------------------\n";
	   
423
    foreach my $sw1 (@list) {
424
	foreach my $sw2 (keys(%{$results->{$sw1}})) {
425 426 427 428 429
	  again:
	    next
		if (exists($shown{"$sw2:$sw1"}));
	    $shown{"$sw2:$sw1"} = 1;
	    
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	    my $counts  = $results->{$sw1}->{$sw2};
	    my $swname1 = $sw1;
	    my $swname2 = $sw2;

	    # Utah thing.
	    $swname1 =~ s/procurve-pgeni/pg/;
	    $swname2 =~ s/procurve-pgeni/pg/;
	    # Other names to shorten;
	    $swname1 =~ s/interconnect-//;
	    $swname2 =~ s/interconnect-//;
	    
	    printf("%-20s ", "$swname1:$swname2");
	    if (!defined($counts)) {
		print "  *** No counts! ***\n";
		next;
	    }
	    printf("%12d %12d %10d %12d %12d %10d\n",
		   $counts->{"inoctets"},
		   $counts->{"inupkts"}, $counts->{"innpkts"},
		   $counts->{"outoctets"},
		   $counts->{"outupkts"}, $counts->{"outnpkts"});
451 452 453 454 455 456 457 458 459 460 461

	    #
	    # But we want to show the other direction right underneath
	    # it, cause that is way less confusing then showing later.
	    #
	    if (exists($results->{$sw2}) && exists($results->{$sw2}->{$sw1})) {
		my $tmp = $sw1;
		$sw1 = $sw2;
		$sw2 = $tmp;
		goto again;
	    }
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
	}
    }
}

#
# Subtract after-before packet numbers into after.
#
sub diffResults($$)
{
    my ($before, $after) = @_;

    foreach my $sw1 (keys(%{$after})) {
	foreach my $sw2 (keys(%{$after->{$sw1}})) {
	    my $acounts  = $after->{$sw1}->{$sw2};
	    my $bcounts  = $before->{$sw1}->{$sw2};

	    next
		if (! (defined($acounts) && defined($bcounts)));

	    foreach my $key (keys(%{$acounts})) {
		$acounts->{$key} -= $bcounts->{$key};
	    }
	}
    }
}

sub fatal($)
{
    my($mesg) = $_[0];

    die("*** $0\n".
	"    $mesg\n");
}