Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 37 ยท Templates and Discovery

Templates and Template Inheritance

Learn how Zabbix templates bundle items, triggers and graphs, how nesting and linking work, and how to avoid conflicts when many templates meet on one host.

A template is a reusable set of monitoring definitions: items, triggers, graphs, dashboards, discovery rules and tags. You build it once, link it to a hundred hosts, and every host gets the same checks. When you improve the template, all hosts improve with it. Without templates, a monitoring system turns into a pile of hand-edited hosts that nobody trusts.

This lesson covers how templates are structured, how linking and nesting behave, and the rules that decide what happens when two templates want to define the same thing. Zabbix is not available on this site, so the menus and field names are described in words, and the runnable examples are small Python stand-ins that model the same logic.

Menu names differ slightly between Zabbix versions. The concepts below (templates, linking, nesting, macros) have been stable since Zabbix 2.x. Check your own version's manual for exact paths.

What a template contains

Open Data collection > Templates in a current Zabbix frontend and create a template. It has a technical name, a visible name, and one or more template groups. Inside it you can define:

  • Items: what to collect (system.cpu.load[all,avg1], vfs.fs.size[/,pfree]).
  • Triggers: conditions over item data. They use a host-independent form such as last(/Template OS Linux/vfs.fs.size[/,pfree])<10, written against the template name.
  • Graphs and dashboards: visual views that every linked host receives.
  • Discovery rules: automatic creation of items per filesystem or per network card (see low-level discovery).
  • Macros: user macros like {$CPU.LOAD.MAX} with default values that individual hosts can override.
  • Tags: labels such as component:storage that are inherited by items and triggers.

A template is never "monitored" itself. It only has meaning once it is linked to a host.

Linking and unlinking

When you link a template to a host, Zabbix creates copies of the template entities on that host. The copies stay connected to the template: change the template and the host entities update. In the host's item list those entities show the template name in front of them, and most fields are read-only there.

Unlinking has two flavors:

  • Unlink: the template is removed but the copied items stay on the host as ordinary, editable entities.
  • Unlink and clear: the template and everything it created disappear, including the collected history of those items.

Choose "unlink and clear" with care. History is deleted along with the items.

Nesting templates

A template can itself link other templates. A common layout is:

TEXT
Template App MySQL
  linked: Template OS Linux by Zabbix agent
    linked: Template Module ICMP Ping

A host linked to Template App MySQL therefore receives the operating system items and the ping items too. This is Zabbix's inheritance: lower templates are the base, upper templates add and specialize.

Design rule of thumb: build small module templates (ICMP ping, Linux network interfaces, generic SNMP), then compose application templates from them, and link only the topmost template to hosts. That keeps host configuration to one or two links.

graph TD
  H["Host web01"] --> A["Template App Nginx"]
  A --> B["Template OS Linux"]
  B --> C["Template Module ICMP Ping"]
  H --> D["Template Custom Tags"]

Host web01 inherits everything below the templates it links.

Conflicts: the same key twice

Two templates cannot both define an item with the same key on one host. Zabbix rejects the link with an error. The same holds for triggers with an identical name and expression, and for graphs with identical names. The fix is to restructure: move the shared item into a base template that both of them link, so the item exists once.

Macros behave differently, because they are designed to be overridden. The lookup order for {$CPU.LOAD.MAX} is:

  1. Host macro
  2. Macros of templates linked to the host (first linked template, then its parents, breadth-first)
  3. Global macro
  4. If nothing is found, the macro text stays unresolved

That order is why you set a host macro when one server is special (a database host that is always busy) rather than editing the template.

A model of macro resolution

The following Python stand-in is not Zabbix code. It models the lookup order above so you can experiment with it.

Python Macro resolution order (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.

Change host so it also defines {$CPU.LOAD.MAX} and run it again to see the host value win.

The second stand-in checks the rule that two linked templates must not define the same item key.

Python Key clash checker (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.

In real Zabbix, the cure is to put agent.ping into one shared base template that both templates link.

Practical advice

  • Never edit a host's inherited entity: the next template update would overwrite it. Override through macros or through a separate, clearly named template.
  • Clone community templates before changing them, so that an upstream upgrade does not wipe your edits.
  • Put thresholds into macros with context, for example {$FS.FREE.MIN:"/var"}, rather than into trigger expressions.
  • Use template tags to mark ownership (team:dba) so alerts can be routed later.
  • Nagios has a similar idea; see Nagios object inheritance.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens to the copied items when you choose plain Unlink for a template?

    Unlink keeps the entities; only Unlink and clear deletes them with their history.
  2. 2. Which macro value wins when host, template and global macros all define the same name?

    Lookup goes host first, then templates, then global.
  3. 3. Two templates define the same item key and you link both to one host. What does Zabbix do?

    Duplicate keys on one host are not allowed. Move the shared item to a common base template.
  4. 4. Why use small module templates composed into application templates?

    Modules avoid duplication and keep configuration changes in one place.

Summary

  • A template bundles items, triggers, graphs, discovery rules, macros and tags for reuse.
  • Linked entities stay connected to the template; unlink keeps them, unlink and clear deletes them.
  • Templates nest; lower templates are the base and upper templates specialize.
  • Duplicate keys, trigger names or graph names across linked templates are rejected.
  • Macros resolve host, then template, then global; override on the host for special cases.

Next: Low-Level Discovery