Commit c069a419 authored by Robert Ricci's avatar Robert Ricci

Change the way the switch modules for snmpit get their configuration

information. Rather than pass it all in, which was getting very
cumbersome, and inconsistent between Cisco and Intel switches, the
modules query the database themselves (via a new function in
snmpit_lib.pm).

Also in this commit are two new options for switch stacks - the
ability to specify minimum and maximum VLAN number to use.
parent edc4eac8
......@@ -1020,3 +1020,9 @@ last_net_act,last_cpu_act,last_ext_act);
have to read that file!
alter table nodes add sfshostid varchar(128) default NULL;
1.194: Add a range of VLANs to be used, per switch stack. NULL values will
default to the old VLAN range.
alter table switch_stack_types add column min_vlan int default NULL;
alter table switch_stack_types add column max_vlan int default NULL;
......@@ -105,12 +105,12 @@ foreach my $name (keys %portMap) {
SWITCH: for ($type) {
/cisco/ && do {
require snmpit_cisco;
$device = new snmpit_cisco($name,0,$type);
$device = new snmpit_cisco($name,0);
last;
};
/intel/ && do {
require snmpit_intel;
$device = new snmpit_intel($name);
$device = new snmpit_intel($name,0);
last;
};
......
......@@ -18,7 +18,6 @@
use lib '@prefix@/lib';
my $TESTMODE = @TESTMODE@;
my $TB = '@prefix@';
my $COMMUNITY ='@SNMP_RW_COMMUNITY@';
use libdb;
use snmpit_lib;
......@@ -32,10 +31,6 @@ use strict;
#
my $debug = 0;
if (!$COMMUNITY) {
$COMMUNITY = "public";
}
######################################################################
# Step 1 - Process command-line arguments
#
......@@ -417,26 +412,18 @@ foreach my $command (@commands) {
die "Switch stack $stack_id does not support private VLANs\n";
}
#
# If we got back a special community to use for this switch, use it -
# otherwise, use the default we got from configure
#
if (!$community) {
$community = $COMMUNITY;
}
my $stack;
debug("Stack $stack_id has type $stack_type\n");
SWITCH: for ($stack_type) {
(/cisco/ || /catalyst/) && do {
require snmpit_cisco_stack;
$stack = new snmpit_cisco_stack($stack_id,$debug,$community,
$supports_private, $single_domain, @{$stacks{$stack_id}});
$stack = new snmpit_cisco_stack($stack_id,$debug,$single_domain,
@{$stacks{$stack_id}});
last;
}; # /cisco/
/intel/ && do {
require snmpit_intel_stack;
$stack = new snmpit_intel_stack($stack_id,$debug,$community,
$stack = new snmpit_intel_stack($stack_id,$debug,
@{$stacks{$stack_id}});
last;
};
......
......@@ -54,18 +54,13 @@ my $PORT_FORMAT_IFINDEX = 1;
my $PORT_FORMAT_MODPORT = 2;
my $PORT_FORMAT_NODEPORT = 3;
#
# Highest-numbered VLAN we will try
#
my $MAX_VLAN_NUMBER = 999;
#
# Creates a new object.
#
# usage: new($classname,$devicename,$debuglevel)
# usage: new($classname,$devicename,$debuglevel,$community)
# returns a new object, blessed into the snmpit_cisco class.
#
sub new($$$$$$) {
sub new($$$;$) {
# The next two lines are some voodoo taken from perltoot(1)
my $proto = shift;
......@@ -73,9 +68,7 @@ sub new($$$$$$) {
my $name = shift;
my $debugLevel = shift;
my $switchtype = shift;
my $community = shift;
my $supportsPrivate = shift;
#
# Create the actual object
......@@ -93,12 +86,32 @@ sub new($$$$$$) {
$self->{BLOCK} = 1;
$self->{BULK} = 1;
$self->{NAME} = $name;
$self->{COMMUNITY} = $community;
$self->{SUPPORTS_PRIVATE} = $supportsPrivate;
# Figure out some stuff about this switch
$switchtype =~ /^(\w+)(-ios)?$/;
#
# Get config options from the database
#
my $options = getDeviceOptions($self->{NAME});
if (!$options) {
warn "ERROR: Getting switch options for $self->{NAME}\n";
return undef;
}
$self->{SUPPORTS_PRIVATE} = $options->{'supports_private'};
$self->{MIN_VLAN} = $options->{'min_vlan'};
$self->{MAX_VLAN} = $options->{'max_vlan'};
if ($community) { # Allow this to over-ride the default
$self->{COMMUNITY} = $community;
} else {
$self->{COMMUNITY} = $options->{'snmp_community'};
}
#
# We have to change our behavior depending on what OS the switch runs
#
$options->{'type'} =~ /^(\w+)(-ios)?$/;
$self->{SWITHCTYPE} = $1;
if ($2) {
$self->{OSTYPE} = "IOS";
} else {
......@@ -623,10 +636,9 @@ sub createVlan($$;$$$) {
if (!$vlan_number) {
#
# Find a free VLAN number to use. Since 1 is the default VLAN on
# Ciscos, we start with number 2.
# Find a free VLAN number to use.
#
$vlan_number = 2; # We need to start at 2
$vlan_number = $self->{MIN_VLAN};
my $RetVal = snmpitGetWarn($self->{SESS},
[$VlanRowStatus,"1.$vlan_number"]);
if (!defined($RetVal)) {
......@@ -638,7 +650,7 @@ sub createVlan($$;$$$) {
}
$self->debug("Row $vlan_number got '$RetVal'\n",2);
while (($RetVal ne 'NOSUCHINSTANCE') &&
($vlan_number <= $MAX_VLAN_NUMBER)) {
($vlan_number <= $self->{MAX_VLAN})) {
$vlan_number += 1;
$RetVal = snmpitGetWarn($self->{SESS},
[$VlanRowStatus,"1.$vlan_number"]);
......@@ -653,7 +665,7 @@ sub createVlan($$;$$$) {
$self->debug("Row $vlan_number got '$RetVal'\n",2);
}
}
if ($vlan_number > $MAX_VLAN_NUMBER) {
if ($vlan_number > $self->{MAX_VLAN}) {
#
# We must have failed to find one
#
......
......@@ -35,7 +35,7 @@ use libdb;
# usage: new(string name, string stack_id, int debuglevel, list of devicenames)
# returns a new object blessed into the snmpit_cisco_stack class
#
sub new($$$#@) {
sub new($$$$@) {
# The next two lines are some voodoo taken from perltoot(1)
my $proto = shift;
......@@ -43,8 +43,6 @@ sub new($$$#@) {
my $stack_id = shift;
my $debuglevel = shift;
my $community = shift;
my $supports_private = shift;
my $uses_vtp = shift;
my @devicenames = @_;
......@@ -102,8 +100,7 @@ sub new($$$#@) {
(/65\d\d/ || /40\d\d/ || /29\d\d/ || /55\d\d/)
&& do {
use snmpit_cisco;
$device = new snmpit_cisco($devicename,$self->{DEBUG},$type,
$community,$supports_private);
$device = new snmpit_cisco($devicename,$self->{DEBUG});
if (!$device) {
die "Failed to create a device object for $devicename\n";
} else {
......@@ -126,8 +123,7 @@ sub new($$$#@) {
# XXX: For simplicity, we assume for now that the leader is a Cisco
use snmpit_cisco;
my $type = getDeviceType($self->{STACKID});
$self->{LEADER} = new snmpit_cisco($self->{STACKID}, $self->{DEBUG},
$type, $community, $supports_private)
$self->{LEADER} = new snmpit_cisco($self->{STACKID}, $self->{DEBUG});
}
bless($self,$class);
......
......@@ -77,7 +77,24 @@ sub new {
$self->{BLOCK} = 1;
$self->{CONFIRM} = 1;
$self->{NAME} = $name;
$self->{COMMUNITY} = $community;
#
# Get config options from the database
#
my $options = getDeviceOptions($self->{NAME});
if (!$options) {
warn "ERROR: Getting switch options for $self->{NAME}\n";
return undef;
}
$self->{MIN_VLAN} = $options->{'min_vlan'};
$self->{MAX_VLAN} = $options->{'max_vlan'};
if ($community) { # Allow this to over-ride the default
$self->{COMMUNITY} = $community;
} else {
$self->{COMMUNITY} = $options->{'snmp_community'};
}
if ($self->{DEBUG}) {
print "snmpit_intel module initializing... debug level $self->{DEBUG}\n"
......@@ -462,8 +479,14 @@ sub createVlan($$;@) {
#
# Intel provides us with a handy way to pick a VLAN number
# NOTE: This will respect the MAX_VLAN variable, but not the MIN_VLAN
# one
#
my $vlan_number = $self->{SESS}->get([[$NextVLANId,0]]);
if ($vlan_number > $self->{MAX_VLAN}) {
print STDERR "ERROR: Returned VLAN was too high ($vlan_number)\n";
return 0;
}
#
# Pick a name if one was not given
......
......@@ -37,7 +37,7 @@ use libdb;
# returns a new object blessed into the snmpit_intel_stack class
#
sub new($$$#@) {
sub new($$$@) {
# The next two lines are some voodoo taken from perltoot(1)
my $proto = shift;
......@@ -45,7 +45,6 @@ sub new($$$#@) {
my $stack_id = shift;
my $debuglevel = shift;
my $community = shift;
my @devicenames = @_; # Devicenames are not presently needed for Intel
# stacks
......@@ -74,7 +73,7 @@ sub new($$$#@) {
# talk to one (for now) to do all the setup we need.
#
use snmpit_intel;
$self->{LEADER} = new snmpit_intel($stack_id,$self->{DEBUG},$community);
$self->{LEADER} = new snmpit_intel($stack_id,$self->{DEBUG});
#
# Check for failed object creation
......
......@@ -18,7 +18,7 @@ use Exporter;
getTestSwitches getControlSwitches getVlanPorts
getExperimentVlans getDeviceNames getDeviceType
getInterfaceSettings mapPortsToDevices getSwitchStack
getStackType getTrunks getTrunksFromSwitches
getStackType getDeviceOptions getTrunks getTrunksFromSwitches
getExperimentPorts snmpitGetWarn snmpitGetFatal
snmpitWarn snmpitFatal printVars tbsort );
......@@ -52,7 +52,7 @@ my %vlanids=();
my $snmpitErrorString;
#
# Initialize the
# Initialize the library
#
sub init($) {
$debug = shift || $debug;
......@@ -370,6 +370,52 @@ sub getStackType($) {
}
}
#
# Get a hash that describes the configuration options for a switch. The idea is
# that the device's object will call this method to get some options. Right
# now, all this stuff actually comes from the stack, but there could be
# switch-specific configuration in the future. Provides defaults for NULL
# columns
#
# We could probably make this look more like an object, for type checking, but
# that just doesn't seem necessary yet.
#
sub getDeviceOptions($) {
my $switch = shift;
my %options;
my $result = DBQueryFatal("SELECT supports_private, " .
"single_domain, snmp_community, min_vlan, max_vlan " .
"FROM switch_stacks AS s left join switch_stack_types AS t " .
" ON s.stack_id = t.stack_id ".
"WHERE s.node_id='$switch'");
if (!$result->numrows()) {
print STDERR "No switch $switch found, or it is not in a stack\n";
return undef;
}
my ($supports_private, $single_domain, $snmp_community, $min_vlan,
$max_vlan) = $result->fetchrow();
$options{'supports_private'} = $supports_private;
$options{'single_domain'} = $single_domain;
$options{'snmp_community'} = $snmp_community || "public";
$options{'min_vlan'} = $min_vlan || 2;
$options{'max_vlan'} = $max_vlan || 1000;
$options{'type'} = getDeviceType($switch);
if ($debug) {
print "Options for $switch:\n";
while (my ($key,$value) = each %options) {
print "$key = $value\n"
}
}
return \%options;
}
#
# Returns a structure representing all trunk links. It's a hash, keyed by
# switch, that contains hash references. Each of the second level hashes
......
......@@ -135,7 +135,7 @@ SWITCH:
my @vlanList;
my $device;
if ($useindexing) {
$device = new snmpit_cisco($switch,0,$community,0);
$device = new snmpit_cisco($switch,$debug,$community);
@vlanList = $device->listVlans();
} else {
#
......
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