Templates in NConf

What are templates?

One of the most powerful features of Nagios is that it allows you to use object inheritance in your configuration files. Items can be defined as templates, allowing other items to inherit properties from them. Individual object properties can override the inherited ones. This considerably facilitates maintenance of the Nagios configuration.

An example:

Here we define a template named ‘generic-host’. Setting ‘register’ to ‘0’ means that the template itself will not be visible as a host within Nagios:

define host{
    name                    generic-host
    register                0
    check_command           check-host-alive
    notification_options    d,u,r
    max_check_attempts      5
}
 
define host{
    host_name               localhost
    use                     generic-host
    address                 127.0.0.1
    max_check_attempts      3
}

The host ‘localhost’ inherits the properties ‘check_command’ and ‘notification_options’ from the template, but overrides the ‘max_check_attempts’ value from the template with a value of its own.

Templates can be chained to create a hierarchical structure of inheritance. Properties can also be inherited from multiple templates.

For a detailed description of Nagios templates, refer to the Nagios Objects Inheritance manual.

Templates in NConf

Templates are a feature that was introduced in NConf 1.2.6. NConf allows you to define host- and service-templates: host-templates can be assigned either to hosts or other host-templates (chaining). Likewise, service-templates can be assigned either to services or other service-templates.

Both host- and service-templates can be linked with timeperiods, nagios-collectors and nagios-monitors. Service-templates can additionally be linked to checkcommands. These linked templates will then be assigned to all hosts / services who are linked with any of those items. Prior to NConf 1.2.6, some properties (e.g. “max_check_attempts”, “check_interval”, “active_checks_enabled” etc.) were statically copied from timeperiods / collectors / monitors onto hosts and services. As of NConf 1.3 this ‘static inheriting’ has been fully replaced by templates.

We assume there are two main kinds of NConf users:

A) users who were previously using static attributes within timeperiods / collectors / monitors and who ran the conversion script during the update to 1.3. They now assign the same attributes to their hosts / services via templates.

B) users who directly assign templates to each host & service or who set these properties on a host / service level


Information concerning “type A)” users:
As a replacement for the former ‘static inheriting’, we introduced the possibility for users to link a template with a timeperiod, a collector or a monitor. These templates will then be assigned to all hosts / services who are linked with any of those items. The goal was for users, who want to continue using the old static attributes, to switch to these new templates under timeperiods / collectors / monitors.


Information concerning “type B)” users:
If you want to assign templates to hosts and services individually, do the following:

  1. Make sure you unlink all templates from your timeperiods / colelctors / monitors
  2. Create new host- & service-templates containing exactly those attributes that you need
  3. Assign them individually to your hosts or services (can be done using the ‘multi-modify’ feature)

Template application priority

The following sequence illustrates in which order of precedence attributes are applied to a host (the same order naturally also applies to services):

  1. highest priority have attributes defined on a host or service level directly
  2. second priority have attributes defined in a template, that is directly linked to a host or service
  3. third priority have attributes defined in a template, that is linked to a service over its ‘checkcommand’
  4. fourth priority have attributes defined in a template, that is linked to a host or serivce over its ‘notification_period’
  5. fifth priority have attributes defined in a template, that is linked to a host or service over its ‘check_period’
  6. sixth priority have attributes defined in a template, that is linked to a host or service over a collector / monitor