checkports.in 3.76 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;
Weibin Sun's avatar
Weibin Sun committed
51
use Port;
Robert Ricci's avatar
Robert Ricci committed
52

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

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

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

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

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

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

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

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

	# '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
	#
	my $nickname;
Weibin Sun's avatar
Weibin Sun committed
155 156
	if (defined($port->node_id())) {
	    my $portnum = $port->port();
Robert Ricci's avatar
Robert Ricci committed
157
	    my ($junk, $vname);
Weibin Sun's avatar
Weibin Sun committed
158
	    NodeidToExp($port->node_id(),\$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) {
Weibin Sun's avatar
Weibin Sun committed
176 177
	print "Switch port for $nickname (".$port->toString().
	    ") should be $adminStatus, " .
Robert Ricci's avatar
Robert Ricci committed
178 179 180 181 182
	    "but is $operStatus\n";
	$errors++;
    }
}

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