checkports.in 4.47 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2
#
3
# Copyright (c) 2000-2012 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
# 
# {{{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/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23
#
24 25 26
use English;
use Getopt::Std;
use strict;
Leigh Stoller's avatar
Leigh Stoller committed
27

Robert Ricci's avatar
Robert Ricci committed
28 29 30 31 32 33 34
#
# checkports - Check to make sure that all ports for an experiment are
# up
#
# TODO: Add duplex checking
# TODO: Add the ability to check all ports at once
#
35 36 37 38 39 40 41 42 43
sub usage()
{
    print "Usage: $0 [-h] <eid>\n";
    print "  Checks all ports in pid/eid to make sure that the operational\n";
    print "  status (whether or not the port has carrier) matches the\n";
    print "  administrative status (whether or not the port is enabled.)\n";
    exit(-1);
}
my $optlist = "h";
Robert Ricci's avatar
Robert Ricci committed
44 45 46 47 48 49

#
# Configure variables
#
use lib '@prefix@/lib';

50 51 52 53
#
# Turn off line buffering on output
#
$| = 1;
Robert Ricci's avatar
Robert Ricci committed
54

55 56 57
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
Robert Ricci's avatar
Robert Ricci committed
58

59 60 61 62 63 64 65
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use Experiment;
66
use User;
67 68 69

# Optional alternate version of libraries.
use lib '@prefix@/lib/snmpit_test';
70
use snmpit_lib;
Robert Ricci's avatar
Robert Ricci committed
71

72 73
# The set of OIDs we care about
my @oids = ('ifAdminStatus','ifOperStatus');
Robert Ricci's avatar
Robert Ricci committed
74 75 76 77

#
# Process command-line arguments
#
78 79 80
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
Robert Ricci's avatar
Robert Ricci committed
81
}
82 83 84 85 86
if (defined($options{"h"})) {
    usage();
}
if (@ARGV != 1) {
    usage();
Robert Ricci's avatar
Robert Ricci committed
87 88
}

89 90 91
# Lookup user.
my $this_user = User->ThisUser();
if (! defined($this_user)) {
92
    die("You ($UID) do not exist!");
93
}
Robert Ricci's avatar
Robert Ricci committed
94

95 96 97
# Lookup experiment.
my $experiment = Experiment->Lookup($ARGV[0]);
if (!defined($experiment)) {
98
    die("Could not lookup experiment object for $ARGV[0]!")
Robert Ricci's avatar
Robert Ricci committed
99
}
100 101
my $pid       = $experiment->pid();
my $eid       = $experiment->eid();
Robert Ricci's avatar
Robert Ricci committed
102 103 104 105

#
# Make sure they have access to it
#
Robert Ricci's avatar
Robert Ricci committed
106
if (!$experiment->AccessCheck($this_user, TB_EXPT_READINFO)) {
Robert Ricci's avatar
Robert Ricci committed
107 108 109
    die "You do not have permission to view experiment $pid/$eid\n";
}

110
snmpit_lib::init(0);
Robert Ricci's avatar
Robert Ricci committed
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

#
# Get a list of all ports in the experiment
#
my @ports = getExperimentPorts($pid,$eid);

#
# Just exit now if there are no ports for this experiment
#
if (!@ports) {
    print "There are no switch ports for $pid/$eid\n";
    exit(0);
}

#
# Find out which devices these ports are on
#
my %portMap = mapPortsToDevices(@ports);

my @portinfo;
foreach my $name (keys %portMap) {
    my @ports = @{$portMap{$name}};

    #
    # Connect to the switch and get the data we want off of it
    #
    my $type = getDeviceType($name);
    my $device;
    SWITCH: for ($type) {
	/cisco/ && do {
	    require snmpit_cisco;
142
	    $device = new snmpit_cisco($name,0);
Robert Ricci's avatar
Robert Ricci committed
143 144 145 146
	    last;
	};
	/intel/ && do {
	    require snmpit_intel;
147
	    $device = new snmpit_intel($name,0);
Robert Ricci's avatar
Robert Ricci committed
148
	    last;
149 150 151 152 153 154
	};

	/foundry/ && do {
	    require snmpit_foundry;
	    $device = new snmpit_foundry($name,0);
	    last;
155 156 157 158 159
	};
	/nortel/ && do {
	    require snmpit_nortel;
	    $device = new snmpit_nortel($name,0);
	    last;
Robert Ricci's avatar
Robert Ricci committed
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
	};

	# 'default' case
	die "Unknown switch type ($type) for $name\n";
    }
    my @results = $device->getFields(\@ports,\@oids);

    foreach my $result (@results) {
	my $port = shift @ports;

	#
	# Try to translate the port name to the node's vname
	#
	$port =~ /^(.+):(\d+)/;
	my $nickname;
	if ($1) {
	    my $portnum = $2;
	    my ($junk, $vname);
	    NodeidToExp($1,\$junk,\$junk,\$vname);
	    $nickname = "$vname:$portnum";
	}

	#
	# Throw this onto a list, so that we can sort it
	#
	push @portinfo, [$nickname,$port,@$result];
    }
}

#
# Finally, print out the results
#
my $errors = 0;
foreach my $line (sort {$$a[0] cmp $$b[0]} @portinfo) {
    my ($nickname,$port,$adminStatus,$operStatus) = @$line;
    if ($adminStatus ne $operStatus) {
	print "Switch port for $nickname ($port) should be $adminStatus, " .
	    "but is $operStatus\n";
	$errors++;
    }
}

202
if (!$errors) {
Robert Ricci's avatar
Robert Ricci committed
203 204 205 206 207 208
    print "There were no errors found\n";
    exit(1);
} else {
    print "There were $errors errors found\n";
    exit(0);
}