This is a translation of the original English documentation page. Help us make it better.

1 マクロ関数

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 format.
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の値をカスタマイズする機能を提供します。

マクロは、必ずしも簡単に扱えない値を解決することがあります。値が長かったり、抽出したい特定の部分文字列が
含まれていたりする場合があります。このような場合に、マクロ関数が役に立ちます。

マクロ関数の書式は:

{<macro>.<func>(<params>)}

ここで:

  • <macro> - カスタマイズするマクロ (例 {ITEM.VALUE} や {L#LDMACRO} など)です。
  • <func> - 適用する関数です。
  • <params> - カンマで区切られた関数パラメータのリストです。パラメータが '' '' (スペース) で始まる場合、 " で始まる場合、あるいは ), , を含む場合は、引用符で囲む必要があります。

例:

{{TIME}.fmttime(format,time_shift)}
       {{ITEM.VALUE}.regsub(pattern, output)}
       {{#LLDMACRO}.regsub(pattern, output)}
btoa

マクロ値をBase64エンコードします。Base64エンコードはバイナリデータをテキストとして表現する方法で、テキストベースのプロトコルでバイナリコンテンツを保存したり、安全に転送したりするのに役立ちます。

例:

{{ITEM.VALUE}.btoa()} - "zabbix" のような値をBase64エンコードして "emFiYml4"に変換します。

この関数はZabbix 7.0.4以降でサポートされています。

fmtnum(digits)

小数点以下の桁数を制御する数値書式

パラメータ:

  • digits - 小数点以下の桁数。有効範囲: 0~20。末尾にゼロは付加されません。

例:

{{ITEM.VALUE}.fmtnum(2)} - 受信した値"24.3483523"に対して"24.35"を返します。
       {{ITEM.VALUE}.fmtnum(0)} - 受信した値"24.3483523"に対して"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
htmldecode

HTML エンコードからマクロ値をデコードします。この関数はZabbix 7.0.4以降でサポートされています。

以下の文字がサポートされています。

デコードされた値
& &
< <
> >
" "
' '
' '

例:

{{ITEM.VALUE}.htmldecode()} - "<" のような値を "<" に HTML デコードします。
htmlencode

マクロ値をHTMLエンコードします。この関数はZabbix 7.0.4以降でサポートされています。

以下の文字がサポートされています。

エンコードされた値
& &
< <
> >
" "
' '

例:

{{ITEM.VALUE}.htmlencode()} - "<"のような文字を"<"にHTMLエンコードします。
iregsub(pattern,output)

正規表現マッチによる部分文字列抽出(大文字と小文字は区別されません)

パラメータ:

  • pattern - マッチさせる正規表現
  • output - 出力オプション。\1 - \9 プレースホルダはキャプチャグループとしてサポートされています。\0 はマッチしたテキストを返します。

コメント:

  • 正規表現に一致するものがない場合、関数は何も返しません。
  • 関数パターンが不正な正規表現である場合、マクロは 'UNKNOWN' と評価されます。 (低レベル検出マクロの場合は関数は無視され、マクロは未解決のままになります。)
  • 置換文字列内の存在しないキャプチャグループへの参照は、空文字列に置き換えられます。

例:

{{ITEM.VALUE}.iregsub("fail|error|fault|problem","ERROR")} - "fail""error""fault"、または"problem"の部分文字列を受け取った場合は"ERROR"に解決されます(大文字と小文字は区別されません)。一致するものがない場合、何も解決されません。
小文字

マクロの値をすべて小文字に変換します。シングルバイト文字セット(ASCII など)で動作し、UTF-8 はサポートされていません。この関数はZabbix 7.0.4以降でサポートされています。

例:

{{ITEM.VALUE}.lowercase()} - "Zabbix SERVER"のような値を"zabbix server"(小文字)に変換します。

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

Replacement of character/substring in macro value. This function is supported since Zabbix 7.0.4.

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 はマッチしたテキストを返します。

コメント:

  • 正規表現に一致するものがない場合、関数は何も返しません。
  • 関数パターンが不正な正規表現である場合、マクロは 'UNKNOWN' と評価されます。 (低レベル検出マクロの場合は関数は無視され、マクロは未解決のままになります。)
  • 置換文字列内の存在しないキャプチャグループへの参照は、空文字列に置き換えられます。

例:

{{ITEM.VALUE}.regsub("^([0-9]+)", 問題ID: \1)} - "123 Log line" のような値を受け取った場合、"問題ID: 123"に解決されます。
       {{ITEM.VALUE}.regsub("fail|error|fault|problem","ERROR")} - "fail""error""fault""problem"のいずれかの部分文字列を受け取った場合、"ERROR"に解決されます(大文字と小文字は区別されます)。一致するものがない場合、何も返されません。

その他の例を参照してください。

tr(characters,replacement)

Transliteration of macro value characters. This function is supported since Zabbix 7.0.4.

  • 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 occurencies 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

マクロの値をすべて大文字に変換します。シングルバイト文字セット(ASCII など)で動作し、UTF-8 はサポートされていません。この関数はZabbix 7.0.4以降でサポートされています。

例:

{{ITEM.VALUE}.uppercase()} - "Zabbix Server"のような値を"ZABBIX SERVER"(大文字)に変換します。
urldecode

URLエンコードからマクロ値をデコードします。この関数はZabbix 7.0.4以降でサポートされています。

例:

{{ITEM.VALUE}.urldecode()} - "%2F"のような値を"/"にURLデコードします。
urlencode

マクロ値をURLエンコードします。この関数はZabbix 7.0.4以降でサポートされています。

例:

{{ITEM.VALUE}.urlencode()} - "/"などの文字を"%2F"にURLエンコードします

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)