Commit 438cef80 authored by Mac Newbold's avatar Mac Newbold
Browse files

First big round of bug fixes in modularized snmpit.

parent 536f0dcd
......@@ -2,12 +2,7 @@
#
# Testbed Power Control script
#
# Mac Newbold, Flux Research Group, Univ. of Utah Computer Science Dept.
# Created: April 18, 2000
# Last Changed: April 18, 2000
#
# Syntax:
#
# power on <machine> <machine> ...
# power off <machine> <machine> ...
# power cycle <machine> <machine> ...
......@@ -16,6 +11,13 @@
# useful values are 0, 1, 2 and 3
my $verbose = 0;
#If there is -d or -v, turn it on right now...
if ( join(" ",@ARGV) =~ /^-[d|v](\s*)(\d*)\s[oc]/ ) {
shift;
if (defined $1 && $1 ne "") { shift; }
if (defined $2 && $2 ne "") { $verbose = $2; } else { $verbose = 1; }
}
print "VERBOSE ON: Set to level $verbose\n" if $verbose;
#
# Configure variables
......@@ -40,9 +42,9 @@ my $exitval = 0;
my $snmpit="$TB/bin/snmpit";
#Must have at least an op and a machine, so at least 2 ARGV
die("Usage:\npower on <machine> <machine> ...\n",
"power off <machine> <machine> ...\n",
"power cycle <machine> <machine> ...\n")
die("Usage:\npower [-v[n]] on <machine> <machine> ...\n",
"power [-v[n]] off <machine> <machine> ...\n",
"power [-v[n]] cycle <machine> <machine> ...\n")
if ( @ARGV < 2 );
#Read in ARGV
......@@ -211,11 +213,14 @@ foreach $node ( sort @machines) {
print "Power controller for $node not found. Skipping...\n";
$exitval++;
} else {
my $cmd=join(" ",
($snmpit,'-v','-i',$IPList{$node},$op,$OutletList{$node}));
print "Calling snmptit: '$cmd'\n" if $verbose > 1;
my $cmd = ( $verbose > 1 ? "-debug $verbose " : "");
$cmd .= join(" ",
('-v','-i',$IPList{$node},$op,$OutletList{$node}));
$cmd = "$snmpit ".$cmd;
print "Calling snmpit: '$cmd'\n" if $verbose > 1;
open(OUT,$cmd." |");
while (<OUT>) {
if ($verbose) { print $_; }
if (/was outlet([OnfRebot]+)/) {
print "$node was $1";
}
......
......@@ -39,6 +39,9 @@ if ( join(" ",@ARGV) =~ /-debug\s*(\d*)/ ) {
}
my $device;
my $pid;
my $eid;
my $t = 0;
&myMain;
......@@ -82,6 +85,20 @@ sub myMain {
# "m=$m vlan=",@vlan," r=",@r,"u=$u b=$b c=$c v=$v g=$g on=$on ",
# "off=$off cyc=$cyc all=$all device=$device\n" if $debug;
my $sess2;
if ($device eq "APC") {
use SNMP;
$SNMP::debugging = ($debug - 5) if $debug > 5;
&SNMP::addMibDirs('/usr/local/share/snmp/mibs');
&SNMP::addMibFiles('/usr/local/share/snmp/mibs/PowerNet-MIB.txt');
$SNMP::save_descriptions = 1; # must be set prior to mib initialization
SNMP::initMib(); # parses default list of Mib modules
$SNMP::use_enums = 1; #use enum values instead of only ints
print "Opening SNMP session to $i..." if $debug;
$sess2 =new SNMP::Session(DestHost => $i, Community => 'private');
print "".(defined $sess2 ? "Succeeded" : "Failed")."\n" if $debug;
}
#
# Port Level Control
#
......@@ -187,7 +204,7 @@ sub AutoVLANConfig {
local(*f) = @_;
my @VLANS= ();
my @names= ();
&ReadIRFile(*f,\@names,\@VLANS,\%Interfaces);
&ReadIRFile(*f,\@names,\@VLANS);
print "AutoVLAN got VLANs named: ",join (" ",@names),"\n" if $debug;
$device->vlanLock;
my $m="";
......@@ -202,6 +219,30 @@ sub AutoVLANConfig {
}
sub UpdateField {
local(*session,*OID,*ports,*val,*b,*c,*v)= @_;
print "sess=$session $OID '",join(" ",@ports),"' $val $b $c $v\n" if $debug;
my $Status = 0;
my $retval;
my $i = $session->{DestHost};
foreach my $port (@ports) {
print "Checking port $port of $i for $val..." if $v;
$Status = $session->get([[$OID,$port]]);
if (!defined $Status) {
print STDERR "Port $port, change to $val: No answer from device\n";
} else {
print "Okay.\nPort $port was $Status\n" if $v;
if ($Status ne $val) {
print "Setting $port to $val..." if $v;
$retval = $session->set([[$OID,$port,$val,"INTEGER"]]);
print "Set returned '$retval'" if $v;
}
}
}
1;
}
sub ParseArgs {
local(*CMDS,*i,*p,*d,*e,*a,*dup,*spd,*s,*f,*l,*m,*vlan,*r,*u,*b,*c,*v,*g,
*on,*off,*cyc,*all,*outlets) = @_;
......@@ -250,6 +291,8 @@ sub ParseArgs {
elsif (/^-l(.*)/) {$l = 1;}
elsif (/^-u(.*)/) {$u = 1;}
elsif (/^-f(.*)/) {$f = ($1 ? $1 : shift(@CMDS));}
elsif (/^-t(.*)/) {$pid = ($1 ? $1 : shift(@CMDS));
$eid = shift(@CMDS); }
elsif (/^-m(.*)/) {$m = ( !(@CMDS==0 || $CMDS[0] =~ /^(-|\+)(.*)/) ?
($1 ? $1 : shift(@CMDS)) : "");}
elsif (/^-vlan(.*)/) {
......@@ -280,15 +323,13 @@ sub ParseArgs {
"snmpit - A general purpose SNMP Tool - Version 1.1\n",
"Syntax:\n",
" snmpit [-h] [-v] -i<ip>\n",
"\t[-u] [-l] [-t <pid>/<eid>] [-m<vlan name>] [-vlan<port>]\n",
"\t[-u] [-l] [-t <pid> <eid>] [-m<vlan name>] [-vlan<port>]\n",
"\t[-f<filename>] [-r<vlan #> <vlan #> ... ]\n",
"\t[+b|-b] [+c|-c] [-g] [-s] [-p <port> <port x>..<port y> ... ]\n",
"\t[-d|-e] [+a|-a] [-spd<speed>] [-dup<duplex>]\n";
# Hidden Power control functionality
#,
# "\t[ [-on|-off|-cyc] [-all] | [-<n>] ]\n";
}
if ($help > 1) {
print
......@@ -315,7 +356,6 @@ sub ParseArgs {
" +a/-a Enable/Disable Port Auto-Negotiation of speed/duplex\n",
" -spd Port Speed (10 or 100 Mbits)\n",
" -dup Port Duplex (half or full)\n";
# Hidden Power control functionality
#,
# " -on Turn on outlets\n",
......@@ -324,7 +364,6 @@ sub ParseArgs {
# " -all All outlets\n",
# " -(n) Outlet #(n) (n=1-8)\n";
}
#Unused letters of the alphabet:
#
# j, k, n, q, w, x, y, z
......@@ -333,15 +372,17 @@ sub ParseArgs {
die("\b\n") if ($help);
#Now die on any combinations that don't make sense...
die("What shall I do?\n")
if (!($d||$e||$a||$spd||$dup||$s||$g||@vlan||$f||@r||$l
if (!($d||$e||$a||$spd||$dup||$s||$g||@vlan||$f||$t||@r||$l
||$on||$off||$cyc));
if (!defined($i)||!$i||$i eq "") { $i = "cisco"; }
if ( defined Dev($i) ) {
if (Dev($i) =~ /cisco/i) {
$device = new snmpit_cisco($i);
$device = new snmpit_cisco
($i,"debug=$debug","block=$b","confirm=$c","verbose=$v");
} elsif ($i =~ /cisco/i) {
$device = new snmpit_cisco(Dev($i));
$device = new snmpit_cisco
(Dev($i),"debug=$debug","block=$b","confirm=$c","verbose=$v");
# } elsif ($i =~ /intel/ || Dev($i) =~ /intel/) {
# $device = new snmpit_intel;
} else { $device="APC"; }
......@@ -494,5 +535,3 @@ sub ReadIRFile {
}
close (IR);
}
##### EOF #####
......@@ -2,31 +2,18 @@
#
# snmpit module for Cisco Catalyst 6509 switches
#
# Supported methods:
#
# new Needs to take IP, and initialize snmp session
# portControl (cmd,ports)
# cmd = {enable, disable, 10mbit, 100mbit, full, half, auto}
# ports = list of "pcX:Y"
# showPorts print settings of all ports
# getStats print various stats
# vlanLock Get a vlan editing lock/token/buffer, and
# vlanUnlock Release it when done
# setupVlan (name,ports) - take name and list of "pcX:Y", and vlan them
# removeVlan (vlan) - takes a vlan number
# listVlans returns a list of entries, with each entry having an
# ID, a name, and a list of members (pcX:Y)
package snmpit_cisco;
$| = 1; # Turn off line buffering on output
my $debug = 1;
my $block = 1;
my $verbose = 0 + $debug;
local $debug = 0;
local $block = 1;
local $verbose = 0 + $debug;
use SNMP;
use snmpit_lib;
use English;
my $sess; # My snmp session - initialized in new()
my $ip;
......@@ -38,6 +25,17 @@ sub new {
my $classname = shift;
$ip = shift;
while ($arg = shift ) {
my ($var,$val) = split("=",$arg);
${$var} = $val;
print "$var=${$var}\n" if $debug || $verbose;
}
if ($debug) {
print "snmpit_cisco module initializing... debug level $debug\n";
}
#die("\n");
$SNMP::debugging = ($debug - 2) if $debug > 2;
my $mibpath = '/usr/local/share/snmp/mibs';
&SNMP::addMibDirs($mibpath);
......@@ -157,15 +155,14 @@ sub setupVlan {
# This is to be called ONLY after vlanLock has been called successfully!
shift;
my $name = shift;
my $origname = $name;
my @vlan = @_;
foreach $mac (@vlan) {
my $node = $Interfaces{$mac};
foreach $node (@vlan) {
if (! NodeCheck($node)) {
print STDERR "You are not authorized to control $node.\n";
return 1;
}
}
my $okay = 0;
my $attempts = 0;
my $VlanType = '.1.3.6.1.4.1.9.9.46.1.4.2.1.3.1'; # vlan # is index
......@@ -174,6 +171,7 @@ sub setupVlan {
my $VlanRowStatus = '.1.3.6.1.4.1.9.9.46.1.4.2.1.11.1'; # vlan # is index
while (($okay == 0) && ($attempts <3)) {
$attempts++;
$name = $origname;
print "***Okay=$okay, Attempts=$attempts (limit of 3)\n" if $debug;
my $N = 2;
my $RetVal = $sess->get([[$VlanRowStatus,$N]]);
......@@ -193,11 +191,11 @@ sub setupVlan {
[$VlanName,$N,$name,"OCTETSTR"],
[$VlanSAID,$N,$SAID,"OCTETSTR"]]);
print "",($RetVal? "Succeeded":"Failed"), ".\n";
if (!$RetVal) {
print STDERR "VLAN Create '$name' as VLAN $N failed.\n";
if (!$RetVal) {
print STDERR "VLAN Create '$name' as VLAN $N failed.\n";
} else {
$RetVal = &vlanUnlock(*sess,$verbose,1); #send last as 1 to get result
print "Got $RetVal from Release Token\n" if $debug;
print "Got $RetVal from vlanUnlock\n" if $debug;
# If RetVal isn't succeeded, then I can try and fix it
my $tries = 0;
&vlanLock(\$sess,\$v);
......@@ -206,17 +204,22 @@ sub setupVlan {
$tries += 1;
if ($RetVal eq "someOtherError" && $tries <= 2) {
#Duplicate name... correct and try again..
my $name = $sess->get([[$VlanName,$N]]);
print STDERR "VLAN name '$name' might already be in use. ".
"Renaming to '_$name'.\n";
$name = $sess->set([[$VlanName,$N,"_$name","OCTETSTR"]]);
$RetVal = &vlanUnlock(*sess,$verbose,1); #send last as 1 to get result
print "Got $RetVal from Release Token\n" if $debug;
&vlanLock(\$sess,\$verbose);
print STDERR "VLAN name '$name' might already be in use.\n".
"Renaming to '_$name'...";
$name = "_$name";
#$RetVal = $sess->set([[$VlanName,$N,"$name","OCTETSTR"]]);
$RetVal = $sess->set([[$VlanRowStatus,$N,"createAndGo","INTEGER"],
[$VlanType,$N,"ethernet","INTEGER"],
[$VlanName,$N,$name,"OCTETSTR"],
[$VlanSAID,$N,$SAID,"OCTETSTR"]]);
print "",($RetVal? "Succeeded":"Failed"), ".\n";
$RetVal = &vlanUnlock(*sess,$verbose,1); #send last 1 to get result
print "Got $RetVal from vlanUnlock\n" if $debug;
&vlanLock(\$sess,\$verbose);
} else {
# Try again from the beginning... maybe we're using a bad VLAN #
print STDERR "VLAN creation failed.".
($attempts<3 ?" Trying again...\n":
($attempts<3 ?" Making attempt number $attempts...\n":
" Third failure, giving up.\n");
$okay=0;
last;
......@@ -228,17 +231,17 @@ sub setupVlan {
# VLAN exists now - Add the ports:
# @vlan is a list of MACs, so I need to find out what they are...
my $PortVlanMemb = ".1.3.6.1.4.1.9.9.68.1.2.2.1.2"; #index is ifIndex
foreach $mac (@vlan) {
my $if = $mac;
foreach $node (@vlan) {
my $if = $node;
if ($if =~ /(sh\d+)-\d(:\d)/) { $if = "$1$2"; }
my $port = (split(":",portnum($if)))[1];
my $IF = $ifIndex{$port};
print "Found $mac -> $if -> $port -> $IF\n" if $debug;
if (!defined $IF) {
print " Addding MAC address $mac - port not found ... Failed\n";
print "Found $node -> $if -> $port -> $IF\n" if $debug;
if (!defined $port) {
print " Addding $node - port not found ... Failed\n";
next;
}
print " Adding MAC Address $mac (ifIndex $IF) ... ";
print " Adding $node (port $port) ... ";
$RetVal = $sess->set([[$PortVlanMemb,$IF,$N,'INTEGER']]);
print "",($RetVal? "Succeeded":"Failed"), ".\n";
my $Admin = ".1.3.6.1.2.1.2.2.1.7";
......@@ -250,6 +253,7 @@ sub setupVlan {
#If everything went okay, break out of the loop
$okay = 1;
}
print "Currently $attempts attempts.\n" if $debug;
}
}
......@@ -461,41 +465,19 @@ sub listVlans {
my $id;
my $memberstr;
# I'm trying out using formats. See perldoc perlform for details.
format =
format vlan =
@<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
($id),($Names{$id}), $memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$memberstr
.
$FORMAT_NAME = "vlan";
foreach $id ( sort num keys (%Names) ) {
$memberstr = (defined(@{$Members{$id}}) ?
join(" ",sort alphanum(@{$Members{$id}})) : "");
write;
}
$FORMAT_NAME = "STDOUT"; # Go back to the normal format
}
sub showPorts {
......@@ -588,15 +570,17 @@ sub getStats {
print "Port Statistics, Testbed Switch $ip\n";
print " InUcast InNUcast In In Unknown OutUcast OutNUcast Out Out OutQueue\n";
print "Interface InOctets Packets Packets Discards Errors Proto. OutOctets Packets Packets Discards Errors Length\n";
print "------------------------------------------------------------------------------------------------------------------------------------\n";
format =
print "------------------------------------------------------------------------------------------------------------------------------------\n";
format stats =
#nterface InOctets Packets Packets Discards Errors Proto. Out Octets Packets Packets Discards Errors Length
@<<<<<<<< @<<<<<<<<<<< @<<<<<<<<< @<<<<<<<< @<<<<<<< @<<<<< @<<<<<< @<<<<<<<<<<< @<<<<<<<<< @<<<<<<<< @<<<<<<< @<<<<< @<<<<<<<
$port,$inOctets{$port},$inUcast{$port},$inNUcast{$port},$inDiscard{$port},$inErr{$port},$inUnkProt{$port},$outOctets{$port},$outUcast{$port},$outNUcast{$port},$outDiscard{$port},$outErr{$port},$outQLen{$port}
.
$FORMAT_NAME = "stats";
foreach $port ( sort alphanum2 keys (%inOctets) ) {
write;
}
$FORMAT_NAME = "STDOUT"; # Go back to normal format
}
sub readifIndex {
......
......@@ -2,20 +2,6 @@
#
# snmpit module for Intel EtherExpress 510T switches
#
# Supported methods:
#
# new Needs to take IP, and initialize snmp session
# portControl (cmd,ports)
# cmd = {enable, disable, 10mbit, 100mbit, full, half, auto}
# ports = list of "pcX:Y"
# showPorts print settings of all ports
# getStats print various stats
# vlanLock Get a vlan editing lock/token/buffer, and
# vlanUnlock Release it when done
# setupVlan (name,ports) - take name and list of "pcX:Y", and vlan them
# removeVlan (vlan) - takes a vlan number
# listVlans returns a list of entries, with each entry having an
# ID, a name, and a list of members (pcX:Y)
package snmpit_intel;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment