Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Looking for a simple way to just send an SNMP V1 Trap

by quester (Vicar)
on Jun 11, 2007 at 22:32 UTC ( [id://620589]=note: print w/replies, xml ) Need Help??


in reply to Looking for a simple way to just send an SNMP V1 Trap

Look at "SNMP support for Perl 5" in http://www.switch.ch/misc/leinen/snmp/perl/. This is the same SNMP interface parkace used by MRTG.

For a quick example, adapted from the documentation for snmptrap on that page,

# use warnings; # use strict; my $version=1; use lib '/usr/lib/mrtg2'; # location of SNMP_Session.pm and BER.pm use SNMP_Session; use BER; my $trap_receiver = "10.1.2.3"; my $trap_community = "SNMP_Traps"; my $trap_session = $version eq '1' ? SNMP_Session->open ($trap_receiver, $trap_community, 162) : SNMPv2c_Session->open ($trap_receiver, $trap_community, 162); my $myIpAddress = "10.1.2.4"; my $start_time = time; sub link_down_trap ($$) { my ($if_index, $version) = @_; my $genericTrap = 2; # linkDown my $specificTrap = 0; my @ifIndex_OID = ( 1,3,6,1,2,1,2,2,1,1 ); # UPDATE: not ifIn +dexOID my @ifDescr_OID = ( 1,3,6,1,2,1,2,2,1,2 ); # UPDATE: added my $upTime = int ((time - $start_time) * 100.0); my @myOID = ( 1,3,6,1,4,1,2946,0,8,15 ); warn "Sending trap failed" unless ($version eq '1') ? $trap_session->trap_request_send (encode_oid (@myOID), encode_ip_address ($myIpAd +dress), encode_int ($genericTrap), encode_int ($specificTrap) +, encode_timeticks ($upTime) +, [encode_oid (@ifIndex_OID, +$if_index), encode_int ($if_index)], [encode_oid (@ifDescr_OID, +$if_index), encode_string ("foo")]) : $trap_session->v2_trap_request_send (\@linkDown_OID, $up +Time, [encode_oid (@ifInd +ex_OID,$if_index), encode_int ($if_in +dex)], [encode_oid (@ifDes +cr_OID,$if_index), encode_string ("fo +o")]); } link_down_trap 123, $version;

UPDATE:I fixed the definition of ifIndexOID to ifIndex_OID and added the definition of ifDescr_OID. Sorry, I thought I was copying a working example, but evidently it wasn't.

Perversely, without correct definitions the program still runs with no warnings, even with warnings and strict enabled, and it sends an snmptrap packet... but the oid=value pairs in the packet are not valid.

In order to debug this sort of problem, you can send the snmptrap packets to a system running Wireshark or a Unix system running snmptrapd. But be aware that the NET-SNMP version 5.0.9 version of snmptrapd won't log anything at all for a badly formatted trap packet, unless you include you include the hyperverbose -D (debug) option.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-28 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found