portstats.in 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 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 108 109 110 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 142 143 144 145 146 147 148 149 150 151 152
#!/usr/bin/perl -w
#
# portstats - Get port statistics for all nodes in an experiment
#

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

use libdb;
use snmpit_lib;
use snmpit_cisco;

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

sub usage {
    print "Usage: $0 <pid> <eid> [vname ...] [vname:port ...]\n\n";
    print "If only pid and eid are given, prints out information about all\n";
    print "ports in the experiment. Otherwise, output is limited to the\n";
    print "nodes and/or ports given.\n";
    return 1;
}

if (@ARGV < 2) {
    exit &usage;
}
my ($pid,$eid) = (shift,shift);

my @passedPorts = @ARGV;


#
# 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";
}

snmpit_lib::init(0);

#
# Get a list of all ports in the experiment
#
my @experimentPorts = getExperimentPorts($pid,$eid);
my @ports;
if (@passedPorts) {
    #
    # Handle a set of passed-in ports
    #
    foreach my $port (@passedPorts) {
	$port =~ /^([^:]+)(:(\d+))?$/;
	my ($hostname,$portnumber) = ($1,$3);
	my $nodeid;
	if (!VnameToNodeid($pid,$eid,$hostname,\$nodeid)) {
	    die "There is no node $hostname in $pid/$eid\n";
	}

	if (defined($portnumber)) {
	    # They gave us a specific interface
	    push @ports, "$nodeid:$portnumber";
	} else {
	    # We need to find all experimental ports for this node
	    push @ports, grep(/^$nodeid:(\d+)$/,@experimentPorts);
	}

    }
} else {
    #
    # They didn't ask for specific ports, so we'll use 'em all
    #
    @ports = @experimentPorts;
}

#
# List of OIDs we want to look at for each port
#
my @oids = ('ifInOctets','ifInUcastPkts','ifInNUcastPkts','ifInDiscards',
            'ifInErrors','ifInUnknownProtos','ifOutOctets','ifOutUcastPkts',
	    'ifOutNUcastPkts','ifOutDiscards','ifOutErrors','ifOutQLen');

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

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

    #
    # Connect to the switch and get the data we want off of it
    #
    my $device = new snmpit_cisco($name);
    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+)/;
	if ($1) {
	    my $portnum = $2;
	    my ($junk, $vname);
	    NodeidToExp($1,\$junk,\$junk,\$vname);
	    $port = "$vname:$portnum";
	}

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

#
# Print out the column headers and define the output format
#
my ($port, $inoctets, $inunicast, $innunicast, $indiscards, $inerr, $inunk,
    $outoctets, $outunicast, $outnunicast, $outdiscards, $outerr,$outq);

print << "END";
                       In  InUnicast InNUnicast         In         In    Unknown        Out OutUnicast  OutNUcast        Out        Out   OutQueue
Port               Octets    Packets    Packets   Discards     Errors   Protocol     Octets    Packets    Packets   Discards     Errors     Length
---------------------------------------------------------------------------------------------------------------------------------------------
END
format stats =
@<<<<<<<<<<<<< @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> @>>>>>>>>> 
$port,    $inoctets, $inunicast,$innunicast,$indiscards,$inerr,  $inunk,    $outoctets,$outunicast,$outnunicast,$outdiscards,$outerr,$outq
.
$FORMAT_NAME = 'stats';

#
# Finally, print out the results
#
foreach my $line (sort {$$a[0] cmp $$b[0]} @stats) {
    ($port, $inoctets, $inunicast, $innunicast, $indiscards, $inerr, $inunk,
     $outoctets, $outunicast, $outnunicast, $outdiscards, $outerr, $outq)
	 = @$line;
    write;
}