Blame local/traptoemail

Packit fcad23
#!/usr/bin/perl
Packit fcad23
Packit fcad23
# This is a snmptrapd handler script to convert snmp traps into email
Packit fcad23
# messages.
Packit fcad23
Packit fcad23
# Usage:
Packit fcad23
# Put a line like the following in your snmptrapd.conf file:
Packit fcad23
#  traphandle TRAPOID|default /usr/local/bin/traptoemail [-f FROM] [-s SMTPSERVER]b ADDRESSES
Packit fcad23
#     FROM defaults to "root"
Packit fcad23
#     SMTPSERVER defaults to "localhost"
Packit fcad23
Packit fcad23
use Net::SMTP;
Packit fcad23
use Getopt::Std;
Packit fcad23
use POSIX qw(strftime);
Packit fcad23
Packit fcad23
$opts{'s'} = "localhost";
Packit fcad23
$opts{'f'} = 'root@' . `hostname`;
Packit fcad23
chomp($opts{'f'});
Packit fcad23
getopts("hs:f:", \%opts);
Packit fcad23
Packit fcad23
if ($opts{'h'}) {
Packit fcad23
    print "
Packit fcad23
traptoemail [-s smtpserver] [-f fromaddress] toaddress [...]
Packit fcad23
Packit fcad23
  traptoemail shouldn't be called interatively by a user.  It is
Packit fcad23
  designed to be called as an snmptrapd extension via a \"traphandle\"
Packit fcad23
  directive in the snmptrapd.conf file.  See the snmptrapd.conf file for
Packit fcad23
  details.
Packit fcad23
Packit fcad23
  Options:
Packit fcad23
    -s smtpserver      Sets the smtpserver for where to send the mail through.
Packit fcad23
    -f fromaddress     Sets the email address to be used on the From: line.
Packit fcad23
    toaddress          Where you want the email sent to.
Packit fcad23
Packit fcad23
";
Packit fcad23
    exit;
Packit fcad23
}
Packit fcad23
Packit fcad23
die "no recepients to send mail to" if ($#ARGV < 0);
Packit fcad23
Packit fcad23
# process the trap:
Packit fcad23
$hostname = <STDIN>;
Packit fcad23
chomp($hostname);
Packit fcad23
$ipaddress = <STDIN>;
Packit fcad23
chomp($ipaddress);
Packit fcad23
Packit fcad23
$maxlen = 0;
Packit fcad23
while(<STDIN>) {
Packit fcad23
    ($oid, $value) = /([^\s]+)\s+(.*)/;
Packit fcad23
    push @oids, $oid;
Packit fcad23
    push @values, $value;
Packit fcad23
    $maxlen = (length($oid) > $maxlen) ? length($oid) : $maxlen;
Packit fcad23
}
Packit fcad23
$maxlen = 60 if ($maxlen > 60);
Packit fcad23
$formatstr = "%" . $maxlen . "s  %s\n";
Packit fcad23
Packit fcad23
die "illegal trap" if ($#oids < 1);
Packit fcad23
Packit fcad23
# send the message
Packit fcad23
$message = Net::SMTP->new($opts{'s'}) || die "can't talk to server $opts{'s'}\n";
Packit fcad23
$message->mail($opts{'f'});
Packit fcad23
$message->to(@ARGV) || die "failed to send to the recepients ",join(",",@ARGV),": $!";
Packit fcad23
$message->data();
Packit fcad23
$message->datasend("To: " . join(", ",@ARGV) . "\n");
Packit fcad23
$message->datasend("From: $opts{f}\n");
Packit fcad23
$message->datasend("Date: ".strftime("%a, %e %b %Y %X %z", localtime())."\n");
Packit fcad23
$message->datasend("Subject: trap received from $hostname: $values[1]\n");
Packit fcad23
$message->datasend("\n");
Packit fcad23
$message->datasend("Host: $hostname ($ipaddress)\n");
Packit fcad23
for($i = 0; $i <= $#oids; $i++) {
Packit fcad23
    $message->datasend(sprintf($formatstr, $oids[$i], $values[$i]));
Packit fcad23
}
Packit fcad23
$message->dataend();
Packit fcad23
$message->quit;