Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / local / traptoemail
1 #!/usr/bin/perl
2
3 # This is a snmptrapd handler script to convert snmp traps into email
4 # messages.
5
6 # Usage:
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"
11
12 use Net::SMTP;
13 use Getopt::Std;
14
15 $opts{'s'} = "localhost";
16 $opts{'f'} = 'root@' . `hostname`;
17 chomp($opts{'f'});
18 getopts("hs:f:", \%opts);
19
20 if ($opts{'h'}) {
21     print "
22 traptoemail [-s smtpserver] [-f fromaddress] toaddress [...]
23
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
27   details.
28
29   Options:
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.
33
34 ";
35     exit;
36 }
37
38 die "no recepients to send mail to" if ($#ARGV < 0);
39
40 # process the trap:
41 $hostname = <STDIN>;
42 chomp($hostname);
43 $ipaddress = <STDIN>;
44 chomp($ipaddress);
45
46 $maxlen = 0;
47 while(<STDIN>) {
48     ($oid, $value) = /([^\s]+)\s+(.*)/;
49     push @oids, $oid;
50     push @values, $value;
51     $maxlen = (length($oid) > $maxlen) ? length($oid) : $maxlen;
52 }
53 $maxlen = 60 if ($maxlen > 60);
54 $formatstr = "%" . $maxlen . "s  %s\n";
55
56 die "illegal trap" if ($#oids < 1);
57
58 # send the message
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),": $!";
62 $message->data();
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]));
70 }
71 $message->dataend();
72 $message->quit;