alerts:html_mail2.0

Advanced HTML Mail Alarm

Author Helge Jacobsen
Compatibility Xymon 4.3.26
Requirements Perl, MIME::Lite
Download None
Last Update 2016-03-21

Sends e-mail notifications with better handling of HTML and embedded icons. Preserves colour of status. Message sent as multipart/alternative to allow plaintext version to be preserved. New Features:

  • Different Mail Subject depending on Errorlevel
  • Duration Time for any Message
  • Flapping Status included in Subject and Mailtext
  • Colorstatus depends on BBALPHAMSG for Recover Status. Nesessary if you using yellow for OKCOLOR
  • Add Enable/Disable Mail Subject / Removing Backgroundcolor: not accessible in Outlook and makes mails in Thunderbird unreadable
  • Fix for disable “until okay”: Convert XYmon Recover Bodytext “for -1 minutes” to “until okay”

Tested with Outlook 2010 and Thunderbird

- Place html_mail.pl in the ext/ directory in your xymon sever installation.

  1. Make the script executable:
    chmod 755 html_mail.pl
  2. Be sure that you have MIME::Lite.pm
  1. Add the following line in whichever alert for which you want messages
      SCRIPT [XYMON_SERVER_ROOT]/ext/html_mail.pl [RECIPIENT] FORMAT=PLAIN RECOVERED

If you want enable/disable notification :

  SCRIPT [XYMON_SERVER_ROOT]/ext/html_mail.pl [RECIPIENT] FORMAT=PLAIN RECOVERED NOTICE 
 
- And of course you replace [XYMON_SERVER_ROOT] with the full path to your xymon server directory 

(e.g. /home/xymon/server ).

Show Code ⇲

Hide Code ⇱

#!/usr/bin/perl -w
 
# This perl script was taken from a shell/perl script combo that was originally
# authored by Andy France.
# I have modified it so that the workings of the shell script are done inside
# the perl script, thus removing one file and speeding up to processing.
#
# This script require the MIME::Lite Perl module, availablde from CPAN here:
# http://search.cpan.org/~rjbs/MIME-Lite-3.021/lib/MIME/Lite.pm
#
# Don't forget to update the first line in this script to point to the
# correct location for your Perl executable.
#
# This script should be installed in a known location and the following line
# added to the hobbit-alerts.cfg file:
# SCRIPT /opt/hobbit/server/ext/html_mail support@email.com FORMAT=PLAIN
#
# Version       Author          Details
# -------       ------          -------
# 1.0           Martin Ward     Initial version. See code for details.
# 1.1           David Baldwin   Clean up, remove hard-coded paths.
# 1.2           David Baldwin   format message as alternative parts for plaintext and HTML views
# 1.3           David Baldwin   try to prevent word-wrap for <PRE> tags
# 1.4           David Baldwin   Add CSS handling for background image
# 1.5		Helge Jacobsen  Add different Mail Subject depends on Errorlevel
# 1.6		Helge Jacobsen  Add Duration Time for any Message 
# 1.7		Helge Jacobsen	Add Flapping Status 
# 2.0 		Helge Jacobsen  Add Colorstatus depends on BBALPHAMSG for Recover Status. Nesessary if you using yellow for OKCOLORS 2016-03-11 running on XYmon 4.3.26
# 2.01		Helge Jacobsen	Add Enable/Disable Mail Subject / Removing Backgroundcolor: not accessible in Outlook and makes mails in Thunderbird unreadable 
# 2.02		Helge Jacobsen	Fix for disable "until okay": Convert XYmon Recover Bodytext "for -1 minutes" to "until okay"
# 
 
###############################################################################
# Uses and requires
 
use strict;
# only necessary if you have no access to the perl system libs
# use lib "/home/xymon/lib/perl";

use MIME::Lite;
 
###############################################################################
# Configuration variables
 
my $ICONPATH = defined($ENV{XYMONHOME}) ? ("$ENV{XYMONWWWDIR}/gifs" || "$ENV{XYMONHOME}/www/gifs") : ("$ENV{BBWWW}/gifs" || "$ENV{BBHOME}/www/gifs");
 
###############################################################################
# Global variables
 
my $BBVER = `$ENV{BB} --version`;
my ($MSGSUBJTAG) = ($BBVER =~ /^(\w+) /);

my $RCPT;       # The recipient's email address.
my $BBHOSTSVC;  # HOSTNAME.SERVICE that the alert is about.
my $BBCOLORLEVEL;       # The current color of the status.
my $HTML_BODY = "";     # Holds the text for the BODY tag.
my $BBALPHAMSG; # The full text of the status log triggering the alert
my $BBHTMLMSG;  # The full HTML text of the status log triggering the alert
my $ACKCODE;    # The "cookie" that can be used to acknowledge the alert
my $BBSERVERWWWNAME; # The name of the web server
my $BBSERVERCGIURL; # The path to CGI scripts
my $BBHOSTNAME; # The name of the host that the alert is about
my $MACHIP;     # The IP-address of the host that has a problem
my $BBSVCNAME;  # The name of the service that the alert is about
my $BBSVCNUM;   # The numeric code for the service. From SVCCODES definition.
my $BBHOSTSVCCOMMAS;    # As BBHOSTSVC, but dots in the hostname replaced
                        # with commas.
my $BBNUMERIC;  # A 22-digit number made by BBSVCNUM, MACHIP and ACKCODE.
my $RECOVERED;  # Is "1" if the service has recovered.
my $EVENTSTART; # Timestamp when the current status (color) began
my $DOWNSECS;   # Number of seconds the service has been down.
my $DOWNSECSMSG;        # When recovered, holds the text "Event duration : N"
                        # where N is the DOWNSECS value.
my $CFID;       # Line-number in the hobbit-alerts.cfg file that caused the
                # script to be invoked. Can be useful when troubleshooting
                # alert configuration rules.
my $SUBJECT="CRITICAL"; # Subject depends on RECOVERERY Status
my $RECOEVENT="Alarm"; #  Subject for Event Duration
my $FLAPPING;	# Flapping
my $FLAPPM=" ";	# Flapping Message
my $DISABLE;	# Disable Flag
 
###############################################################################
# send_email - Creates a MIME-compatible email using $HTML_BODY, which we
# created in another subroutine, and $BBALPHAMSG, which is the plain text
# version. It then adds in any icons required to display the MIME email
# and sends it off.
 
sub send_email {
        my $htmldata = shift;
        my $subject = "$MSGSUBJTAG [$ACKCODE] $BBHOSTSVC $SUBJECT (".uc $BBCOLORLEVEL.")";
        # Create the MIME email as alternative view to allow fo plain text and HTML versions
        my $msg = MIME::Lite->new
        (
                Subject => $subject,
                To      => $RCPT,
                Type    => 'multipart/alternative'
        );
        # attach the plain text version of the email.
        $msg->attach
        (
                Type    => 'TEXT',
                Data    => $BBALPHAMSG,
        );
 
	# new message to contain HTML and icon attachments
	my $htmlmsg = MIME::Lite->new
        (
                Type    => 'multipart/related'
        );
 
        # Attach the HTML
        $htmlmsg->attach
        (
                Type    => 'text/html',
                Data    => $htmldata,
        );
 
        # attach all icons required by the HTML version.
	foreach my $col (qw{red yellow purple blue green clear unknown}) {
		if($htmldata =~ m/$col.gif/)
			{$htmlmsg->attach(Type => 'image/gif', Id => "$col.gif", Path => "$ICONPATH/$col.gif")}
		if($htmldata =~ m/bkg-$col.gif/)
			{$htmlmsg->attach(Type => 'image/gif', Id => "bkg-$col.gif", Path => "$ICONPATH/bkg-$col.gif")}
	}
        # attach the HTML version of the message to the e-mail to be sent.
        $msg->attach($htmlmsg);
 
        # Send the email.
        $msg->send();
}
 
 
###############################################################################
# parse_info - This code will reformat $BBHTMLMSG to make it HTML-like.
 
sub parse_info {
        #
        # If RECOVERED == 1 then color depending on BBALPHAMSG
        #
        if ($RECOVERED eq "1") {
             #  $BBCOLORLEVEL = "green";
		$SUBJECT = "Recovered to ";
		$RECOEVENT ="Recover";
		$BBCOLORLEVEL = uc((split(/ /, $BBALPHAMSG))[0])

        }
	#
	# Flapping Detection
	#
	$FLAPPING=(`xymon 127.0.0.1 "xymondboard host=$BBHOSTNAME test=$BBSVCNAME fields=flapinfo" | cut -d "/" -f1`);
	if ($FLAPPING  eq  "1\n")	{
		$FLAPPM = "F L A P P I N G";
		$SUBJECT = "is Flapping";
	}
	#
	# Disable Detection
	$DISABLE=(split(/ /, $BBALPHAMSG))[1];
	if ($DISABLE =~ /INFO/)		{
		if ($BBALPHAMSG =~ /DISABLED/) {
		$SUBJECT = "is DISABLED" ;
		}
		if ($BBALPHAMSG =~ /ENABLED/) {
		$SUBJECT = "is ENABLED" ;
		}
	$BBCOLORLEVEL = "Info";
        }
 	# Convert XYmon Recover Bodytext "for -1 minutes" to until okay
         $BBHTMLMSG =~ s/for -1 minutes/until Service is okay/g;

        #
	
	#	
	# Try and figure out what colour we should set the background
	# to.
	#
	my $bkgdone = 0;
	foreach my $col (qw{red yellow purple blue green clear unknown}) {
	    if (! $bkgdone && ($BBHTMLMSG =~ m/&$col/ || $BBCOLORLEVEL eq $col || $col eq "clear" )) {
		$HTML_BODY="<BODY BACKGROUND=\"cid:bkg-$col.gif\">";
		$bkgdone=1;
	    }
	    #
	    # Convert Xymon colour tags to embedded image tags
	    #
	    $BBHTMLMSG =~ s/&$col/<img src="cid:$col.gif">/g;
        }
 
        #
        # Fix link at base of message as they are removed in PLAIN format
        #
        $BBHTMLMSG =~ s/(http:\S*)/<a href="$1">$1<\/a>/g;
	# body CSS definitions 
	#     - show background image repeating vertically only on black with off-white text
	#     - background image URL here can't be specified by cid: - use BACKGROUND on BODY tag

        my $htmldata = <<EOF;
<head>
<style type="text/css">
body { 
	color: #000000; 
	background-color: white;
	background-repeat: repeat-y;
}
a:link { color: #003300; text-decoration: underline; } 
a:visited { color: #000066; text-decoration: underline; } 
pre {
 white-space: pre-wrap;       /* css-3 */
 white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
 white-space: -pre-wrap;      /* Opera 4-6 */
 white-space: -o-pre-wrap;    /* Opera 7 */
 word-wrap: break-word;       /* Internet Explorer 5.5+ */
 max-width: 99%;  /* needed for PRE in table */
 width: 900px;  /* needed for PRE in table */
 _white-space: pre;   /* IE only hack to re-specify in addition to word-wrap  */
 overflow: auto;  /* another IE hack */
 table-layout: fixed;  /* another IE hack */
}
</style>
</head>
EOF
        my $link .= "http://$BBSERVERWWWNAME$BBSERVERCGIURL/bb-hostsvc.sh?HOST=$BBHOSTNAME&SERVICE=$BBSVCNAME";
#        $htmldata .= "$HTML_BODY\n<FONT FACE=\"Tahoma, Arial, Helvetica\" SIZE=\"3\">
        $htmldata .= "<FONT FACE=\"Tahoma, Arial, Helvetica\" SIZE=\"3\">
	$FLAPPM <p> See <a href=\"$link\">$link</a><BR>
<PRE>$BBHTMLMSG\n$RECOEVENT Event Duration Time:  $DOWNSECS Secs\n</PRE>\n</FONT>\n</BODY>";
	return $htmldata;
}
 
 
 
###############################################################################
# Start of main code
 
#
# Retrieve the data from the environment variables
#
$ACKCODE = $ENV{'ACKCODE'} || "";
$BBALPHAMSG = $ENV{'BBALPHAMSG'} || "";
$BBHTMLMSG = $BBALPHAMSG;
$BBCOLORLEVEL = $ENV{'BBCOLORLEVEL'} || "";
$BBSERVERWWWNAME = $ENV{'BBSERVERWWWNAME'} || "";
$BBSERVERCGIURL = $ENV{'BBSERVERCGIURL'} || "";
$BBHOSTNAME = $ENV{'BBHOSTNAME'} || "";
$BBHOSTSVC = $ENV{'BBHOSTSVC'} || "";
$BBSVCNAME = $ENV{'BBSVCNAME'} || "";
$BBHOSTSVCCOMMAS = $ENV{'BBHOSTSVCCOMMAS'} || "";
$BBNUMERIC = $ENV{'BBNUMERIC'} || "";
$BBSVCNAME = $ENV{'BBSVCNAME'} || "";
$BBSVCNUM = $ENV{'BBSVCNUM'} || "";
$CFID = $ENV{'CFID'} || "";
$DOWNSECS = $ENV{'DOWNSECS'} || "";
$DOWNSECSMSG = $ENV{'DOWNSECSMSG'} || "";
$EVENTSTART = $ENV{'EVENTSTART'} || "";
$MACHIP = $ENV{'MACHIP'} || "";
$RCPT = $ENV{'RCPT'} || "";
$RECOVERED = $ENV{'RECOVERED'} || "0";
 
#
# Rewrite the data
#
my $htmlmsg = parse_info;
 
#
# Send the email
#
send_email $htmlmsg;
 
0;

Martin Ward original version and David Baldwin updated version

  • 2016-03-14
    • 2.0 Initial release
  • 2016-03-21
    • 2.02 Little Fix and a smart update
  • alerts/html_mail2.0.txt
  • Last modified: 2016/03/21 11:58
  • by helge