#!/usr/bin/perl -w

#
# Translate SNMP traps to mon traps
#
# Dan Urist, durist@world.std.com
#
# Copyright (C) 2002 Dan Urist
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

my $DEBUG = 0;                             # Set this to nonzero if you want logging
my $LOGFILE = "/var/log/snmp2montrap.log"; # Location of logfile
my $TUSER = "montrap";                     # Mon trap user
my $TPASSWD = "montrap";                   # Mon trap password

#
# Pattern is a regexp for the relevant line in the trap
# Whatever is in parens gets sent as the trap message
# "group" and "service" tell mon which hostgroup/service to
# user for the trap; "ignore" means ignore the trap
# Unrecognized traps are sent to default/default
my $Patterns = {
		# Windows backup exec
		'^enterprises\.1302\.3\.3\.5(.*)' => {
 						      group => 'WinBackup',
 						      service => 'winbackup_trap',
						     },

		'\.iso\.org\.dod\.internet\.snmpV2\.snmpModules\.snmpMIB\.snmpMIBObjects\.snmpTraps\.authenticationFailure' => { ignore => 1},
	       };

#
# Nothing past here should need reconfig
#

use Mon::Client;
my $Mtrap = new Mon::Client(
			    host => "localhost",
			    username => $TUSER,
			    password => $TPASSWD,
			   );


my @Raw = <>;

if( $DEBUG && open(OUT, ">>$LOGFILE" )){
  print OUT join(" ", ">>>>>", scalar localtime, $0, "<<<<<"), "\n";
  print OUT @Raw;
  close OUT;
}


chomp( my($Hostname, $IPaddress, @Varbinds) = @Raw );

my $pattern;
my $matched;
foreach $pattern (keys %$Patterns){
  if( ($matched) = grep(/$pattern/, @Varbinds) ){
    exit if $Patterns->{$pattern}->{ignore};
    $matched =~ /$pattern/;
    $msg = $1;
    $Mtrap->send_trap(
		      group           => $Patterns->{$pattern}->{group},
		      service         => $Patterns->{$pattern}->{service},
		      retval          => 1,
		      opstatus        => 'fail',
		      summary         => $Hostname . ': ' . $msg,
		      detail          => $Hostname . ': ' . $msg,
		     );

    exit;
  }
}

# Send a default trap if nothing else matched
print "$0: Sending default trap\n";
$Mtrap->send_trap(
		  group           => 'default',
		  service         => 'default',
		  retval          => 1,
		  opstatus        => 'fail',
		  summary         => "$0: Unrecognized SNMP trap",
		  detail          => "=================\n" . join("", @Raw) . "=================\n",
		 ) or warn "Mon::Client::sendtrap failed\n";
