2 User macros
Overview
User macros can be defined on global, template and host level. These macros have a special syntax:
Zabbix resolves macros according to the following precedence:
- macros defined for first level templates of the host (i.e., templates linked directly to the host), sorted by template ID
- macros defined for second level templates of the host, sorted by template ID
- macros defined for third level templates of the host, sorted by template ID, etc.
- global macros (checked last)
In other words, if a macro does not exist for a host, Zabbix will try to find it in the host templates of increasing depth. If still not found, a global macro will be used, if exists.
If a macro with the same name exists on multiple linked templates of the same level, the macro from the template with the lowest ID will be used. Thus having macros with the same name in multiple templates is a configuration risk.
If Zabbix is unable to find a macro, the macro will not be resolved.
Macros (including user macros) are left unresolved in the Configuration section (for example, in the trigger list) by design to make complex configuration more transparent.
User macros can be used in:
- item key parameter
- item update intervals and flexible intervals
- trigger name and description
- trigger expression parameters and constants (see )
- many other locations - see the full list
Common use cases of global and host macros
- use a global macro in several locations; then change the macro value and apply configuration changes to all locations with one click
- take advantage of templates with host-specific attributes: passwords, port numbers, file names, regular expressions, etc.
It is advisable to use host macros instead of global macros because adding, updating or deleting global macros forces incremental configuration update for all hosts. For more information, see Upgrade notes for 6.4.0.
Configuration
To define user macros, go to the corresponding location in the frontend:
- for host and template level macros, open host or template properties and look for the Macros tab
A user macro has the following attributes:
In trigger expressions user macros will resolve if referencing a parameter or constant. They will NOT resolve if referencing a host, item key, function, operator or another trigger expression. Secret macros cannot be used in trigger expressions.
Examples
Example 1
Use of host-level macro in the “Status of SSH daemon” item key:
This item can be assigned to multiple hosts, providing that the value of {$SSH_PORT} is defined on those hosts.
Example 2
Use of host-level macro in the “CPU load is too high” trigger:
Such a trigger would be created on the template, not edited in individual hosts.
Example 3
Use of two macros in the “CPU load is too high” trigger:
Note that a macro can be used as a parameter of trigger function, in this example function min().
Example 4
Synchronize the agent unavailability condition with the item update interval:
- define {$INTERVAL} macro and use it in the item update interval;
- use {$INTERVAL} as parameter of the agent unavailability trigger:
Example 5
Centralize configuration of working hours:
- create a global {$WORKING_HOURS} macro equal to ;
- use it in the Working time field in Administration → General → GUI;
- use it in the When active field in Users → Users, Media tab of a user;
- use it to set up more frequent item polling during working hours:
- use it in the Time period action condition;
- adjust the working time in Administration → Macros, if needed.
Example 6
Use host prototype macro to configure items for discovered hosts:
- on a host prototype define user macro {$SNMPVALUE} with {#SNMPVALUE} macro as a value:
- assign Generic SNMPv2 template to the host prototype;