3 # This is a snmptrapd handler script to convert snmp traps into email
7 # Put a line like the following in your snmptrapd.conf file:
8 # traphandle TRAPOID|default /usr/local/bin/traptoemail [-f FROM] [-s SMTPSERVER]b ADDRESSES
9 # FROM defaults to "root"
10 # SMTPSERVER defaults to "localhost"
15 $opts{'s'} = "localhost";
16 $opts{'f'} = 'root@' . `hostname`;
18 getopts("hs:f:", \%opts);
22 traptoemail [-s smtpserver] [-f fromaddress] toaddress [...]
24 traptoemail shouldn't be called interatively by a user. It is
25 designed to be called as an snmptrapd extension via a \"traphandle\"
26 directive in the snmptrapd.conf file. See the snmptrapd.conf file for
30 -s smtpserver Sets the smtpserver for where to send the mail through.
31 -f fromaddress Sets the email address to be used on the From: line.
32 toaddress Where you want the email sent to.
38 die "no recepients to send mail to" if ($#ARGV < 0);
48 ($oid, $value) = /([^\s]+)\s+(.*)/;
51 $maxlen = (length($oid) > $maxlen) ? length($oid) : $maxlen;
53 $maxlen = 60 if ($maxlen > 60);
54 $formatstr = "%" . $maxlen . "s %s\n";
56 die "illegal trap" if ($#oids < 1);
59 $message = Net::SMTP->new($opts{'s'}) || die "can't talk to server $opts{'s'}\n";
60 $message->mail($opts{'f'});
61 $message->to(@ARGV) || die "failed to send to the recepients ",join(",",@ARGV),": $!";
63 $message->datasend("To: " . join(", ",@ARGV) . "\n");
64 $message->datasend("From: $opts{f}\n");
65 $message->datasend("Subject: trap received from $hostname: $varbinds[1]\n");
66 $message->datasend("\n");
67 $message->datasend("Host: $hostname ($ipaddress)\n");
68 for($i = 0; $i <= $#oids; $i++) {
69 $message->datasend(sprintf($formatstr, $oids[$i], $values[$i]));