checkports.in 3.2 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2 3 4 5 6 7 8

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

Robert Ricci's avatar
Robert Ricci committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 97 98 99 100 101 102 103 104 105 106 107
#
# 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
#


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

use libdb;
use snmpit_lib;

use English;
use Getopt::Long;
use strict;

sub usage {
    print << "END";
Usage: $0 [-h] <pid> <eid>
-h    This message

Checks all ports in pid/eid to make sure that the operational status (whether
or not the port has carrier) matches the administrative status (whether or not
the port is enabled.)
END

    return 1;
}

#
# Process command-line arguments
#
my %opt = ();
GetOptions(\%opt,'h');

if ($opt{h}) {
    exit &usage;
}

my @oids = ('ifAdminStatus','ifOperStatus'); # The set of OIDs we care about

if (@ARGV != 2) {
    exit &usage;
}

snmpit_lib::init(0);

my($pid,$eid) = (shift,shift);

#
# First, make sure the experiment exists
#
if (!ExpState($pid,$eid)) {
    die "There is no experiment $eid in project $pid\n";
}

#
# Make sure they have access to it
#
if (!TBExptAccessCheck($UID,$pid,$eid,TB_EXPT_READINFO)) {
    die "You do not have permission to view experiment $pid/$eid\n";
}


#
# 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;
108
	    $device = new snmpit_cisco($name,0);
Robert Ricci's avatar
Robert Ricci committed
109 110 111 112
	    last;
	};
	/intel/ && do {
	    require snmpit_intel;
113
	    $device = new snmpit_intel($name,0);
Robert Ricci's avatar
Robert Ricci committed
114
	    last;
115 116 117 118 119 120
	};

	/foundry/ && do {
	    require snmpit_foundry;
	    $device = new snmpit_foundry($name,0);
	    last;
121 122 123 124 125
	};
	/nortel/ && do {
	    require snmpit_nortel;
	    $device = new snmpit_nortel($name,0);
	    last;
Robert Ricci's avatar
Robert Ricci committed
126 127 128 129 130 131 132 133 134 135 136 137 138 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
	};

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

168
if (!$errors) {
Robert Ricci's avatar
Robert Ricci committed
169 170 171 172 173 174
    print "There were no errors found\n";
    exit(1);
} else {
    print "There were $errors errors found\n";
    exit(0);
}