这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.

1 宏函数

宏函数提供自定义 macro 值的功能(例如,缩短或提取特定的子字符串)使它们更容易处理。

所有在这里列出的函数都支持所有类型的宏:

这些宏函数可以在支持上述宏的所有位置使用。 除非明确指定只能使用宏(例如,在配置 主机宏 或低级别自动发现规则 过滤器时),否则适用此规则。

这些函数列出时未提供额外信息。点击函数以查看完整详情。

函数 描述
fmtnum 数字格式化,用于控制小数点后打印的位数。
fmttime 时间格式化。
iregsub 使用不区分大小写的正则表达式匹配提取子字符串。
regsub 使用区分大小写的正则表达式匹配提取子字符串。

Supported functions

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

Function Description
btoa Encoding macro value into Base64 encoding.
fmtnum Number formatting to control the number of digits printed after the decimal point.
fmttime Time formatting.
htmldecode Decoding macro value from HTML encoding.
htmlencode Encoding macro value into HTML encoding.
iregsub Substring extraction by a regular expression match (case-insensitive).
lowercase Transformation of macro value characters into lowercase.
regrepl Replacement of character/substring in macro value.
regsub Substring extraction by a regular expression match (case-sensitive).
tr Transliteration of macro value characters.
uppercase Transformation of macro value characters into uppercase.
urldecode Decoding macro value from URL encoding.
urlencode Encoding macro value into URL encoding.

函数详情

宏函数的语法如下:

{macro.func(params)}
  • macro - 要定义的宏,例如 {ITEM.VALUE} 或者 {#LLDMACRO};
  • func - 要应用的函数;
  • params - 以逗号分隔的函数参数列表, 如果满足以下情况必须用引号引起来:
    • 参数以空格或双引号开头;
    • 包含闭合括号 "``" 或逗号。

可选的函数参数用 < > 表示。

btoa

Encoding a macro value into Base64 encoding. Base64 encoding is a method for representing binary data as text, useful for storing and secure transmission of binary content over text-based protocols.

Example:

{{ITEM.VALUE}.btoa()} - Base64-encode a value like "zabbix" into "emFiYml4"

fmtnum(digits)

用于控制小数点后打印的数字位数。

参数:

  • digits - t小数点后的位数。有效范围为:0-20。不会产生最后一位是零。

示例:

宏函数 输入值 输出值
{{ITEM.VALUE}.fmtnum(2)} 24.3413523 24.34
{{ITEM.VALUE}.fmtnum(0)} 24.3413523 24

fmttime(format,<time_shift>)

时间格式化函数。
注意,此函数可用于解析以下时间格式之一的宏:

  • hh:mm:ss
  • yyyy-mm-ddThh:mm:ss[tz] (ISO8601 标准)
  • unix 时间戳

参数:

  • format - 必需的格式字符串, 与 strftime 函数格式兼容;
  • time_shift (可选)- 在格式化之前应用于时间的时间偏移量;应以 -<N><time_unit>+<N><time_unit>开头,其中:
    • N - 要添加或减去的时间单位数量;
    • time_unit - h (小时), d (天), w (周), M (月) or y (年)。

注释:

  • time_shift 参数支持多步时间操作,可以包括/<time_unit> 用于将时间移动到时间单位的开始处 (/d - 午夜, /w - 上周的第一天(星期一), /M - 上个月的第一天等)。 示例:-1w - 后退7天; -1w/w - 上周一; -1w/w+1d - 上周二。
  • 在时间操作中,从左到右计算,没有优先级。例如 -1M/d+1h/w 将解析为 ((-1M/d)+1h)/w.
宏函数 输入值 输出值
{{TIME}.fmttime(%B)} 12:36:01 October
{{TIME}.fmttime(%d %B,-1M/M)} 12:36:01 1 September

htmldecode

Decoding a macro value from HTML encoding.

The following characters are supported:

Value Decoded value
&amp; &
&lt; <
&gt; >
&quot; "
&#039; '
&#39; '

Example:

{{ITEM.VALUE}.htmldecode()} - HTML-decode a value like "&lt;" into "<"

htmlencode

Encoding a macro value into HTML encoding.

The following characters are supported:

Value Encoded value
& &amp;
< &lt;
> &gt;
" &quot;
' &#39;

Example:

{{ITEM.VALUE}.htmlencode()} - HTML-encode a character like "<" into "&lt;"

iregsub(pattern,output)

正则表达式匹配提取子字符串(不区分大小写)。

参数:

  • pattern - 要匹配的正则表达式;
  • output - 输出选项。支持 \1 - \9 占位符用于捕获组。 使用\0 返回匹配的文本。

注释:

  • 如果函数中的 pattern 是一个不正确的正则表达式,则宏的计算结果将为 'UNKNOWN' (除了低级别发现宏,此时函数将被忽略,宏保持未解析状态)。

lowercase

Transformation of all macro value characters into lowercase. Works with single-byte character sets (such as ASCII) and does not support UTF-8.

Example:

{{ITEM.VALUE}.lowercase()} - transform a value like "Zabbix SERVER" into "zabbix server" (lowercase)

regrepl(pattern,replacement,<pattern2>,<replacement2>,...)

Replacement of character/substring in macro value. Note that this function is only supported with the libpcre2 library. If Zabbix server/proxy was compiled with libpcre, this function will return UNKNOWN.

Parameters:

  • pattern - the regular expression to match;
  • replacement - the replacement string.

Comments:

  • The patterns and replacements are processed sequentially, with each subsequent pair being applied in accordance with the outcome of the previous replacement;
  • This function also allows referencing captured groups in replacement strings using \1 to \9 placeholders.

Examples:

{{ITEM.VALUE}.regrepl("oldParam", "newParam")} - replace "oldParam" with "newParam"
       {{ITEM.VALUE}.regrepl("([^a-z])","\\\1")} - escape all non-letter characters with a backslash
       {{ITEM.VALUE}.regrepl("_v1\.0", "_v2.0", "\(final\)", "")} - replace multiple parts in item value

regsub(pattern,output)

通过正则表达式匹配进行子字符串提取(区分大小写)。

参数:

  • pattern - 要匹配的正则表达式;
  • output - 输出选项。支持使用\1 - \9 占位符来捕获分组。使用 \0 返回匹配的文本。

注释:

  • 如果函数的 pattern 参数是不正确的正则表达式,则宏将评估为 'UNKNOWN' (除了低级别发现宏,在这种情况下,函数将被忽略,宏仍将保持未解析状态)。

示例:

宏函数 输入值 输出值
{{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)

tr(characters,replacement)

Transliteration of macro value characters.

  • characters - the set of characters to replace;
  • replacement - the set of positionally corresponding replacement characters.

Examples:

{{ITEM.VALUE}.tr(abc, xyz)} - replace all occurrences of "a" with "x", "b" with "y", "c" with "z"
       {{ITEM.VALUE}.tr(abc, xyzq)} - replace all occurrences of "a" with "x", "b" with "y", "c" with "z" ("q" is ignored)
       {{ITEM.VALUE}.tr(abcde, xyz)} - replace all occurrences of "a" with "x", "b" with "y", "c" with "z", "d" with "z", "e" with "z" (i.e. xyzzz)
       {{ITEM.VALUE}.tr("\\\'", "\/\"")} - replace all occurrences of backslash with forward slash, ' with "
       {{ITEM.VALUE}.tr(A-Z,a-z)} - convert all letters to lowercase
       {{ITEM.VALUE}.tr(0-9a-z,*)} - replace all numbers and lowercase letters with "*"
       {{ITEM.VALUE}.tr(0-9,ab)} - replace all occurrences of 0 with "a", and replace all occurrences of 1, 2, 3, 4, 5, 6, 7, 8, and 9 with "b"
       {{ITEM.VALUE}.tr(0-9abcA-L,*)} - replace all numbers, "abc" characters, and A-L range with "*"
       {{ITEM.VALUE}.tr("\n","*")} - replace end-of-line occurrences with *
       {{ITEM.VALUE}.tr("e", "\n")} - replace all "e" to end-of-line

To include literal characters:

backslash - must be escaped as \\
       single quote - must be escaped as \'
       double quote - must be escaped as \"

Supported escape sequences with backslash:

\\\\ => \\ - double backslash to single backslash
       \\a  => \a - alert
       \\b  => \b - backspace
       \\f  => \f - form feed
       \\n  => \n - newline
       \\r  => \r - return
       \\t  => \t - horizontal tab
       \\v  => \v - vertical tab

uppercase

Transformation of all macro value characters into uppercase. Works with single-byte character sets (such as ASCII) and does not support UTF-8.

Example:

{{ITEM.VALUE}.uppercase()} - transform a value like "Zabbix Server" into "ZABBIX SERVER" (uppercase)

urldecode

Decoding a macro value from URL encoding.

Example:

{{ITEM.VALUE}.urldecode()} - URL-decode a value like "%2F" into "/"

urlencode

Encoding a macro value into URL encoding.

Example:

{{ITEM.VALUE}.urlencode()} - URL-encode a character like "/" into "%2F"

Additional examples

The table below shows more examples of using macro functions.

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(".*", "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)