#!/usr/bin/perl -w # # Copyright (c) 2002-2004 Stéphane Urbanovski # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # you should have received a copy of the GNU General Public License # along with this program (or with Nagios); if not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA # # use strict; # should never be differently :-) use warnings; use File::Basename; # get basename() use lib dirname($0); # to get local libs use libplugins qw(:DEFAULT ); use Net::SMTP; use Sys::Hostname; use Time::Local; # for date conversions use POSIX qw(strftime); #use Data::Dumper; # debug tool my $DEBUG=0; # Default values : my %DEFVALUES = ( 'VERSION' => { 'v' => "1.2", 'desc' => _("Plugin Version"), }, ); # Get argument : use vars qw( $opt_V $opt_h $opt_m ); use vars qw( $warn $crit $smtp_server $mail_sender $mail_addr $pop_key); Getopt::Long::GetOptions ( "V" => \$opt_V, "version" => \$opt_V, "h|?" => \$opt_h, "help" => \$opt_m, "man" => \$opt_m, "d" => \$DEBUG, "debug=i" => \$DEBUG, "w=i" => \$warn, "warning=i" => \$warn, "c=i" => \$crit, "critical=i"=> \$crit, "H=s" => \$smtp_server, "host=s" => \$smtp_server, "s=s" => \$mail_sender, "sender=s" => \$mail_sender, "m=s" => \$mail_addr, "mail=s" => \$mail_addr, "k=s" => \$pop_key, "key=s" => \$pop_key, ); my $state = "UNKNOWN"; my $answer = ""; if ( defined ($opt_h) || defined ($opt_m) ) { &showUsage($opt_m,\%DEFVALUES); } elsif (defined ($opt_V)) { &showVersion($DEFVALUES{VERSION}{v}); } if ( (!defined ($smtp_server)) || ($smtp_server eq "")) { print _("Missing 'smtp server' parameter !")."\n"; &showUsage(); exit $EXIT_CODES{$state}; } if ( (!defined ($mail_addr)) || ($mail_addr eq "")) { print _("Missing 'mail address' parameter !")."\n"; &showUsage(); exit $EXIT_CODES{$state}; } $pop_key = $pop_key || $smtp_server; my $hostname = hostname; $mail_sender = $mail_sender || $mail_addr || $ENV{USER}."@".$hostname ; my $mail_sender_full = $mail_sender; if ( $mail_sender !~ /<.*>/ ) { $mail_sender_full = "Racvision mailler <".$mail_sender.">"; } #$warn = $warn || 20; #$crit = $crit || 15; $state = "CRITICAL"; if ($DEBUG) { print "DEBUG: smtp_server='$smtp_server' mailaddr='$mail_addr' pop_key='$pop_key'\n" } $state = "OK"; $answer = sprintf(_("Message (key %s) successfully sended."),$pop_key); my $smtp = Net::SMTP->new($smtp_server, Debug => $DEBUG); if ($smtp) { $smtp->mail($mail_sender); $smtp->to($mail_addr); $smtp->data(); $smtp->datasend("To: $mail_addr\n"); $smtp->datasend("From: $mail_sender_full\n"); $smtp->datasend("Subject: $pop_key\n"); $smtp->datasend("\n"); $smtp->datasend("PING!\n\n"); $smtp->datasend("Message de test de messagerie.\n\n"); $smtp->datasend("Clef: $pop_key\n\n"); $smtp->datasend("Date: ".&getDate()."\n\n"); $smtp->dataend(); $smtp->quit; } else { $state = "CRITICAL"; $answer = sprintf(_("Last message '%s' could not be sended !"),$pop_key); } sub getDate { return strftime("%A %e %B %Y %H:%M:%S", localtime(time())) } print $answer."\n"; exit $EXIT_CODES{$state}; __END__ =head1 NAME This plugins send mail used by check_mail_pop3.pl. =head1 SYNOPSIS B S<-H I> S<-m I> [S<-k I>] [S<-s I>] =head1 DESCRIPTION Nagios plugins to check smtp server. This plugins also allow you to periodicaly send a mail that could be checked by check_mail_pop3.pl Nagios plugin. =head1 OPTIONS =over 4 =item B<-H> I The SMTP mail server. =item B<-m> I The mail address we used for our tests. =item B<-k> I This is a key used in message subject that will be used by check_mail_pop3.pl. =item B<-s> I Sender mail address. Default to . =back =head1 NAGIOS CONGIGURATIONS In F you have to add : # 'check_mail_smtp' # @param $ARG1$ mail address # @param $ARG2$ key define command { command_name check_mail_smtp command_line $USER1$/check_mail_smtp.pl -H $HOSTADDRESS$ -m $ARG1$ -k $ARG2$ } =head1 AUTHOR Stéphane Urbanovski =cut