1 매크로 함수

원본 보기

1 매크로 함수

Overview

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

The syntax of a macro function is:

{macro.func(params)}

where

  • macro - the macro to customize;
  • func - the function to apply (see supported functions);
  • params - a comma-delimited list of function parameters, which must be double-quoted if:
    • start with a space or double quotes;
    • contain closing parentheses or a comma.

For example:

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

Macro functions are supported for:

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).

See also: known issues.

A single function per macro is supported; multiple macro functions in chain are not supported.

Please see escaping examples for cases when macro functions are used inside other contexts (function, item key, another macro, etc).

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.

함수 세부사항

선택적 함수 매개변수는 < >로 표시됩니다.

btoa

매크로 값을 Base64 인코딩으로 인코딩합니다. Base64 인코딩은 바이너리 데이터를 텍스트로 표현하는 방법으로, 텍스트 기반 프로토콜을 통해 바이너리 콘텐츠를 저장하고 안전하게 전송하는 데 유용합니다.

예시:

{{ITEM.VALUE}.btoa()} - "zabbix"와 같은 값을 "emFiYml4"로 Base64 인코딩합니다

이 함수는 Zabbix 7.0.4부터 지원됩니다.

fmtnum(digits)

소수점 이후에 출력할 자릿수를 제어하는 숫자 형식 지정.

매개변수:

  • digits - 소수점 이후 자릿수. 유효 범위: 0-20. 후행 0은 생성되지 않습니다.

예시:

{{ITEM.VALUE}.fmtnum(2)} - "24.3483523" 값으로부터 "24.35"를 반환합니다
{{ITEM.VALUE}.fmtnum(0)} - "24.3483523" 값으로부터 "24"를 반환합니다
fmttime(format,<time_shift>)

시간 포맷팅.
이 함수는 다음 중 하나의 시간 형식으로 값을 변환하는 매크로와 함께 사용할 수 있습니다:

  • hh:mm:ss
  • yyyy-mm-ddThh:mm:ss[tz] (ISO8601 표준)
  • UNIX timestamp

매개변수:

  • format - 필수 형식 문자열, strftime 함수 형식과 호환;
  • time_shift (선택사항) - 포맷팅 전에 시간에 적용되는 시간 이동; -<N><time_unit> 또는 +<N><time_unit>로 시작해야 하며, 여기서:
    • N - 더하거나 뺄 시간 단위의 수;
    • time_unit - h (시간), d (일), w (주), M (월) 또는 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)} - "1633098961" 값에서 "October"를 반환합니다
{{TIME}.fmttime(%d %B,-1M/M)} - "1633098961" 값에서 "1 September"를 반환합니다
htmldecode

Decoding a macro value from HTML encoding. This function is supported since Zabbix 7.0.4.

The following characters are supported:

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

Example:

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

Encoding a macro value into HTML encoding. This function is supported since Zabbix 7.0.4.

The following characters are supported:

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

Example:

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

정규 표현식 일치를 통한 하위 문자열 추출 (대소문자 구분 안함).

매개변수:

  • pattern - 일치시킬 정규 표현식;
  • output - 출력 옵션. \1 - \9 플레이스홀더는 캡처 그룹을 지원합니다. \0은 일치하는 텍스트를 반환합니다.

주석:

  • 정규 표현식과 일치하는 것이 없으면, 함수는 빈 문자열을 반환합니다.
  • 함수 패턴이 잘못된 정규 표현식인 경우, 매크로는 'UNKNOWN'으로 평가됩니다 (저수준 탐지 매크로는 제외, 이 경우 함수는 무시되고 매크로는 해결되지 않은 상태로 유지됩니다).
  • 대체 문자열에서 존재하지 않는 캡처 그룹에 대한 참조는 빈 문자열로 대체됩니다.

예제:

{{ITEM.VALUE}.iregsub("fail|error|fault|problem","ERROR")} - "fail", "error", "fault", 또는 "problem" 하위 문자열이 수신되면 "ERROR"로 해결됩니다 (대소문자 구분 안함); 일치하는 것이 없으면 빈 문자열을 반환합니다
lowercase

모든 매크로 값 문자를 소문자로 변환합니다. 단일 바이트 문자 집합(ASCII 등)에서만 작동하며 UTF-8을 지원하지 않습니다. 이 함수는 Zabbix 7.0.4부터 지원됩니다.

예제:

{{ITEM.VALUE}.lowercase()} - "Zabbix SERVER"와 같은 값을 "zabbix server"(소문자)로 변환합니다
regrepl(pattern,replacement,<pattern2>,<replacement2>,...)

매크로 값에서 문자/부분 문자열 치환.
이 함수는 libpcre2 라이브러리에서만 지원됩니다. Zabbix server/proxy가 libpcre로 컴파일된 경우, 이 함수는 UNKNOWN을 반환합니다. 이 함수는 Zabbix 7.0.4부터 지원됩니다.

매개변수:

  • pattern - 일치시킬 정규표현식;
  • replacement - 치환 문자열. 치환 문자열에서 캡처 그룹을 위한 \1 - \9 플레이스홀더가 지원됩니다.

주의사항:

  • 패턴과 치환은 순차적으로 처리되며, 각 후속 쌍은 이전 치환의 결과에 따라 적용됩니다;
  • 치환 문자열에서 존재하지 않는 캡처 그룹에 대한 참조는 빈 문자열로 치환됩니다.

예시:

{{ITEM.VALUE}.regrepl("oldParam", "newParam")} - "oldParam"을 "newParam"으로 치환합니다
{{ITEM.VALUE}.regrepl("([^a-z])","\\\1")} - 모든 비문자 문자를 백슬래시로 이스케이프 처리합니다
{$THRESHOLD:"{{#FSNAME}.regrepl(\"\\$\",\"\")}"} - 후행 백슬래시를 제거합니다 (예: "C:\"를 "C:"로 변경)
{{ITEM.VALUE}.regrepl("_v1\.0", "_v2.0", "\(final\)", "")} - 항목 값의 여러 부분을 치환합니다
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 there is no match for the regular expression, the function returns an empty string.
  • 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).
  • References to non-existent capture groups in the replacement string are replaced with an empty string.

Examples:

{{ITEM.VALUE}.regsub("^([0-9]+)", Problem ID: \1)} - will resolve to "Problem ID: 123" if a value like "123 Log line" is received
{{ITEM.VALUE}.regsub("fail|error|fault|problem","ERROR")} - will resolve to "ERROR" if "fail", "error", "fault", or "problem" substrings are received (case-sensitive); will return an empty string if there is no match

See more examples.

tr(characters,replacement)

매크로 값 문자의 음역 변환입니다. 이 함수는 Zabbix 7.0.4부터 지원됩니다.

  • characters - 바꿀 문자 집합;
  • replacement - 위치적으로 대응하는 교체 문자 집합.

예제:

{{ITEM.VALUE}.tr(abc, xyz)} - 모든 "a"를 "x"로, "b"를 "y"로, "c"를 "z"로 바꿉니다
{{ITEM.VALUE}.tr(abc, xyzq)} - 모든 "a"를 "x"로, "b"를 "y"로, "c"를 "z"로 바꿉니다 ("q"는 무시됨)
{{ITEM.VALUE}.tr(abcde, xyz)} - 모든 "a"를 "x"로, "b"를 "y"로, "c"를 "z"로, "d"를 "z"로, "e"를 "z"로 바꿉니다 (즉, xyzzz)
{{ITEM.VALUE}.tr("\\\'", "\/\"")} - 모든 백슬래시를 슬래시로, 작은따옴표를 큰따옴표로 바꿉니다
{{ITEM.VALUE}.tr(A-Z,a-z)} - 모든 문자를 소문자로 변환합니다
{{ITEM.VALUE}.tr(0-9a-z,*)} - 모든 숫자와 소문자를 "*"로 바꿉니다
{{ITEM.VALUE}.tr(0-9,ab)} - 모든 0을 "a"로 바꾸고, 모든 1, 2, 3, 4, 5, 6, 7, 8, 9를 "b"로 바꿉니다
{{ITEM.VALUE}.tr(0-9abcA-L,*)} - 모든 숫자, "abc" 문자, A-L 범위를 "*"로 바꿉니다
{{ITEM.VALUE}.tr("\n","*")} - 줄 끝 문자를 *로 바꿉니다
{{ITEM.VALUE}.tr("e", "\n")} - 모든 "e"를 줄 끝 문자로 바꿉니다

리터럴 문자를 포함하려면:

백슬래시 - \\로 이스케이프해야 함
작은따옴표 - \'로 이스케이프해야 함
큰따옴표 - \"로 이스케이프해야 함

백슬래시를 사용한 지원되는 이스케이프 시퀀스:

\\\\ => \\ - 이중 백슬래시를 단일 백슬래시로
\\a  => \a - 알람
\\b  => \b - 백스페이스
\\f  => \f - 폼 피드
\\n  => \n - 새 줄
\\r  => \r - 리턴
\\t  => \t - 수평 탭
\\v  => \v - 수직 탭
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

Encoding a macro value into URL encoding. This function is supported since Zabbix 7.0.4.

Example:

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

추가 예제

아래 표는 매크로 함수 사용에 대한 더 많은 예제를 보여줍니다.

:::noteinfo {#IFALIAS}LLD 매크로이며 저수준 검색 컨텍스트(검색 규칙, 프로토타입 및 이들로부터 생성된 아이템/트리거)에서만 정의됩니다. LLD 외부에서 사용하면 토큰이 확장되지 않은 상태로 남아있습니다. :::

매크로 함수 수신된 값 출력
{{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* (잘못된 정규 표현식)
{{#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)} (잘못된 정규 표현식)
{$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)}"} (잘못된 정규 표현식)
"{$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)}\"}" (잘못된 정규 표현식)
전체 항목 값 보기

text/log 항목에 대한 해결된 {ITEM.VALUE} 및 {ITEM.LASTVALUE} 매크로의 긴 값들은 일부 프론트엔드 위치에서 20글자로 잘립니다. 이러한 매크로의 전체 값을 보려면 매크로 함수를 사용할 수 있습니다. 예를 들어:

{{ITEM.VALUE}.regsub("(.*)", \1)}
{{ITEM.LASTVALUE}.regsub("(.*)", \1)}

참고: {ITEM.VALUE} 및 {ITEM.LASTVALUE} 매크로 세부사항.

다음은 무엇인가요?