====== HTML Mail Alert ====== ^ Author | [[ david.baldwin@ausport.gov.au | David Baldwin ]] | ^ Compatibility | Xymon 4.2 | ^ Requirements | Perl, MIME::Lite | ^ Download | None | ^ Last Update | 2010-04-07 | ===== Description ===== 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. Tested with Thunderbird 2.0 and Outlook 2007. ===== Installation ===== - Place html_mail.pl in the ext/ directory in your xymon sever installation. -Make the script executable: chmod 755 html_mail.pl - Add the following line in whichever alert for which you want messages SCRIPT [XYMON_SERVER_ROOT]/ext/html_mail.pl [RECIPIENT] FORMAT=PLAIN - And of course you replace [XYMON_SERVER_ROOT] with the full path to your xymon server directory (e.g. /usr/lib/hobbit/server ). ===== Source ===== ==== html_mail.pl ==== #!/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
 tags
# 1.4           David Baldwin   Add CSS handling for background image
 
 
###############################################################################
# Uses and requires
 
use strict;
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.
 
 
###############################################################################
# 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 CRITICAL (".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=green
        #
        if ($RECOVERED eq "1") {
                $BBCOLORLEVEL = "green";
        }
	#
	# 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="";
		$bkgdone=1;
	    }
	    #
	    # Convert Xymon colour tags to embedded image tags
	    #
	    $BBHTMLMSG =~ s/&$col//g;
        }
 
        #
        # Fix link at base of message as they are removed in PLAIN format
        #
        $BBHTMLMSG =~ s/(http:\S*)/$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
        my $link .= "http://$BBSERVERWWWNAME$BBSERVERCGIURL/bb-hostsvc.sh?HOST=$BBHOSTNAME&SERVICE=$BBSVCNAME";
        $htmldata .= "$HTML_BODY\n
See $link
\n$BBHTMLMSG\n
\n
\n"; 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'} || ""; $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;
===== Known Bugs and Issues ===== ===== To Do ===== Option to link to icons from hobbit server rather than embed in message. ===== Credits ===== Martin Ward original version ===== Changelog ===== * **2010-04-07** * Initial release * **2011-06-07** * updated with v1.3 (old version still accessible under Revisions) * **2011-07-18** * updated with v1.4 - fixed background image * **2011-07-19** * updated v1.4 - cleanup of message composition and sending