1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include "checks_calculated.h"
21 #include "zbxserver.h"
22 #include "log.h"
23 
get_value_calculated(DC_ITEM * dc_item,AGENT_RESULT * result)24 int	get_value_calculated(DC_ITEM *dc_item, AGENT_RESULT *result)
25 {
26 	int			ret = NOTSUPPORTED;
27 	char			*error = NULL;
28 	zbx_eval_context_t	ctx;
29 	zbx_variant_t		value;
30 	zbx_timespec_t		ts;
31 	zbx_expression_eval_t	eval;
32 
33 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() key:'%s' expression:'%s'", __func__, dc_item->key_orig, dc_item->params);
34 
35 	if (NULL == dc_item->formula_bin)
36 	{
37 		zabbix_log(LOG_LEVEL_DEBUG, "%s() serialized formula is not set", __func__);
38 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot evaluate calculated item:"
39 				" serialized formula is not set"));
40 		goto out;
41 	}
42 
43 	zbx_eval_deserialize(&ctx, dc_item->params, ZBX_EVAL_PARSE_CALC_EXPRESSSION, dc_item->formula_bin);
44 	zbx_timespec(&ts);
45 
46 	zbx_expression_eval_init(&eval, ZBX_EXPRESSION_AGGREGATE, &ctx);
47 	zbx_expression_eval_resolve_item_hosts(&eval, dc_item);
48 
49 	if (SUCCEED != zbx_expression_eval_execute(&eval, &ts, &value, &error))
50 	{
51 		zabbix_log(LOG_LEVEL_DEBUG, "%s() error:%s", __func__, error);
52 		SET_MSG_RESULT(result, error);
53 		error = NULL;
54 	}
55 	else
56 	{
57 		zabbix_log(LOG_LEVEL_DEBUG, "%s() value:%s", __func__, zbx_variant_value_desc(&value));
58 
59 		switch (value.type)
60 		{
61 			case ZBX_VARIANT_DBL:
62 				SET_DBL_RESULT(result, value.data.dbl);
63 				break;
64 			case ZBX_VARIANT_UI64:
65 				SET_UI64_RESULT(result, value.data.ui64);
66 				break;
67 			case ZBX_VARIANT_STR:
68 				SET_TEXT_RESULT(result, value.data.str);
69 				break;
70 			default:
71 				SET_MSG_RESULT(result, zbx_dsprintf(NULL, "unsupported calculated value result \"%s\""
72 						" of type \"%s\"", zbx_variant_value_desc(&value),
73 						zbx_variant_type_desc(&value)));
74 				zbx_variant_clear(&value);
75 				break;
76 		}
77 
78 		if (ZBX_VARIANT_NONE != value.type)
79 		{
80 			zbx_variant_set_none(&value);
81 			ret = SUCCEED;
82 		}
83 	}
84 
85 	zbx_expression_eval_clear(&eval);
86 	zbx_eval_clear(&ctx);
87 out:
88 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
89 
90 	return ret;
91 }
92