checkports.in 3.7 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-2007 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 49
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use Experiment;
use snmpit_lib;
Robert Ricci's avatar
Robert Ricci committed
50

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

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

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

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

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

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

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

	/foundry/ && do {
	    require snmpit_foundry;
	    $device = new snmpit_foundry($name,0);
	    last;
134 135 136 137 138
	};
	/nortel/ && do {
	    require snmpit_nortel;
	    $device = new snmpit_nortel($name,0);
	    last;
Robert Ricci's avatar
Robert Ricci committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	};

	# '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++;
    }
}

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