Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Templates and Discovery

Nagios Object Inheritance and Config Generation

See how Nagios Core uses templates with the use directive, register 0 objects and hostgroups to avoid repetition, and how to generate config files with scripts.

Nagios Core predates Zabbix's web-based templates. Everything is configured in plain text files, and the way to avoid repetition is object inheritance: an object declares use template-name and receives all directives of the template, then overrides what it needs. This lesson explains the mechanism, the multi-template rules and the tooling that keeps large Nagios setups manageable.

Nagios is not available on this site. The config snippets below are read-only examples; the runnable parts are Python stand-ins that model inheritance and generate configuration.

Templates are objects with register 0

A template is an ordinary object definition with register 0, which tells Nagios not to create a real host or service from it:

TEXT
define host {
    name                    generic-host
    notifications_enabled   1
    check_period            24x7
    max_check_attempts      3
    check_command           check-host-alive
    contact_groups          admins
    register                0
}

define host {
    name                    linux-server
    use                     generic-host
    check_interval          5
    register                0
}

define host {
    use                     linux-server
    host_name               web01
    address                 10.0.0.11
    max_check_attempts      5
}

web01 is real (register defaults to 1). It receives generic-host through linux-server, and its own max_check_attempts 5 wins. Note the name directive: templates are referred to by name, real objects by host_name.

Rules of inheritance

  • Directives on the object itself beat inherited ones.
  • With use a,b,c, earlier templates beat later ones, and each template's own parents are searched after it. The order is deliberate and documented in the Nagios manual.
  • A + before a value in a list directive (contact_groups +dba) adds to the inherited list instead of replacing it. A leading ! in some contexts disables an inherited item. Without +, a value replaces the inherited one.
  • null can reset an inherited directive.
  • Templates can be nested to any depth, but keep them to three levels or the origin of a value becomes hard to guess.

Services, hostgroups and service templates

Most repetition is in services. Instead of one service per host, bind a service to a hostgroup:

TEXT
define service {
    use                     generic-service
    hostgroup_name          linux-servers
    service_description     SSH
    check_command           check_ssh
}

Every host in linux-servers automatically has an SSH service. Adding a host to the group is the only step. This is Nagios's version of "link a template". The directive hostgroup_name also supports ! exclusions, such as hostgroup_name linux-servers,!lab-servers, and host_name supports regular expressions when use_regexp_matching is enabled (rarely a good idea).

Service dependencies, escalations and event handlers use the same inheritance, so you will see use generic-escalation and similar names in larger setups.

Custom variables and macros

Directives starting with an underscore define custom variables: _DISK_WARN 20% on a host or service. In commands they are read as $_HOSTDISK_WARN$ or $_SERVICEDISK_WARN$. This gives you per-host thresholds without writing a separate command definition for every host, and parallels Zabbix's user macros.

TEXT
define command {
    command_name    check_disk_custom
    command_line    $USER1$/check_disk -w $_HOSTDISK_WARN$ -c $_HOSTDISK_CRIT$ -p /
}

Managing configuration at scale

A flat file with 500 hosts is not fun. Teams usually do one of:

  • Split into directories and use cfg_dir=/etc/nagios/conf.d so that each host or team has its own file.
  • Generate files from a source of truth (a CMDB, YAML, Ansible inventory) with a template engine such as Jinja or a short script.
  • Use Nagios-compatible front ends and forks (Naemon, Icinga, Thruk, Check_MK with its own rules) that add web configuration.
  • Always run nagios -v /etc/nagios/nagios.cfg before reload. It validates syntax, missing references and circular inheritance, and prints a count of objects.

Stand-in: resolving inheritance

This Python code resolves the use chain with the first-wins rule and shows the final effective values. It is a simplified model; real Nagios also handles + merging, which is added in the second example.

Python Resolve use chains (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in: generating host definitions

Repeating define host blocks by hand is the problem generators solve. This script turns a small inventory into Nagios syntax.

Python Generate host and service definitions (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Zabbix versus Nagios in one table

Topic Zabbix Nagios Core
Reuse unit Template linked to host use template and hostgroup services
Configuration Web UI, API, import/export Text files
Discovery Built-in LLD and network discovery Not built in; scripts or add-ons
History Built-in database, graphs Plugin output and status; graphs via add-ons
Per-host thresholds User macros with context Custom variables

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does register 0 mean in a Nagios object?

    Templates are defined with register 0 so Nagios does not create a real object.
  2. 2. How does a service apply to every host in a group?

    hostgroup_name binds the service to all members of the group.
  3. 3. What does contact_groups +dba do on an object that inherits contact_groups admins?

    The plus sign appends to the inherited value instead of replacing it.
  4. 4. What should you run before reloading Nagios?

    The -v flag checks syntax, references and circular inheritance.

Summary

  • Nagios inheritance uses use and register 0 templates; the object's own values win.
  • Hostgroups attach services to many hosts at once.
  • Custom variables give per-host thresholds, and + extends inherited lists.
  • Generate files from a source of truth and always validate with nagios -v.

Next: Dashboards and Widgets