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

Hello, i asked this question before but didn't get much progress on it. can you see what is wrong here?

i am using snmp and alarm together to timeout a snmp query( to printer ) in mod_perl environment. however, once a timeout happens with an alarm, the next snmp query with a different snmp session always inherit the previous failed snmp query result.

i also use the timeout property in snmp, but it doesn't seem to do anything.

anyway here is code.

{ foreach $pr_section_name (values %{$config}) { my $pr_name = $pr_section_name->{pr_name}; next unless $pr_name; $pr_names{$pr_name}=""; next if $get_pr_names == 1; my $cmnt = $pr_section_name->{cmnt}; my $ip = $pr_section_name->{ip}; my $lcd_mib_1 = $pr_section_name->{lcd_mib_1}; my $lcd_mib_2 = $pr_section_name->{lcd_mib_2}; my ($session, $error) = Net::SNMP->session( -hostname => $ip, -community => chomp($cmnt), -port => 161, -timeout => 5, ); if (!defined($session)) { print STDERR $error; next; } my $pr_status = &_snmp_query($session,$status_mib) if $status_mib; my $pr_lcd_msg = &_snmp_query($session,$lcd_msg_mib) if $lcd_msg_mib; my $lcd_msg_1 = &_snmp_query($session,$lcd_mib_1) if $lcd_mib_1; my $lcd_msg_2 = &_snmp_query($session,$lcd_mib_2) if $lcd_mib_2; } } # snmp query sub _snmp_query { my ($s,$mib) = @_; my $result; # printer status eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 2; $result = $s->get_request( -varbindlist => [$mib] ); alarm 0; }; if ($@) { return unless $@ eq "alarm\n"; } else { return lc($result->{$mib}); } }

regards,

Qiang

Replies are listed 'Best First'.
Re: alarm problem with snmp query
by Anonymous Monk on Mar 12, 2004 at 02:53 UTC
    use Net::SNMP; my $sysDescr = '.1.3.6.1.2.1.1.1.0'; my $sess = Net::SNMP->session( -hostname => $ARGV[0], -community => $ARGV[1], -timeout => $ARGV[2], -retries => $ARGV[3], ) or die "no session!$/"; print time, $/; my $res = $sess->get_request( -varbindlist => [ $sysDescr ] ) or warn "no get: *error message here*$/"; print time, $/; print $res->{$sysDescr}, $/;

    play with this, looks like the default for -retries is 1 based on the times i get. your code (without the alarm evil) will take 10 seconds to fail since it tries twice.

      that was it!

      i saw the 10 sec delay for each snmp query in the log but i guess i wasn't thinking at all...

      anyway, thanks!!

      wish i could ++ you :)