Nagios email notifications & Puppet

I’ve been writing a bit about my Nagios configuration lately, but there was still at least one important part missing from my Nagios configuration: Email notifications. With email notifications configured there will be an email alert every time one of my nagios hosts or it’s services reaches warning or critical state.

Postfix will be used to send emails and I will include a Puppet module for the configuration as well.

Tested on:
Ubuntu 12.10
Nagios Core 3.4.1
Postfix  2.9.3

Postfix

sudo apt-get install postfix

Options to choose during the installation:

General type of mail configuration:
Internet site

System mail name:
mydomain.com

Replace mydomain.com with your domain.

/etc/postfix/main.cf (pay special attention to the coloured parts)

#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = YOUR HOSTNAME
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, localhost.localdomain, localhost
relayhost = SEE DESCRIPTION BELOW*
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

*You might have to add your ISP’s email server as relayhost to get your mails sent.

You can put your domain to the myorigin part. If you use the default /etc/mailname, make sure you have something in /etc/mailname.

Restart postfix service if you made any changes to the main.cf
sudo service postfix restart

Nagios

You need to add your email address to the contacts config (this is the email address that will receive the notifications):

/etc/nagios3/conf.d/nagios2_contacts

define contact{
  contact_name root
  alias Root
  service_notification_period 24x7
  host_notification_period 24x7
  service_notification_options w,c
  host_notification_options
  service_notification_commands notify-service-by-email
  host_notification_commands notify-host-by-email
  email SOMETHING@SOME_DOMAIN.COM
}

define contactgroup{
  contactgroup_name admins
  alias Nagios Administrators
  members root
}

/etc/nagios3/objects/templates

# Windows host definition template
define host{
  name windows-server
  use generic-host
  check_period 24x7
  check_interval 5
  retry_interval 1
  max_check_attempts 10
  check_command check-host-alive
  notification_period 24x7
  notification_interval 30
  notification_options d,r
  contact_groups admins
  hostgroups winhosts
  register 0
}

# Linux host definition template
define host{
  name linux-box
  use generic-host
  check_period 24x7
  check_interval 5
  retry_interval 1
  max_check_attempts 10
  check_command check-host-alive
  notification_period 24x7
  notification_interval 30
  notification_options d,r
  contact_groups admins
  hostgroups linuxhosts
  register 0
}

Instead of templates, you can use the contact_groups directly in your host definitions.

Restart nagios3 service after you’ve done your changes.
sudo service nagios3 restart

Puppet

My Puppet module for Postfix:
Postfix module

My full Nagios3 module with notification configs added:
Nagios3 module

Sources:

Nagios email alert tutorial by Jack Wallen:
http://www.linux.com/learn/tutorials/316105:setting-up-email-alerts-for-network-monitoring-with-nagios

Postfix tutorial by Jack Wallen:
http://www.linux.com/component/content/article/174-tutorials/308917-install-and-configure-a-postfix-mail-server

Official Postfix guide:
http://www.postfix.org/STANDARD_CONFIGURATION_README.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: