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;
1 |
/usr/local/nagios/etc/objects/hosts.cfg |
Any new files/directories created must be added to nagios.cfg;
1 |
vi /usr/local/nagios/etc/nagios.cfg |
New files are added as follows;
1 |
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg |
Once you have created the new file, open it with your text editor;
1 |
vi /usr/local/nagios/etc/objects/linux/hosts.cfg |
A simple linux host is defined as follows;
1 |
define host{ |
1 |
use linux-server |
1 |
host_name <hostname> |
1 |
alias <hostalias> |
1 |
address <hostip> |
1 |
hostgroups <hostgroup/s to be part of> |
1 |
} |
The “use” part of this host definition is taken from;
1 |
/usr/local/nagios/etc/objects/templates.cfg |
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;
http://nagios.sourceforge.net/docs/3_0/objectdefinitions.html#host
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;
1 |
/usr/local/nagios/etc/objects/commands.cfg |
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;
1 |
/usr/local/nagios/libexec |
To know what command is required for a plugin to work, you can request help on the plugins usage;
1 |
cd /usr/local/nagios/libexec/ |
1 |
./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;
1 |
Usage: |
1 |
check_ping -H <host_address> -w <wrta>,<wpl>% -c <crta>,<cpl>% |
1 |
[-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;
1 |
vi /usr/local/nagios/etc/objects/commands.cfg |
1 |
define command{ |
1 |
command_name check_ping |
1 |
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5 |
1 |
} |
1 |
command_name |
– This is the name you use to define this command. This is used in the service definition to reference this command.
1 |
$USER1$ |
– 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.
1 |
-H $HOSTADDRESS$ |
– 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.
1 |
-w $ARG1$ |
– This sets the warning value which is required for the plugin to work. The $ARG1$ is defined in the service command.
1 |
-c $ARG2$ |
– This sets the critical value which is required for the plugin to work. The $ARG2$ is defined in the service command.
1 |
-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;
1 |
/usr/local/nagios/etc/objects/services.cfg |
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.
1 |
vi /usr/local/nagios/etc/objects/services.cfg |
The service for ping is defined as follows;
1 |
define service{ |
1 |
use local-service |
1 |
host_name <hostname> |
1 |
hostgroup_name <hostgroup name> |
1 |
service_description PING |
1 |
check_command check_ping!100.0,20%!500.0,60% |
1 |
} |
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.
1 |
check_ping |
– 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, “!”.
1 |
!100.0,20% |
– This the response time of the ping request. If it’s sufficient for this argument, you will receive a WARNING status.
1 |
!500.0,60% |
– 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;
http://nagios.sourceforge.net/docs/3_0/objectdefinitions.html#service
Adding a definition to services.cfg overrides the template.
You now have a new host configured with the ping service.
Hostgroups
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;
1 |
vi /usr/local/nagios/etc/objects/hostgroups.cfg |
1 |
define hostgroup{ |
1 |
hostgroup_name <hostgroup name> |
1 |
alias <hostgroup alias> |
1 |
} |
Note that when adding hostgroups/hostgroup_name to the host/service definitions, respectively, they are seperated by commas.
e.g.
1 |
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;
1 |
/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;
1 |
service nagios restart |
On a web browser;
http://<nagios server ip>/nagios
The following will be how the ping service is displayed;
Tags: monitoring, nagios, sysadmin