I hate to post something so huge to SOPW, but I'm having a problem with a script I wrote using Net::SNMP.
Now the problem is where this line is...
# Now, run the SNMP command.
my $response = undef;
$response = $session->get_request($cmd);
# Check for SNMP command error.
if (!defined($response)) {
This works fine as long as there isn't an error with a node, which is basicly a router ip. If there is an error, for some reason $response will always be undefined with the set of snmp commands for a paritcular ip, until I kill the script and then restart it. I have many other ip's and their lists of snmp commands that still run fine, even with this one being run in the middle. But all the snmp commands for this particular ip crash and burn after an error. I'm at my wits end because I don't see anything wrong with my code. I'm re-defining $response and even undef'ing it before I run get_request again, so I'm thinking that it's somehow caching the result of the snmp get_request for that ip and then in turn giving an error for every related snmp command for that ip. Does Net::SNMP do that? Is there something I am not refreshing? This script runs for days at a time, every 2 seconds, until that one error, then it runs fine for the ip's that didn't generate an error, but fails miserably for the ip that generated the error. I have to kill it and restart it and it's immediately fine afterwards.
If anybody can help out, I thank you in advance.
btw, this code is running with Perl 5.6 on Mandrake Linux.
####
# Just for informational purposes, not in my script this way.
# Usually loaded in from a db and usually more than one structure.
####
$snmpInfo = {
node => '100',
ip => '192.168.1.101',
location => 'Somewhere, PA',
snmpCmd => [ '.1.3.6.1.4.1.449.2.1.3.1.1.26.1.1.6.19',
'.1.3.6.1.4.1.449.2.1.3.1.1.26.1.1.6.20' ],
snmpExecDesc => [ 'Port 19', 'Port 20' ],
snmpCmdResult => [ 'Disconnected', 'Disconnected' ],
snmpCmdDesc => [ 'Checking Somewhere's port 19',
'Checking Somewhere's port 20' ]
};
###
# From here on is my code, with some logging cut out to lower the post
+ content.
###
for my $cr (sort keys %{$snmpInfo}) {
# Create SNMP Session.
my ($session, $error) = Net::SNMP->session (
-hostname => "$snmpInfo->{$cr}->{'ip'}",
-community => "$community",
-timeout => $config->{'snmp_timeout'}
);
# Check for SNMP Session Error.
if (!defined($session)) {
print $LOG "\n There was an error defining an SNMP session for
+";
print $LOG " $snmpInfo->{$cr}->{'ip'}.\n";
print $LOG " Error: $error\n";
print $LOG " Error Time: " . grab_time() . "\n\n";
# Update value to error.
update_snmp_value($dbc, "Error - Check Log",
$snmpInfo->{$cr}->{'node'}, '', 'node');
} else {
# Loop through the snmp arrays and run the snmp commands.
for my $x (0 .. $#{$snmpInfo->{$cr}->{'snmpCmd'}}) {
my ($cmd, $snmpCmdResult, $execDesc) = '';
$cmd = $snmpInfo->{$cr}->{'snmpCmd'}[$x];
$snmpCmdResult = $snmpInfo->{$cr}->{'snmpCmdResult'}[$x];
$execDesc = $snmpInfo->{$cr}->{'snmpExecDesc'}[$x];
# Now, run the SNMP command.
my $response = undef;
$response = $session->get_request($cmd);
# Check for SNMP command error.
if (!defined($response)) {
print $LOG " : ********************************************
+**\n";
print $LOG " : There was an error running an SNMP Command f
+or ";
print $LOG "$snmpInfo->{$cr}->{'ip'}.\n";
print $LOG " : Error: " . $session->error() . "\n";
print $LOG " : Error Time: " . grab_time() . "\n";
print $LOG " : ********************************************
+**\n";
# Update value to error.
if ($snmpCmdResult ne "Error - Check Log") {
update_snmp_value($dbc, "Error - Check Log",
$snmpInfo->{$cr}->{'node'}, $cmd, 'cmd');
}
} else {
print $LOG " : Value - [$response->{$cmd}]\n" if ($log
+Enabled);
# Check response, if not same as before, then update va
+lue.
if ($response->{$cmd} ne $snmpCmdResult) {
update_snmp_value($dbc, $response->{$cmd},
$snmpInfo->{$cr}->{'node'}, $cmd, 'cmd');
print $LOG " : Value different [$snmpCmdResult] - db
+ updated.\n"
if ($logEnabled);
}
}
}
}
# Close the SNMP session.
$session->close() if (defined($session));
$session = undef;
}
If there's anymore info I can give to be helpful, let me know.