Nagios
Nagios
This guide will cover the installation process of Nagios on Ubuntu Operating
System and will also serve as a user guide on how to configure Nagios. If any command
does not work there may be a need to place sudo before it.
Required Packages
• Apache 2
• GCC compiler and development libraries
• GD development libraries
You can use apt-get to install these packages by running the following commands:
If these commands do not work then the repositories must be manually installed
from the Ubuntu CD.
sudo -s
/usr/sbin/useradd nagios
passwd nagios
On Ubuntu server edition (6.01 and possible newer versions), you will need to also add a
nagios group (it's not created by default). You should be able to skip this step on desktop
editions of Ubuntu.
/usr/sbin/groupadd nagios
/usr/sbin/usermod -G nagios nagios
Create a new nagcmd group for allowing external commands to be submitted through the
web interface. Add both the nagios user and the apache user to the group.
/usr/sbin/groupadd nagcmd
1
/usr/sbin/usermod -G nagcmd nagios
/usr/sbin/usermod -G nagcmd www-data
mkdir ~/downloads
cd ~/downloads
Download the source code tarballs of both Nagios and the Nagios plugins. The
downloads folder created would be the preferred place to save these files.
The plugin and source code can be found at www.nagios.org under the download section.
cd ~/downloads
tar xzf nagios-3.xx.tar.gz (3.xx being what ever name the version of nagios that is
downloaded)
cd nagios-3.xx (folder will be the same name as file that was extracted)
Run the Nagios configure script, passing the name of the group you created earlier like
so:
./configure --with-command-group=nagcmd
make all
Install binaries, init script, sample config files and set permissions on the external
command directory.
make install
make install-init
make install-config
make install-commandmode
Don't start Nagios yet - there's still more that needs to be done...
2
Sample configuration files have now been installed in the /usr/local/nagios/etc directory.
These sample files should work fine for getting started with Nagios. You'll need to make
just one change before you proceed...
Edit the /usr/local/nagios/etc/objects/contacts.cfg config file with your favorite editor and
change the email address associated with the nagiosadmin contact definition to the
address you'd like to use for receiving alerts.
gedit /usr/local/nagios/etc/objects/contacts.cfg
Install the Nagios web config file in the Apache conf.d directory.
make install-webconf
Create a nagiosadmin account for logging into the Nagios web interface. Remember the
password you assign to this account - you'll need it later.
/etc/init.d/apache2 reload
cd ~/downloads
tar xzf nagios-plugins-xxx.tar.gz (xxx being what ever name of the plugin file that is
downloaded)
ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios
3
Verify the sample Nagios configuration files.
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios start
You should now be able to access the Nagios web interface at the URL below. You'll be
prompted for the username (nagiosadmin) and password you specified earlier.
http://localhost/nagios/
Click on the "Service Detail" navbar link to see details of what's being monitored on your
local machine. It will take a few minutes for Nagios to check all the services associated
with your machine, as the checks are spread out over time.
4
Configuring Nagios
Step 1) The first time you configure Nagios to monitor a machine, you'll need to edit a
certain file.
gedit /usr/local/nagios/etc/nagios.cfg
Remove the leading pound (#) sign from the following line in the main configuration file:
#cfg_file=/usr/local/nagios/etc/objects/servers
This tells nagios where to search for cfg files and will run everything in that folder.
Step 2) Now it's time to define some object definitions in your Nagios configuration files
in order to monitor services that have been set up.
Before you can monitor a service, you first need to define a host that is associated with
the service. These definition are placed in the cfg file created.
For this example, lets say you want to monitor a variety of services on a remote host.
Let's call that host remotehost. The host definition can be placed in its own file or added
to an already exiting object configuration file. Here's what the host definition for
remotehost might look like:
define host{
use generic-host ; Inherit default values from a template
host_name remotehost ; The name we're giving to this host
alias Some Remote Host ; A longer name associated with the host
address 192.168.1.50 ; IP address of the host
hostgroups allhosts ; Host groups this host is associated with
}
5
Step 4) Creating Service Definitions
For each service you want to monitor, you need to define a service in Nagios that is
associated with the host definition you just created. You can place service definitions in
any object configuration file specified by a cfg_file directive or placed in a directory
specified by a cfg_dir directive.
Some example service definitions for monitoring common public service (HTTP, FTP,
etc) are given below.
Monitoring HTTP
Chances are you're going to want to monitor web servers at some point - either yours or
someone else's. The check_http plugin is designed to do just that. It understands the
HTTP protocol and can monitor response time, error codes, strings in the returned
HTML, server certificates, and much more.
The commands.cfg file contains a command definition for using the check_http plugin. It
looks like this:
define command{
name check_http
command_name check_http
command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
}
A simple service definition for monitoring the HTTP service on the remotehost machine
might look like this:
define service{
use generic-service ; Inherit default values from a template
host_name remotehost
service_description HTTP
check_command check_http
}
This simple service definition will monitor the HTTP service running on remotehost. It
will produce alerts if the web server doesn't respond within 10 seconds or if it returns
HTTP errors codes (403, 404, etc.). That's all you need for basic monitoring. Pretty
simple, huh?
Tip: For more advanced monitoring, run the check_http plugin manually with --help as a
command-line argument to see all the options you can give the plugin. This --help syntax
works with all of the plugins I'll cover in this document.
6
A more advanced definition for monitoring the HTTP service is shown below. This
service definition will check to see if the /download/index.php URI contains the string
"latest-version.tar.gz". It will produce an error if the string isn't found, the URI isn't valid,
or the web server takes longer than 5 seconds to respond.
define service{
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description Product Download Link
check_command check_http!-u /download/index.php -t 5 -s "latest-
version.tar.gz"
}
Monitoring FTP
When you need to monitor FTP servers, you can use the check_ftp plugin. The
commands.cfg file contains a command definition for using the check_ftp plugin, which
looks like this:
define command{
command_name check_ftp
command_line $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$
}
A simple service definition for monitoring the FTP server on remotehost would look like
this:
define service{
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description FTP
check_command check_ftp
}
This service definition will monitor the FTP service and generate alerts if the FTP server
doesn't respond within 10 seconds.
A more advanced service definition is shown below. This service will check the FTP
server running on port 1023 on remotehost. It will generate an alert if the server doesn't
respond within 5 seconds or if the server response doesn't contain the string "Pure-FTPd
[TLS]".
define service{
7
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description Special FTP
check_command check_ftp!-p 1023 -t 5 -e "Pure-FTPd [TLS]"
}
Monitoring SSH
When you need to monitor SSH servers, you can use the check_ssh plugin. The
commands.cfg file contains a command definition for using the check_ssh plugin, which
looks like this:
define command{
command_name check_ssh
command_line $USER1$/check_ssh $ARG1$ $HOSTADDRESS$
}
A simple service definition for monitoring the SSH server on remotehost would look like
this:
define service{
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description SSH
check_command check_ssh
}
This service definition will monitor the SSH service and generate alerts if the SSH server
doesn't respond within 10 seconds.
Monitoring SMTP
The check_smtp plugin can be using for monitoring your email servers. The
commands.cfg file contains a command definition for using the check_smtp plugin, which
looks like this:
define command{
command_name check_smtp
command_line $USER1$/check_smtp -H $HOSTADDRESS$ $ARG1$
}
A simple service definition for monitoring the SMTP server on remotehost would look
like this:
8
define service{
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description SMTP
check_command check_smtp
}
This service definition will monitor the SMTP service and generate alerts if the SMTP
server doesn't respond within 10 seconds.
A more advanced service definition is shown below. This service will check the SMTP
server and generate an alert if the server doesn't respond within 5 seconds or if the
response from the server doesn't contain "mygreatmailserver.com".
define service{
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description SMTP Response Check
check_command check_smtp!-t 5 -e "mygreatmailserver.com"
}
Monitoring POP3
The check_pop plugin can be using for monitoring the POP3 service on your email
servers. The commands.cfg file contains a command definition for using the check_pop
plugin, which looks like this:
define command{
command_name check_pop
command_line $USER1$/check_pop -H $HOSTADDRESS$ $ARG1$
}
A simple service definition for monitoring the POP3 service on remotehost would look
like this:
define service{
use generic-service ; Inherit default values from a template
host_name remotehost
service_description POP3
check_command check_pop
}
This service definition will monitor the POP3 service and generate alerts if the POP3
server doesn't respond within 10 seconds.
9
A more advanced service definition is shown below. This service will check the POP3
service and generate an alert if the server doesn't respond within 5 seconds or if the
response from the server doesn't contain "mygreatmailserver.com".
define service{
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description POP3 Response Check
check_command check_pop!-t 5 -e "mygreatmailserver.com"
}
Monitoring IMAP
The check_imap plugin can be using for monitoring IMAP4 service on your email
servers. The commands.cfg file contains a command definition for using the check_imap
plugin, which looks like this:
define command{
command_name check_imap
command_line $USER1$/check_imap -H $HOSTADDRESS$ $ARG1$
}
A simple service definition for monitoring the IMAP4 service on remotehost would look
like this:
define service{
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description IMAP
check_command check_imap
}
This service definition will monitor the IMAP4 service and generate alerts if the IMAP
server doesn't respond within 10 seconds.
A more advanced service definition is shown below. This service will check the IAMP4
service and generate an alert if the server doesn't respond within 5 seconds or if the
response from the server doesn't contain "mygreatmailserver.com".
define service{
use generic-service ; Inherit default values from a
template
host_name remotehost
service_description IMAP4 Response Check
10
check_command check_imap!-t 5 -e "mygreatmailserver.com"
}
Restarting Nagios
Once you've added the new host and service definitions to your object configuration
file(s), you're ready to start monitoring them. To do this, you'll need to verify your
configuration and restart Nagios.
If the verification process produces any errors messages, fix your configuration file
before continuing. Make sure that you don't (re)start Nagios until the verification process
completes without any errors!
The command can be found in the first in earlier section of the guide.
11