/[nagios-plugins-perl]/trunk/plugins/notify_by_mail.pl
ViewVC logotype

Contents of /trunk/plugins/notify_by_mail.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 158 - (show annotations) (download)
Fri Mar 20 14:16:17 2015 UTC (5 years, 8 months ago) by racvision
File MIME type: text/plain
File size: 6062 byte(s)
remaster mail template and add long output to mail notification
1 #!/usr/bin/perl -w
2 #
3 # (c) 2002-2005 St├ęphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty
12 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # you should have received a copy of the GNU General Public License
16 # along with this program (or with Netsaint); if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA
19 #
20 #
21 # $Id: notify_by_mail.pl,v 1.4 2005-11-08 10:47:44 uid5000 Exp $
22
23 use strict; # should never be differently :-)
24 use warnings;
25
26 use File::Basename; # get basename()
27 use lib dirname($0); # to get local libs
28 use libplugins qw(%EXIT_CODES &showUsage &showVersion);
29
30 use POSIX qw(strftime);
31 use Net::SMTP; # used to send mail
32
33 use Sys::Hostname;
34
35 use Data::Dumper; # debug tool
36
37
38 # Default values :
39 my %DEFVALUES = (
40 'TEMPLATEFILE' => {
41 'v' => "/etc/racvision/nagios/mailtemplate.txt",
42 'desc' => _("Template file path"),
43 },
44 'VERSION' => {
45 'v' => "1.5",
46 'desc' => _("Plugin Version"),
47 },
48 );
49
50 my $HOSTNAME = &hostname || die "Enable to get hostname";
51 my $SENDER = _("Nagios Application")." <nagios\@$HOSTNAME>";
52
53
54 my $DEBUG = 0;
55
56 # Get the options :
57 use vars qw($opt_V $opt_h $opt_m $opt_mail $opt_template);
58
59
60 $opt_template = $DEFVALUES{'TEMPLATEFILE'}{'v'};
61
62 my %info;
63 foreach my $k ( 'NOTIFICATIONTYPE','SERVICEDESC','HOSTNAME','HOSTADDRESS','SERVICESTATE','OUTPUT' ) {
64 $info{$k} = '( '._("No information").' )';
65 }
66 $info{'DATETIME'} = &getDate;
67 $info{'LONGOUTPUT'} = '';
68 $info{'CONTACTEMAIL'} = $opt_mail;
69 $info{'NAGIOSHOST'} = $HOSTNAME;
70
71 Getopt::Long::GetOptions (
72 "V" => \$opt_V, "version" => \$opt_V,
73 "h|?" => \$opt_h, "help" => \$opt_m,
74 "man" => \$opt_m,
75
76 "d" => \$DEBUG, "debug=i" => \$DEBUG,
77
78 "nt=s" => \$info{'NOTIFICATIONTYPE'},
79 "sd=s" => \$info{'SERVICEDESC'},
80 "hn=s" => \$info{'HOSTNAME'},
81 "ha=s" => \$info{'HOSTADDRESS'},
82 "ss=s" => \$info{'SERVICESTATE'},
83 "date=s" => \$info{'DATETIME'},
84 "i=s" => \$info{'OUTPUT'},
85 "l=s" => \$info{'LONGOUTPUT'},
86 "f=s" => \$SENDER,
87 "m=s" => \$opt_mail,
88 "t=s" => \$opt_template,
89 );
90
91 if ( defined ($opt_h) || defined ($opt_m) ) {
92 &showUsage($opt_m,\%DEFVALUES);
93 } elsif (defined ($opt_V)) {
94 &showVersion($DEFVALUES{'VERSION'}{'v'});
95 }
96
97 if ( (!defined ($opt_mail)) || ($opt_mail eq "")) {
98 print _("Missing mail parameter !");
99 &showUsage();
100 }
101
102 # print Dumper(%info);print "\n\n";
103 my @content ;
104
105 if ( -f $opt_template ) {
106 # use a template
107 open (TEMPLATE,"<".$opt_template);
108
109 while (my $in = <TEMPLATE>) {
110
111 while ( $in =~ /\$([A-Z]+)\$/g ) {
112 if ( defined($info{$1}) ) {
113 my $k = $1;
114 $in =~ s/\$$k\$/$info{$k}/g;
115 }
116 }
117
118 push (@content,$in);
119 }
120
121 close(TEMPLATE);
122
123 } else {
124 # use basic message
125 push (@content,"\n***** "._("Nagios Notification")." *****\n\n");
126 push (@content,sprintf(_("Notification Type: %s")."\n\n",$info{'NOTIFICATIONTYPE'}));
127 push (@content,sprintf(_("Service: %s")."\n",$info{'SERVICEDESC'}));
128 push (@content,sprintf(_("Host: %s")."\n",$info{'HOSTNAME'}));
129 push (@content,sprintf(_("Address: %s")."\n",$info{'HOSTADDRESS'}));
130 push (@content,sprintf(_("State: %s")."\n\n",$info{'SERVICESTATE'}));
131 push (@content,sprintf(_("Date/Time: %s")."\n\n.",$info{'DATETIME'}));
132 push (@content,_("Alert information:").sprintf("\n\n%s\n",$info{'OUTPUT'}));
133 push (@content,_("Additional information:").sprintf("------\n\n%s\n",$info{'OUTPUT'}));
134
135 }
136
137 if ( $DEBUG ) {
138
139 print "From: $SENDER\n";
140 print "To: $opt_mail\n";
141 print "Subject: ".sprintf(_("** %s alert - %s/%s is %s **"),$info{'NOTIFICATIONTYPE'},$info{'HOSTNAME'},$info{'SERVICEDESC'},$info{'SERVICESTATE'})."\n";
142
143 foreach my $l (@content) {
144 print $l;
145 }
146
147 } else {
148
149 # send mail
150 my $smtp = new Net::SMTP ('localhost');
151
152 $smtp->mail($SENDER);
153 $smtp->to($opt_mail);
154
155 $smtp->data();
156 $smtp->datasend("From: $SENDER\n");
157 $smtp->datasend("To: $opt_mail\n");
158 $smtp->datasend("Subject: ".sprintf(_("** %s alert - %s/%s is %s **"),$info{'NOTIFICATIONTYPE'},$info{'HOSTNAME'},$info{'SERVICEDESC'},$info{'SERVICESTATE'})."\n");
159 $smtp->datasend("X-MailFilter: nagios\n");
160
161 foreach my $l (@content) {
162 $smtp->datasend($l);
163 }
164
165 $smtp->dataend();
166 $smtp->quit;
167
168 }
169
170 sub getDate {
171 return strftime("%A %e %B %Y %H:%M:%S", localtime(time()))
172 }
173
174 __END__
175
176 =head1 NAME
177
178 notify_by_mail.pl - Nagios mail notifier.
179
180 =head1 SYNOPSIS
181
182 B<notify_by_mail.pl>
183
184 =head1 DESCRIPTION
185
186 Send Nagios notifications by mail. It can send a simple text message or a mime coded one if you give it a template.
187 See the default template /etc/racvision/nagios/mailtemplate.txt for example.
188
189 =head1 OPTIONS
190
191 =over 4
192
193 =item B<-nt> I<notification type>
194
195 Notification type.
196
197 =item B<-sd> I<service description>
198
199 Service description.
200
201 =item B<-ss> I<service state>
202
203 Service state.
204
205 =item B<-ha> I<host address>
206
207 Checked host address.
208
209 =item B<-hn> I<hostname>
210
211 Checked host name.
212
213 =item B<-i> I<plugin ouput>
214
215 Plugin ouput.
216
217 =item B<-l> I<plugin long ouput>
218
219 Plugin long ouput (after 1st line).
220
221 =item B<-m> I<mail>
222
223 Destination mail address.
224
225 =item B<-f> I<from>
226
227 Mail sender (from).
228
229 =back
230
231 =head1 EXAMPLE
232
233 notify_by_mail.pl -nt PROBLEM -sd "Test service" -ha "racvision" -i "Just a test" -ss "CRITICAL" -m "s.urbanovski@ac-nancy-metz.fr"
234
235 =head1 NAGIOS EXAMPLE
236
237 define command {
238 command_name notify-by-email-mime
239 command_line $USER1$/notify_by_mail.pl -nt "$NOTIFICATIONTYPE$" -sd "$SERVICEDESC$" -ss "$SERVICESTATE$" -ha "$HOSTADDRESS$" -hn "$HOSTNAME$" -i "$OUTPUT$" -m "$CONTACTEMAIL$" -f "Racvision"
240 #~ type service_notification
241 #~ _comment Notification par couriel (mime/html)
242 }
243
244 =head1 AUTHOR
245
246 St├ęphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
247
248 =cut

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8