switchmac.in 10.2 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
Leigh Stoller's avatar
Leigh Stoller committed
5
# Copyright (c) 2003, 2005 University of Utah and the Flux Group.
6 7 8 9 10 11 12 13 14
# All rights reserved.
#

#
# switchmac - a tool for getting MAC address listings from Cisco switches.
# Reports all MACs learned on all experimental switches.
#
# The output of this script is supposed to be machine-readable, rather than
# human-readable. The output is:
15
# <mac>,<switch>/<module>.<port>,<vlan>,<interface>,<class>
16 17 18 19 20 21
#
# <mac> is the learned MAC address, no puncuation, in lowercase.
# <switch>, <module>, and <port> are what they sound like.
# <vlan> indicates which VLAN the MAC was learned in
# <interface> is the <node>:<iface> of the interface that matches the MAC
#     address, if any. 'unknown' if the MAC is not in our database
22
# <class> is 'experimental' or 'control', depending on the primary function
Leigh Stoller's avatar
Leigh Stoller committed
23
#     of the switch the MAC address was learned from. 
24 25 26 27
#

use lib '@prefix@/lib';
use libdb;
Leigh Stoller's avatar
Leigh Stoller committed
28
use libxmlrpc;
29 30
use snmpit_lib;
use snmpit_cisco;
31
use snmpit_foundry;
32 33 34 35 36
use SNMP;

use strict;

my $MASTER_COMMUNITY = "@SNMP_RW_COMMUNITY@";
Leigh Stoller's avatar
Leigh Stoller committed
37 38 39 40
my $ELABINELAB = @ELABINELAB@;
my $RPCSERVER  = "@OUTERBOSS_NODENAME@";
my $RPCPORT    = "@OUTERBOSS_XMLRPCPORT@";
my $RPCCERT    = "@OUTERBOSS_SSLCERTNAME@";
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#
# We use SNMP directly, because we have to use some pretty dumb tricks that I
# don't want to have to teach the snmpit libraries.
#
my $mibpath = "/usr/local/share/snmp/mibs";
my @mibs = ("$mibpath/SNMPv2-SMI.txt", "$mibpath/SNMPv2-TC.txt",
    "$mibpath/SNMPv2-MIB.txt", "$mibpath/IANAifType-MIB.txt",
    "$mibpath/IF-MIB.txt", "$mibpath/BRIDGE-MIB.txt");

#
# Don't know what damage users could do with this script, but why not, let's be
# paranoid about it.
#
if (!TBAdmin($>)) {
    die "Sorry, only admins can use this script\n";
}

if (@ARGV) {
    die "usage: $0\n";
}

Leigh Stoller's avatar
Leigh Stoller committed
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
#
# ElabinElab is special; Ask outer boss for the info.
#
if ($ELABINELAB) {
    libxmlrpc::Config({"server"  => $RPCSERVER,
		       "verbose" => 0,
		       "cert"    => $RPCCERT,
		       "portnum" => $RPCPORT});

    my $rval = libxmlrpc::CallMethod("elabinelab", "switchmac", {});

    if (!defined($rval)) {
	exit(-1);
    }
      
    #
    # Convert the stuff the RPC server back into the proper string format.
    #
    foreach my $mac (keys(%{ $rval })) {
	my $aref        = $rval->{$mac};
	my $iface       = $aref->{"iface"};
	my $role        = $aref->{"role"};
	my $switch_id   = $aref->{"switch_id"};
	my $switch_card = $aref->{"switch_card"};
	my $switch_port = $aref->{"switch_port"};

	print "${mac},${switch_id}/${switch_card}.${switch_port},".
	    "elabinelab,${iface},${role}\n";
    }
    exit(0);
}
&SNMP::addMibFiles(@mibs);
&SNMP::initMib();

97 98 99 100 101
#
# Yeah, it's slow, but we want to use all the test switches so that we don't
# have to worry about missing something, and don't have to get the user to
# guess which switch they're using
#
102
my @test_switches = map {[$_,"experimental"]} getTestSwitches();
103

104
if (!@test_switches) {
105 106 107 108
    die "No experimental switches found - make sure they're in the nodes ".
	"table!\n";
}

109
my @control_switches = map {[$_,"control"]} getControlSwitches();
110 111 112 113 114 115 116
if (!@control_switches) {
    warn "Warning - No control switches found - you probably want to add them " .
	"to the nodes table\n";
}

my @switches = (@test_switches, @control_switches);

117 118 119 120 121 122 123 124 125 126 127
my $debug = 0;

sub DEBUG {
    if ($debug) {
	print @_;
    }
}

#
# Loop through each switch indvidually
#
128
SWITCHLOOP:
129 130 131
foreach my $switchref (@switches) {
    my ($switch, $class) = @$switchref;
    DEBUG "Looking at switch $switch, class $class\n";
132

Leigh Stoller's avatar
Leigh Stoller committed
133 134 135
    my $class_str = (($class eq "experimental") ?
		     TBDB_IFACEROLE_EXPERIMENT() : TBDB_IFACEROLE_CONTROL());

136 137 138 139
    #
    # I have _no_ idea if this will work on other switches!
    #
    my $type = getDeviceType($switch);
140 141 142
    my ($snmpversion, $useindexing, $switchtype);
SWITCH:
    for ($type) {
Robert Ricci's avatar
Robert Ricci committed
143
	(/^cisco/ || /^catalyst/) && do { $snmpversion = "2c"; $useindexing = 1;
144 145 146 147 148
	    $switchtype = "cisco"; last;};
	(/^intel/) && do { $snmpversion = "1"; $useindexing = 0;
	    $switchtype = "intel"; last;};
	(/^nortel/) && do { $snmpversion = "1"; $useindexing = 0;
	    $switchtype = "nortel"; last;};
149 150
	(/^foundry/) && do { $snmpversion = "1"; $useindexing = 0;
	    $switchtype = "foundry"; last;};
151 152 153

	warn "WARNING: Switch type $type is not supported for $switch!\n";
	next SWITCHLOOP;
154 155
    }

156

157 158 159 160 161 162 163
    #
    # Get the community string for this switch
    #
    my $community = "public";
    if ($MASTER_COMMUNITY) {
	$community = $MASTER_COMMUNITY;
    }
164

165
    my $stack = getSwitchPrimaryStack($switch);
166 167 168 169 170 171 172 173 174 175 176
    if (!$stack) {
	die "No stack found for switch $switch\n";
    }
    my $stack_community = (getStackType($stack))[3];
    if ($stack_community) {
	$community = $stack_community;
    }

    DEBUG "Decided on community $community";
    
    #
177 178
    # Get a list of VLANS from the switch - we'll need them later. But, only
    # for some types of switches.
179
    #
180 181 182
    my @vlanList;
    my $device;
    if ($useindexing) {
183
	$device = new snmpit_cisco($switch,$debug,$community);
184
	@vlanList = ([1,1,[]],$device->listVlans());
185 186 187 188 189
    } else {
	#
	# Make up a fake VLAN, just so we get into the loop below
	#
	@vlanList = ([0,0,[]]);
190 191 192
	if ($switchtype eq "foundry") {
	    $device = new snmpit_foundry($switch,$debug,$community);
	}
193
    }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

    #
    # Loop through all VLANs - we have to start a new session for each one.
    #
    foreach my $vlan (@vlanList) {

	my ($vlan_id, $vlan_number, $memberref) = @$vlan;
	DEBUG "Looking at VLAN $vlan_number\n";

	#
	# We have to start a new session every time, because - get this - the
	# community string we use affects which VLAN we see MACs for. How sick
	# is that?! (This abomination, BTW, is called 'Community String
	# Indexing')
	#
209 210 211 212 213 214 215 216
	my $thiscommunity;
	if ($useindexing) {
	    $thiscommunity = "$community\@$vlan_number";
	} else {
	    $thiscommunity = $community;
	}
	my $session = new SNMP::Session(DestHost => $switch,
		Version => "$snmpversion", Community => "$thiscommunity");
217
	if (!$session) {
218
	    die "Unable to open session to $switch $thiscommunity\n";
219 220 221 222 223
	}

	#
	# Walk the table that contains the MACs for this VLAN
	#
224 225 226 227 228 229 230 231 232 233 234 235
	my $rows;
	if ($snmpversion eq "2c") {
	    ($rows) = $session->bulkwalk(0,32,["dot1dTpFdbTable"]);
	} else {
	    my $oid = ["dot1dTpFdbTable",0];
	    $session->get($oid);
	    while ($$oid[0] =~ /^dot1dTpFdb/) {
		push @$rows, [@$oid];
		$session->getnext($oid);
	    }
	}

236 237 238 239
	my %MACs;
	my %interfaces;
	my %bridgeports;
	my %status;
240 241 242 243 244 245
	my $firsttime = 1;

	#
	# What we loop on here depends on whether we're using bulkwalk with
	# v2c, or the old, slow, getnext() walking with version 1
	#
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
	foreach my $rowref (@$rows) {
	    my ($oid, $index, $value) = @$rowref;

	    #
	    # Convert the index into something perl is more comfortable with
	    #
	    $index = unpack("H*",$index);

	    SWITCH: for ($oid) {
		/^dot1dTpFdbAddress/ && do {
		    #
		    # This is a MAC - we need to move it from a binay string to
		    # a set of octets
		    #
		    my $MAC = unpack("H*",$value);

		    #
		    # Check to see if this MAC is in the database
		    #
		    my $res = DBQueryFatal("select node_id, iface from " .
			"interfaces where mac ='$MAC'");
		    my $interface;
		    if (!$res->num_rows()) {
			$interface = "unknown";
		    } else {
			my ($node_id, $iface) = $res->fetchrow();
			$interface  = "$node_id:$iface";
		    }

		    $MACs{$index} = $MAC;
		    $interfaces{$index} = $interface;
		    DEBUG "Got MAC $MAC (index $index)\n";
		    last SWITCH;
		};
		/^dot1dTpFdbPort/ && do {
		    #
		    # Just record for later use
		    #
		    $bridgeports{$index} = $value;
		    DEBUG "Got port $value (index $index)\n";
		    last SWITCH;
		};
		/^dot1dTpFdbStatus/ && do {
		    #
		    # Just record for later use
		    #
292
		    DEBUG "Got status $value (index $index)\n";
293 294 295 296 297 298 299 300 301 302 303 304 305 306
		    $status{$index} = $value;
		    last SWITCH;
		};

	    } # SWITCH

	}

	#
	# So many layers of indirection! Get the table that maps port numbers
	# returned by the BRIDGE-MIB to the REAL ifIndices!
	#
	my %realports;
	if (keys %MACs) {
307 308 309 310 311 312 313 314 315 316 317
	    my $rows;
	    if ($snmpversion eq "2c") {
		($rows) = $session->bulkwalk(0,32,["dot1dBasePortIfIndex"]);
	    } else {
		my $oid = ["dot1dBasePortIfIndex",0];
		$session->get($oid);
		while ($$oid[0] =~ /^dot1dBasePortIfIndex/) {
		    push @$rows, [@$oid];
		    $session->getnext($oid);
		}
	    }
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	    my %ifIndexMap;
	    foreach my $rowref (@$rows) {
		my ($oid, $index, $value) = @$rowref;
		$ifIndexMap{$index} = $value;
		DEBUG "Put $index => $value into \%ifIndexMap\n";
	    }

	    #
	    # Ask the snmpit module to convert the ifIndex to
	    # module.port format for us
	    #
	    foreach my $index (keys %bridgeports) {
		my $bridgeport = $bridgeports{$index};
		my $ifIndex = $ifIndexMap{$bridgeport};
		if (!$ifIndex) {
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
		    if ($switchtype eq "nortel") {
			#
			# Bad hack for Nortel switches
			#
			$ifIndex = $bridgeport - 47;
		    } else  {
			DEBUG "ifIndex conversion failed for $bridgeport!\n";
			next;
		    }
		}

		#
		# If the device is a Cisco, we have to convert ifindex to a
		# module.port number - for now, the other switches we support
		# have only one module, and require no ifindex conversion.
		#
		my $modport;
350
		if (($switchtype eq "cisco") || ($switchtype eq "foundry")) {
Robert Ricci's avatar
Robert Ricci committed
351
		    ($modport) = $device->convertPortFormat(2, $ifIndex);
352 353
		} else {
		    $modport = "1.$ifIndex";
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
		}
		my $switchport;
		if ($modport) {
		    $switchport = $switch . "/" . $modport;
		} else {
		    $switchport = $switch . "/ifIndex." . $bridgeport;
		}

		$realports{$index} = $switchport;
	    }
	}

	#
	# Okay, now print them out
	#
	foreach my $index (keys %MACs) {
	    #
	    # We only want to see learned MAC addresses - not ones internal
	    # to the switch, etc. Also skip ones that we couldn't figure
	    # out a name for - this means they probably came from
	    # off-switch (eg. a trunk port)
	    #
	    DEBUG "printing for index $index\n";
377
	    next unless ($status{$index} && ($status{$index} eq "learned" || 
378 379 380 381 382 383 384 385
		$status{$index} eq "3"));
	    #
	    # As far as I can tell, this is a bug with (at least some versions
	    # of) CatOS - we sometimes get back MACs that are not even in
	    # this vlan, and those end up with an empty $realport
	    #
	    next if ((!$realports{$index}) ||
		     ($realports{$index} =~ /ifIndex/));
386
	    print "$MACs{$index},$realports{$index},$vlan_number,". 
Leigh Stoller's avatar
Leigh Stoller committed
387
		"$interfaces{$index},$class_str\n";
388 389 390
	}
    }
}