Archive for March, 2013

Configure Nagios

This will guide you through a basic setup of a host and some services for Nagios.


First thing to note is that Nagios Core has no web based UI for configuring hosts, services or commands.



Adding a Host


To start the configuration, add a host. You can choose the default location to create a hosts file;



Any new files/directories created must be added to nagios.cfg;

vi /usr/local/nagios/etc/nagios.cfg


New files are added as follows;



Once you have created the new file, open it with your text editor;

vi /usr/local/nagios/etc/objects/linux/hosts.cfg


A simple linux host is defined as follows;

define host{
use         linux-server
host_name   <hostname>
alias       <hostalias>
address     <hostip>
hostgroups   <hostgroup/s to be part of>


The “use” part of this host definition is taken from;



Here you can choose the default settings for a host, e.g. contact groups, notifications enabled, retry attempts.


A full list of possible host definitions can be found here;


If you wish to set a specific setting for just a few hosts, adding it to the hosts.cfg folder overrides the template.



Plugin Usage and Adding Commands


Commands are added through the commands.cfg file located here;



Adding a command requires a plugin for the command you wish to add, as well as knowledge on how to use the plugin.


The default locations for plugins you wish to add can be found here;



To know what command is required for a plugin to work, you can request help on the plugins usage;

cd /usr/local/nagios/libexec/
./check_<plugin> -h


Note that some plugins may require pre-requisites, which will be prompted when you attempt to use the plugin in the command line.


An example would be the plugin check_ping which has the following output when you use the “-h” argument;

check_ping -H <host_address> -w <wrta>,<wpl>% -c <crta>,<cpl>%
[-p packets] [-t timeout] [-4|-6]


This helps you understand how to define a command.


Now edit the commands.cfg file and look for the check_ping command definition;

vi /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_ping
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5



– This is the name you use to define this command. This is used in the service definition to reference this command.


– This is defined in /usr/local/nagios/etc/objects/resources.cfg. In this case it defines the plugin directory being used (/usr/local/nagios/libexec).
“check_ping” requires the arguments -H, -w and -c.



– This defines the host which needs to be checked, and is common in nearly all plugins. The $HOSTADDRESS$ part is an inbuilt Nagios macro which uses the hostname defined in the service command. This won’t need to be defined in each service which uses this commands, as you will see below.

-w $ARG1$

– This sets the warning value which is required for the plugin to work. The $ARG1$ is defined in the service command.


-c $ARG2$

– This sets the critical value which is required for the plugin to work. The $ARG2$ is defined in the service command.


-p 5

– This argument is an optional addition to the command definition. By not using $ARG3$ we are saying that every time this command is used, set “-p” to “5”.

Adding Services


Now you have a host and a command, you need a service!


This services config file can be created/found in the same place as the host file;



Remember to add this location to nagios.cfg if it isn’t already there.


Now you need to add some services to the config file for your host.

vi /usr/local/nagios/etc/objects/services.cfg


The service for ping is defined as follows;

define service{
use                     local-service
host_name               <hostname>
hostgroup_name          <hostgroup name>
service_description     PING
check_command           check_ping!100.0,20%!500.0,60%


With regards to the “check_command”, this is where you define the command to be used for this service by entering the “command_name” defined in commands.cfg.



– This is the “command_name”. This is always entered first.


After the command_name, you must enter the details for $ARG1$, $ARG2$, etc. These are separated by an exclamation mark, “!”.



– This the response time of the ping request. If it’s sufficient for this argument, you will receive a WARNING status.



– This the response time of the ping request. If it’s sufficient for this argument, you will receive a CRITICAL status.

As before, the “use” part is defined in templates.cfg.


A full list of service definitions can be found here;


Adding a definition to services.cfg overrides the template.

You now have a new host configured with the ping service.





If you will have, say, 10 linux servers, all requiring the “ping” command, it would be best to define a hostgroup in the host and service definitions instead of the host name.


A hostgroup is created as follows;

vi /usr/local/nagios/etc/objects/hostgroups.cfg


define hostgroup{
hostgroup_name     <hostgroup name>
alias              <hostgroup alias>


Note that when adding hostgroups/hostgroup_name to the host/service definitions, respectively, they are seperated by commas.


hostgroups        hostgroup1, hostgroup2, ...

These basic steps can be repeated for most plugins – the only differences come when using NRPE, NSCA, or a custom plugin, but even then the basics are essential, or when you are missing pre-requisites for the plugin to work.

When you are finished with your customization, run the command below to confirm that there are no issues with the configuration files;

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Any problems are made apparent to you after running this command, telling you both the file and line where the error occurs.


This makes debugging errors very easy.


If there are no problems, you can restart nagios and head to the web page;

service nagios restart


On a web browser;

http://<nagios server ip>/nagios


The following will be how the ping service is displayed;


Install Nagios on Ubuntu

The following is a guide to install Nagios Core on an Ubuntu machine.


Preparing for the Installation


First you will need to be root;

sudo -s


For convenience it is best to install ssh and vim;

apt-get install ssh vim


Now you need to install some pre-requisites required for Nagios to correctly install/run;

apt-get install apache2
apt-get install libapache2-mod-php5
apt-get install build-essential
apt-get install libgd2-xpm-dev

apt-get install libssl-dev


Now add the user ‘nagios’ to the system;

/usr/sbin/useradd -m -s /bin/bash nagios
passwd nagios


Create nagcmd group to allow commands through the web;

/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd www-data



Installing Nagios and the Plugins


First, make a directory to download the plugins to

mkdir ~/downloads

cd ~/downloads


Now you need to download Nagios and Nagios plugins;




Extracting Nagios so it can be compiled/installed;

 cd ~/downloads
tar -zxvf nagios-3.4.1.tar.gz
 cd nagios


Now you must run the install script and compile the source code;

./configure --with-command-group=nagcmd


make all

make install

make install-init

make install-config

make install-commandmode


Run the following command to install the web server

 make install-webconf


The next step is to create a password for the nagios web server login account, ‘nagiosadmin’

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin


Remember this password as it is required to log on to the web interface.


Restart apache to apply the changes

/etc/init.d/apache2 reload


Now you must extract the plugins;

 cd ~/downloads
tar -zxvf nagios-plugins-1.4.16.tar.gz

cd nagios-plugins-1.4.16


Use the commands below to compile and install the plugins

./configure --with-nagios-user=nagios --with-nagios-group=nagios
make install


Now configure Nagios to start on system start

ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios


Check the nagios configuration with the following command

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


Make note of this command, it is very important later on for debugging issues with config files.


If there are no errors, you are ready to start Nagios;

 /etc/init.d/nagios start


Now access the web interface from a web browser, navigating to
or from within the network;
Congratulations, you are now done with the installation!

Please refer to our blog for a post on configuring your first host.


Contact Us

9th Floor Capital House
40-42 Weston Street
SE1 3QD 0207 100 1499 Follow us Like us

Specialists in integrated and flexible communications

Copyright © 2013 Globility Limited. All rights reserved.