#!/usr/bin/perl -w use strict; # # $Id: traptoxmpp,v 1.7 2005/07/11 05:13:16 mike Exp $ # # This is an snmptrapd handler script to convert snmp traps into xmpp messages. # It is based off of traptoemail which can be found in the net-snmp release. # sendxmpp is required to be installed in the PATH. # Usage: # Create ~root/.sendxmpprc as per the sendxmpp manpage and put a line like # the following in your snmptrapd.conf file: # traphandle TRAPOID|default /usr/local/bin/traptoxmpp JabberID # # EXIT CODE: # 0 = success # 1 = print_help function (or incorrect commandline) # my $AUTHOR = 'Mike Arnold '; my $VERSION = '20050710'; # my $debug = 0; if (exists($ENV{'DEBUG'})) { $debug = 1; } # ###################################################################### use Getopt::Long; # Function to print the help screen. sub print_help () { print "Usage: $0 [-r resource] [-t] [-c] JabberID\n"; print "\n"; print "traptoxmpp shouldn't be called interatively by a user. It is\n"; print "designed to be called as an snmptrapd extension via a \"traphandle\"\n"; print "directive in the snmptrapd.conf file. See the snmptrapd.conf file for\n"; print "details.\n"; print "\n"; print "Options:\n"; print " -r resource Sets the Jabber resource to use for the message.\n"; print " -t Connect securely, using TLS.\n"; print " -c Send the message to a chatroom.\n"; print " JabberID The Jabber user to whom you want the message sent.\n\n"; exit (1); } # Function to print the version screen. sub print_version () { print "\tSnmptrapd handler script to convert snmp traps into xmpp messages.\n"; print "\tVersion: $VERSION\n"; print "\tWritten by: $AUTHOR\n"; exit (0); } # Process arguments. my $tls; my $chatroom; my $resource = "traptoxmpp"; my $help; my $version; GetOptions ( "t|tls" => \$tls, "c|chatroom" => \$chatroom, "r|resource=s" => \$resource, "h|help" => \$help, "v|version" => \$version, ); print_version() if $version; print_help() if $help; print_help() if ($#ARGV < 0); # main # Build the sendxmpp options string. my $options = "-r " . $resource; $options = $options . " -t" if $tls; $options = $options . " -c" if $chatroom; print "DEBUG:: options: " . $options . "\n" if $debug; # Process the trap. my $hostname = ; chomp $hostname; my $ipaddress = ; chomp $ipaddress; my $maxlen = 0; my $oid; my $value; my @oids; my @values; while() { ($oid, $value) = /([^\s]+)\s+(.*)/; push @oids, $oid; push @values, $value; $maxlen = (length($oid) > $maxlen) ? length($oid) : $maxlen; } $maxlen = 60 if ($maxlen > 60); my $formatstr = "%" . ($maxlen + 1) . "s %s\n"; print "DEBUG:: formatstr: " . $formatstr . "\n" if $debug; # Exit if we did not receive and OIDs in the trap. die "illegal trap" if ($#oids < 1); # Add the finishing touch to the sendxmpp options string. my $subject = "trap received from $hostname: $values[1]"; $options = $options . " -s '" . $subject . "'"; print "DEBUG:: options: " . $options . "\n" if $debug; # Send the message. # http://perldoc.perl.org/perlipc.html#Using-open()-for-IPC $SIG{PIPE} = 'IGNORE'; open (SENDXMPP, "| sendxmpp " . $options . " " . $ARGV[0]) or die "can't fork: $!"; print "DEBUG:: To: " . $ARGV[0] . "\n" if $debug; print "DEBUG:: Subject: " . $subject . "\n" if $debug; print "DEBUG:: Message:\n" if $debug; print "DEBUG:: Host: $hostname ($ipaddress)\n" if $debug; print SENDXMPP "Host: $hostname ($ipaddress)\n" or die "can't write: $!"; for (my $i = 0; $i <= $#oids; $i++) { print sprintf("%s " . $formatstr, "DEBUG::", $oids[$i], $values[$i]) if $debug; print SENDXMPP sprintf($formatstr, $oids[$i], $values[$i]) or die "can't write: $!"; } close SENDXMPP or die "can't close: status=$?"; exit (0);