Undetectable Humanizer: Lifetime Subscription
Transform AI-Generated Text into Human-Like, High-Ranking Content & Bypass Even the Most Sophisticated AI Detectors
Get 95% Deal

Configure alerts for email and contact groups in Nagios

Jack Wallen
Apr 4, 2010
Updated • Nov 30, 2012
Network
|
4

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@localhost
}

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!

Advertisement

Previous Post: «
Next Post: «

Comments

  1. Richard said on October 17, 2019 at 9:44 am
    Reply

    Is the contact_groups directive part of the hostgroup still?

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

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.