101 for Email Notifications in Icinga2

(How to change the layout of the notification is explained in an another post.)

Problem

  • You want to enable Email notifications for your hosts and services in Icinga2.
  • Install mail on Ubuntu.
  • You don’t have the time to read the very good Icinga2 documentation (because it’s a classic tl;dr).

Setting

  • Installed Icinga2 (see my HowTo)
  • Configured hosts and service (see my HowTo)
  • Ubuntu Server 14.04.3

Sending mails

In the default configuration Icinga2 uses the mail command:

/usr/bin/printf "%b" "$template" | mail -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME - $SERVICEDISPLAYNAME is $SERVICESTATE" $USEREMAIL

For this tutorial i do not change it. If your setup does not allow mail, you have to change it in the /etc/icinga2/scripts/mail-host-notification.sh and /etc/icinga2/scripts/mail-service-notification.sh and stop reading this tutorial.
For a simple setup using mail in Ubuntu you need mailutils and sSMTP. sSMTP just sends mails to a SMTP server you have access to.
Install

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install mailutils ssmtp -y

Configure sSMTP
Edit the /etc/ssmtp/ssmtp.conf. Uncomment FromLineOverride=YES and add your mailhub configuration. Here is a sample sSMTP config for a GMail SMTP:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
AuthUser=@gmail.com
AuthPass=Your-Gmail-Password
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

# Where will the mail seem to come from?
rewriteDomain=Icinga2.Host

# The full hostname
hostname=Icinga2

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

Test your setup

echo "Hello receiver" | mail -s "Test" someone@2342424422.com

If you don’t immediately receive an Email you either have greylisting enabled on your mail server or just have a wrong configuration.

icinga_custom_notification

Configure user (and usergroups)

In the default user configuration of Icinga2 you will find a UserGroup icingaadmins. If you add a user to this group he will get Emails, if the host of Icinga has some problems. (Please note that the user TestUser is not a IcingaWeb2 login user (which can be created in the configuration section)). A simple user looks like this:

object UserGroup "icingaadmins" {
display_name = "Icinga 2 Admin Group"
}

object User "TestUser" {
display_name = "A Name"
groups = [ "icingaadmins" ]
email = "Mail@gmail.com"
period = "24x7"
states = [ OK, Warning, Critical, Unknown ]
types = [ Problem, Recovery ]
vars.additional_notes = "Me"
}

If you need more information on the attributes just have a look at the documentation.

Assign Notification to Service and User

But if you want mails for hosts or services you have to use assign (see documentation). In the following example i assign the command mail-service-notification (defined in “/etc/icinga2/scripts/”) to every service, which runs on the host named “MySQL”. I only inform the newly created TestUser and he should only be informed once a state changes (“interval = 0”)

apply Notification "Alarm-Test" to Service {
command = "mail-service-notification"
users = ["TestUser"]
interval = 0
assign where service.host_name == "MySQL"
}

The assignment for hosts is very similar.

Fin.

Advertisements

One thought on “101 for Email Notifications in Icinga2

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