alerts:html_mail

HTML Mail Alert

Author Martin Ward
Compatibility Xymon 4.2
Requirements Perl, MIME::Lite
Download None
Last Update 2008-07-08
  1. Place html_mail.pl in the ext/ directory in your xymon sever installation.
  2. Make the script executable:
    chmod 755 html_mail.pl
  3. Add the following line in whichever alert for which you want messages
      SCRIPT [XYMON_SERVER_ROOT]/ext/html_mail.pl [RECIPIENT] FORMAT=PLAIN
  4. And of course you replaced [XYMON_SERVER_ROOT] with the full path to your xymon server directory.

html_mail.pl

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.
 
 
###############################################################################
# Uses and requires
 
use strict;
use MIME::Lite;
 
 
###############################################################################
# Global variables
 
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 $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 $subject = $BBHOSTSVC . " " . $BBCOLORLEVEL;
        my $htmldata = join("",
                "$HTML_BODY\n",
                "<FONT FACE=\"Tahoma, Arial, Helvetica\" SIZE=\"3\">\n",
                "<PRE>\n",
                $BBHTMLMSG,
                "\n",
                "</PRE>\n",
                "</FONT>\n",
                "</BODY>"
        );
 
        # Create the MIME email
 
        my $msg = MIME::Lite->new
        (
                Subject => $subject,
                To      => $RCPT,
                Type    => 'multipart/mixed'
                # Type    => 'multipart/related'
        );
 
        # Attach the HTML version of the email.
 
        $msg->attach
        (
                Type    => 'text/html',
                Data    => $htmldata,
        );
 
        # attach the plain text version of the email.
 
        $msg->attach
        (
                Type    => 'TEXT',
                Data    => $BBALPHAMSG,
        );
 
        # attach all icons required by the web page version.
 
        if($htmldata =~ m/blue.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'blue.gif', Path => '/opt/hobbit/server/www/gifs/blue.gif')}
        if($htmldata =~ m/clear.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'clear.gif', Path => '/opt/hobbit/server/www/gifs/clear.gif')}
        if($htmldata =~ m/green.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'green.gif', Path => '/opt/hobbit/server/www/gifs/green.gif')}
        if($htmldata =~ m/purple.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'purple.gif', Path => '/opt/hobbit/server/www/gifs/purple.gif')}
        if($htmldata =~ m/red.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'red.gif', Path => '/opt/hobbit/server/www/gifs/red.gif')}
        if($htmldata =~ m/yellow.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'yellow.gif', Path => '/opt/hobbit/server/www/gifs/yellow.gif')}
        if($htmldata =~ m/bkg-red.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'bkg-red.gif', Path => '/opt/hobbit/server/www/gifs/bkg-red.gif')}
        if($htmldata =~ m/bkg-yellow.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'bkg-yellow.gif', Path => '/opt/hobbit/server/www/gifs/bkg-yellow.gif')}
        if($htmldata =~ m/bkg-purple.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'bkg-purple.gif', Path => '/opt/hobbit/server/www/gifs/bkg-purple.gif')}
        if($htmldata =~ m/bkg-blue.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'bkg-blue.gif', Path => '/opt/hobbit/server/www/gifs/bkg-blue.gif')}
        if($htmldata =~ m/bkg-green.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'bkg-green.gif', Path => '/opt/hobbit/server/www/gifs/bkg-green.gif')}
        if($htmldata =~ m/bkg-clear.gif/)
                {$msg->attach(Type => 'image/gif', Id => 'bkg-clear.gif', Path => '/opt/hobbit/server/www/gifs/bkg-clear.gif')}
 
        # Send the email.
 
        $msg->send();
}
 
 
###############################################################################
# parse_info - This code will reformat $BBHTMLMSG to make it HTML-like.
 
sub parse_info {
        my $BBCOLOR;
        #
        # If RECOVERED == 1 then color=green
        #
        if ($RECOVERED eq "1") {
                $BBCOLORLEVEL = "green";
                $HTML_BODY='<BODY BGCOLOR="green" BACKGROUND="cid:bkg-green.gif" TEXT="#D8D8BF" LINK="#00FFAA" VLINK="#FFFF44">';
 
        } else {
                #
                # Try and figure out what colour we should set the background
                # to.
                #
                if ($BBHTMLMSG =~ m/&red/) {
                        $BBCOLORLEVEL = "red";
                        $HTML_BODY='<BODY BGCOLOR="red" BACKGROUND="cid:bkg-red.gif" TEXT="#D8D8BF" LINK="#00FFAA" VLINK="#FFFF44">';
                } elsif ($BBHTMLMSG =~ m/&yellow/) {
                        $BBCOLORLEVEL = "yellow";
                        $HTML_BODY='<BODY BGCOLOR="yellow" BACKGROUND="cid:bkg-yellow.gif" TEXT="#D8D8BF" LINK="#00FFAA" VLINK="#FFFF44">';
                } elsif ($BBHTMLMSG =~ m/&purple/) {
                        $BBCOLORLEVEL = "purple";
                        $HTML_BODY='<BODY BGCOLOR="purple" BACKGROUND="cid:bkg-purple.gif" TEXT="#D8D8BF" LINK="#00FFAA" VLINK="#FFFF44">';
                } elsif ($BBHTMLMSG =~ m/&blue/) {
                        $BBCOLORLEVEL = "blue";
                        $HTML_BODY='<BODY BGCOLOR="blue" BACKGROUND="cid:bkg-blue.gif" TEXT="#D8D8BF" LINK="#00FFAA" VLINK="#FFFF44">';
                } elsif ($BBHTMLMSG =~ m/&green/) {
                        $BBCOLORLEVEL = "green";
                        $HTML_BODY='<BODY BGCOLOR="green" BACKGROUND="cid:bkg-green.gif" TEXT="#D8D8BF" LINK="#00FFAA" VLINK="#FFFF44">';
                } else {
                        $BBCOLORLEVEL = "clear";
                        $HTML_BODY='<BODY BGCOLOR="clear" BACKGROUND="cid:bkg-clear.gif" TEXT="#D8D8BF" LINK="#00FFAA" VLINK="#FFFF44">';
                }
        }
 
        #
        # Convert Xymon colour tags to embedded image tags
        #
        $BBHTMLMSG =~ s/&red/<img src="cid:red.gif">/g;
        $BBHTMLMSG =~ s/&yellow/<img src="cid:yellow.gif">/g;
        $BBHTMLMSG =~ s/&purple/<img src="cid:purple.gif">/g;
        $BBHTMLMSG =~ s/&blue/<img src="cid:blue.gif">/g;
        $BBHTMLMSG =~ s/&clear/<img src="cid:clear.gif">/g;
        $BBHTMLMSG =~ s/&green/<img src="cid:green.gif">/g;
        $BBHTMLMSG =~ s/&unknown/<img src="cid:unknown.gif">/g;
 
        #
        # Fix link at base of message as they are removed in PLAIN format
        #
        $BBHTMLMSG =~ s/(http:.*)/<a href="$1">$1<\/a>/g;
}
 
 
 
###############################################################################
# Start of main code
 
#
# Retrieve the data from the environment variables
#
$ACKCODE = $ENV{'ACKCODE'};       $ACKCODE    = "" if (! defined($ACKCODE));
$BBALPHAMSG = $ENV{'BBALPHAMSG'}; $BBALPHAMSG = "" if (! defined($BBALPHAMSG));
$BBHTMLMSG = $BBALPHAMSG;
$BBCOLORLEVEL = $ENV{'BBCOLORLEVEL'}; $BBCOLORLEVEL = "" if (! defined($BBCOLORLEVEL));
$BBHOSTNAME = $ENV{'BBHOSTNAME'}; $BBHOSTNAME = "" if (! defined($BBHOSTNAME));
$BBHOSTSVC = $ENV{'BBHOSTSVC'};   $BBHOSTSVC  = "" if (! defined($BBHOSTSVC));
$BBHOSTSVCCOMMAS = $ENV{'BBHOSTSVCCOMMAS'}; $BBHOSTSVCCOMMAS = "" if (! defined($BBHOSTSVCCOMMAS));
$BBNUMERIC = $ENV{'BBNUMERIC'};   $BBNUMERIC  = "" if (! defined($BBNUMERIC));
$BBSVCNAME = $ENV{'BBSVCNAME'};   $BBSVCNAME  = "" if (! defined($BBSVCNAME));
$BBSVCNUM = $ENV{'BBSVCNUM'};     $BBSVCNUM   = "" if (! defined($BBSVCNUM));
$CFID = $ENV{'CFID'};             $CFID       = "" if (! defined($CFID));
$DOWNSECS = $ENV{'DOWNSECS'};     $DOWNSECS   = "" if (! defined($DOWNSECS));
$DOWNSECSMSG = $ENV{'DOWNSECSMSG'}; $DOWNSECSMSG = "" if (! defined($DOWNSECSMSG));
$EVENTSTART = $ENV{'EVENTSTART'}; $EVENTSTART = "" if (! defined($EVENTSTART));
$MACHIP = $ENV{'MACHIP'};         $MACHIP     = "" if (! defined($MACHIP));
$RCPT = $ENV{'RCPT'};             $RCPT       = "" if (! defined($RCPT));
$RECOVERED = $ENV{'RECOVERED'};   $RECOVERED  = "0" if (! defined($RECOVERED));
 
#
# Rewrite the data
#
parse_info;
 
#
# Send the email
#
send_email;
 
0;
  • 2008-07-08
    • Initial release
  • alerts/html_mail.txt
  • Last modified: 2009/11/23 06:53
  • by 127.0.0.1