Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Net::SNMP get_request() caching results?

by the_0ne (Pilgrim)
on Nov 08, 2001 at 07:58 UTC ( [id://123993]=perlquestion: print w/replies, xml ) Need Help??

the_0ne has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Net::SNMP get_request() caching results?
by c (Hermit) on Nov 08, 2001 at 10:05 UTC
    i'm not 100% certain that i completely understand the problem that you are seeing, however i've done my share of wrestling with net::snmp. the one bit of advice that i would give you, is that perhaps rather than testing on whether $response is defined, you may want to test on if $error exists. i generally make a seperate call to grab the error return if it exists

    my $session = Net::SNMP->session ( ... ); my $error = $session->error;

    perhaps a next if ($error); or just test on $error's existance, rather than checking if $session is defined?

    humbly -c

      Thanks for the reply c and I understand your confusion. It's not easy to explain without giving the entire code and having a go at it and there's a lot more code than that.

      The whole jist of it is I'm running snmp commands on routers on our network. So, I define a session for a router by it's ip and then I run the snmp commands. (On the same $session that was defined.) So, if I have 4 snmp commands to run on that same ip, I run them in a loop using the same SNMP session that was defined. Well, when I get a response that is an error like not being able to resolve the name or some kind of other error, the error never seems to clear. Even though I set $response as undef, the get_request() seems to not reset for that particular ip on the next go around, until I kill the script and re-run it.

      Thanks again for your reply.
        Everything you're saying sounds *really* familiar to me. I've written a utility that runs against routers using net::snmp. The script that is in the code section of the website isnt the most up to date, so I've gone through a few revisions since I posted. The most current one is here. Not sure if it could be of any insight, but I would imagine that I've faced some of your hurdles as well.

        I think I have a bit clearer view of what you're doing now with the loop. Does it just fail for this one remote node? And is the failure the result of the script not being able to resolve the node name to an ip address or is it a timeout general?

        humbly -c

Re: Net::SNMP get_request() caching results?
by fokat (Deacon) on Nov 08, 2001 at 18:26 UTC
    I've seen this before. Perhaps someone knowing the guts of Net::SNMP better than myself could throw more light into this.

    Something that has worked quite reliably for me is to destroy and re-create the SNMP object after each error. This is probably not too costly, since creating a UDP socket does not require a connection setup.

    The code I use, looks like the following:

    sub my_create_snmp { my $snmp = ... # Place here the code to create your # SNMP handler. return $snmp; } ... $response = $session->get_request($cmd); if (!defined($response)) { ... $session = my_create_snmp(); next; }
    Note however that you must ensure that the required parameters to create a new SNMP session are available to my_create_snmp(). This probably includes global variables or constants. I tend to prefer the later.

    Regards.

Re: Net::SNMP get_request() caching results?
by demerphq (Chancellor) on Nov 08, 2001 at 18:46 UTC
    Just a minor point regarding your wellfounded concerns regarding the sie of your post. Perlmonks supports special tags to minimize the size of a post on the front pages etc. Just put
    <readmore> </readmore>
    Tags around the the bulky stuff and it wont show up unless someone reads the post explicitly.

    HTH

    Yves / DeMerphq
    --
    Have you registered your Name Space?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://123993]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found