Debian Codegraph @

local/traptoemail

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
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 use POSIX qw(strftime);
15
16 # for sane timestamp format
17 $ENV{LANG} = 'C';
18
19 $opts{'s'} = "localhost";
20 $opts{'f'} = 'root@' . `hostname`;
21 chomp($opts{'f'});
22 getopts("hs:f:", \%opts);
23
24 if ($opts{'h'}) {
25 print "
26 traptoemail [-s smtpserver] [-f fromaddress] toaddress [...]
27
28 traptoemail shouldn't be called interatively by a user. It is
29 designed to be called as an snmptrapd extension via a \"traphandle\"
30 directive in the snmptrapd.conf file. See the snmptrapd.conf file for
31 details.
32
33 Options:
34 -s smtpserver Sets the smtpserver for where to send the mail through.
35 -f fromaddress Sets the email address to be used on the From: line.
36 toaddress Where you want the email sent to.
37
38 ";
39 exit;
40 }
41
42 die "no recepients to send mail to" if ($#ARGV < 0);
43
44 # process the trap:
45 $hostname = <STDIN>;
46 chomp($hostname);
47 $ipaddress = <STDIN>;
48 chomp($ipaddress);
49
50 $maxlen = 0;
51 while(<STDIN>) {
52 ($oid, $value) = /([^\s]+)\s+(.*)/;
53 push @oids, $oid;
54 push @values, $value;
55 $maxlen = (length($oid) > $maxlen) ? length($oid) : $maxlen;
56 }
57 $maxlen = 60 if ($maxlen > 60);
58 $formatstr = "%" . $maxlen . "s %s\n";
59
60 die "illegal trap" if ($#oids < 1);
61
62 # send the message
63 $message = Net::SMTP->new($opts{'s'}) || die "can't talk to server $opts{'s'}\n";
64 $message->mail($opts{'f'});
65 $message->to(@ARGV) || die "failed to send to the recepients ",join(",",@ARGV),": $!";
66 $message->data();
67 $message->datasend("To: " . join(", ",@ARGV) . "\n");
68 $message->datasend("From: $opts{f}\n");
69 $message->datasend("Date: ".strftime("%a, %e %b %Y %X %z", localtime())."\n");
70 $message->datasend("Subject: trap received from $hostname: $values[1]\n");
71 $message->datasend("\n");
72 $message->datasend("Host: $hostname ($ipaddress)\n");
73 for($i = 0; $i <= $#oids; $i++) {
74 $message->datasend(sprintf($formatstr, $oids[$i], $values[$i]));
75 }
76 $message->dataend();
77 $message->quit;