Source
/*
** Zabbix
** 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 General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
int get_value_calculated(DC_ITEM *dc_item, AGENT_RESULT *result)
{
int ret = NOTSUPPORTED;
char *error = NULL;
zbx_eval_context_t ctx;
zbx_variant_t value;
zbx_timespec_t ts;
zbx_expression_eval_t eval;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() key:'%s' expression:'%s'", __func__, dc_item->key_orig, dc_item->params);
if (NULL == dc_item->formula_bin)
{
zabbix_log(LOG_LEVEL_DEBUG, "%s() serialized formula is not set", __func__);
SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot evaluate calculated item:"
" serialized formula is not set"));
goto out;
}
zbx_eval_deserialize(&ctx, dc_item->params, ZBX_EVAL_PARSE_CALC_EXPRESSION, dc_item->formula_bin);
zbx_timespec(&ts);
zbx_expression_eval_init(&eval, ZBX_EXPRESSION_AGGREGATE, &ctx);
zbx_expression_eval_resolve_item_hosts(&eval, dc_item);
zbx_expression_eval_resolve_filter_macros(&eval, dc_item);
if (SUCCEED != zbx_expression_eval_execute(&eval, &ts, &value, &error))
{
zabbix_log(LOG_LEVEL_DEBUG, "%s() error:%s", __func__, error);
SET_MSG_RESULT(result, error);
error = NULL;
}
else
{
zabbix_log(LOG_LEVEL_DEBUG, "%s() value:%s", __func__, zbx_variant_value_desc(&value));
switch (value.type)
{
case ZBX_VARIANT_DBL:
SET_DBL_RESULT(result, value.data.dbl);