LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

Install Nagios (CentOS) for Server Monitoring

lowendtutorial

Nagios is one of the most easy, expandable and trustworthy ways to monitor the status of your server(s). It is highly configurable and even lets you write your own plugins in whatever language you want.

This article will focus on installing nagios on a single server (and monitor just that server). In order to monitor more than just the nagios server, see my Remote server monitoring with Nagios article. It will be up for CentOS next week. In the context of that article, this will be the nagios server.

This article also assumes you use a recent version of RHEL/CentOS, but could very well work on other distributions as well. I’ve also assumed you’ve already added EPEL.

Installing Nagios up and running on CentOS

Getting Nagios up and running in itself is actually pretty easy.

First make sure the system is up-to-date:

yum update

Once that’s done, we can now start by installing Nagios and a few plugins:

yum install nagios nagios-plugins-load nagios-plugins-swap nagios-plugins-ssh nagios-plugins-http nagios-plugins-ping nagios-plugins-disk nagios-plugins-procs nagios-plugins-users

It’s a lot of packages, as you may notice. That’s because all nagios plugins are separate packages. This can actually be quite nice since you don’t have all the plugins present that you don’t need.

Finally, let’s set the password for nagiosadmin. The default is ‘nagiosadmin’ and it’s not that good ;-) To change the password, use:

htpasswd /etc/nagios/passwd nagiosadmin

It will then prompt for your password twice.

That’s it! You’ve now got Nagios up and running. If you go to http://example.net/nagios and log in with ‘nagiosadmin’ and the password you just entered, you’ll see the Nagios Web Interface. If you click ‘Services’ in the left-hand menu, you’ll see localhost listed and some checks on ‘OK’ and some checks on ‘Pending’. Eventually, they should all become green and ‘OK’. Nagios just needs some time for the initial checks.

Getting it all working

On Ubuntu, there was a lot to be done before Nagios was actually working completely. For example: scheduling downtime wasn’t possible without some tweaking.  On CentOS, however, it just works out of the box! So, let’s head on and define our first contact…

Defining a contact
Now that we’ve got nagios up and running and have our initial checks for localhost in place, let’s make sure we do get e-mail when something goes wrong.

Open up the templates configuration file (vim /etc/nagios/objects/templates.cfg) and look for a code block like this:

define contact{
name generic-contact
service_notification_period 24×7
host_notification_period 24×7
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
register 0
}

Let’s explain what you’re seeing here and how you’re going to be receiving e-mail.

The first line defines the name of the contact (which is actually a template contact).

The next two lines refer to a time period from /etc/nagios/objects/timeperiods.cfg. In that file, several time periods are configured per day. Check the file out to see which time periods are available. We’re using the timeperiod_name as listed in the timeperiods file in our contacts file:

service_notification_period 24×7
host_notification_period 24×7

Those two lines tell us during which time period to send notifications for service and host problems respectively, but only to this contact. You probably see the flexibility we’ve got here.

Next we look at the type of service/host states we will be receiving notifications for:

service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s

This is set to send notifications when a service is WARNING (w), UNKOWN (u), CRITICAL (c), when it recovers from a problem (r), when it is flapping (f) or when there’s scheduled downtime (s). For hosts, we get notified when they are DOWN (d), UNKNOWN (u), when it recovers from a problem (r), when it’s status is flapping (f) or when there’s scheduled downtime (s). You can specify to receive no notifications at all for a certain type (service or host) by putting an ‘n’ there.

The next two lines refer to commands defined in the commands file /etc/nagios/objects/commands.cfg. Open up this file and look around a bit. In our contacts configuration it looks like this:

service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email

When a service notification is going to be sent, it uses the command defined in notify-service-by-email and sends out the e-mail. Same goes for host notifications. Feel free to customize the e-mail in the commands file.

The last line of this file turns this contact into a template:

register 0

Which means this contact isn’t actually registered as one.

Now, on to the actual contact. Open up /etc/nagios/objects/contact.cfg and look for a block like this:

define contact{contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email nagios@localhost
}

The first line is an identifier for this contact. It would be best if it didn’t contain spaces.

The second line tells Nagios that this contact inherits the configuration from ‘generic-contact’, which is the one I’ve just shown you above.

The third line is an alias for this contact: it can be whatever you want.

The last line of this configuration file is what we’ve actually been looking for. The e-mail setting:

email nagios@localhost

This is where you can define your own e-mail address. When done, reload nagios:

sudo service nagios reload

And you should be receiving e-mail notifications when a problem occurs.

A closing word on Nagios for CentOS

There’s a lot more to tell about nagios, but you’ve probably already noticed that. I’ve referred to several files which we’ve not handled in detail. The most important next thing is monitoring remote servers and defining checks. For that, I recommend you read my article ‘Remote server monitoring with Nagios’ which is a follow-up to this one. In that article, we’re going to add a remote server and define a host check. The host check definition explained there can also be used on localhost.

mpkossen

20 Comments

  1. Nice guide. Should be very easy for people to follow.

    July 20, 2013 @ 10:15 am | Reply
  2. Thank you for your contributions on Centos tutorials. Keep it up.

    July 20, 2013 @ 10:21 pm | Reply
    • Maarten Kossen:

      I will try to do more of them :-) I’m an Ubuntu guy myself, so doing stuff on CentOS usually takes me longer. But I’m getting used to CentOS, so it’s getting easier.

      July 21, 2013 @ 1:01 pm | Reply
  3. Nice guide, maybe throw in one with cacti and npc, that is a nice setup!

    July 21, 2013 @ 12:10 am | Reply
  4. Thanks for making Nagios less daunting for people.

    July 21, 2013 @ 2:38 am | Reply
    • Maarten Kossen:

      Thanks! Nagios is actually quite easy on CentOS, especially if you compare it to Ubuntu.

      July 21, 2013 @ 1:00 pm | Reply
  5. Asanka G:

    Why don’t you add a category called tutorials so that we can easily find these tutes. Otherwise we’ll have to go through each and every page to find them which is what I did just a few minutes ago.

    July 21, 2013 @ 10:07 pm | Reply
    • Maarten Kossen:

      Liam is going to do this soon :-)

      July 23, 2013 @ 9:17 am | Reply
  6. shawn:

    Great! Is it best to install on a separate VPS? What are the minimum specs?

    July 22, 2013 @ 9:43 pm | Reply
    • Maarten Kossen:

      That’s totally up to you. I run my nagios server on a VPS that also runs Mumble, TeamSpeak and some other services.

      For this tutorial, I’ve used a 128MB VPS and the current memory usage is:


      [root@buf ~]# free -m
      total used free shared buffers cached
      Mem: 128 41 86 0 0 24
      -/+ buffers/cache: 17 110
      Swap: 128 3 124
      [root@buf ~]#

      July 23, 2013 @ 9:19 am | Reply
  7. I didn’t know installing Nagios would be this easy. Thanks!

    July 25, 2013 @ 2:57 am | Reply
    • Maarten Kossen:

      It surprised me as well, on CentOS, that is. I know how to do it on Ubuntu and it’s quite complicated there.

      July 25, 2013 @ 4:32 am | Reply
  8. Shawn_Ky:

    Very nice!! Looking forward to the next tutorial…

    July 25, 2013 @ 3:20 am | Reply
  9. DalComp:

    Will you write Nagios remote server checks for CentOS?

    July 25, 2013 @ 11:11 pm | Reply
    • Maarten Kossen:

      Yes, if all goes well Liam will push it out tomorrow. When I finish an article, he proof-reads it and usually publishes it :-)

      July 26, 2013 @ 7:16 pm | Reply
  10. I really like nagious the main think is its free and best server monitoring tool

    September 4, 2013 @ 9:30 am | Reply
  11. Mark:

    /etc/nagios/objects/contact.cfg should be /etc/nagios/objects/contacts.cfg

    September 18, 2013 @ 2:48 am | Reply
  12. c arlos:

    Prueba

    September 22, 2015 @ 10:59 am | Reply

Leave a Reply to Rg Cancel reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *