Nagios Core object files define the hosts, services, contacts, commands, and templates that the scheduler loads before monitoring starts. Adding a dedicated object directory keeps local monitoring definitions separate from packaged examples, plugin command fragments, and generated files when a Nagios server starts to grow.
The main nagios.cfg file can load individual object files with cfg_file or whole directories with cfg_dir. A cfg_dir entry processes every .cfg file in the named directory and recurses through subdirectories, so only active object definitions should live below that path.
On Ubuntu and Debian package installs, the main file is /etc/nagios4/nagios.cfg, the packaged service is nagios4, and the object cache used by the CGIs is /var/lib/nagios4/objects.cache. Source installs usually keep the same directives but place the binary and configuration under /usr/local/nagios.
$ grep '^cfg_dir=' /etc/nagios4/nagios.cfg cfg_dir=/etc/nagios-plugins/config cfg_dir=/etc/nagios4/conf.d
Do not add a duplicate cfg_dir entry for a directory that is already listed. Ubuntu and Debian packages include /etc/nagios4/conf.d by default.
$ sudo install -d -m 0755 /etc/nagios4/sites.d
The nagios4 daemon must be able to read the directory and every .cfg file below it.
$ sudoedit /etc/nagios4/sites.d/web01.cfg
define host {
use linux-server
host_name web01.example.net
alias Web 01
address 192.0.2.10
}
define service {
use generic-service
host_name web01.example.net
service_description HTTP
check_command check_http
}
Replace web01.example.net, 192.0.2.10, and the service definition with a real object for the monitoring site. The packaged linux-server and generic-service templates supply required defaults for this minimal example.
Related: How to add a host in Nagios Core
$ sudoedit /etc/nagios4/nagios.cfg
cfg_dir=/etc/nagios-plugins/config cfg_dir=/etc/nagios4/conf.d cfg_dir=/etc/nagios4/sites.d
Nagios Core processes only files ending in .cfg from a cfg_dir path. Renaming a file to another extension is enough to exclude it from that directory on the next validation and reload.
$ sudo nagios4 -v /etc/nagios4/nagios.cfg Nagios Core 4.4.6 ##### snipped ##### Reading configuration data... Read main config file okay... Read object config files okay... ##### snipped ##### Total Warnings: 0 Total Errors: 0 Things look okay - No serious problems were detected during the pre-flight check
Do not reload Nagios Core while Total Errors is greater than 0. Fix the first reported object, command, template, or path error before applying the new directory.
$ sudo systemctl reload nagios4
The packaged systemd unit sends SIGHUP to the daemon during reload. Use the service name and reload method for the local installation when Nagios Core was installed from source.
Related: How to manage the Nagios Core system service
$ grep 'web01.example.net' /var/lib/nagios4/objects.cache
host_name web01.example.net
host_name web01.example.net
Use the local object_cache_file path from nagios.cfg if the installation does not use /var/lib/nagios4/objects.cache. After the reload, the Nagios Core web interface should also list web01.example.net under Hosts.