You are viewing documentation for the development version, it may be incomplete.
Join our translation project and help translate Zabbix documentation into your native language.

1 Macro functions

Macro functions offer the ability to customize macro values (for example, shorten or extract specific substrings), making them easier to work with.

All functions listed here are supported with all types of macros:

Macro functions can be used in all locations supporting the listed macros. This applies unless explicitly stated that only a macro is expected (for example, when configuring host macros or low-level discovery rule filters).

The functions are listed without additional information. Click on the function to see the full details.

Function Description
fmtnum Number formatting to control the number of digits printed after the decimal point.
fmttime Time formatting.
iregsub Substring extraction by a regular expression match (case-insensitive).
regsub Substring extraction by a regular expression match (case-sensitive).

Function details

The syntax of a macro function is:

{macro.func(params)}
  • macro - the macro to customize, for example {ITEM.VALUE} or {#LLDMACRO};
  • func - the function to apply;
  • params - a comma-delimited list of function parameters, which must be quoted if:
    • start with a space or double quotes;
    • contain closing parentheses "``" or a comma.

Optional function parameters are indicated by < >.

fmtnum(digits)

Number formatting to control the number of digits printed after the decimal point.

Parameters:

  • digits - the number of digits after decimal point. Valid range: 0-20. No trailing zeros will be produced.

Examples:

Macro function Received value Output
{{ITEM.VALUE}.fmtnum(2)} 24.3413523 24.34
{{ITEM.VALUE}.fmtnum(0)} 24.3413523 24

fmttime(format,<time_shift>)

Time formatting.
Note that this function can be used with macros that resolve to a value in one of the following time formats:

  • hh:mm:ss
  • yyyy-mm-ddThh:mm:ss[tz] (ISO8601 standard)
  • unix timestamp

Parameters:

  • format - mandatory format string, compatible with strftime function formatting;
  • time_shift (optional) - the time shift applied to the time before formatting; should start with -<N><time_unit> or +<N><time_unit>, where:
    • N - the number of time units to add or subtract;
    • time_unit - h (hour), d (day), w (week), M (month) or y (year).

Comments:

  • The time_shift parameter supports multistep time operations and may include /<time_unit> for shifting to the beginning of the time unit (/d - midnight, /w - 1st day of the week (Monday), /M - 1st day of the month, etc.). Examples: -1w - exactly 7 days back; -1w/w - Monday of the previous week; -1w/w+1d - Tuesday of the previous week.
  • Time operations are calculated from left to right without priorities. For example, -1M/d+1h/w will be parsed as ((-1M/d)+1h)/w.

Examples:

Macro function Received value Output
{{TIME}.fmttime(%B)} 12:36:01 October
{{TIME}.fmttime(%d %B,-1M/M)} 12:36:01 1 September

iregsub(pattern,output)

Substring extraction by a regular expression match (case-insensitive).

Parameters:

  • pattern - the regular expression to match;
  • output - the output options. \1 - \9 placeholders are supported to capture groups. \0 returns the matched text.

Comments:

  • If the function pattern is an incorrect regular expression, then the macro evaluates to 'UNKNOWN' (except for low-level discovery macros, in which case the function will be ignored, and the macro will remain unresolved).

regsub(pattern,output)

Substring extraction by a regular expression match (case-sensitive).

Parameters:

  • pattern - the regular expression to match;
  • output - the output options. \1 - \9 placeholders are supported to capture groups. \0 returns the matched text.

Comments:

  • If the function pattern is an incorrect regular expression, then the macro evaluates to 'UNKNOWN' (except for low-level discovery macros, in which case the function will be ignored, and the macro will remain unresolved).

Examples:

Macro function Received value Output
{{ITEM.VALUE}.regsub(^[0-9]+, Problem)} 123Log line Problem
{{ITEM.VALUE}.regsub("^([0-9]+)", "Problem")} 123 Log line Problem
{{ITEM.VALUE}.regsub("^([0-9]+)", Problem ID: \1)} 123 Log line Problem ID: 123
{{ITEM.VALUE}.regsub(".*", "Problem ID: \1")} Log line ''Problem ID: ''
{{ITEM.VALUE}.regsub("^(\w+).*?([0-9]+)", " Problem ID: \1_\2 ")} MySQL crashed errno 123 '' Problem ID: MySQL_123 ''
{{ITEM.VALUE}.regsub("([1-9]+", "Problem ID: \1")} 123 Log line *UNKNOWN* (invalid regular expression)
{{#IFALIAS}.regsub("(.*)_([0-9]+)", \1)} customername_1 customername
{{#IFALIAS}.regsub("(.*)_([0-9]+)", \2)} customername_1 1
{{#IFALIAS}.regsub("(.*)_([0-9]+", \1)} customername_1 {{#IFALIAS}.regsub("(.*)_([0-9]+", \1)} (invalid regular expression)
{$MACRO:"{{#IFALIAS}.regsub(\"(.*)_([0-9]+)\", \1)}"} customername_1 {$MACRO:"customername"}
{$MACRO:"{{#IFALIAS}.regsub(\"(.*)_([0-9]+)\", \2)}"} customername_1 {$MACRO:"1"}
{$MACRO:"{{#IFALIAS}.regsub(\"(.*)_([0-9]+\", \1)}"} customername_1 {$MACRO:"{{#M}.regsub(\"(.*)_([0-9]+\", \1)}"} (invalid regular expression)
"{$MACRO:\"{{#IFALIAS}.regsub(\\"(.*)_([0-9]+)\\", \1)}\"}" customername_1 "{$MACRO:\"customername\"}"
"{$MACRO:\"{{#IFALIAS}.regsub(\\"(.*)_([0-9]+)\\", \2)}\"}" customername_1 "{$MACRO:\"1\"}")
"{$MACRO:\"{{#IFALIAS}.regsub(\\"(.*)_([0-9]+\\", \1)}\"}" customername_1 "{$MACRO:\"{{#IFALIAS}.regsub(\\"(.*)_([0-9]+\\", \1)}\"}") (invalid regular expression)