diff options
Diffstat (limited to 'src/libs/zbxserver/evalfunc.c')
-rw-r--r-- | src/libs/zbxserver/evalfunc.c | 284 |
1 files changed, 139 insertions, 145 deletions
diff --git a/src/libs/zbxserver/evalfunc.c b/src/libs/zbxserver/evalfunc.c index 9dd6ddd8b7f..43760a834ea 100644 --- a/src/libs/zbxserver/evalfunc.c +++ b/src/libs/zbxserver/evalfunc.c @@ -25,11 +25,6 @@ #include "evalfunc.h" #include "zbxregexp.h" -int cmp_double(double a, double b) -{ - return fabs(a - b) < TRIGGER_EPSILON ? SUCCEED : FAIL; -} - static int __get_function_parameter_uint31(zbx_uint64_t hostid, const char *parameters, int Nparam, int *value, int *flag, int defaults_on_empty, int def_value, int def_flag) { @@ -154,19 +149,20 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_LOGEVENTID(char *value, DB_ITEM *item, const char *function, const char *parameters, +static int evaluate_LOGEVENTID(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { - const char *__function_name = "evaluate_LOGEVENTID"; - char *arg1 = NULL; - int ret = FAIL; - zbx_vector_ptr_t regexps; - zbx_vector_history_record_t values; + const char *__function_name = "evaluate_LOGEVENTID"; + + char *arg1 = NULL; + int found, ret = FAIL; + zbx_vector_ptr_t regexps; + zbx_history_record_t vc_value; + zbx_timespec_t ts = {now, 999999999}; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_vector_ptr_create(®exps); - zbx_history_record_vector_create(&values); if (ITEM_VALUE_TYPE_LOG != item->value_type) goto out; @@ -174,25 +170,22 @@ static int evaluate_LOGEVENTID(char *value, DB_ITEM *item, const char *function, if (1 < num_param(parameters)) goto out; - if (SUCCEED != get_function_parameter_str(item->hostid, parameters, 1, &arg1)) + if (SUCCEED != get_function_parameter_str(item->host.hostid, parameters, 1, &arg1)) goto out; if ('@' == *arg1) DCget_expressions_by_name(®exps, arg1 + 1); - if (SUCCEED == zbx_vc_get_value_range(item->itemid, item->value_type, &values, 0, 1, now) && - 0 < values.values_num) + if (SUCCEED == zbx_vc_get_value(item->itemid, item->value_type, &ts, &vc_value, &found) && 1 == found) { - char *logeventid = NULL; - size_t size = 0, offset = 0; + char logeventid[16]; - zbx_snprintf_alloc(&logeventid, &size, &offset, "%d", values.values[0].value.log->logeventid); + zbx_snprintf(logeventid, sizeof(logeventid), "%d", vc_value.value.log->logeventid); if (SUCCEED == regexp_match_ex(®exps, logeventid, arg1, ZBX_CASE_SENSITIVE)) zbx_strlcpy(value, "1", MAX_BUFFER_LEN); else zbx_strlcpy(value, "0", MAX_BUFFER_LEN); - - zbx_free(logeventid); + zbx_history_record_clear(&vc_value, item->value_type); ret = SUCCEED; } @@ -204,8 +197,6 @@ out: zbx_regexp_clean_expressions(®exps); zbx_vector_ptr_destroy(®exps); - zbx_history_record_vector_destroy(&values, item->value_type); - zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; @@ -224,16 +215,16 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_LOGSOURCE(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_LOGSOURCE(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { - const char *__function_name = "evaluate_LOGSOURCE"; - char *arg1 = NULL; - int ret = FAIL; - zbx_vector_history_record_t values; + const char *__function_name = "evaluate_LOGSOURCE"; - zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); + char *arg1 = NULL; + int found, ret = FAIL; + zbx_history_record_t vc_value; + zbx_timespec_t ts = {now, 999999999}; - zbx_history_record_vector_create(&values); + zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (ITEM_VALUE_TYPE_LOG != item->value_type) goto out; @@ -241,16 +232,16 @@ static int evaluate_LOGSOURCE(char *value, DB_ITEM *item, const char *function, if (1 < num_param(parameters)) goto out; - if (SUCCEED != get_function_parameter_str(item->hostid, parameters, 1, &arg1)) + if (SUCCEED != get_function_parameter_str(item->host.hostid, parameters, 1, &arg1)) goto out; - if (SUCCEED == zbx_vc_get_value_range(item->itemid, item->value_type, &values, 0, 1, now) && - 0 < values.values_num) + if (SUCCEED == zbx_vc_get_value(item->itemid, item->value_type, &ts, &vc_value, &found) && 1 == found) { - if (0 == strcmp(values.values[0].value.log->source, arg1)) + if (0 == strcmp(NULL == vc_value.value.log->source ? "" : vc_value.value.log->source, arg1)) zbx_strlcpy(value, "1", MAX_BUFFER_LEN); else zbx_strlcpy(value, "0", MAX_BUFFER_LEN); + zbx_history_record_clear(&vc_value, item->value_type); ret = SUCCEED; } @@ -259,8 +250,6 @@ static int evaluate_LOGSOURCE(char *value, DB_ITEM *item, const char *function, zbx_free(arg1); out: - zbx_history_record_vector_destroy(&values, item->value_type); - zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; @@ -279,31 +268,30 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_LOGSEVERITY(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_LOGSEVERITY(char *value, DC_ITEM *item, const char *function, const char *parameters, + time_t now) { - const char *__function_name = "evaluate_LOGSEVERITY"; - int ret = FAIL; - zbx_vector_history_record_t values; + const char *__function_name = "evaluate_LOGSEVERITY"; - zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); + int found, ret = FAIL; + zbx_history_record_t vc_value; + zbx_timespec_t ts = {now, 999999999}; - zbx_history_record_vector_create(&values); + zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (ITEM_VALUE_TYPE_LOG != item->value_type) goto out; - if (SUCCEED == zbx_vc_get_value_range(item->itemid, item->value_type, &values, 0, 1, now) && - 0 < values.values_num) + if (SUCCEED == zbx_vc_get_value(item->itemid, item->value_type, &ts, &vc_value, &found) && 1 == found) { - zbx_snprintf(value, MAX_BUFFER_LEN, "%d", values.values[0].value.log->severity); + zbx_snprintf(value, MAX_BUFFER_LEN, "%d", vc_value.value.log->severity); + zbx_history_record_clear(&vc_value, item->value_type); ret = SUCCEED; } else zabbix_log(LOG_LEVEL_DEBUG, "result for LOGSEVERITY is empty"); out: - zbx_history_record_vector_destroy(&values, item->value_type); - zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; @@ -380,33 +368,33 @@ static int evaluate_COUNT_one(unsigned char value_type, int op, history_value_t switch (op) { case OP_EQ: - if (value->dbl > arg2_double - TRIGGER_EPSILON && - value->dbl < arg2_double + TRIGGER_EPSILON) + if (value->dbl > arg2_double - ZBX_DOUBLE_EPSILON && + value->dbl < arg2_double + ZBX_DOUBLE_EPSILON) { return SUCCEED; } break; case OP_NE: - if (!(value->dbl > arg2_double - TRIGGER_EPSILON && - value->dbl < arg2_double + TRIGGER_EPSILON)) + if (!(value->dbl > arg2_double - ZBX_DOUBLE_EPSILON && + value->dbl < arg2_double + ZBX_DOUBLE_EPSILON)) { return SUCCEED; } break; case OP_GT: - if (value->dbl >= arg2_double + TRIGGER_EPSILON) + if (value->dbl >= arg2_double + ZBX_DOUBLE_EPSILON) return SUCCEED; break; case OP_GE: - if (value->dbl > arg2_double - TRIGGER_EPSILON) + if (value->dbl > arg2_double - ZBX_DOUBLE_EPSILON) return SUCCEED; break; case OP_LT: - if (value->dbl <= arg2_double - TRIGGER_EPSILON) + if (value->dbl <= arg2_double - ZBX_DOUBLE_EPSILON) return SUCCEED; break; case OP_LE: - if (value->dbl < arg2_double + TRIGGER_EPSILON) + if (value->dbl < arg2_double + ZBX_DOUBLE_EPSILON) return SUCCEED; break; } @@ -473,7 +461,7 @@ static int evaluate_COUNT_one(unsigned char value_type, int op, history_value_t * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_COUNT(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_COUNT(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_COUNT"; int arg1, flag, op, numeric_search, nparams, count = 0, i, ret = FAIL; @@ -490,17 +478,17 @@ static int evaluate_COUNT(char *value, DB_ITEM *item, const char *function, cons if (4 < (nparams = num_param(parameters))) goto out; - if (SUCCEED != get_function_parameter_uint31(item->hostid, parameters, 1, &arg1, &flag) || 0 == arg1) + if (SUCCEED != get_function_parameter_uint31(item->host.hostid, parameters, 1, &arg1, &flag) || 0 == arg1) goto out; - if (2 <= nparams && SUCCEED != get_function_parameter_str(item->hostid, parameters, 2, &arg2)) + if (2 <= nparams && SUCCEED != get_function_parameter_str(item->host.hostid, parameters, 2, &arg2)) goto out; if (3 <= nparams) { int fail = 2; - if (SUCCEED != get_function_parameter_str(item->hostid, parameters, 3, &arg3)) + if (SUCCEED != get_function_parameter_str(item->host.hostid, parameters, 3, &arg3)) goto out; if ('\0' == *arg3) @@ -553,7 +541,7 @@ static int evaluate_COUNT(char *value, DB_ITEM *item, const char *function, cons { int time_shift, time_shift_flag; - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 4, &time_shift, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 4, &time_shift, &time_shift_flag, 0, ZBX_FLAG_SEC) || ZBX_FLAG_SEC != time_shift_flag) { goto out; @@ -618,7 +606,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_SUM(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_SUM(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_SUM"; int nparams, arg1, flag, i, ret = FAIL, seconds = 0, nvalues = 0; @@ -634,14 +622,14 @@ static int evaluate_SUM(char *value, DB_ITEM *item, const char *function, const if (2 < (nparams = num_param(parameters))) goto out; - if (SUCCEED != get_function_parameter_uint31(item->hostid, parameters, 1, &arg1, &flag) || 0 == arg1) + if (SUCCEED != get_function_parameter_uint31(item->host.hostid, parameters, 1, &arg1, &flag) || 0 == arg1) goto out; if (2 == nparams) { int time_shift, time_shift_flag; - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 2, &time_shift, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 2, &time_shift, &time_shift_flag, 0, ZBX_FLAG_SEC) || ZBX_FLAG_SEC != time_shift_flag) { goto out; @@ -699,7 +687,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_AVG(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_AVG(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_AVG"; int nparams, arg1, flag, ret = FAIL, i, seconds = 0, nvalues = 0; @@ -715,14 +703,14 @@ static int evaluate_AVG(char *value, DB_ITEM *item, const char *function, const if (2 < (nparams = num_param(parameters))) goto out; - if (SUCCEED != get_function_parameter_uint31(item->hostid, parameters, 1, &arg1, &flag) || 0 == arg1) + if (SUCCEED != get_function_parameter_uint31(item->host.hostid, parameters, 1, &arg1, &flag) || 0 == arg1) goto out; if (2 == nparams) { int time_shift, time_shift_flag; - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 2, &time_shift, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 2, &time_shift, &time_shift_flag, 0, ZBX_FLAG_SEC) || ZBX_FLAG_SEC != time_shift_flag) { goto out; @@ -781,7 +769,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_LAST(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_LAST(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_LAST"; int arg1, flag, ret = FAIL; @@ -789,7 +777,7 @@ static int evaluate_LAST(char *value, DB_ITEM *item, const char *function, const zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 1, &arg1, &flag, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 1, &arg1, &flag, 1, ZBX_FLAG_VALUES)) { goto out; @@ -805,7 +793,7 @@ static int evaluate_LAST(char *value, DB_ITEM *item, const char *function, const { int time_shift, time_shift_flag; - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 2, &time_shift, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 2, &time_shift, &time_shift_flag, 0, ZBX_FLAG_SEC) || ZBX_FLAG_SEC != time_shift_flag) { goto out; @@ -846,7 +834,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_MIN(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_MIN(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_MIN"; int nparams, arg1, flag, i, ret = FAIL, seconds = 0, nvalues = 0; @@ -862,14 +850,14 @@ static int evaluate_MIN(char *value, DB_ITEM *item, const char *function, const if (2 < (nparams = num_param(parameters))) goto out; - if (SUCCEED != get_function_parameter_uint31(item->hostid, parameters, 1, &arg1, &flag) || 0 == arg1) + if (SUCCEED != get_function_parameter_uint31(item->host.hostid, parameters, 1, &arg1, &flag) || 0 == arg1) goto out; if (2 == nparams) { int time_shift, time_shift_flag; - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 2, &time_shift, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 2, &time_shift, &time_shift_flag, 0, ZBX_FLAG_SEC) || ZBX_FLAG_SEC != time_shift_flag) { goto out; @@ -933,7 +921,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_MAX(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_MAX(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_MAX"; int nparams, arg1, flag, ret = FAIL, i, seconds = 0, nvalues = 0; @@ -949,14 +937,14 @@ static int evaluate_MAX(char *value, DB_ITEM *item, const char *function, const if (2 < (nparams = num_param(parameters))) goto out; - if (SUCCEED != get_function_parameter_uint31(item->hostid, parameters, 1, &arg1, &flag) || 0 == arg1) + if (SUCCEED != get_function_parameter_uint31(item->host.hostid, parameters, 1, &arg1, &flag) || 0 == arg1) goto out; if (2 == nparams) { int time_shift, time_shift_flag; - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 2, &time_shift, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 2, &time_shift, &time_shift_flag, 0, ZBX_FLAG_SEC) || ZBX_FLAG_SEC != time_shift_flag) { goto out; @@ -1020,7 +1008,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_DELTA(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_DELTA(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_DELTA"; int nparams, arg1, flag, ret = FAIL, i, seconds = 0, nvalues = 0; @@ -1036,14 +1024,14 @@ static int evaluate_DELTA(char *value, DB_ITEM *item, const char *function, cons if (2 < (nparams = num_param(parameters))) goto out; - if (SUCCEED != get_function_parameter_uint31(item->hostid, parameters, 1, &arg1, &flag) || 0 == arg1) + if (SUCCEED != get_function_parameter_uint31(item->host.hostid, parameters, 1, &arg1, &flag) || 0 == arg1) goto out; if (2 == nparams) { int time_shift, time_shift_flag; - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 2, &time_shift, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 2, &time_shift, &time_shift_flag, 0, ZBX_FLAG_SEC) || ZBX_FLAG_SEC != time_shift_flag) { goto out; @@ -1119,7 +1107,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_NODATA(char *value, DB_ITEM *item, const char *function, const char *parameters) +static int evaluate_NODATA(char *value, DC_ITEM *item, const char *function, const char *parameters, char **error) { const char *__function_name = "evaluate_NODATA"; int arg1, flag, now, ret = FAIL; @@ -1130,10 +1118,18 @@ static int evaluate_NODATA(char *value, DB_ITEM *item, const char *function, con zbx_history_record_vector_create(&values); if (1 < num_param(parameters)) + { + if (NULL != error) + *error = zbx_strdup(*error, "too many parameters"); goto out; + } - if (SUCCEED != get_function_parameter_uint31(item->hostid, parameters, 1, &arg1, &flag)) + if (SUCCEED != get_function_parameter_uint31(item->host.hostid, parameters, 1, &arg1, &flag)) + { + if (NULL != error) + *error = zbx_strdup(*error, "invalid first parameter"); goto out; + } if (ZBX_FLAG_SEC != flag) goto out; @@ -1149,8 +1145,25 @@ static int evaluate_NODATA(char *value, DB_ITEM *item, const char *function, con { int seconds; - if (SUCCEED != DCget_data_expected_from(item->itemid, &seconds) || seconds + arg1 > now) + if (SUCCEED != DCget_data_expected_from(item->itemid, &seconds)) + { + if (NULL != error) + { + *error = zbx_strdup(*error, "item does not exist, is disabled" + " or belongs to a disabled host"); + } goto out; + } + + if (seconds + arg1 > now) + { + if (NULL != error) + { + *error = zbx_strdup(*error, "item does not have enough data" + " after server start or item creation"); + } + goto out; + } zbx_strlcpy(value, "1", MAX_BUFFER_LEN); } @@ -1177,7 +1190,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_ABSCHANGE(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_ABSCHANGE(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_ABSCHANGE"; int ret = FAIL; @@ -1249,7 +1262,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_CHANGE(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_CHANGE(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_CHANGE"; int ret = FAIL; @@ -1318,7 +1331,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_DIFF(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_DIFF(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_DIFF"; int ret = FAIL; @@ -1335,12 +1348,11 @@ static int evaluate_DIFF(char *value, DB_ITEM *item, const char *function, const switch (item->value_type) { case ITEM_VALUE_TYPE_FLOAT: - if (SUCCEED == cmp_double(values.values[0].value.dbl, values.values[1].value.dbl)) + if (SUCCEED == zbx_double_compare(values.values[0].value.dbl, values.values[1].value.dbl)) zbx_strlcpy(value, "0", MAX_BUFFER_LEN); else zbx_strlcpy(value, "1", MAX_BUFFER_LEN); break; - break; case ITEM_VALUE_TYPE_UINT64: if (values.values[0].value.ui64 == values.values[1].value.ui64) zbx_strlcpy(value, "0", MAX_BUFFER_LEN); @@ -1353,7 +1365,6 @@ static int evaluate_DIFF(char *value, DB_ITEM *item, const char *function, const else zbx_strlcpy(value, "1", MAX_BUFFER_LEN); break; - case ITEM_VALUE_TYPE_STR: case ITEM_VALUE_TYPE_TEXT: if (0 == strcmp(values.values[0].value.str, values.values[1].value.str)) @@ -1409,7 +1420,7 @@ static int evaluate_STR_one(int func, zbx_vector_ptr_t *regexps, const char *val return FAIL; } -static int evaluate_STR(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_STR(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_STR"; char *arg1 = NULL; @@ -1440,12 +1451,12 @@ static int evaluate_STR(char *value, DB_ITEM *item, const char *function, const if (2 < (nparams = num_param(parameters))) goto out; - if (SUCCEED != get_function_parameter_str(item->hostid, parameters, 1, &arg1)) + if (SUCCEED != get_function_parameter_str(item->host.hostid, parameters, 1, &arg1)) goto out; if (2 == nparams) { - if (SUCCEED != get_function_parameter_uint31_default(item->hostid, parameters, 2, &arg2, &flag, + if (SUCCEED != get_function_parameter_uint31_default(item->host.hostid, parameters, 2, &arg2, &flag, 1, ZBX_FLAG_VALUES) || 0 == arg2) { goto out; @@ -1531,7 +1542,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_STRLEN(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_STRLEN(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_STRLEN"; int ret = FAIL; @@ -1566,17 +1577,16 @@ clean: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_FUZZYTIME(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_FUZZYTIME(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { - const char *__function_name = "evaluate_FUZZYTIME"; - int arg1, flag, ret = FAIL; - zbx_vector_history_record_t values; - history_value_t *lastvalue; - zbx_uint64_t fuzlow, fuzhig; + const char *__function_name = "evaluate_FUZZYTIME"; - zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); + int arg1, flag, found, ret = FAIL; + zbx_history_record_t vc_value; + zbx_uint64_t fuzlow, fuzhig; + zbx_timespec_t ts = {now, 999999999}; - zbx_history_record_vector_create(&values); + zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (ITEM_VALUE_TYPE_FLOAT != item->value_type && ITEM_VALUE_TYPE_UINT64 != item->value_type) goto out; @@ -1584,41 +1594,38 @@ static int evaluate_FUZZYTIME(char *value, DB_ITEM *item, const char *function, if (1 < num_param(parameters)) goto out; - if (SUCCEED != get_function_parameter_uint31(item->hostid, parameters, 1, &arg1, &flag)) + if (SUCCEED != get_function_parameter_uint31(item->host.hostid, parameters, 1, &arg1, &flag)) goto out; if (ZBX_FLAG_SEC != flag || now <= arg1) goto out; - if (SUCCEED != zbx_vc_get_value_range(item->itemid, item->value_type, &values, 0, 1, now) || - 1 > values.values_num) - { + if (SUCCEED != zbx_vc_get_value(item->itemid, item->value_type, &ts, &vc_value, &found) || 1 != found) goto out; - } fuzlow = (int)(now - arg1); fuzhig = (int)(now + arg1); - lastvalue = &values.values[0].value; - if (ITEM_VALUE_TYPE_UINT64 == item->value_type) { - if (lastvalue->ui64 >= fuzlow && lastvalue->ui64 <= fuzhig) + if (vc_value.value.ui64 >= fuzlow && vc_value.value.ui64 <= fuzhig) zbx_strlcpy(value, "1", MAX_BUFFER_LEN); else zbx_strlcpy(value, "0", MAX_BUFFER_LEN); } else { - if (lastvalue->dbl >= fuzlow && lastvalue->dbl <= fuzhig) + if (vc_value.value.dbl >= fuzlow && vc_value.value.dbl <= fuzhig) zbx_strlcpy(value, "1", MAX_BUFFER_LEN); else zbx_strlcpy(value, "0", MAX_BUFFER_LEN); } + zbx_history_record_clear(&vc_value, item->value_type); + ret = SUCCEED; out: - zbx_history_record_vector_destroy(&values, item->value_type); + zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; } @@ -1644,7 +1651,7 @@ out: * FAIL - failed to evaluate function * * * ******************************************************************************/ -static int evaluate_BAND(char *value, DB_ITEM *item, const char *function, const char *parameters, time_t now) +static int evaluate_BAND(char *value, DC_ITEM *item, const char *function, const char *parameters, time_t now) { const char *__function_name = "evaluate_BAND"; char *last_parameters = NULL; @@ -1659,7 +1666,7 @@ static int evaluate_BAND(char *value, DB_ITEM *item, const char *function, const if (3 < (nparams = num_param(parameters))) goto clean; - if (SUCCEED != get_function_parameter_uint64(item->hostid, parameters, 2, &mask, &mask_flag) || + if (SUCCEED != get_function_parameter_uint64(item->host.hostid, parameters, 2, &mask, &mask_flag) || ZBX_FLAG_SEC != mask_flag) { goto clean; @@ -1697,15 +1704,16 @@ clean: * FAIL - evaluation failed * * * ******************************************************************************/ -int evaluate_function(char *value, DB_ITEM *item, const char *function, const char *parameter, time_t now) +int evaluate_function(char *value, DC_ITEM *item, const char *function, const char *parameter, time_t now, + char **error) { const char *__function_name = "evaluate_function"; int ret; struct tm *tm = NULL; - zabbix_log(LOG_LEVEL_DEBUG, "In %s() function:'%s.%s(%s)'", __function_name, - zbx_host_key_string_by_item(item), function, parameter); + zabbix_log(LOG_LEVEL_DEBUG, "In %s() function:'%s:%s.%s(%s)'", __function_name, + item->host.host, item->key_orig, function, parameter); *value = '\0'; @@ -1743,7 +1751,7 @@ int evaluate_function(char *value, DB_ITEM *item, const char *function, const ch } else if (0 == strcmp(function, "nodata")) { - ret = evaluate_NODATA(value, item, function, parameter); + ret = evaluate_NODATA(value, item, function, parameter, error); } else if (0 == strcmp(function, "date")) { @@ -1816,7 +1824,7 @@ int evaluate_function(char *value, DB_ITEM *item, const char *function, const ch } else { - zabbix_log(LOG_LEVEL_WARNING, "unsupported function:%s", function); + *error = zbx_strdup(*error, "function is not supported"); ret = FAIL; } @@ -2021,7 +2029,7 @@ static void add_value_suffix_normal(char *value, size_t max_len, const char *uni value_double /= base * base * base * base; } - if (SUCCEED != cmp_double((int)(value_double + 0.5), value_double)) + if (SUCCEED != zbx_double_compare((int)(value_double + 0.5), value_double)) { zbx_snprintf(tmp, sizeof(tmp), ZBX_FS_DBL_EXT(2), value_double); del_zeroes(tmp); @@ -2193,39 +2201,24 @@ int evaluate_macro_function(char *value, const char *host, const char *key, cons { const char *__function_name = "evaluate_macro_function"; - DB_ITEM item; - DB_RESULT result; - DB_ROW row; - char *host_esc, *key_esc; - int ret; - - zabbix_log(LOG_LEVEL_DEBUG, "In %s() function:'%s:%s.%s(%s)'", - __function_name, host, key, function, parameter); + zbx_host_key_t host_key = {host, key}; + DC_ITEM item; + char *error = NULL; + int ret = FAIL, errcode; - host_esc = DBdyn_escape_string(host); - key_esc = DBdyn_escape_string(key); - - result = DBselect( - "select %s" - " where h.host='%s'" - " and h.hostid=i.hostid" - " and i.key_='%s'", - ZBX_SQL_ITEM_SELECT, host_esc, key_esc); + zabbix_log(LOG_LEVEL_DEBUG, "In %s() function:'%s:%s.%s(%s)'", __function_name, host, key, function, parameter); - zbx_free(host_esc); - zbx_free(key_esc); + DCconfig_get_items_by_keys(&item, &host_key, &errcode, 1); - if (NULL == (row = DBfetch(result))) + if (SUCCEED != errcode) { - DBfree_result(result); - zabbix_log(LOG_LEVEL_WARNING, "Function [%s:%s.%s(%s)] not found. Query returned empty result", + zabbix_log(LOG_LEVEL_DEBUG, + "cannot evaluate function \"%s:%s.%s(%s)\": item does not exist", host, key, function, parameter); - return FAIL; + goto out; } - DBget_item_from_db(&item, row); - - if (SUCCEED == (ret = evaluate_function(value, &item, function, parameter, time(NULL)))) + if (SUCCEED == (ret = evaluate_function(value, &item, function, parameter, time(NULL), &error))) { if (SUCCEED == str_in_list("last,prev", function, ',')) { @@ -2245,10 +2238,11 @@ int evaluate_macro_function(char *value, const char *host, const char *key, cons } } - DBfree_result(result); /* cannot call DBfree_result until evaluate_FUNC */ + zbx_free(error); +out: + DCconfig_clean_items(&item, &errcode, 1); - zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s value:'%s'", __function_name, - zbx_result_string(ret), value); + zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s value:'%s'", __function_name, zbx_result_string(ret), value); return ret; } |