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.


In reply to Re: Looking for a simple way to just send an SNMP V1 Trap by quester
in thread Looking for a simple way to just send an SNMP V1 Trap by w3ntp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.