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.
Related Posts:
- How to Rapidly Install Java, OpenJDK & Oracle JDK on your VPS - December 14, 2015
- It’s been a great ride - December 14, 2015
- Cheap Windows VPS – $21/quarter 1GB KVM-based Windows VPS in 11 worldwide locations - November 30, 2015
Nice guide. Should be very easy for people to follow.
Thank you for your contributions on Centos tutorials. Keep it up.
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.
Nice guide, maybe throw in one with cacti and npc, that is a nice setup!
Thanks for making Nagios less daunting for people.
Thanks! Nagios is actually quite easy on CentOS, especially if you compare it to Ubuntu.
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.
Liam is going to do this soon :-)
Great! Is it best to install on a separate VPS? What are the minimum specs?
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 ~]#
I didn’t know installing Nagios would be this easy. Thanks!
It surprised me as well, on CentOS, that is. I know how to do it on Ubuntu and it’s quite complicated there.
Very nice!! Looking forward to the next tutorial…
Will you write Nagios remote server checks for CentOS?
Yes, if all goes well Liam will push it out tomorrow. When I finish an article, he proof-reads it and usually publishes it :-)
Thanks! :D
Anxiously awaiting….
I really like nagious the main think is its free and best server monitoring tool
/etc/nagios/objects/contact.cfg should be /etc/nagios/objects/contacts.cfg
Prueba