Commit 27e353a5 authored by Robert Ricci's avatar Robert Ricci
Browse files

Convert snmpit_cisco to use the snmpitGet functions.

Re-worked the snmpitGet functions slightly to make them more
convenient and to work around some ickiness in the SNMP library.
parent 51bc0de4
......@@ -345,10 +345,10 @@ sub vlanUnlock($;$) {
my $RetVal = $self->{SESS}->set([[$EditOp,1,"apply","INTEGER"]]);
$self->debug("Apply set: '$RetVal'\n");
$RetVal = $self->{SESS}->get([[$ApplyStatus,1]]);
$RetVal = snmpitGetWarn($self->{SESS},[$ApplyStatus,1]);
$self->debug("Apply gave $RetVal\n");
while ($RetVal eq "inProgress") {
$RetVal = $self->{SESS}->get([[$ApplyStatus,1]]);
$RetVal = snmpitGetWarn($self->{SESS},[$ApplyStatus,1]);
$self->debug("Apply gave $RetVal\n");
}
......@@ -493,11 +493,13 @@ sub createVlan($$) {
# XXX: The maximum VLAN number is hardcoded at 1000
#
my $vlan_number = 2; # We need to start at 2
my $RetVal = $self->{SESS}->get([$VlanRowStatus,$vlan_number]);
my $RetVal = snmpitGetFatal($self->{SESS},
[$VlanRowStatus,$vlan_number]);
$self->debug("Row $vlan_number got '$RetVal'\n",2);
while (($RetVal ne 'NOSUCHINSTANCE') && ($vlan_number <= 1000)) {
$vlan_number += 1;
$RetVal = $self->{SESS}->get([[$VlanRowStatus,$vlan_number]]);
$RetVal = snmpitGetFatal($self->{SESS},
[$VlanRowStatus,$vlan_number]);
$self->debug("Row $vlan_number got '$RetVal'\n",2);
}
if ($vlan_number > 1000) {
......@@ -781,7 +783,7 @@ sub UpdateField($$$@) {
$trans = "???";
}
$self->debug("Checking port $port ($trans) for $val...");
$Status = $self->{SESS}->get([[$OID,$port]]);
$Status = snmpitGetFatal($self->{SESS},[$OID,$port]);
if (!defined $Status) {
warn "Port $port ($trans), change to $val: No answer from device\n";
return -1; # return error
......@@ -796,7 +798,7 @@ sub UpdateField($$$@) {
if ($self->{BLOCK}) {
my $n = 0;
while ($Status ne $val) {
$Status=$self->{SESS}->get([[$OID,$port]]);
$Status = snmpitGetFatal($self->{SESS},[$OID,$port]);
$self->debug("Value for $port was $Status\n");
select (undef, undef, undef, .25); # wait .25 seconds
$n++;
......@@ -1014,7 +1016,7 @@ sub setVlansOnTrunk($$$$) {
# the channel ifIndex the the port is in a channel. Not sure that
# this is _always_ beneficial, though
#
my $channel = $self->{SESS}->get(["pagpGroupIfIndex",$ifIndex]);
my $channel = snmpitGetFatal($self->{SESS},["pagpGroupIfIndex",$ifIndex]);
if (($channel =~ /^\d+$/) && ($channel != 0)) {
$ifIndex = $channel;
}
......@@ -1022,7 +1024,8 @@ sub setVlansOnTrunk($$$$) {
#
# Get the exisisting bitfield for allowed VLANs on the trunk
#
my $bitfield = $self->{SESS}->get(["vlanTrunkPortVlansEnabled",$ifIndex]);
my $bitfield = snmpitGetFatal($self->{SESS},
["vlanTrunkPortVlansEnabled",$ifIndex]);
my $unpacked = unpack("B*",$bitfield);
# Put this into an array of 1s and 0s for easy manipulation
......
......@@ -515,17 +515,17 @@ sub getTrunksFromSwitches($@) {
# Get a set of SNMP variables from an SNMP session, retrying in case there are
# transient errors.
#
# usage: snmpitGet(session, vars, [retries])
# usage: snmpitGet(session, var, [retries])
# args: session - SNMP::Session object, already connected to the SNMP
# device
# vars - An SNMP::VarList or SNMP::Varbind object containing the
# OID(s) to fetch
# var - An SNMP::Varbind or a reference to a two-element array
# (similar to a single Varbind)
# retries - Number of times to retry in case of failure
# returns: 1 on sucess, 0 on failure
# returns: the value on sucess, undef on failure
#
sub snmpitGet($$;$) {
my ($sess,$vars,$retries) = @_;
my ($sess,$var,$retries) = @_;
if (! defined($retries) ) {
$retries = $DEFAULT_RETRIES;
......@@ -536,19 +536,20 @@ sub snmpitGet($$;$) {
#
if (!$sess) {
$snmpitErrorString = "No valid SNMP session given!\n";
return 0;
return undef;
}
if ((ref($vars) ne "SNMP::VarList") && (ref($vars) ne "SNMP::Varbind")) {
$snmpitErrorString = "No valid SNMP variables given!\n";
return 0;
if ((ref($var) ne "SNMP::Varbind") &&
((ref($var) ne "ARRAY") || (@$var != 2))) {
$snmpitErrorString = "Invalid SNMP variable given!\n";
return undef;
}
#
# Retry several times
#
foreach my $retry ( 1 .. $retries) {
my $status = $sess->get($vars);
my $status = $sess->get($var);
#
# Avoid unitialized variable warnings when printing errors
#
......@@ -567,7 +568,7 @@ sub snmpitGet($$;$) {
$snmpitErrorString .= "Error string is: $sess->{ErrorStr}\n";
}
} else {
return 1;
return $var->[2];
}
#
......@@ -579,17 +580,17 @@ sub snmpitGet($$;$) {
#
# If we made it out, all of the attempts must have failed
#
return 0;
return undef;
}
#
# Same as snmpitGet, but send mail if any error occur
#
sub snmpitGetWarn($$;$) {
my ($sess,$vars,$retries) = @_;
my ($sess,$var,$retries) = @_;
my $result;
$result = snmpitGet($sess,$vars,$retries);
$result = snmpitGet($sess,$var,$retries);
if (! $result) {
snmpitWarn("SNMP GET failed");
......@@ -602,10 +603,10 @@ sub snmpitGetWarn($$;$) {
# failure.
#
sub snmpitGetFatal($$;$) {
my ($sess,$vars,$retries) = @_;
my ($sess,$var,$retries) = @_;
my $result;
$result = snmpitGet($sess,$vars,$retries);
$result = snmpitGet($sess,$var,$retries);
if (! $result) {
snmpitFatal("SNMP GET failed");
......
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