Tips for configuring Nagios: Wildcards and cfg_dir
Nagios is one of the most widely used monitoring tool today. Its open source, customizable , easy to use and highly efficient. While managing a large fleet of servers which includes different category of servers like webservers, db servers, api servers. This gets further more complicated if the number of servers in the fleet keeps on changing frequently.
This article aims at focusing on some useful configuration tips for configuring Nagios monitoring system to save time and configuring host and services.
Before proceeding with this blog further enable regexp in your nagios.cfg by enabling the following current setting:
use_regexp_matching=1
1. Single entry for including all the configurations.
By default Nagios uses cfg_file entry in nagios.cfg to include the configuration. In a large setup keeping all the configuration in a couple of files can cause issues if the no. of hosts and configuration keeps changing frequently. To overcome this add following entry in your nagios.cfg file:
cfg_dir=/path/to/a/directory #e.g. cfg_dir=/usr/local/nagios/etc/hosts/
Reload your Nagios service. Now any .cfg file place inside hosts folder or on any subfolder/hierarchical level inside hosts folder will automatically get included in your Nagios setup.
2. Wildcards
Let’s consider a scenario; your AWS fleet is serving example.com. This fleet includes DB servers, API servers, web servers etc.
Create a hostgroup named primary.cfg (you can name the config file as per your wish. Important point to note is the file should end with .cfg):
[js]
define hostgroup {
hostgroup_name example.com-servers
alias example.com Servers
members .*
}
[/js]
Now, create the sub hostgroups:
[js]
define hostgroup {
hostgroup_name example-web-servers
alias Example-web Servers
members .*_webservers
}
define hostgroup {
hostgroup_name example-db-servers
alias example db Servers
members .*_dbserver
}
[/js]
Now, create host as below:
[js]
define host {
use generic-host
host_name server-ip_dbserver
check_command check-host-alive
alias server-ip_dbserver
address server-ip
contact_groups admins
}
define host {
use generic-host
host_name server-ip_webserver
check_command check-host-alive
alias server-ip_webserver
address server-ip
contact_groups admins
}
[/js]
After reloading the Nagios service. You will notice all the servers are listed in the primary groups however the webserver hostgroup shows only the webserver and DB server hostgroup shows only the DB server. In future, while adding new server, we would only have to consider the format of host_name and hostgroup i.e., server-ip_webserver or server-ip_dbserver and the new server will automatically become member of the respective hostgroups (Nagios service reload is required), hosts are now not required to be added to the members list of respective hostgroup cfg file.