Source
xxxxxxxxxx
$this->assertEquals($expectedItemValue, $lastValues[1], 'Test case for macros {$TEMPLATEMACRO1} {$TEMPLATEMACRO1:regex:abc} {$TEMPLATEMACRO1:regex:x} in agent item key failed');
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
require_once dirname(__FILE__).'/../include/CIntegrationTest.php';
require_once dirname(__FILE__).'/../include/CAPITest.php';
/**
* Test suite for macros with regular expression context.
*
* This test is based on ZBX-25419.
*
* https://www.zabbix.com/documentation/7.0/en/manual/config/macros/user_macros_context
* > Note that a macro with regular expression context can only be defined in user macro configuration.
* > If the regex: prefix is used elsewhere as user macro context, like in a trigger expression,
* > it will be treated as static context.
*
* > If more than one user macro with context exists, Zabbix will try to match the simple context macros first and then
* > context macros with regular expressions in an undefined order.
* > If a macro with its context is not found on host, linked templates or globally, then the macro without context is
* searched for.
*
* Test case X.
*
* 1. Create host with the following macros:
* {$TEST} =>33
* {$TEST:regex:x} => 15, this regex matches any string which contains symbols "x".
* 2. Create a trapper item with key "trap" on this host.
* 3. Create a trigger that will use the macro with
* trigger name
* trigger {$TEST:regex:abc}
* trigger expression:
* last(/<host>/<trapper item key>)>{$TEST:regex:abc}
* 4. Send a value to the trapper item, that would be higher then the value of the context macro
* but lower than the value of the plain user macro, for example 17.
* 5. Zabbix should treat the macro "{$TEST:regex:abc}" in trigger expression as a macro with static
* context "regex:abc" which contains symbol "x" and which should be resolved to "15".
* The problem event should start because the trigger expression becomes true.
* 6. Send a value to the trapper item, that would be higher then the value of the plain user macro, for example 34.
* 7. Make sure that the problem still exists.
*
* Test case Y.
*
* It is different from the test case X in with macro configuration.
*
* 1. Create host with the following macros:
* {$TEST} =>15
* {$TEST:regex:y} => 33, this regex matches any string which contains symbols "y".
* 2. Create a trapper item with key "trap" on this host.
* 3. Create a trigger that will use the macro with
* trigger name
* trigger {$TEST:regex:abc}
* trigger expression:
* last(/<host>/<trapper item key>)>{$TEST:regex:abc}
* 4. Send a value to the trapper item, that would be higher then the value of the context macro
* but lower than the value of the plain user macro, for example 17.
* 5. Zabbix should treat the macro "{$TEST:regex:abc}" in trigger expression as a macro with static
* context "regex:abc" which contains symbol "x" and which should be resolved to "15". It should not match
* the macro {$TEST:regex:y} => 33 in configuration since its static context "regex:abc" does not have the symbol "y".
* The problem event should start because the trigger expression becomes true.
* 6. Send a value to the trapper item, that would be higher then the value of the plain user macro, for example 34.
* 7. Make sure that the problem still exists.
*
* @onAfter clearData
*/
class testUserMacrosWithContextRegex extends CIntegrationTest {
const TEMPLATE_NAME_T1 = 'T1'; // template level 1
const HOSTNAME = 'host_user_macros_with_context_regex';
const TRAPPER_ITEM_NAME_H = 'trapper item|{$HOSTMACRO}|{$HOSTMACRO:regex:abc}|{$HOSTMACRO:regex:x}|';
const TRAPPER_ITEM_NAME_T1 = 'trapper item|{$TEMPLATEMACRO1}|{$TEMPLATEMACRO1:regex:abc}|{$TEMPLATEMACRO1:regex:x}|';
const TRAPPER_ITEM_NAME_G = 'trapper item|{$GLOBALMACRO}|{$GLOBALMACRO:regex:abc}|{$GLOBALMACRO:regex:x}|';
const TRAPPER_ITEM_KEY_H = 'trap_h';
const TRAPPER_ITEM_KEY_T1 = 'trap_t1';
const TRAPPER_ITEM_KEY_G = 'trap_g';
const TRIGGER_EXPRESSION_H = 'last(/'.self::HOSTNAME.'/'.self::TRAPPER_ITEM_KEY_H.')>{$HOSTMACRO:regex:abc}';
const TRIGGER_EXPRESSION_T1 = 'last(/'.self::HOSTNAME.'/'.self::TRAPPER_ITEM_KEY_T1.')>{$TEMPLATEMACRO1:regex:abc}';
const TRIGGER_EXPRESSION_G = 'last(/'.self::HOSTNAME.'/'.self::TRAPPER_ITEM_KEY_G.')>{$GLOBALMACRO:regex:abc}';