Nagios : Améliorer le look des emails de notification

From Deimos.fr / Bloc Notes Informatique
Jump to: navigation, search

1 Introduction

If you already used standart email notification, you'll find it cool but not really sexy. That's why a project has been initiated and called "Flexible Notifications for Nagios".

I will explain here how to use it easily. The goal is to have this kind of HTML email instead of plain text :

Nagios html alert.png

You also can do others stuff like adding image, carbon copy, different languages... but I'll explain here only what I need.

2 Installation

Very simple, create a scripts folder and put the 2 required scripts in it :

Command
mkdir -p /etc/nagios3/scripts
cd /etc/nagios3/scripts
wget "http://nagios.frank4dd.com/howto/source/nagios_send_host_mail.pl"
wget "http://nagios.frank4dd.com/howto/source/nagios_send_service_mail.pl"
chmod 755 nagios_send_host_mail.pl nagios_send_service_mail.pl

Also we'll need to install perl dependancies :

Command aptitude
aptitude install libmail-sendmail-perl librrds-perl

3 Configuration

3.1 commands.cfg

Here is the file containing default email commands. You just have to comment the one commented here and add the new lines :

Configuration File /etc/nagios3/commands.cfg
###############################################################################
# COMMANDS.CFG - SAMPLE COMMAND DEFINITIONS FOR NAGIOS 
################################################################################
 
 
################################################################################
# NOTIFICATION COMMANDS
################################################################################
 
# 'notify-service-by-email' command definition
define command{
  command_name  notify-host-by-email
  command_line  /bin/sleep 1
}
 
# 'notify-host-by-email' command definition
#define command{
#   command_name    notify-host-by-email
#   command_line    /usr/bin/printf "%b" "***** INTERNAL Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s "** Internal $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
#   }
 
# sends HTML e-mails for hosts
define command{
        command_name    host-notify-by-email
        command_line    /etc/nagios3/scripts/nagios_send_service_mail.pl -c "$CONTACTADDRESS1$" -f html -u -p "Deimos.fr Monitoring Tool"
}
 
# 'notify-host-by-email' command definition
#define command{
# command_name  host-notify-by-email
# command_line  /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n$HOSTACKAUTHOR$: $NOTIFICATIONCOMMENT$\n\n$HOSTNOTES$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
# }
 
# sends HTML e-mails for services
define command{
        command_name    notify-service-by-email
        command_line    /etc/nagios3/scripts/nagios_send_service_mail.pl -c "$CONTACTADDRESS1$" -f html -u -p "Deimos.fr Monitoring Tool"
}
 
# 'notify-service-by-email' command definition
#define command{
#   command_name    notify-service-by-email
#   command_line    /usr/bin/printf "%b" "***** INTERNAL Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/bin/mail -s "** $Internal NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
#   }

3.2 nagios_send_host_mail.pl and nagios_send_service_mail.pl

We now need to modify little things like URL, names etc... so let's open files and set custom fields :

Configuration File
 ...
 my $mail_sender        = "Nagios Monitoring <deimos\@deimos.fr>";
 my $nagios_cgiurl      = "http://nagios/nagios3/cgi-bin";
 my $o_smtphost         = "127.0.0.1";
 ...

Those fiels are corresponding to :

  • $mail_sender : Email sender name
  • $nagios_cgiurl : The CGI URL to nagios server. It's needed for links in the mail
  • $o_smtphost : The SMTP server

That's all. Reload your Nagios now