checkports.in 3.71 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2010 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
7 8 9
use English;
use Getopt::Std;
use strict;
Leigh Stoller's avatar
Leigh Stoller committed
10

Robert Ricci's avatar
Robert Ricci committed
11 12 13 14 15 16 17
#
# 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
#
18 19 20 21 22 23 24 25 26
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
27 28 29 30 31 32

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

33 34 35 36
#
# Turn off line buffering on output
#
$| = 1;
Robert Ricci's avatar
Robert Ricci committed
37

38 39 40
# 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
41

42 43 44 45 46 47 48
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use Experiment;
49
use User;
50
use snmpit_lib;
Robert Ricci's avatar
Robert Ricci committed
51

52 53
# The set of OIDs we care about
my @oids = ('ifAdminStatus','ifOperStatus');
Robert Ricci's avatar
Robert Ricci committed
54 55 56 57

#
# Process command-line arguments
#
58 59 60
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
Robert Ricci's avatar
Robert Ricci committed
61
}
62 63 64 65 66
if (defined($options{"h"})) {
    usage();
}
if (@ARGV != 1) {
    usage();
Robert Ricci's avatar
Robert Ricci committed
67 68
}

69 70 71
# Lookup user.
my $this_user = User->ThisUser();
if (! defined($this_user)) {
72
    die("You ($UID) do not exist!");
73
}
Robert Ricci's avatar
Robert Ricci committed
74

75 76 77
# Lookup experiment.
my $experiment = Experiment->Lookup($ARGV[0]);
if (!defined($experiment)) {
78
    die("Could not lookup experiment object for $ARGV[0]!")
Robert Ricci's avatar
Robert Ricci committed
79
}
80 81
my $pid       = $experiment->pid();
my $eid       = $experiment->eid();
Robert Ricci's avatar
Robert Ricci committed
82 83 84 85

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

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

#
# 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;
122
	    $device = new snmpit_cisco($name,0);
Robert Ricci's avatar
Robert Ricci committed
123 124 125 126
	    last;
	};
	/intel/ && do {
	    require snmpit_intel;
127
	    $device = new snmpit_intel($name,0);
Robert Ricci's avatar
Robert Ricci committed
128
	    last;
129 130 131 132 133 134
	};

	/foundry/ && do {
	    require snmpit_foundry;
	    $device = new snmpit_foundry($name,0);
	    last;
135 136 137 138 139
	};
	/nortel/ && do {
	    require snmpit_nortel;
	    $device = new snmpit_nortel($name,0);
	    last;
Robert Ricci's avatar
Robert Ricci committed
140 141 142 143 144 145 146 147 148 149 150 151 152
	};

	# '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
	#
153
	$port =~ /^(.+):(\d+)/;
Robert Ricci's avatar
Robert Ricci committed
154
	my $nickname;
155 156
	if ($1) {
	    my $portnum = $2;
Robert Ricci's avatar
Robert Ricci committed
157
	    my ($junk, $vname);
158
	    NodeidToExp($1,\$junk,\$junk,\$vname);
Robert Ricci's avatar
Robert Ricci committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	    $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) {
176
	print "Switch port for $nickname ($port) should be $adminStatus, " .
Robert Ricci's avatar
Robert Ricci committed
177 178 179 180 181
	    "but is $operStatus\n";
	$errors++;
    }
}

182
if (!$errors) {
Robert Ricci's avatar
Robert Ricci committed
183 184 185 186 187 188
    print "There were no errors found\n";
    exit(1);
} else {
    print "There were $errors errors found\n";
    exit(0);
}