Simple HTML email alert template for Icinga 2

Published by shyam jos on

I have created a very simple HTML email alert Template for Icinga 2 to replace the default boring text based email alert.This template can be easily installed by replacing existing alert scripts inside /etc/icinga2/scripts.

Update: I have replaced mail command with mutt due to incompatibility of options among different linux distributions. Also I have added new color highlight feature for different states (ok=green,warning=orange and critical=red).

Screenshots 

Prerequisite 
Make Sure mutt package is installed on your server

How to install

Backup existing  script files (mail-service-notification.sh and mail-host-notification.sh) inside /etc/icinga2/scripts.

Replace mail-service-notification.sh with below code.

#!/bin/sh
# shyamjos.com (2017)


if [ "$SERVICESTATE" = "CRITICAL" ]
then
    color=#FF5566

elif [ "$SERVICESTATE" = "WARNING" ]
then
    color=#FFAA44

elif [ "$SERVICESTATE" = "UNKNOWN" ]
then
    color=#90A4AE

elif [ "$SERVICESTATE" = "DOWN" ]
then
    color=#FF5566

#else [ "$SERVICESTATE" = "OK" ]
#then
else
    color=#44BB77

fi


template=`cat <<TEMPLATE
<!DOCTYPE html>
<html>
<head>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #1bd0b2;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #ffffff;
}
</style>
</head>
<body>

<table>
<th colspan=2 bgcolor=#17B294><center>Icinga Server Monitoring</center></th>
<tr>
<td>Notification Type:</td>
<td>$NOTIFICATIONTYPE</td>
</tr>
<tr>
<td>Service</td>
<td>$SERVICEDESC</td>
</tr>
<tr>
<td>Host</td>
<td>$HOSTALIAS</td>
</tr>
<tr>
<td>IP Address</td>
<td>$HOSTADDRESS</td>
</tr>
<tr>
<td>State</td>
<td><b>$SERVICESTATE</b></td>
</tr>
<tr>
<td>Date/Time</td>
<td>$LONGDATETIME</td>
</tr>
<tr>
<td>Additional Info</td>
<td bgcolor=$color><b>$SERVICEOUTPUT</b></td>
</tr>
<tr>
<td>Comment</td>
<td>[$NOTIFICATIONAUTHORNAME] : $NOTIFICATIONCOMMENT</td>
</tr>
<tr>
<td>Alert History</td>
<td><a  target="_blank"  href="http://192.168.60.1/icingaweb2/dashboard#!/icingaweb2/monitoring/host/history?host=$HOSTALIAS"> Open Dashboard </a></td>
</tr>
</table>

</body>
</html>
TEMPLATE
`
#Do not remove -e 'my_hdr From:', This is used for setting 'from' address in mutt

/usr/bin/printf "%b" "$template" | mutt -e "set content_type=text/html" -e 'my_hdr From:Icinga Alert <icinga-alerts@example.com>' -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME - $SERVICEDISPLAYNAME is $SERVICESTATE" $USEREMAIL

 

Replace mail-host-notification.sh script with below code.

#!/bin/sh
# shyamjos.com (2017)

if [ "$HOSTSTATE" = "DOWN" ]
then
        color=#FF5566

#elif [ "$HOSTSTATE" = "UP" ]
#then
else
        color=#44BB77

fi



template=`cat <<TEMPLATE
<!DOCTYPE html>
<html>
<head>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #1bd0b2;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #fffff;
}
</style>
</head>
<body>

<table>
<th colspan=2 bgcolor=#17B294><center>Icinga Server Monitoring</center></th>

<tr>
<td>Notification Type</td>
<td>$NOTIFICATIONTYPE</td>
</tr>
<tr>
<td>Host</td>
<td>$HOSTALIAS</td>
</tr>



<tr>
<td>IP Address</td>
<td>$HOSTADDRESS</td>
</tr>

<tr>
<td>State </td>
<td bgcolor=$color><b>$HOSTSTATE</b></td>
</tr>

<tr>
<td>Date/Time</td>
<td>$LONGDATETIME</td>
</tr>


<tr>
<td>Additional Info</td>
<td>$HOSTOUTPUT</td>
</tr>

<tr>
<td>Comment</td>
<td>[$NOTIFICATIONAUTHORNAME] : $NOTIFICATIONCOMMENT</td>
</tr>

<tr>
<td>Alert History</td>
<td><a  target="_blank"  href="http://192.168.60.1/icingaweb2/dashboard#!/icingaweb2/monitoring/host/history?host=$HOSTALIAS"> Open Dashboard </a></td>
</tr>
</table>

</body>
</html>
TEMPLATE
`
#Do not remove -e 'my_hdr From:', This is used for setting 'from' address in mutt

/usr/bin/printf "%b" "$template" | mutt -e "set content_type=text/html" -e 'my_hdr From:Icinga Alert <icinga-alerts@example.com>' -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME is $HOSTSTATE" $USEREMAIL

 

Note: you can download the above scripts from this github repo
Now Send a custom notification to test new HTML email template.

Categories: DevOpsTutorials

shyam jos

My name is Shyam Jos, I am a passionate Linux system administrator from Kochi, India. I am a strong advocate of using Linux and Open-Source software.

23 Comments

Michel · June 13, 2017 at 12:59 PM

Hello. Great idea, but both scripts does not work here. I always get this error (for host and service):

[2017-06-13 09:26:46 +0200] warning/PluginNotificationTask: Notification command for object ‘xyz’ (PID: 12977, arguments: ‘/etc/icinga2/scripts/mail-host-notification.sh’) terminated with exit code 1, output: MIME-Version: 1.0: No such file or directory

[2017-06-13 09:26:46 +0200] warning/PluginNotificationTask: Notification command for object ‘xyz’ (PID: 12976, arguments: ‘/etc/icinga2/scripts/mail-host-notification.sh’) terminated with exit code 1, output: MIME-Version: 1.0: No such file or directory

Any idea?

Best regards
Michel

    shyam jos · June 13, 2017 at 9:08 PM

    Hi Michel,

    This is because mail command in your system does not support -a option (mail header) , BTW which OS you are using ? , I will update you in this comment thread once the issue is fixed. 🙂

      Michel · June 13, 2017 at 11:29 PM

      Hi, it’s an SUSE Linux Enterprise 12 Server with Service Pack 2. Thank you for your quick reply 🙂

        shyam jos · June 14, 2017 at 11:41 PM

        Oh okay , Will fix this issue this weekend 🙂

          Michel · June 30, 2017 at 5:29 PM

          Thank you! 🙂

          shyam jos · June 30, 2017 at 8:22 PM

          Glad you liked it 🙂

          Michel · July 7, 2017 at 1:54 AM

          Hey 🙂 Is your template already fixed for use with SuSE? Thank you! Best regards, Michel

          shyam jos · July 9, 2017 at 11:34 PM

          Hey Michel, Sorry for the delayed reply , Yes this is fixed in the new version by replacing mail command with mutt. Let me know if this worked for you 🙂

Artur · September 22, 2017 at 2:19 AM

Hi,

How can I include the service description into the body email? Do I need to pass a variable to the script?
Sorry, I very newb using icinga2.
Thanks in advance!

Artur.

    shyam jos · September 22, 2017 at 8:32 AM

    Yes you need to include that variable in commands.conf and also in the template part of this script.

Paul · October 20, 2017 at 12:42 AM

Great scripts!
A couple of changes, since Icinga 2.7 $HOSTDISPLAYNAME replaces $HOSTALIAS and $SERVICENAME replaces $SERVICEDESC.

This may help others using mutt with a remote SMTP server.

/usr/bin/printf “%b” “$template” | mutt -e “set smtp_url=smtp://smtp.example.com:25” -e “set content_type=text/html” -e ‘my_hdr From:Icinga Alert ‘ -s “$NOTIFICATIONTYPE – $HOSTDISPLAYNAME is $HOSTSTATE” $USEREMAIL

    shyam jos · November 4, 2017 at 6:16 PM

    Hi Paul,

    Thanks for the Tip!

John H. · January 30, 2018 at 10:31 PM

Tried your script and it seems to work if using the “mail -a” option for me but no matter the method of sending the mail the script doesn’t seem to pull variables correctly. If I hard code an email address into the send statement it sends the table just fine but it is not populated with any of the variable info nor is the subject line, only “-” and “is” appear in the subject. Any ideas?

    shyam jos · January 31, 2018 at 8:47 AM

    Try mutt command instead of mail

      John H. · January 31, 2018 at 9:47 PM

      It didn’t matter between mutt or mail, the variables would not populate properly for a fresh Icinga2 install. I did get them to work however by combining the old notification script with this one. Thanks!

        shyam jos · January 31, 2018 at 10:05 PM

        Thanks for the info

        Normally I always skip the upgrade of notification script when updating icinga , maybe that’s why I haven’t faced any issues so far.

        Franck-Act · May 9, 2018 at 9:01 PM

        Hi,

        I face the same problem you mentionned, I think I will have to mix the 2 script too but I don’t know witch part I have to take from the “basic” icinga2 scrit.

        Could you share your modification ?

        Thank you !

John H. · May 10, 2018 at 1:32 AM

I have changed these up so the whole table changes color based on status but other than that they are pretty stock looking. It appears some of the variables have changed over time also and have been corrected. These are tested working with Icinga 2.5.1. I will try to post the code, I don’t post much here so hopefully standard code tags work. If not sorry!

Here is mail-host-notification.sh

#!/usr/bin/env bash
#
# Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)

PROG="`basename $0`"
ICINGA2HOST="`hostname`"
MAILBIN="mail"

if [ -z "`which $MAILBIN`" ] ; then
echo "$MAILBIN not found in \$PATH. Consider installing it."
exit 1
fi

## Function helpers
Usage() {
cat <&2
Error ;;
:) echo "Missing option argument for -$OPTARG" >&2
Error ;;
*) echo "Unimplemented option: -$OPTARG" >&2
Error ;;
esac
done

shift $((OPTIND - 1))

## Check whether author and comment was specified.
if [ -n "$NOTIFICATIONCOMMENT" ] ; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE

Comment by $NOTIFICATIONAUTHORNAME:
$NOTIFICATIONCOMMENT"
fi

## Modify this for your own needs!

if [ "$HOSTSTATE" = "DOWN" ]
then
color=#FF5566

#elif [ "$HOSTSTATE" = "UP" ]
#then
else
color=#44BB77

fi

template=`cat <<TEMPLATE

table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid $color;
text-align: left;
padding: 8px;

}

tr:nth-child(even) {
background-color: #ffffff;
}

Company HOST Notification

Notification Type
$NOTIFICATIONTYPE

Host
$HOSTDISPLAYNAME

IP Address
$HOSTADDRESS

State
$HOSTSTATE

Date/Time
$LONGDATETIME

Additional Info
$HOSTOUTPUT

Comment
[$NOTIFICATIONAUTHORNAME] : $NOTIFICATIONCOMMENT

Alert History
Open Dashboard

TEMPLATE
`

/usr/bin/printf "%b" "$template" | mail -a "FROM:" -a "Content-Type:text/html;" -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME is $HOSTSTATE" $USEREMAIL

Here is mail-service-notification.sh

#!/usr/bin/env bash
#
# Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)

PROG="`basename $0`"
ICINGA2HOST="`hostname`"
MAILBIN="mail"

if [ -z "`which $MAILBIN`" ] ; then
echo "$MAILBIN not found in \$PATH. Consider installing it."
exit 1
fi

## Function helpers
Usage() {
cat <&2
Usage ;;
:) echo "Missing option argument for -$OPTARG" >&2
Usage ;;
*) echo "Unimplemented option: -$OPTARG" >&2
Usage ;;
esac
done

if [ "$SERVICESTATE" = "CRITICAL" ]
then
color=#FF5566

elif [ "$SERVICESTATE" = "WARNING" ]
then
color=#FFAA44

elif [ "$SERVICESTATE" = "UNKNOWN" ]
then
color=#90A4AE

elif [ "$SERVICESTATE" = "DOWN" ]
then
color=#FF5566

#else [ "$SERVICESTATE" = "OK" ]
#then
else
color=#44BB77

fi

template=`cat <<TEMPLATE

table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid $color;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #ffffff;
}

Company Service Notification

Notification Type:
$NOTIFICATIONTYPE

Service
$SERVICEDISPLAYNAME

Host
$HOSTDISPLAYNAME

IP Address
$HOSTADDRESS

State
$SERVICESTATE

Date/Time
$LONGDATETIME

Additional Info
$SERVICEOUTPUT

Comment
[$NOTIFICATIONAUTHORNAME] : $NOTIFICATIONCOMMENT

Alert History
Open Dashboard

TEMPLATE
`

/usr/bin/printf "%b" "$template" | mail -a "FROM:" -a "Content-Type:text/html;" -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME - $SERVICEDISPLAYNAME is $SERVICESTATE" $USEREMAIL

Elson P Ealias · June 27, 2018 at 8:13 AM

Hello,

I’m a newbie to both Linux and Icinga2, how can i configure mutt for sending email ?

    Elson P Ealias · June 27, 2018 at 8:13 AM

    I’m only getting mails into Spam folder.

      shyam jos · June 29, 2018 at 12:24 AM

      You may need to whitelist your Icinga mail id in your email client

juan · November 15, 2018 at 2:30 AM

Hello
thanks for your template. It looks very good and easy to understand
we are trying to use your template but we only see the html code as a plain text.

We have this in our mail-service-notification.sh after the notification message:

## Check whether IPv4 was specified.
if [ -n “$HOSTADDRESS” ] ; then
NOTIFICATION_MESSAGE=”$NOTIFICATION_MESSAGE
IPv4: $HOSTADDRESS”
fi

## Check whether IPv6 was specified.
if [ -n “$HOSTADDRESS6″ ] ; then
NOTIFICATION_MESSAGE=”$NOTIFICATION_MESSAGE
IPv6: $HOSTADDRESS6”
fi

## Check whether author and comment was specified.
if [ -n “$NOTIFICATIONCOMMENT” ] ; then
NOTIFICATION_MESSAGE=”$NOTIFICATION_MESSAGE

Comment by $NOTIFICATIONAUTHORNAME:
$NOTIFICATIONCOMMENT”
fi

## Check whether Icinga Web 2 URL was specified.
if [ -n “$ICINGAWEB2URL” ] ; then
NOTIFICATION_MESSAGE=”$NOTIFICATION_MESSAGE

$ICINGAWEB2URL/monitoring/service/show?host=$HOSTNAME&service=$SERVICENAME”
fi

## Check whether verbose mode was enabled and log to syslog.
if [ “$VERBOSE” == “true” ] ; then
logger “$PROG sends $SUBJECT => $USEREMAIL”
fi

## Send the mail using the $MAILBIN command.
## If an explicit sender was specified, try to set it.
if [ -n “$MAILFROM” ] ; then

## Modify this for your own needs!

## Debian/Ubuntu use mailutils which requires `-a` to append the header
if [ -f /etc/debian_version ]; then
/usr/bin/printf “%b” “$template” | $MAILBIN -a “Content-Type: text/html;” -a “From: $MAILFROM” -s “$SUBJECT” $USEREMAIL
## Other distributions (RHEL/SUSE/etc.) prefer mailx which sets a sender address with `-r`
else
/usr/bin/printf “%b” “$template” | $MAILBIN -a “Content-Type: text/html;” -r “$MAILFROM” -s “$SUBJECT” $USEREMAIL
fi

else
/usr/bin/printf “%b” “$template” \
| $MAILBIN -s “$SUBJECT” $USEREMAIL
fi

we put the -a “Content-Type: text/html;” but we still seeing the html code as a plain text.

i’ll appreciate your help.
Thanks

    shyam jos · November 15, 2018 at 9:37 PM

    what is your mail client? , Have you tried mutt ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.