Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Perl SNMP Error Handling

by skunix (Novice)
on Jul 11, 2013 at 18:02 UTC ( #1043802=perlquestion: print w/replies, xml ) Need Help??
skunix has asked for the wisdom of the Perl Monks concerning the following question:

I am polling SNMP data from a list of devices using the SNMP module based off of the Net-SNMP library.

Everything works fine when I make a new SNMP::Session with an IP address that is reachable however if the IP is unreachable the program crashes when I try to get the SNMP data like so...

my $vars = new SNMP::VarList(['sysDescr',0]['sysUpTime',0]); my ($descr,$uptime) = $snmp->get($vars);

Which will crash with the follow error...

Can't call method "get" on an undefined value at...

Is there a way to handle this error without crashing?

Replies are listed 'Best First'.
Re: Perl SNMP Error Handling
by hippo (Monsignor) on Jul 11, 2013 at 19:08 UTC

    Of course. Simply test the defined nature of your object before trying to call the method.

    if (defined $snmp) { ($descr,$uptime) = $snmp->get($vars); } else { die 'Horrors! My $snmp object is undefined!'; }

    ... or similar.

      Man... I feel like an idiot -_-
Re: Perl SNMP Error Handling
by choroba (Chancellor) on Jul 11, 2013 at 19:01 UTC
    You can use eval or Try::Tiny.
    my ($descr, $uptime); if (eval { ($descr, $uptime) = $snmp->get($vars); 1 }) { ... } else { warn $@; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1043802]
Approved by Perlbotics
Front-paged by toolic
help
Chatterbox?
[Discipulus]: oh well we cant afford two monks fired in a day!
[LanX]: any news from tye ' s search?
[MidLifeXis]: Ugh - who else?
[Discipulus]: so the right term is 'gired'
[Discipulus]: look at ot by karl
Discipulus gired: adj the work position that stands between hired and fired..
LanX recommends inner immigration, work less hard and enjoy pay roll
[MidLifeXis]: ahh, yeah, didn't look through nodes yet today.
[erix]: haha
[MidLifeXis]: Ahh well, time to get back to it, so there isn't a reason to take that choice out of my hands :-)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2017-03-23 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (286 votes). Check out past polls.