/usr/bin/traptoemail is in snmptrapd 5.7.3+dfsg-1.8ubuntu3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #!/usr/bin/perl
# This is a snmptrapd handler script to convert snmp traps into email
# messages.
# Usage:
# Put a line like the following in your snmptrapd.conf file:
# traphandle TRAPOID|default /usr/local/bin/traptoemail [-f FROM] [-s SMTPSERVER]b ADDRESSES
# FROM defaults to "root"
# SMTPSERVER defaults to "localhost"
use Net::SMTP;
use Getopt::Std;
use POSIX qw(strftime);
# for sane timestamp format
$ENV{LANG} = 'C';
$opts{'s'} = "localhost";
$opts{'f'} = 'root@' . `hostname -f`;
chomp($opts{'f'});
getopts("hs:f:", \%opts);
if ($opts{'h'}) {
print "
traptoemail [-s smtpserver] [-f fromaddress] toaddress [...]
traptoemail shouldn't be called interatively by a user. It is
designed to be called as an snmptrapd extension via a \"traphandle\"
directive in the snmptrapd.conf file. See the snmptrapd.conf file for
details.
Options:
-s smtpserver Sets the smtpserver for where to send the mail through.
-f fromaddress Sets the email address to be used on the From: line.
toaddress Where you want the email sent to.
";
exit;
}
die "no recepients to send mail to" if ($#ARGV < 0);
# process the trap:
$hostname = <STDIN>;
chomp($hostname);
$ipaddress = <STDIN>;
chomp($ipaddress);
$maxlen = 0;
while(<STDIN>) {
($oid, $value) = /([^\s]+)\s+(.*)/;
push @oids, $oid;
push @values, $value;
$maxlen = (length($oid) > $maxlen) ? length($oid) : $maxlen;
}
$maxlen = 60 if ($maxlen > 60);
$formatstr = "%" . $maxlen . "s %s\n";
die "illegal trap" if ($#oids < 1);
# send the message
$message = Net::SMTP->new($opts{'s'}) || die "can't talk to server $opts{'s'}\n";
$message->mail($opts{'f'});
$message->to(@ARGV) || die "failed to send to the recepients ",join(",",@ARGV),": $!";
$message->data();
$message->datasend("To: " . join(", ",@ARGV) . "\n");
$message->datasend("From: $opts{f}\n");
$message->datasend("Date: ".strftime("%a, %e %b %Y %X %z", localtime())."\n");
$message->datasend("Subject: trap received from $hostname: $values[1]\n");
$message->datasend("\n");
$message->datasend("Host: $hostname ($ipaddress)\n");
for($i = 0; $i <= $#oids; $i++) {
$message->datasend(sprintf($formatstr, $oids[$i], $values[$i]));
}
$message->dataend();
$message->quit;
|