#!/usr/bin/perl -w
###
### trap-send HOST TRAP-COMMUNITY
###
### Author:	  Simon Leinen  <simon@switch.ch>
### Date Created: 03-Jun-1998
###
### Send linkDown trap to the designated management host/community
###
### This is intended to be a very simple example for sending SNMP(v1)
### traps.  It doesn't do anything otherwise useful.
### 
use strict;
use BER '0.58';
use SNMP_Session '0.58';

my $version = '1';

### Forward declarations
sub link_down_trap ($$);
sub usage ($);

while (defined $ARGV[0] && $ARGV[0] =~ /^-/) {
    if ($ARGV[0] =~ /^-v/) {
	if ($ARGV[0] eq '-v') {
	    shift @ARGV;
	    usage (1) unless defined $ARGV[0];
	} else {
	    $ARGV[0] = substr($ARGV[0], 2);
	}
	if ($ARGV[0] eq '1') {
	    $version = '1';
	} elsif ($ARGV[0] eq '2' || $ARGV[0] eq '2c') {
	    $version = '2';
	} else {
	    usage (1);
	}
    } elsif ($ARGV[0] eq '-h') {
	usage (0);
	exit 0;
    } else {
	usage (1);
    }
    shift @ARGV;
}
my $trap_receiver = shift @ARGV || usage (1);
my $trap_community = shift @ARGV || 'public';
my $trap_session = $version eq '1'
    ? SNMP_Session->open ($trap_receiver, $trap_community, 162)
    : SNMPv2c_Session->open ($trap_receiver, $trap_community, 162);
my $start_time = time;

link_down_trap (1,$version);
1;

sub link_down_trap ($$) {
  my ($if_index, $version) = @_;
  my $genericTrap = 2;		# linkDown
  my $specificTrap = 0;
  my @sysUptime_OID = ( 1,3,6,1,2,1,1,3 );
  my @snmpTrapOID_OID = ( 1,3,6,1,6,3,1,1,4,1 );
  my @ifIndex_OID = ( 1,3,6,1,2,1,2,2,1,1 );
  my @ifDescr_OID = ( 1,3,6,1,2,1,2,2,1,2 );
  my $upTime = int ((time - $start_time) * 100);
  my $myIpAddress = pack "CCCC", 130, 59, 4, 2;
  my @myOID = ( 1,3,6,1,4,1,2946,0,8,15 );
  my @linkDown_OID = ( 1,3,6,1,6,3,1,1,5,3 );

  warn "Sending trap failed"
    unless ($version eq '1')
	? $trap_session->trap_request_send (encode_oid (@myOID),
					    encode_ip_address ($myIpAddress),
					    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, $upTime,
						   [encode_oid (@ifIndex_OID,$if_index),
						    encode_int ($if_index)],
						   [encode_oid (@ifDescr_OID,$if_index),
						    encode_string ("foo")]);
}

sub usage ($) {
    warn <<EOM;
Usage: $0 [-v (1|2c)] destination [community]
       $0 -h

  -h           print this usage message and exit.

  -v version   can be used to select the trap type.  The default is "1",
               which causes SNMPv1 Traps to be generated.  The other
	       possibility is "2" (or "2c"), which causes an SNMPv2
               Trap to be generated.  Use the highest trap version
               that is supported by your trap receiver.

  destination  hostname or IP address of the trap receiver

  community    SNMP community string to use.  Defaults to "public".
EOM
    exit (1) if $_[0];
}
