[ZBX-5540] User macros do not expand in formula field for calculated items Created: 2012 Sep 05  Updated: 2017 May 30  Resolved: 2012 Sep 07

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: Server (S)
Affects Version/s: 2.0.3rc1, 2.1.0
Fix Version/s: 2.0.0

Type: Incident report Priority: Major
Reporter: Alexey Pustovalov Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: calculateditems, macros
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate

 Description   

The user macros are not expanded in calculated items.

For example:

  • creating a virtual host
  • definition of 2 macros {$HOST_MACRO1} et {$HOST_MACRO2} containing the names of known hosts zabbix
  • application of a template with a calculated item defined as follows:
    last("{$HOST_MACRO1}:proc.num[snmpd]")+last("{$HOST_MACRO2}:proc.num[snmpd]")

This item goes unsupported and the error message tells me clearly that the macro is ignored:
Can not Evaluate function [last ()]: item [test {$HOST_MACRO1} proc.num [snmpd]] not found



 Comments   
Comment by Alexey Pustovalov [ 2012 Sep 05 ]

3225:20120905:191711.150 In get_value_calculated() key:'test2' expression:'last("{$HOST_MACRO1}:proc.num[snmpd]")+last("{$HOST_MACRO2}:proc.num[snmpd]")'
3225:20120905:191711.150 In calcitem_parse_expression() expression:'last("{$HOST_MACRO1}:proc.num[snmpd]")+last("{$HOST_MACRO2}:proc.num[snmpd]")'
3225:20120905:191711.151 calcitem_parse_expression() functionid:1 function:'last("{$HOST_MACRO1}:proc.num[snmpd]")'
3225:20120905:191711.151 calcitem_parse_expression() functionid:2 function:'last("{$HOST_MACRO2}:proc.num[snmpd]")'
3225:20120905:191711.151 calcitem_parse_expression() expression:'

{1}+{2}'
3225:20120905:191711.151 In substitute_simple_macros() data:'{1}

+

{2}'
3225:20120905:191711.151 Mvar:'{1}'
3225:20120905:191711.152 Mvar:'{2}

'
3225:20120905:191711.152 End substitute_simple_macros() data:'

{1}

+

{2}

'
3225:20120905:191711.152 End of calcitem_parse_expression():SUCCEED
3225:20120905:191711.152 In calcitem_evaluate_expression()
3225:20120905:191711.152 calcitem_evaluate_expression() function:'test:{$HOST_MACRO1}:proc.num[snmpd].last()'
3225:20120905:191711.152 calcitem_evaluate_expression() function:'test:{$HOST_MACRO2}:proc.num[snmpd].last()'
3225:20120905:191711.152 query [txnlev:0] [select i.itemid,i.key_,h.host,i.type,i.history,i.lastvalue,i.prevvalue,i.hostid,i.value_type,i.delta,i.prevorgvalue,i.lastclock,i.units,i.multiplier,i.formula,i.status,i.valuemapid,i.trends,i.
data_type from hosts h,items i where h.hostid=i.hostid and h.status=0 and i.status=0 and ((h.host='test' and i.key_='{$HOST_MACRO1}:proc.num[snmpd]') or (h.host='test' and i.key_='{$HOST_MACRO2}:proc.num[snmpd]')) and h.hostid be
tween 0 and 99999999999999]
3225:20120905:191711.155 End of get_value_calculated():NOTSUPPORTED
3225:20120905:191711.155 Item [test:test2] error: Cannot evaluate function [last()]: item [test:{$HOST_MACRO1}:proc.num[snmpd]] not found

Comment by Alexey Pustovalov [ 2012 Sep 05 ]

I prepared the patch for solve the problem:
Index: src/zabbix_server/poller/checks_calculated.c
===================================================================
— src/zabbix_server/poller/checks_calculated.c (revision 30125)
+++ src/zabbix_server/poller/checks_calculated.c (working copy)
@@ -98,6 +98,10 @@
assert(dc_item);
assert(exp);

+ if (FAIL == (ret = substitute_simple_macros(NULL, NULL, &dc_item->host, NULL, NULL,
+ &dc_item->params, MACRO_TYPE_ITEM_EXPRESSION, error, max_error_len)))
+ ret = NOTSUPPORTED;
+
exp->exp = zbx_malloc(exp->exp, exp_alloc);

for (e = dc_item->params; '\0' != *e; e++)
@@ -129,10 +133,6 @@

zabbix_log(LOG_LEVEL_DEBUG, "%s() expression:'%s'", __function_name, exp->exp);

  • if (FAIL == (ret = substitute_simple_macros(NULL, NULL, &dc_item->host, NULL, NULL,
  • &exp->exp, MACRO_TYPE_ITEM_EXPRESSION, error, max_error_len)))
  • ret = NOTSUPPORTED;
    -
    zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));

return ret;
Index: src/libs/zbxserver/expression.c
===================================================================
— src/libs/zbxserver/expression.c (revision 30125)
+++ src/libs/zbxserver/expression.c (working copy)
@@ -2640,8 +2640,8 @@
if (0 == strncmp(m, "{$", 2)) /* user defined macros */

{ DCget_user_macro(&dc_host->hostid, 1, m, &replace_to); - if (NULL != replace_to && FAIL == (res = is_double_suffix(replace_to)) && NULL != error) - zbx_snprintf(error, maxerrlen, "Macro '%s' value is not numeric", m); +// if (NULL != replace_to && FAIL == (res = is_double_suffix(replace_to)) && NULL != error) +// zbx_snprintf(error, maxerrlen, "Macro '%s' value is not numeric", m); }

}
else if (macro_type & MACRO_TYPE_FUNCTION_PARAMETER)

I do not know why value of user defined macro should be numeric.

Comment by Alexei Vladishev [ 2012 Sep 07 ]

Discussed with Sasha, there is nothing to fix. Actually it works as designed, support of macros in item references, function name, etc may introduce ambiguity.

It should be documented clearly.

Comment by Karim GUEDIDER [ 2012 Sep 07 ]

Hi,
Could you explain ?
use of user macro in calculated items is not supported ?
but it's cleary said in the doc
so what is supported ? a macro in a calculated item but not in the reference ?
Can you give some example or details please ?

thanks

Comment by Martins Valkovskis [ 2012 Sep 07 ]

Rules for expanding user macros in a calculated item formula are documented in a note at:

http://www.zabbix.com/documentation/2.0/manual/config/items/itemtypes/calculated#configurable_fields

Comment by hamid sfandiari [ 2012 Sep 09 ]

How can I use User macro as HOSTNAME or Key_ name in calculated items formulla?
it's very critical for me

Comment by Alexei Vladishev [ 2012 Sep 10 ]

Now everything works according to documentation. I am closing it.

Comment by Filipe Paternot [ 2014 Jun 13 ]

I think this would be a great feature. Actually i think this should be supported by design.

I say that because you might want to do something consistent like this:

(VALID) - create a few items using some usermacro
(NOT SUPPORTED) - create a calculated item to perform some math on all other items (not lld items)

This template would not work (unless you set it manually). You would not have the references since the macros are not expanded.

I cant imagine a valid scenario that this would harm in some way.

Generated at Fri Mar 29 02:40:17 EET 2024 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.