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.

Simple HTML email alert template for Icinga 2
Tagged on:

16 thoughts on “Simple HTML email alert template for Icinga 2

  • June 13, 2017 at 12:59 PM
    Permalink

    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

    Reply
    • June 13, 2017 at 9:08 PM
      Permalink

      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. 🙂

      Reply
      • June 13, 2017 at 11:29 PM
        Permalink

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

        Reply
        • June 14, 2017 at 11:41 PM
          Permalink

          Oh okay , Will fix this issue this weekend 🙂

          Reply
          • June 30, 2017 at 5:29 PM
            Permalink

            Thank you! 🙂

          • June 30, 2017 at 8:22 PM
            Permalink

            Glad you liked it 🙂

          • July 7, 2017 at 1:54 AM
            Permalink

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

          • July 9, 2017 at 11:34 PM
            Permalink

            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 🙂

  • September 22, 2017 at 2:19 AM
    Permalink

    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.

    Reply
    • September 22, 2017 at 8:32 AM
      Permalink

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

      Reply
  • October 20, 2017 at 12:42 AM
    Permalink

    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

    Reply
    • November 4, 2017 at 6:16 PM
      Permalink

      Hi Paul,

      Thanks for the Tip!

      Reply
  • January 30, 2018 at 10:31 PM
    Permalink

    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?

    Reply
    • January 31, 2018 at 8:47 AM
      Permalink

      Try mutt command instead of mail

      Reply
      • January 31, 2018 at 9:47 PM
        Permalink

        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!

        Reply
        • January 31, 2018 at 10:05 PM
          Permalink

          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.

          Reply

Leave a Reply

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

%d bloggers like this: