Notifications using external server ( send_mail.pl )

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Jacek2
Posts: 1
Joined: Wed Apr 20, 2016 1:46 pm

Notifications using external server ( send_mail.pl )

Post by Jacek2 »

Hi everyone,

I need to send notifications using external server. I tried to use this solution, but I sends me empty variables ( at the bottom ). Can anyone knows how to solve it or can show how to do it ? Thanks in advance.

I have Nagios Core, and send_mail.pl. And configuration as follows:

Code: Select all

#!/usr/local/bin/perl -w

use Net::SMTP;
use Getopt::Std;
use strict;
use warnings;

my $mailhost	=	'j.mail.jjj';
my $maildomain	=	'j.mail.jjj';
my $mailfrom	=	'j.mail.jjj';
my $mailto		= ''j.mail.jjj';
my $timeout		=	30;
my $mailsubject	=	'';							#	Leave blank
my $mailbody	=	'';							#	Leave blank
my $logfile		=	'/tmp/mail.log';			#	Put somewhere better
my $debug		=	1;							#	To enable SMTP session debugging to logfile
if (not open(LOG,">>$logfile") ) {
	$mailsubject = "Nagios Monitoring Alert: Can't open $logfile for append: $!";
	print STDERR "Nagios Monitoring Alert: Can't open $logfile for append: $!";
	sendmail();
	exit 1;
	}
our ($opt_n, $opt_h, $opt_s , $opt_a , $opt_i , $opt_d , $opt_e);
# Get the cmdline options
getopt('nhsaide');
print LOG localtime() . " sending mail to $opt_e with host $opt_h state $opt_s\n";
if (not defined $opt_n or $opt_n eq "") {
	print "option -n not defined!\n";
	$opt_n = "UNDEFINED";
}
elsif (not defined $opt_h or $opt_h eq "") {
	print "option -h not defined!\n";
	$opt_h = "UNDEFINED";
}
elsif (not defined $opt_s or $opt_s eq "") {
	print "option -s not defined!\n";
	$opt_s = "UNDEFINED";
}
elsif (not defined $opt_a or $opt_a eq "") {
	print "option -a not defined!\n";
	$opt_a = "UNDEFINED";
}
elsif (not defined $opt_i or $opt_i eq "") {
	print "option -i not defined!\n";
	$opt_i = "UNDEFINED";
}
elsif (not defined $opt_d or $opt_d eq "") {
	print "option -d not defined!\n";
	$opt_d = "UNDEFINED";
}
elsif (not defined $opt_e or $opt_e eq "") {
	die "CRITICAL: option -e not defined!.  Host $opt_h, Notification: $opt_n, State: $opt_s, Info: $opt_i\n";
}
# You can change the subject here
$mailsubject	=	"Nagios Monitoring Alert: $opt_h is $opt_s";
$mailto			=	"$opt_e";
$mailbody = <<_END_;
***** Nagios  *****

Notification Type: $opt_n
Host: $opt_h
State: $opt_s
Address: $opt_a
Info: $opt_i
Date/Time: $opt_d
_END_
sendmail();
print LOG localtime() . " sent mail to $opt_e successfully\n";

exit 0;
sub sendmail {
	my $smtp = Net::SMTP->new(
								$mailhost,
								Hello => $maildomain,
								Timeout => $timeout,
								Debug   => $debug,
								);

	$smtp->mail($mailfrom);
	$smtp->to($mailto);

	$smtp->data();
	$smtp->datasend("To: $mailto\n");
	$smtp->datasend("From: $mailfrom\n");
	$smtp->datasend("Subject: $mailsubject\n");
	$smtp->datasend("\n");
	$smtp->datasend("$mailbody\n");
	$smtp->dataend();
	$smtp->quit;
}




CONTACT.CFG

Code: Select all

define contact {
        contact_name    j
        use             generic-contact
        alias           j
        email          [email protected]
    }
COMMANDS.CFG

Code: Select all

#'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
        }
#'notify-service-by-email' command definition

Code: Select all

define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }
AND THIS IS WHAT I GET FROM MY E-MAIL:::::


***** Nagios *****

Notification Type: $

Service: $
Host: $
Address: $
State: $

Date/Time: $

Additional Info:

$
Last edited by hsmith on Fri May 13, 2016 10:29 am, edited 1 time in total.
Reason: Please wrap long output in [code][/code] tags to make your post easier to read.
User avatar
hsmith
Agent Smith
Posts: 3539
Joined: Thu Jul 30, 2015 11:09 am
Location: 127.0.0.1
Contact:

Re: Notifications using external server ( send_mail.pl )

Post by hsmith »

What happens if you run the script manually from the terminal?
Former Nagios Employee.
me.
Locked