#!/usr/bin/perl -w
#
# Wriiten by Rob Moss, 2005-10-09
# coding@mossko.com
# (Modified for priority and additional info by John Kramme (jkramme@crownpack.com, jkramme@email.com) 2007-05-22)
#
# A replacement for Nagios's basic mail system which relies on /bin/mailx and local sendmail config
# *added availibility to add Priority header information to the email for rules execution on devices and clients
# *use the '-high' commands for groups that need to have the flag set to Importance
# *use the '-low' commands for groups that need to have the flag set to Low Importance
#
# To use this, just replace the entries in commands.cfg and update your hosts and services from the default "notify-by-email" to use the specific host-notify-by-email or service-notify-by-email
#
# I put this in the plugins directory /usr/local/nagios/libexec but you can put it anywhere. Just update the path
# commands.cfg
# 'notify-by-email' command definition (Normal Priority)(Services)
#define command{
#	command_name	notify-by-email
#	command_line	/usr/local/nagios/libexec/send_mail.pl -n "SERVICE $NOTIFICATIONTYPE$" -h "$HOSTNAME$" -s "$SERVICESTATE$" -a "$HOSTADDRESS$" -i "$SERVICEDESC$" -j "$SERVICEOUTPUT$" -k "$SERVICECHECKCOMMAND$" -d "$LONGDATETIME$" -e "$CONTACTEMAIL$" -p "3" -t "** $NOTIFICATIONTYPE$ alert - $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ ** at $SHORTDATETIME$"
#	}
#
# 'notify-by-email-high' command definition (High Priority)(Services)
#define command{
#	command_name	notify-by-email-high
#	command_line	/usr/local/nagios/libexec/send_mail.pl -n "SERVICE $NOTIFICATIONTYPE$" -h "$HOSTNAME$" -s "$SERVICESTATE$" -a "$HOSTADDRESS$" -i "$SERVICEDESC$" -j "$SERVICEOUTPUT$" -k "$SERVICECHECKCOMMAND$" -d "$LONGDATETIME$" -e "$CONTACTEMAIL$" -p "1" -t "** $NOTIFICATIONTYPE$ alert - $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ ** at $SHORTDATETIME$"
#	}
#
# 'notify-by-email-low' command definition (Low Priority)(Services)
#define command{
#	command_name	notify-by-email-low
#	command_line	/usr/local/nagios/libexec/send_mail.pl -n "SERVICE $NOTIFICATIONTYPE$" -h "$HOSTNAME$" -s "$SERVICESTATE$" -a "$HOSTADDRESS$" -i "$SERVICEDESC$" -j "$SERVICEOUTPUT$" -k "$SERVICECHECKCOMMAND$" -d "$LONGDATETIME$" -e "$CONTACTEMAIL$" -p "5" -t "** $NOTIFICATIONTYPE$ alert - $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ ** at $SHORTDATETIME$"
#	}
#
# 'host-notify-by-email' command definition (Normal Priority)
#define command{
#	command_name	host-notify-by-email
#	command_line	/usr/local/nagios/libexec/send_mail.pl -n "HOST $NOTIFICATIONTYPE$" -h "$HOSTNAME$"  -s "$HOSTSTATE$" -a "$HOSTADDRESS$" -i "$HOSTALIAS$" -j "$HOSTOUTPUT$" -k "$HOSTCHECKCOMMAND$" -d "$LONGDATETIME$" -e "$CONTACTEMAIL$" -p "3" -t ""Host $HOSTSTATE$ alert for $HOSTNAME$ at $SHORTDATETIME$"
#	}
#
# 'host-notify-by-email-high' command definition (High Priority)
#define command{
#	command_name	host-notify-by-email-high
#	command_line	/usr/local/nagios/libexec/send_mail.pl -n "HOST $NOTIFICATIONTYPE$" -h "$HOSTNAME$"  -s "$HOSTSTATE$" -a "$HOSTADDRESS$" -i "$HOSTALIAS$" -j "$HOSTOUTPUT$" -k "$HOSTCHECKCOMMAND$" -d "$LONGDATETIME$" -e "$CONTACTEMAIL$" -p "1" -t "Host $HOSTSTATE$ alert for $HOSTNAME$ at $SHORTDATETIME$"
#	}
#
# 'host-notify-by-email-low' command definition (High Priority)
#define command{
#	command_name	host-notify-by-email-low
#	command_line	/usr/local/nagios/libexec/send_mail.pl -n "HOST $NOTIFICATIONTYPE$" -h "$HOSTNAME$"  -s "$HOSTSTATE$" -a "$HOSTADDRESS$" -i "$HOSTALIAS$" -j "$HOSTOUTPUT$" -k "$HOSTCHECKCOMMAND$" -d "$LONGDATETIME$" -e "$CONTACTEMAIL$" -p "5" -t "Host $HOSTSTATE$ alert for $HOSTNAME$ at $SHORTDATETIME$"
#	}
#
#
# contacts.cfg
#define contact{
#	contact_name				contact
#	host_notification_commands		host-notify-by-email
#	service_notification_commands		notify-by-email
#	email					email@address.com
#}
#define contact{
#	contact_name				contact-high
#	host_notification_commands		host-notify-by-email-high
#	service_notification_commands		notify-by-email-high
#	email					email@address.com
#}
#define contact{
#	contact_name				contact-low
#	host_notification_commands		host-notify-by-email-low
#	service_notification_commands		notify-by-email-low
#	email					email@address.com
#}




use strict;
use Net::SMTP;
use Getopt::Std;

my $mailhost		=	'mailserver.domain.com';				#	Change to your mailserver DNS name
my $maildomain		=	'hostname.domain.com';					#	Change to the DNS name of your Nagios Machine
my $mailfrom		=	'from@domain.com';					#	Change to what you want the "From Name" to be
my $mailto		=	'user@domain.com';					#	Change to your admininstator email address
my $timeout		=	30;							#	SMTP timeout variable
my $mailsubject		=	'';							#	Leave blank
my $mailbody		=	'';							#	Leave blank
my $logfile		=	'/usr/local/nagios/var/mail.log';			#	Put somewhere better
my $debug		=	0;							#	To enable SMTP session debugging to logfile

################################################################
# Email for nagios
#
# Subject: "Host $HOSTSTATE$ alert for $HOSTNAME$ at $SHORTDATETIME$"     							#	For Hosts
# Subject: ""** $NOTIFICATIONTYPE$ alert - $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ ** at $SHORTDATETIME$"		#	For Services
#
# "***** Nagios  *****
#
# Notification Type: $NOTIFICATIONTYPE$
# Host: $HOSTNAME$
# State: $HOSTSTATE$
# Address: $HOSTADDRESS$
# Info: $HOSTALIAS$
#       $HOSTOUTPUT$
#
# Check Command: $HOSTCHECKCOMMAND$
#
# Date/Time: $LONGDATETIME$
#
#################################################################
#
# Commandline options
#
# -a	HOSTADDRESS
# -d	LONGDATETIME
# -e	CONTACTEMAIL
# -f	ACKNOWLEDGMENT COMMENT
# -h	HOSTNAME
# -i	HOSTALIAS
# -j	HOSTOUTPUT
# -k	HOSTCHECKCOMMAND
# -l	SERVICEDESC
# -n	NOTIFICATIONTYPE
# -o	ACKNOLEDGMENT (who)
# -p	X-PRIORITY (header info)
# -s	HOSTSTATE
# -t	SUBJECT
#
#################################################################


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_j, $opt_k, $opt_l, $opt_d, $opt_e, $opt_p, $opt_o, $opt_f, $opt_t, $opt_otemp);
# Get the cmdline options
getopt('nhsaijklofdept');
#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_j or $opt_j eq "") {
	print "option -j not defined!\n";
	$opt_j = "";
}
elsif (not defined $opt_k or $opt_k eq "") {
	print "option -k not defined!\n";
	$opt_k = "";
}
elsif (not defined $opt_l or $opt_l eq "") {
	print "option -l not defined!\n";
	$opt_l = "";
}
elsif (not defined $opt_o or $opt_o eq "" ) {
	print "option -o not defined!\n";
	$opt_o = "";
	$opt_otemp = "";
}
elsif (not defined $opt_f or $opt_f eq "") {
	print "option -f not defined!\n";
	$opt_f = "";
}
elsif (not defined $opt_d or $opt_d eq "") {
	print "option -d not defined!\n";
	$opt_d = "UNDEFINED";
}
elsif (not defined $opt_p or $opt_p eq "") {
	print "option -p not defined!\n";
	$opt_p = "UNDEFINED";
}
elsif (not defined $opt_t or $opt_t eq "") {
	print "option -t not defined!\n";
	$opt_t = "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";
}

if ($opt_n eq "HOST ACKNOWLEDGEMENT") {
	$opt_otemp = "Acknowledged By: ";
}
if ($opt_n eq "SERVICE ACKNOWLEDGEMENT") {
	$opt_otemp = "Acknowledged By: ";
}
# You can change the subject here if not using the passed subject
$mailsubject	=	"$opt_t";
$mailto	=	"$opt_e";

$mailbody = <<_END_;
***** Nagios  *****

Notification Type: $opt_n
Host: $opt_h
State: $opt_s
Address: $opt_a
Info: $opt_i            $opt_l
      $opt_j

$opt_otemp $opt_o
                  $opt_f

Check Command: $opt_k

Date/Time: $opt_d
_END_


sendmail();
print LOG localtime() . " sent mail to $opt_e successfully ($opt_h state $opt_s)\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("X-Priority: $opt_p\n");
	$smtp->datasend("Subject: $mailsubject\n");
	$smtp->datasend("\n");
	$smtp->datasend("$mailbody\n");
	$smtp->dataend();

	$smtp->quit;
}
