snmpit_lib.pm 28.2 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
#
3
# EMULAB-LGPL
4
# Copyright (c) 2000-2009 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5 6 7
# All rights reserved.
#

Mac Newbold's avatar
Mac Newbold committed
8
#
9
# Module of subroutines useful to snmpit and its modules
Mac Newbold's avatar
Mac Newbold committed
10 11 12 13 14 15
#

package snmpit_lib;

use Exporter;
@ISA = ("Exporter");
16
@EXPORT = qw( macport portnum Dev vlanmemb vlanid
17
		getTestSwitches getControlSwitches getSwitchesInStack
18
		getVlanPorts convertPortsFromIfaces convertPortFromIface
19
		getExperimentTrunks setVlanTag
20
		getExperimentVlans getDeviceNames getDeviceType
21 22
		getInterfaceSettings mapPortsToDevices getSwitchPrimaryStack
		getSwitchStacks
23 24
		getStackType getStackLeader
		getDeviceOptions getTrunks getTrunksFromSwitches
25
                getTrunkHash 
26
		getExperimentPorts snmpitGet snmpitGetWarn snmpitGetFatal
27
                getExperimentControlPorts
28
		snmpitSet snmpitSetWarn snmpitSetFatal 
29
                snmpitBulkwalk snmpitBulkwalkWarn snmpitBulkwalkFatal
30
	        setPortEnabled setPortTagged
31
		printVars tbsort );
Mac Newbold's avatar
Mac Newbold committed
32 33

use English;
34
use libdb;
35
use libtestbed;
36
use libtblog qw(tbdie tbwarn tbreport SEV_ERROR);
37 38
use Experiment;
use Lan;
39
use strict;
40 41 42
use SNMP;

my $TBOPS = libtestbed::TB_OPSEMAIL;
Mac Newbold's avatar
Mac Newbold committed
43 44 45

my $debug = 0;

46
my $DEFAULT_RETRIES = 10;
47

48 49
my $SNMPIT_GET = 0;
my $SNMPIT_SET = 1;
50
my $SNMPIT_BULKWALK = 2;
51

Mac Newbold's avatar
Mac Newbold committed
52
my %Devices=();
53
# Devices maps device names to device IPs
Mac Newbold's avatar
Mac Newbold committed
54 55

my %Interfaces=();
56
# Interfaces maps pcX:Y<==>MAC
Mac Newbold's avatar
Mac Newbold committed
57 58

my %Ports=();
59 60 61 62 63 64 65
# Ports maps pcX:Y<==>switch:port

my %vlanmembers=();
# vlanmembers maps id -> members

my %vlanids=();
# vlanids maps pid:eid <==> id
Mac Newbold's avatar
Mac Newbold committed
66

67 68
my $snmpitErrorString;

69 70 71
# Protos
sub getTrunkPath($$$$);

72
#
73
# Initialize the library
74 75 76 77 78
#
sub init($) {
    $debug = shift || $debug;
    &ReadTranslationTable;
    return 0;
Mac Newbold's avatar
Mac Newbold committed
79 80
}

81 82 83
#
# Map between interfaces and mac addresses
#
Mac Newbold's avatar
Mac Newbold committed
84
sub macport {
85 86
    my $val = shift || "";
    return $Interfaces{$val};
Mac Newbold's avatar
Mac Newbold committed
87 88
}

89 90 91
#
# Map between interfaces and port numbers
#
Mac Newbold's avatar
Mac Newbold committed
92
sub portnum {
93 94
    my $val = shift || "";
    return $Ports{$val};
Mac Newbold's avatar
Mac Newbold committed
95 96
}

97 98 99
#
# Map between interfaces and the devices they are attached to
#
Mac Newbold's avatar
Mac Newbold committed
100
sub Dev {
101 102
    my $val = shift || "";
    return $Devices{$val};
Mac Newbold's avatar
Mac Newbold committed
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
#
# This function fills in %Interfaces and %Ports
# They hold pcX:Y<==>MAC and pcX:Y<==>switch:port respectively
#
sub ReadTranslationTable {
    my $name="";
    my $mac="";
    my $switchport="";

    print "FILLING %Interfaces\n" if $debug;
    my $result = DBQueryFatal("select * from interfaces;");
    while ( @_ = $result->fetchrow_array()) {
	$name = "$_[0]:$_[1]";
	if ($_[2] != 1) {$name .=$_[2]; }
	$mac = "$_[3]";
	$Interfaces{$name} = $mac;
	$Interfaces{$mac} = $name;
	print "Interfaces: $mac <==> $name\n" if $debug > 1;
    }

    print "FILLING %Ports\n" if $debug;
    $result = DBQueryFatal("select node_id1,card1,port1,node_id2,card2,port2 ".
	    "from wires;");
128 129 130
    while ( my @row = $result->fetchrow_array()) {
        my ($node_id1, $card1, $port1, $node_id2, $card2, $port2) = @row;
	$name = "$node_id1:$card1";
131
	print "Name='$name'\t" if $debug > 2;
132 133
	print "Dev='$node_id2'\t" if $debug > 2;
	$switchport = "$node_id2:$card2.$port2";
134 135 136 137 138 139
	print "switchport='$switchport'\n" if $debug > 2;
	$Ports{$name} = $switchport;
	$Ports{$switchport} = $name;
	print "Ports: '$name' <==> '$switchport'\n" if $debug > 1;
    }

140 141
}

142 143 144 145 146 147 148 149 150
#
# Returns an array of ports (in node:card form) used by the given VLANs
#
sub getVlanPorts (@) {
    my @vlans = @_;
    # Silently exit if they passed us no VLANs
    if (!@vlans) {
	return ();
    }
151
    my @ports = ();
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    foreach my $vlanid (@vlans) {
	my $vlan = VLan->Lookup($vlanid);
	if (!defined($vlan)) {
	    die("*** $0:\n".
		"    No vlanid $vlanid in the DB!\n");
	}
	my @members;
	if ($vlan->MemberList(\@members) != 0) {
	    die("*** $0:\n".
		"    Unable to load members for $vlan\n");
	}
	foreach my $member (@members) {
	    my $nodeid;
	    my $iface;

	    if ($member->GetAttribute("node_id", \$nodeid) != 0 ||
		$member->GetAttribute("iface", \$iface) != 0) {
		die("*** $0:\n".
		    "    Missing attributes for $member in $vlan\n");
	    }
	    push(@ports, "$nodeid:$iface");
174 175
	}
    }
176 177 178 179
    # Convert from the DB format to the one used by the snmpit modules
    return convertPortsFromIfaces(@ports);
}

180
#
181 182
# Returns an an array of trunked ports (in node:card form) used by an
# experiment
183 184 185 186 187
#
sub getExperimentTrunks($$) {
    my ($pid, $eid) = @_;
    my @ports;

188 189 190 191 192
    my $query_result =
	DBQueryFatal("select distinct r.node_id,i.iface from reserved as r " .
		     "left join interfaces as i on i.node_id=r.node_id " .
		     "where r.pid='$pid' and r.eid='$eid' and " .
		     "      i.trunk!=0");
193

194
    while (my ($node, $iface) = $query_result->fetchrow()) {
195 196 197 198 199 200 201 202 203 204
	$node = $node . ":" . $iface;
	push @ports, $node;
    }
    return convertPortsFromIfaces(@ports);
}

#
# Update database to store vlan tag.
#
sub setVlanTag ($$) {
205 206
    my ($vlan_id, $tag) = @_;
    
207
    # Silently exit if they passed us no VLANs
208
    if (!$vlan_id || !defined($tag)) {
209 210 211
	return ();
    }

212 213 214 215 216
    my $vlan = VLan->Lookup($vlan_id);
    return ()
	if (!defined($vlan));
    return ()
	if ($vlan->SetTag($tag) != 0);
217 218 219 220

    return 0;
}

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
#
# Update database to mark port as enabled or disabled.
#
sub setPortEnabled($$) {
    my ($port, $enabled) = @_;

    $port =~ /^(.+):(\d+)$/;
    my ($node, $card) = ($1, $2);
    $enabled = ($enabled ? 1 : 0);

    DBQueryFatal("update interface_state set enabled=$enabled ".
		 "where node_id='$node' and card='$card'");
    
    return 0;
}
# Ditto for trunked.
sub setPortTagged($$) {
    my ($port, $tagged) = @_;

    $port =~ /^(.+):(\d+)$/;
    my ($node, $card) = ($1, $2);
    $tagged = ($tagged ? 1 : 0);

    DBQueryFatal("update interface_state set tagged=$tagged ".
		 "where node_id='$node' and card='$card'");
}

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
#
# Convert an entire list of ports in port:iface format to into port:card -
# returns other port forms unchanged.
#
sub convertPortsFromIfaces(@) {
    my @ports = @_;
    return map {
        if (/(.+):([A-Za-z].*)/) {
            # Seems to be a node:iface line
            convertPortFromIface($_);
        } else {
            $_;
        }
    } @ports;

}

#
# Convert a port in port:iface format to port:card
#
sub convertPortFromIface($) {
    my ($port) = $_;
    if ($port =~ /(.+):(.+)/) {
        my ($node,$iface) =  ($1,$2);
        my $result = DBQueryFatal("SELECT card FROM interfaces " .
            "WHERE node_id='$node' AND iface='$iface'");
        if (!$result->num_rows()) {
            warn "WARNING: convertPortFromIface($port) - Unable to get card\n";
            return $port;
        }
        my $card = ($result->fetchrow())[0];
        return "$node:$card";
    } else {
        warn "WARNING: convertPortFromIface($port) - Bad port format\n";
        return $port;
    }
284 285
}

286
#
287 288
# Returns an array of all VLAN id's used by a given experiment.
# Optional list of vlan ids restricts operation to just those vlans,
289
#
290 291
sub getExperimentVlans ($$@) {
    my ($pid, $eid, @optvlans) = @_;
292

293 294 295 296 297 298 299 300 301
    my $experiment = Experiment->Lookup($pid, $eid);
    if (!defined($experiment)) {
	die("*** $0:\n".
	    "    getExperimentVlans($pid,$eid) - no such experiment\n");
    }
    my @vlans;
    if (VLan->ExperimentVLans($experiment, \@vlans) != 0) {
	die("*** $0:\n".
	    "    Unable to load VLANs for $experiment\n");
302 303
    }

304 305 306 307 308 309 310
    # Convert to how the rest of snmpit wants to see this stuff.
    my @result = ();
    foreach my $vlan (@vlans) {
	push(@result, $vlan->id())
	    if (!@optvlans || grep {$_ == $vlan->id()} @optvlans);
    }
    return @result;
311 312
}

Robert Ricci's avatar
Robert Ricci committed
313 314 315 316 317 318 319 320 321
#
# Returns an array of all ports used by a given experiment
#
sub getExperimentPorts ($$) {
    my ($pid, $eid) = @_;

    return getVlanPorts(getExperimentVlans($pid,$eid));
}

322 323 324 325 326 327 328 329 330 331 332
#
# Returns an array of control net ports used by a given experiment
#
sub getExperimentControlPorts ($$) {
    my ($pid, $eid) = @_;

    # 
    # Get a list of all *physical* nodes in the experiment
    #
    my $exp = Experiment->Lookup($pid,$eid);
    my @nodes = $exp->NodeList(0,0);
333 334
    # plab and related nodes are still in the list, so filter them out
    @nodes = grep {$_->control_iface()} @nodes; 
335 336 337 338 339 340 341 342 343 344 345 346

    #
    # Get control net interfaces
    #
    my @ports =  map { $_->node_id() . ":" . $_->control_iface() } @nodes;

    #
    # Convert from iface to port number when we return
    #
    return convertPortsFromIfaces(@ports);
}

347 348 349 350 351 352 353 354 355
#
# Usage: getDeviceNames(@ports)
#
# Returns an array of the names of all devices used in the given ports
#
sub getDeviceNames(@) {
    my @ports = @_;
    my %devices = ();
    foreach my $port (@ports) {
Robert Ricci's avatar
Robert Ricci committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	#
	# Accept either node:port or switch.port
	#
	my $device;
	if ($port =~ /^([^:]+):(.+)$/) {
	    my ($node,$card) = ($1,$2);
	    if (!defined($node) || !defined($card)) { # Oops, $card can be 0
		die "Bad port given: $port\n";
	    }
	    my $result = DBQueryFatal("SELECT node_id2 FROM wires " .
		"WHERE node_id1='$node' AND card1=$card");
	    if (!$result->num_rows()) {
		warn "No database entry found for port $port - Skipping\n";
		next;
	    }
	    # This is a loop, on the off chance chance that a single port on a
	    # node can be connected to multiple ports on the switch.
	    while (my @row = $result->fetchrow()) {
		$device = $row[0];
	    }
	} elsif ($port =~ /^([^.]+)\.\d+(\/\d+)?$/) {
		$device = $1;
	} else {
	    warn "Invalid format for port $port - Skipping\n";
380 381
	    next;
	}
Robert Ricci's avatar
Robert Ricci committed
382 383

	$devices{$device} = 1;
Mac Newbold's avatar
Mac Newbold committed
384
    }
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    return (sort {tbsort($a,$b)} keys %devices);
}

#
# Returns a hash, keyed by device, of all ports in the given list that are
# on that device
#
sub mapPortsToDevices(@) {
    my @ports = @_;
    my %map = ();
    foreach my $port (@ports) {
	my ($device) = getDeviceNames($port);
	if (defined($device)) { # getDeviceNames does the job of warning users
	    push @{$map{$device}},$port;
	}
400
    }
401
    return %map;
Mac Newbold's avatar
Mac Newbold committed
402 403
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
#
# Returns the device type for the given node_id
#
sub getDeviceType ($) {

    my ($node) = @_;

    my $result =
	DBQueryFatal("SELECT type FROM nodes WHERE node_id='$node'");

    my @row = $result->fetchrow();
    # Sanity check - make sure the node exists
    if (!@row) {
	die "No such node: $node\n";
    }

    return $row[0];
Mac Newbold's avatar
Mac Newbold committed
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
#
# Returns (current_speed,duplex) for the given interface (in node:port form)
#
sub getInterfaceSettings ($) {

    my ($interface) = @_;

    $interface =~ /^(.+):(\d+)$/;
    my ($node, $port) = ($1, $2);
    if ((!defined $node) || (!defined $port)) {
	die "getInterfaceSettings: Bad interface ($interface) given\n";
    }

    my $result =
	DBQueryFatal("SELECT current_speed, duplex FROM interfaces " .
		     "WHERE node_id='$node' and card=$port");

    my @row = $result->fetchrow();
    # Sanity check - make sure the interface exists
    if (!@row) {
	die "No such interface: $interface\n";
    }

    return @row;
}

449 450 451 452 453 454 455 456 457 458 459 460 461 462
#
# Returns an array with then names of all switches identified as test switches
#
sub getTestSwitches () {
    my $result =
	DBQueryFatal("SELECT node_id FROM nodes WHERE role='testswitch'");
    my @switches = (); 
    while (my @row = $result->fetchrow()) {
	push @switches, $row[0];
    }

    return @switches;
}

463 464 465 466 467 468 469 470 471 472 473 474 475 476
#
# Returns an array with the names of all switches identified as control switches
#
sub getControlSwitches () {
    my $result =
	DBQueryFatal("SELECT node_id FROM nodes WHERE role='ctrlswitch'");
    my @switches = (); 
    while (my @row = $result->fetchrow()) {
	push @switches, $row[0];
    }

    return @switches;
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490
#
# Returns an array with the names of all switches in the given stack
#
sub getSwitchesInStack ($) {
    my ($stack_id) = @_;
    my $result = DBQueryFatal("SELECT node_id FROM switch_stacks " .
	"WHERE stack_id='$stack_id'");
    my @switches = (); 
    while (my @row = $result->fetchrow()) {
	push @switches, $row[0];
    }

    return @switches;
}
491

492
#
493
# Returns the stack_id of a switch's primary stack
494
#
495
sub getSwitchPrimaryStack($) {
496 497
    my $switch = shift;
    my $result = DBQueryFatal("SELECT stack_id FROM switch_stacks WHERE " .
498
    		"node_id='$switch' and is_primary=1");
499
    if (!$result->numrows()) {
500 501 502 503 504
	print STDERR "No primary stack_id found for switch $switch\n";
	return undef;
    } elsif ($result->numrows() > 1) {
	print STDERR "Switch $switch is marked as primary in more than one " .
	    "stack\n";
505 506 507 508 509 510 511
	return undef;
    } else {
	my ($stack_id) = ($result->fetchrow());
	return $stack_id;
    }
}

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
#
# Returns a list of all stack_ids that a switch belongs to
#
sub getSwitchStacks($) {
    my $switch = shift;
    my $result = DBQueryFatal("SELECT stack_id FROM switch_stacks WHERE " .
    		"node_id='$switch'");
    if (!$result->numrows()) {
	print STDERR "No stack_id found for switch $switch\n";
	return undef;
    } else {
	my @stack_ids;
	while (my ($stack_id) = ($result->fetchrow())) {
	    push @stack_ids, $stack_id;
	}
	return @stack_ids;
    }
}

531
#
Robert Ricci's avatar
Robert Ricci committed
532
# Returns the type of the given stack_id. If called in list context, also
533 534
# returns whether or not the stack supports private VLANs, whether it
# uses a single VLAN domain, and the SNMP community to use.
535 536 537
#
sub getStackType($) {
    my $stack = shift;
538
    my $result = DBQueryFatal("SELECT stack_type, supports_private, " .
539 540
	"single_domain, snmp_community FROM switch_stack_types " .
	"WHERE stack_id='$stack'");
541 542 543 544
    if (!$result->numrows()) {
	print STDERR "No stack found called $stack\n";
	return undef;
    } else {
545 546
	my ($stack_type,$supports_private,$single_domain,$community)
	    = ($result->fetchrow());
Robert Ricci's avatar
Robert Ricci committed
547
	if (defined wantarray) {
548
	    return ($stack_type,$supports_private,$single_domain, $community);
Robert Ricci's avatar
Robert Ricci committed
549 550 551
	} else {
	    return $stack_type;
	}
552 553 554
    }
}

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
#
# Returns the leader for the given stack - the meaning of this is vendor-
# specific. May be undefined.
#
sub getStackLeader($) {
    my $stack = shift;
    my $result = DBQueryFatal("SELECT leader FROM switch_stack_types " .
	"WHERE stack_id='$stack'");
    if (!$result->numrows()) {
	print STDERR "No stack found called $stack\n";
	return undef;
    } else {
	my ($leader) = ($result->fetchrow());
	return $leader;
    }
}

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
#
# Get a hash that describes the configuration options for a switch. The idea is
# that the device's object will call this method to get some options.  Right
# now, all this stuff actually comes from the stack, but there could be
# switch-specific configuration in the future. Provides defaults for NULL
# columns
#
# We could probably make this look more like an object, for type checking, but
# that just doesn't seem necessary yet.
#
sub getDeviceOptions($) {
    my $switch = shift;
    my %options;

    my $result = DBQueryFatal("SELECT supports_private, " .
	"single_domain, snmp_community, min_vlan, max_vlan " .
	"FROM switch_stacks AS s left join switch_stack_types AS t " .
	"    ON s.stack_id = t.stack_id ".
	"WHERE s.node_id='$switch'");

    if (!$result->numrows()) {
	print STDERR "No switch $switch found, or it is not in a stack\n";
	return undef;
    }

    my ($supports_private, $single_domain, $snmp_community, $min_vlan,
	$max_vlan) = $result->fetchrow();

    $options{'supports_private'} = $supports_private;
    $options{'single_domain'} = $single_domain;
    $options{'snmp_community'} = $snmp_community || "public";
    $options{'min_vlan'} = $min_vlan || 2;
    $options{'max_vlan'} = $max_vlan || 1000;

    $options{'type'} = getDeviceType($switch);

    if ($debug) {
	print "Options for $switch:\n";
	while (my ($key,$value) = each %options) {
	    print "$key = $value\n"
	}
    }

    return \%options;
}

Robert Ricci's avatar
Robert Ricci committed
618 619 620 621
#
# Returns a structure representing all trunk links. It's a hash, keyed by
# switch, that contains hash references. Each of the second level hashes
# is keyed by destination, with the value being an array reference that
622 623 624
# contains the card.port pairs to which the trunk is conencted. For exammple,
# ('cisco1' => { 'cisco3' => ['1.1','1.2'] },
#  'cisco3' => { 'cisco1' => ['2.1','2.2'] } )
Robert Ricci's avatar
Robert Ricci committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
#
sub getTrunks() {

    my %trunks = ();

    my $result = DBQueryFatal("SELECT node_id1, card1, port1, " .
	"node_id2, card2, port2 FROM wires WHERE type='Trunk'");

    while (my @row = $result->fetchrow()) {
	my ($node_id1, $card1, $port1, $node_id2, $card2, $port2)  = @row;
	push @{ $trunks{$node_id1}{$node_id2} }, "$card1.$port1";
	push @{ $trunks{$node_id2}{$node_id1} }, "$card2.$port2";
    }

    return %trunks;
	
}

#
# Find the best path from one switch to another. Returns an empty list if no
# path exists, otherwise returns a list of switch names. Arguments are:
# A reference to a hash, as returned by the getTrunks() function
# A reference to an array of unvisited switches: Use [keys %trunks]
# Two siwtch names, the source and the destination 
649
#
Robert Ricci's avatar
Robert Ricci committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
sub getTrunkPath($$$$) {
    my ($trunks, $unvisited, $src,$dst) = @_;
    if ($src eq $dst) {
	#
	# The source and destination are the same
	#
	return ($src);
    } elsif ($trunks->{$src}{$dst}) {
	#
	# The source and destination are directly connected
	#
	return ($src,$dst);
    } else {
	# The source and destination aren't directly connected. We'll need to 
	# recurse across other trunks to find solution
	my @minPath = ();

	#
	# We use the @$unvisited list to pick switches to traverse to, so
	# that we don't re-visit switches we've already been to, which would 
	# cause infinite recursion
	#
	foreach my $i (0 .. $#{$unvisited}) {
	    if ($trunks->{$src}{$$unvisited[$i]}) {

		#
		# We need to pull theswitch out of the unvisted list that we
		# pass to it.
		#
		my @list = @$unvisited;
		splice(@list,$i,1);

		#
		# Check to see if the path we get with this switch is the 
		# best one found so far
		#
		my @path = getTrunkPath($trunks,\@list,$$unvisited[$i],$dst);
		if (@path && ((!@minPath) || (@path < @minPath))) {
		    @minPath = @path;
		}
	    }

	}

	#
	# If we found a path, tack ourselves on the front and return. If not,
	# return the empty list of failure.
	#
	if (@minPath) {
	    return ($src,@minPath);
	} else {
	    return ();
	}
    }
}

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
#
# Returns a list of trunks, in the form [src, dest], from a path (as returned
# by getTrunkPath() ). For example, if the input is:
# (cisco1, cisco3, cisco4), the return value is:
# ([cisco1, cisco3], [cisco3, cisco4])
#
sub getTrunksFromPath(@) {
    my @path = @_;
    my @trunks = ();
    my $lastswitch = "";
    foreach my $switch (@path) {
	if ($lastswitch) {
	    push @trunks, [$lastswitch, $switch];
	}
	$lastswitch = $switch;
    }

    return @trunks;
}

#
# Given a list of lists of trunks (returned by multiple getTrunksFromPath() 
# calls), return a list of the unique trunks found in this list
#
sub getUniqueTrunks(@) {
    my @trunkLists = @_;
    my @unique = ();
    foreach my $trunkref (@trunkLists) {
	my @trunks = @$trunkref;
	TRUNK: foreach my $trunk (@trunks) {
	    # Since source and destination are interchangable, we have to
	    # check both possible orderings
	    foreach my $unique (@unique) {
		if ((($unique->[0] eq $trunk->[0]) &&
		     ($unique->[1] eq $trunk->[1])) ||
		    (($unique->[0] eq $trunk->[1]) &&
		     ($unique->[1] eq $trunk->[0]))) {
			 # Yep, it's already in the list - go to the next one
			 next TRUNK;
		}
	    }

	    # Made it through, we must not have seen this one before
	    push @unique, $trunk;
	}
    }

    return @unique;
}

#
# Given a trunk structure (as returned by getTrunks() ), and a list of switches,
# return a list of all trunks (in the [src, dest] form) that are needed to span
# all the switches (ie. which trunks the VLAN must be allowed on)
#
sub getTrunksFromSwitches($@) {
    my $trunks = shift;
    my @switches = @_;

    #
    # First, find the paths between each set of switches
    #
    my @paths = ();
    foreach my $switch1 (@switches) {
	foreach my $switch2 (@switches) {
	    push @paths, [ getTrunkPath($trunks, [ keys %$trunks ],
					$switch1, $switch2) ];
	}
    }

    #
    # Now, make a list of all the the trunks used by these paths
    #
    my @trunkList = ();
    foreach my $path (@paths) {
	push @trunkList, [ getTrunksFromPath(@$path) ];
    }

    #
    # Last, remove any duplicates from the list of trunks
    #
    my @trunks = getUniqueTrunks(@trunkList);

    return @trunks;

}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
#
# Make a hash of all trunk ports for easy checking - the keys into the hash are
# in the form "switch/mod.port" - the contents are 1 if the port belongs to a
# trunk, and undef if not
#
# ('cisco1' => { 'cisco3' => ['1.1','1.2'] },
#  'cisco3' => { 'cisco1' => ['2.1','2.2'] } )
#
sub getTrunkHash() {
    my %trunks = getTrunks();
    my %trunkhash = ();
    foreach my $switch1 (keys %trunks) {
        foreach my $switch2 (keys %{$trunks{$switch1}}) {
            foreach my $port (@{$trunks{$switch1}{$switch2}}) {
                my $portstr = "$switch1/$port";
                $trunkhash{$portstr} = 1;
            }
        }
    }
    return %trunkhash;
}

815
#
816
# Execute and SNMP command, retrying in case there are transient errors.
817
#
818 819 820
# usage: snmpitDoIt(getOrSet, session, var, [retries])
# args:  getOrSet - either $SNMPIT_GET or $SNMPIT_SET
#        session - SNMP::Session object, already connected to the SNMP
821
#                  device
822 823
#        var     - An SNMP::Varbind or a reference to a two-element array
#                  (similar to a single Varbind)
824
#        retries - Number of times to retry in case of failure
825
# returns: the value on sucess, undef on failure
826
#
827
sub snmpitDoIt($$$;$) {
828

829
    my ($getOrSet,$sess,$var,$retries) = @_;
830 831 832 833 834 835 836 837 838 839

    if (! defined($retries) ) {
	$retries = $DEFAULT_RETRIES;
    }

    #
    # Make sure we're given valid inputs
    #
    if (!$sess) {
	$snmpitErrorString = "No valid SNMP session given!\n";
840
	return undef;
841 842
    }

843 844 845
    my $array_size;
    if ($getOrSet == $SNMPIT_GET) {
	$array_size = 2;
846 847
    } elsif ($getOrSet == $SNMPIT_BULKWALK) {
	$array_size = 1;
848 849 850 851
    } else {
	$array_size = 4;
    }

852
    if (((ref($var) ne "SNMP::Varbind") && (ref($var) ne "SNMP::VarList")) &&
853 854
	    ((ref($var) ne "ARRAY") || ((@$var != $array_size) && (@$var != 4)))) {
	$snmpitErrorString = "Invalid SNMP variable given ($var)!\n";
855
	return undef;
856 857 858 859 860 861
    }

    #
    # Retry several times
    #
    foreach my $retry ( 1 .. $retries) {
862
	my $status;
863
        my @return;
864 865
	if ($getOrSet == $SNMPIT_GET) {
	    $status = $sess->get($var);
866 867
	} elsif ($getOrSet == $SNMPIT_BULKWALK) {
	    @return = $sess->bulkwalk(0,32,$var);
868 869 870 871
	} else {
	    $status = $sess->set($var);
	}

872 873 874 875 876 877 878 879 880 881 882 883
	#
	# Avoid unitialized variable warnings when printing errors
	#
	if (! defined($status)) {
	    $status = "(undefined)";
	}

	#
	# We detect errors by looking at the ErrorNumber variable from the
	# session
	#
	if ($sess->{ErrorNum}) {
884 885 886
	    my $type;
	    if ($getOrSet == $SNMPIT_GET) {
		$type = "get";
887 888
	    } elsif ($getOrSet == $SNMPIT_BULKWALK) {
		$type = "bulkwalk";
889 890 891
	    } else {
		$type = "set";
	    }
892 893 894
	    $snmpitErrorString  = "SNMPIT $type failed for device " .
                "$sess->{DestHost} (try $retry of $retries)\n";
            $snmpitErrorString .= "Variable was " .  printVars($var) . "\n";
895
	    $snmpitErrorString .= "Returned $status, ErrorNum was " .
896 897 898 899 900
		   "$sess->{ErrorNum}\n";
	    if ($sess->{ErrorStr}) {
		$snmpitErrorString .= "Error string is: $sess->{ErrorStr}\n";
	    }
	} else {
901 902
	    if ($getOrSet == $SNMPIT_GET) {
		return $var->[2];
903 904
	    } elsif ($getOrSet == $SNMPIT_BULKWALK) {
                return @return;
905 906 907
	    } else {
	        return 1;
	    }
908 909 910
	}

	#
911 912
	# Don't flood requests too fast. Randomize the sleep a little so that
	# we don't end up with all our retries coming in at the same time.
913
	#
914 915
        sleep(1);
	select(undef, undef, undef, rand(1));
916 917 918 919 920
    }

    #
    # If we made it out, all of the attempts must have failed
    #
921
    return undef;
922 923
}

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
#
# usage: snmpitGet(session, var, [retries])
# args:  session - SNMP::Session object, already connected to the SNMP
#                  device
#        var     - An SNMP::Varbind or a reference to a two-element array
#                  (similar to a single Varbind)
#        retries - Number of times to retry in case of failure
# returns: the value on sucess, undef on failure
#
sub snmpitGet($$;$) {
    my ($sess,$var,$retries) = @_;
    my $result;

    $result = snmpitDoIt($SNMPIT_GET,$sess,$var,$retries);

    return $result;
}

942 943 944 945
#
# Same as snmpitGet, but send mail if any error occur
#
sub snmpitGetWarn($$;$) {
946
    my ($sess,$var,$retries) = @_;
947 948
    my $result;

949
    $result = snmpitDoIt($SNMPIT_GET,$sess,$var,$retries);
950

951
    if (! defined $result) {
952 953 954 955 956 957 958 959 960 961
	snmpitWarn("SNMP GET failed");
    }
    return $result;
}

#
# Same as snmpitGetWarn, but also exits from the program if there is a 
# failure.
#
sub snmpitGetFatal($$;$) {
962
    my ($sess,$var,$retries) = @_;
963 964
    my $result;

965
    $result = snmpitDoIt($SNMPIT_GET,$sess,$var,$retries);
966

967
    if (! defined $result) {
968
	tbreport(SEV_ERROR, 'snmp_get_fatal');
969 970 971 972 973
	snmpitFatal("SNMP GET failed");
    }
    return $result;
}

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
#
# usage: snmpitSet(session, var, [retries])
# args:  session - SNMP::Session object, already connected to the SNMP
#                  device
#        var     - An SNMP::Varbind or a reference to a two-element array
#                  (similar to a single Varbind)
#        retries - Number of times to retry in case of failure
# returns: true on success, undef on failure
#
sub snmpitSet($$;$) {
    my ($sess,$var,$retries) = @_;
    my $result;

    $result = snmpitDoIt($SNMPIT_SET,$sess,$var,$retries);

    return $result;
}

#
# Same as snmpitSet, but send mail if any error occur
#
sub snmpitSetWarn($$;$) {
    my ($sess,$var,$retries) = @_;
    my $result;

    $result = snmpitDoIt($SNMPIT_SET,$sess,$var,$retries);

1001
    if (! defined $result) {
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	snmpitWarn("SNMP SET failed");
    }
    return $result;
}

#
# Same as snmpitSetWarn, but also exits from the program if there is a 
# failure.
#
sub snmpitSetFatal($$;$) {
    my ($sess,$var,$retries) = @_;
    my $result;

    $result = snmpitDoIt($SNMPIT_SET,$sess,$var,$retries);

1017
    if (! defined $result) {
1018
	tbreport(SEV_ERROR, 'snmp_set_fatal');
1019 1020 1021 1022 1023
	snmpitFatal("SNMP SET failed");
    }
    return $result;
}

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
#
# usage: snmpitBulkwalk(session, var, [retries])
# args:  session - SNMP::Session object, already connected to the SNMP
#                  device
#        var     - An SNMP::Varbind or a reference to a single-element array
#        retries - Number of times to retry in case of failure
# returns: an array of values on success, undef on failure
#
sub snmpitBulkwalk($$;$) {
    my ($sess,$var,$retries) = @_;
    my @result;

    @result = snmpitDoIt($SNMPIT_BULKWALK,$sess,$var,$retries);

    return @result;
}

#
# Same as snmpitBulkwalk, but send mail if any errors occur
#
sub snmpitBulkwalkWarn($$;$) {
    my ($sess,$var,$retries) = @_;
    my @result;

    @result = snmpitDoIt($SNMPIT_BULKWALK,$sess,$var,$retries);

1050
    if (! @result) {
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	snmpitWarn("SNMP Bulkwalk failed");
    }
    return @result;
}

#
# Same as snmpitBulkwalkWarn, but also exits from the program if there is a 
# failure.
#
sub snmpitBulkwalkFatal($$;$) {
    my ($sess,$var,$retries) = @_;
    my @result;

    @result = snmpitDoIt($SNMPIT_BULKWALK,$sess,$var,$retries);

1066
    if (! @result) {
1067 1068 1069 1070 1071
	snmpitFatal("SNMP Bulkwalk failed");
    }
    return @result;
}

1072 1073 1074 1075 1076
#
# Print out SNMP::VarList and SNMP::Varbind structures. Useful for debugging
#
sub printVars($) {
    my ($vars) = @_;
1077 1078 1079
    if (!defined($vars)) {
	return "[(undefined)]";
    } elsif (ref($vars) eq "SNMP::VarList") {
1080
	return "[" . join(", ",map( {"[".join(",",@$_)."\]";}  @$vars)) . "]";
1081
    } elsif (ref($vars) eq "SNMP::Varbind") {
1082 1083
	return "[" . join(",",@$vars) . "]";
    } elsif (ref($vars) eq "ARRAY") {
1084 1085
	return "[" . join(",",map( {defined($_)? $_ : "(undefined)"} @$vars))
		. "]";
1086
    } else {
1087
	return "[unknown value]";
1088 1089 1090 1091 1092 1093 1094
    }
}

#
# Both print out an error message and mail it to the testbed ops. Prints out
# the snmpitErrorString set by snmpitGet.
#
1095
# usage: snmpitWarn(message,fatal)
1096
#
1097
sub snmpitWarn($$) {
1098

1099
    my ($message,$fatal) = @_;
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113

    #
    # Untaint $PRORAM_NAME
    #
    my $progname;
    if ($PROGRAM_NAME =~ /^([-\w.\/]+)$/) {
	$progname = $1;
    } else {
	$progname = "Tainted";
    }

    my $text = "$message - In $progname\n" .
    	       "$snmpitErrorString\n";
	
1114 1115 1116 1117 1118
    if ($fatal) {
        tbdie({cause => 'hardware'}, $text);
    } else {
        tbwarn({cause => 'hardware'}, $text);
    }
1119 1120 1121 1122 1123 1124 1125 1126

}

#
# Like snmpitWarn, but die too
#
sub snmpitFatal($) {
    my ($message) = @_;
1127
    snmpitWarn($message,1);
1128 1129
}

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
#
# Used to sort a set of nodes in testbed order (ie. pc2 < pc10)
#
# usage: tbsort($a,$b)
#        returns -1 if $a < $b
#        returns  0 if $a == $b
#        returns  1 if $a > $b
#
sub tbsort { 
    my ($a,$b) = @_;
    $a =~ /^([a-z]*)([0-9]*):?([0-9]*)/;
    my $a_let = ($1 || "");
    my $a_num = ($2 || 0);
    my $a_num2 = ($3 || 0);
    $b =~ /^([a-z]*)([0-9]*):?([0-9]*)/;
    my $b_let = ($1 || "");
    my $b_num = ($2 || 0);
    my $b_num2 = ($3 || 0);
    if ($a_let eq $b_let) {
	if ($a_num == $b_num) {
	    return $a_num2 <=> $b_num2;
	} else {
	    return $a_num <=> $b_num;
	}
    } else {
	return $a_let cmp $b_let;
    }
    return 0;
}
Mac Newbold's avatar
Mac Newbold committed
1159 1160 1161
# End with true
1;