portstats.in 5.51 KB
Newer Older
1
#!/usr/bin/perl
2
#
3
# portstats - Get port statistics for nodes in an experiment
4
#
5 6 7 8 9
#
# NOTE: no -w, because $::line below is used in the eval, which perl
# can't pick up on, so it warns about this variable being only used once
#

10 11 12 13 14 15 16 17 18 19 20 21 22 23

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

use libdb;
use snmpit_lib;

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

sub usage {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    print << "END";
Usage: $0 [-h] <pid> <eid> [vname ...] [vname:port ...]
-h    This message
-e    Show only error counters
-a    Show all stats

If only pid and eid are given, prints out information about all ports in the
experiment. Otherwise, output is limited to the nodes and/or ports given.

NOTE: Statistics are reported from the switch's perspective. This means that
'In' packets are those sent FROM the node, and 'Out' packets are those
sent TO the node.

In the output, packets described as 'NUnicast' or 'NUcast' are non-unicast
(broadcast or multicast) packets.
END

41 42 43
    return 1;
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#
# Process command-line arguments
#
my %opt = ();
GetOptions(\%opt,'h','a','e','p','b');

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

my @oids = (); # The set of OIDs we care about

if ($opt{a}) {
    @oids = ('ifInOctets', 'ifInUcastPkts', 'ifInNUcastPkts', 'ifInDiscards',
	     'ifInErrors', 'ifInUnknownProtos', 'ifOutOctets', 'ifOutUcastPkts',
	     'ifOutNUcastPkts', 'ifOutDiscards', 'ifOutErrors', 'ifOutQLen');
} elsif ($opt{e}) {
    @oids = ('ifInDiscards', 'ifInErrors', 'ifInUnknownProtos', 'ifOutDiscards',
	     'ifOutErrors');
} else {
    @oids = ('ifInOctets', 'ifInUcastPkts', 'ifInNUcastPkts',
	     'ifOutOctets', 'ifOutUcastPkts', 'ifOutNUcastPkts');
}

68 69 70 71 72 73 74
if (@ARGV < 2) {
    exit &usage;
}
my ($pid,$eid) = (shift,shift);

my @passedPorts = @ARGV;

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#
# This hash is used to create colmn headers and the format string
#
my %oids = (
    'ifInOctets'        => [['        In','    Octets'], '@>>>>>>>>>'],
    'ifInUcastPkts'     => [[' InUnicast','   Packets'], '@>>>>>>>>>'],
    'ifInNUcastPkts'    => [['InNUnicast','   Packets'], '@>>>>>>>>>'],
    'ifInDiscards'      => [['        In','  Discards'], '@>>>>>>>>>'],
    'ifInErrors'        => [['        In','    Errors'], '@>>>>>>>>>'],
    'ifInUnknownProtos' => [['   Unknown','  Protocol'], '@>>>>>>>>>'],
    'ifOutOctets'       => [['       Out','    Octets'], '@>>>>>>>>>'],
    'ifOutUcastPkts'    => [['OutUnicast','   Pakcets'], '@>>>>>>>>>'],
    'ifOutNUcastPkts'   => [[' OutNUcast','   Packets'], '@>>>>>>>>>'],
    'ifOutDiscards'     => [['       Out','  Discards'], '@>>>>>>>>>'],
    'ifOutErrors'       => [['       Out','    Errors'], '@>>>>>>>>>'],
    'ifOutQLen'         => [['  OutQueue','    Length'], '@>>>>>>>>>']
);
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 153 154 155 156 157

#
# 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
#

#
# 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
    #
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    my $type = getDeviceType($name);
    my $device;
    SWITCH: for ($type) {
	/cisco/ && do {
	    require snmpit_cisco;
	    $device = new snmpit_cisco($name);
	    last;
	};
	/intel/ && do {
	    require snmpit_intel;
	    $device = new snmpit_intel($name);
	    last;
	};

	# 'default' case
	die "Unknown switch type ($type) for $name\n";
    }
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    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];
    }
}

#
199
# Build up the heading a format strings
200
#
201 202 203 204 205 206 207 208 209 210
my @heading1 = ('              ');
my @heading2 = ('Port          ');
my @format   = ('@<<<<<<<<<<<<<');

foreach my $line (@oids{@oids}) {
    my ($heading,$format) = @$line;
    push @heading1, $$heading[0];
    push @heading2, $$heading[1];
    push @format,   $format;
}
211

212 213 214 215 216 217 218
my $heading1 = join(" ",@heading1);
my $heading2 = join(" ",@heading2);

my $format = "format stats =\n" . join(" ",@format) . "\n";
$format .= join(",",map {"\$\$::line[$_]"} (0 .. @oids)) . "\n.\n";

eval $format;
219 220
$FORMAT_NAME = 'stats';

221 222 223 224 225 226 227
#
# Print out the heading
#
print "$heading1\n";
print "$heading2\n";
print "-" x length($heading1),"\n";

228 229 230
#
# Finally, print out the results
#
231
foreach $::line (sort {$$a[0] cmp $$b[0]} @stats) {
232 233
    write;
}