# 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. |