Configure alerts for email and contact groups in Nagios - gHacks Tech News

Configure alerts for email and contact groups in Nagios

If you have been following my latest entries, you know I have been covering the Nagios monitoring system (see my articles "Quickly install Nagios on Ubuntu", "Easily extend Nagios' functionality", and "Configure hostgroups for Nagios".) This time around we are going to look at the notification function of Nagios. Specifically, I am going to show you how to set up Nagios for email alerts to a single email address and multiple addresses with the help of contact groups.

This article will assume the box hosting your Nagios installation has a working email server. If not, take a look at my Postfix articles here on Ghacks. Once that is up and running, email alerts are just a few steps away.

Assumptions

Outside of having a working email system, this article will assume you already have a functioning Nagios installation and that you are familiar with its configurations. I will also assume you have gathered all of the email addresses you need to use with Nagios. That being the case, let's get with the configurations.

Defining your contact

The first item to tackle is to define your contact. This is done with the file /etc/nagios3/conf.d/contacts_nagios2.cfg. In this file you will see a section that looks like:

define contact{
contact_name USERNAME
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email [email protected]
}

Where all text above in bold is user-specific. NOTE: You will want to add a new define for each administrator that needs to be contacted via email. And make sure you define each user with his or her email address in the define.

Before you move on, it would be good to restart Nagios to make sure your definitions are correct. If there is an error you will get a warning in the output of the start command.

Define your contact group

In that same file you will also find a spot to group users together in contact groups. This comes in very handy because you can then define a set of users (administrators) who will only get warnings for certain services or certain hosts.

To define a group add an entry like so:
define contactgroup {
contactgroup_name       web-admins
alias                   Web Server Adminstrators
members                USERNAME1, USERNAME2
}

Where USERNAME1 and USERNAME2 are members for this group. NOTE: the usernames in this group MUST be defined in the define contact section above. If they are not, Nagios will throw errors when it attempts to start.

Attach groups to services

Finally you need to attach your groups to services. As you can see, above, I have created a web_admins group. It should be above that this group should be added to a corresponding service in the file /etc/nagios3/conf.d/services_nagios2.cfg. In this file you will have created an entry like:

define hostgroup {
hostgroup_name http-servers
service_description  HTTP
check_command  check_http
use generic-service
notification_interval 0
}

To this entry you will want to add the line:

contact_groups web-admins

Now when you restart Nagios anytime there is an issue with one of the HTTP servers, only the contact group you have assigned will be notified. Pretty slick.

Final thoughts

I hope you are starting to see just how flexible and powerful Nagios is. If not, don't worry - there's plenty more to come!

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:

Comments

  1. Chris said on November 15, 2012 at 9:57 pm
    Reply

    Under the, ‘Attach groups to services’ Heading, I’m pretty sure, ‘define hostgroup {‘ should be ‘define service {‘

    1. Zabeus said on August 9, 2016 at 4:15 pm
      Reply

      Yes, that entire section is wrong. Trying to modify your host groups results in:
      Invalid hostgroup object directive ‘contact_groups’

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.