<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl 
#===============================================================================
#         FILE:  ./custom_mail_alert.pl
#        USAGE:  ./custom_mail_alert.pl
#  DESCRIPTION: meant as hook called by the check_logfiles plugin to send custom mail alerts 
#
#      OPTIONS:  --sender &lt;sender&gt; --recpt &lt;recpt&gt; --subject &lt;subject&gt; [--display-name sender display name] [--debug-file /path/to/file]
#	   AUTHOR:	 Jess Portnoy
#      VERSION:  0.2
#      CREATED:  02/21/2011 09:27:43 AM EST
#     REVISION:  ---
#===============================================================================

# Can't use these because of the import from check_logfiles hash file :(
#use strict;
#use warnings;
use Getopt::Long;

my ($sender,$recpt,$subject,$display_name,$message,$log_file,$debug_file,$send_if_rc_gt);
GetOptions(
	"sender:s" =&gt; \$sender,
	"recpt:s" =&gt; \$recpt,
	"subject:s" =&gt; \$subject,
	"display-name:s" =&gt; \$display_name,
	"message:s" =&gt; \$message,
	"log-file:s" =&gt; \$log_file,
	"send-if-rc-gt:i" =&gt; \$send_if_rc_gt,
	"debug-file:s" =&gt; \$debug_file,
);
if (defined $debug_file){
	open MY_DEBUGGIE, "&gt;&gt;$debug_file";
	print MY_DEBUGGIE "$0: $sender $recpt $subject $display_name $message $log_file $send_if_rc_gt\n";
	close MY_DEBUGGIE;
}
if (!defined $sender||!defined $recpt||!defined $subject||!defined $log_file ||!defined $message){
	print "Usage: $0 --sender &lt;sender&gt; --recpt &lt;recpt&gt; --subject &lt;subject&gt; --message &lt;mail body&gt; --log-file &lt;/path/to/check_logfiles/tmp/file&gt; [--display-name &lt;sender display name&gt;] [--send-if-rc-gt &lt;1-9&gt; ] [--debug-file &lt;/path/to/file&gt;]\n";
	exit (-1);
}

my $display_name=$display_name||$sender;
my ($escaped_sender,$escaped_recpt)=($sender,$recpt);
# Require the file that defines the hash where the desired log entry resides:
require $log_file;
# if we have service output, send the mail.
if ((defined $state-&gt;{'servicestateid'} &amp;&amp; defined $send_if_rc_gt) &amp;&amp; ($state-&gt;{'servicestateid'} &gt; $send_if_rc_gt)){
	print "$state-&gt;{'servicestateid'} was lower than $send_if_rc_gt\n";
	exit 0;
}
if ($state-&gt;{'serviceoutput'}){
	my $sendmail_msg='From: '.$display_name.' &lt;'.$sender.'\nTo:'.$recpt.'\nSubject: '.$subject.'\n\n'.$message .'\n'.$state-&gt;{'serviceoutput'};
	system ("echo -e \"".$sendmail_msg."\"| /usr/sbin/sendmail -F '".$display_name."' -f '".$sender."' '".$recpt."'");
}
</pre></body></html>