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

I've been assigned a task to create a perl script that will receive SNMP traps (I'm quite new to SNMP) and insert certain values to a database in real time.I was advised to make a listener program myself, so that I don't have to take time reading the traps from logs(with snmptrapd), and instead process the traps immediately upon reciept.

To start off, I've tried out the code provided to Receiving traps in Simon Leinen's Site.(http://ftp.debian.org/doc/libsnmp-session-perl/snmp-session.html) I've been sending a dummy SNMPV2 trap to the script but it prompts "Can't call method "decode_trap_request" on an undefined value at recivtrap.pl" I am not sure if I am doing something wrong, I've placed this on the code:

if (defined($trap)) { print "RECIVED TRAP\n"; my ($community, $enterprise, $agent,$generic, $specific, $sy +sUptime, $bindings) = $session->decode_trap_request ($trap) or die "c +annot decode trap received"; }

My program prints out "Recieved Trap" so that means $trap is defined, so I don't understand why the error "Can't call method "decode_trap_request" on an undefined value at recivtrap.pl" pops out whenever the program receives the trap.

The traps that I'm sending works with SNMPTRAPD perfectly (it shows me the traps that I've received). I'm sending V2 traps.

I've been stuck at this for quite a few days now, and I've emailed Mr. Leinen himself, but he might be too busy (and I misspelled his name in my email to him..forgive me) somebody here might have the answers to my question already. Thanks in advance :)

Replies are listed 'Best First'.
Re: Question about Decoding Traps using SNMP_Session.pm
by roboticus (Chancellor) on Oct 11, 2007 at 12:44 UTC
    jamjam:

    By the error message, I'd guess that $session doesn't contain what you think it does...

    Without seeing the code, it's hard to get any closer than that.

    ...roboticus

      the sender code looks like this (just copied from Essential SNMP Book):
      use SNMP_util "0.54"; # This will load the BER and SNMP_Session for u +s snmptrap("public\@172.20.50.13:162", ".1.3.6.1.4.1.2789", "sunserver1" +, 6, 1247,".1.3.6.1.4.1.2789.1247.1", "int", "2448816");
      the reciever code I got from Mr. Leinen's site:
      my $trap_session = SNMPv1_Session->open_trap_session () or die "cannot open trap session"; my ($trap, $sender_addr, $sender_port) = $trap_session->receive_trap ( +) or die "cannot receive trap"; my ($community, $enterprise, $agent, $generic, $specific, $sysUptime, $bindings) = $session->decode_trap_request ($trap) or die "cannot decode trap received" ... my ($binding, $oid, $value); while ($bindings ne '') { ($binding,$bindings) = &decode_sequence ($bindings); ($oid, $value) = decode_by_template ($binding, "%O%@"); print BER::pretty_oid ($oid)," => ",pretty_print ($value),"\n"; }
      Mr. Leinen has already replied to my inquiry. he pointed out the errors in those codes, though I'm not sure what the second argument is for the decode_by_template: