diff options
Diffstat (limited to 'src/libs/zbxdbhigh/lld.c')
-rw-r--r-- | src/libs/zbxdbhigh/lld.c | 70 |
1 files changed, 20 insertions, 50 deletions
diff --git a/src/libs/zbxdbhigh/lld.c b/src/libs/zbxdbhigh/lld.c index 62f0de3e201..1374f1ac113 100644 --- a/src/libs/zbxdbhigh/lld.c +++ b/src/libs/zbxdbhigh/lld.c @@ -75,8 +75,8 @@ static void lld_condition_free(lld_condition_t *condition) ******************************************************************************/ static int lld_condition_compare_by_macro(const void *item1, const void *item2) { - lld_condition_t *condition1 = *(lld_condition_t**)item1; - lld_condition_t *condition2 = *(lld_condition_t**)item2; + lld_condition_t *condition1 = *(lld_condition_t **)item1; + lld_condition_t *condition2 = *(lld_condition_t **)item2; return strcmp(condition1->macro, condition2->macro); } @@ -121,20 +121,10 @@ static void lld_filter_clean(lld_filter_t *filter) * * * Parameters: filter - [IN] the lld filter * * lld_ruleid - [IN] the lld rule id * - * evaltype - [IN] the evaluation type, see * - * CONDITION_EVAL_TYPE_* macros * - * formula - [IN] the lld filter formula * * error - [OUT] the error description * * * - * Return value: SUCCEED - the filter was loaded successfully * - * FAIL - otherwise * - * * - * Comments: In the case of failure the error description is allocated by * - * this function and must be freed by the caller. * - * * ******************************************************************************/ -static int lld_filter_load(lld_filter_t *filter, zbx_uint64_t lld_ruleid, int evaltype, - const char *formula, char **error) +static int lld_filter_load(lld_filter_t *filter, zbx_uint64_t lld_ruleid, char **error) { DB_RESULT result; DB_ROW row; @@ -180,17 +170,9 @@ static int lld_filter_load(lld_filter_t *filter, zbx_uint64_t lld_ruleid, int ev } DBfree_result(result); - if (CONDITION_EVAL_TYPE_AND_OR == evaltype) + if (CONDITION_EVAL_TYPE_AND_OR == filter->evaltype) zbx_vector_ptr_sort(&filter->conditions, lld_condition_compare_by_macro); - if (CONDITION_EVAL_TYPE_EXPRESSION == evaltype && - FAIL == translate_expression(formula, &filter->expression, error)) - { - goto out; - } - - filter->evaltype = evaltype; - ret = SUCCEED; out: DCconfig_clean_items(&item, &errcode, 1); @@ -344,16 +326,15 @@ static int filter_evaluate_or(lld_filter_t *filter, struct zbx_json_parse *jp_ro * Purpose: check if the lld data passes filter evaluation by custom * * expression * * * - * Parameters: filter - [IN] the lld filter * - * jp_row - [IN] the lld data row * + * Parameters: filter - [IN] the lld filter * + * jp_row - [IN] the lld data row * * * * Return value: SUCCEED - the lld data passed filter evaluation * * FAIL - otherwise * * * * Comments: 1) replace {item_condition} references with action condition * * evaluation results (1 or 0) * - * 2) pack the resulting expression by removing spaces * - * 3) call evaluate() to calculate the final result * + * 2) call evaluate() to calculate the final result * * * ******************************************************************************/ static int filter_evaluate_expression(lld_filter_t *filter, struct zbx_json_parse *jp_row) @@ -361,7 +342,7 @@ static int filter_evaluate_expression(lld_filter_t *filter, struct zbx_json_pars const char *__function_name = "filter_evaluate_expression"; int i, ret = FAIL, id_len; - char *expression, id[32], *src, *dst, error[256]; + char *expression, id[ZBX_MAX_UINT64_LEN + 2], *p, error[256]; double result; zabbix_log(LOG_LEVEL_DEBUG, "In %s() expression:%s", __function_name, filter->expression); @@ -381,29 +362,19 @@ static int filter_evaluate_expression(lld_filter_t *filter, struct zbx_json_pars zbx_snprintf(id, sizeof(id), "{" ZBX_FS_UI64 "}", condition->id); - src = expression; + id_len = strlen(id); + p = expression; - while (NULL != (src = strstr(src, id))) + while (NULL != (p = strstr(p, id))) { - id_len = strlen(id); - - *src = SUCCEED == ret ? '1' : '0'; - memset(src + 1, ' ', id_len - 1); - src += id_len; + *p = (SUCCEED == ret ? '1' : '0'); + memset(p + 1, ' ', id_len - 1); + p += id_len; } } - src = dst = expression; - - do - { - if (' ' != *src) - *dst++ = *src; - } - while ('\0' != *src++); - if (SUCCEED == evaluate(&result, expression, error, sizeof(error))) - ret = (0 == result) ? FAIL : SUCCEED; + ret = (SUCCEED != zbx_double_compare(result, 0) ? SUCCEED : FAIL); zbx_free(expression); @@ -438,6 +409,7 @@ static int filter_evaluate(lld_filter_t *filter, struct zbx_json_parse *jp_row) case CONDITION_EVAL_TYPE_EXPRESSION: return filter_evaluate_expression(filter, jp_row); } + return FAIL; } @@ -523,14 +495,13 @@ void lld_process_discovery_rule(zbx_uint64_t lld_ruleid, char *value, zbx_timesp DB_RESULT result; DB_ROW row; zbx_uint64_t hostid = 0; - char *discovery_key = NULL, *formula = NULL, *error = NULL, *db_error = NULL, *error_esc; + char *discovery_key = NULL, *error = NULL, *db_error = NULL, *error_esc; unsigned char state = 0; unsigned short lifetime; zbx_vector_ptr_t lld_rows; char *sql = NULL; size_t sql_alloc = 128, sql_offset = 0; const char *sql_start = "update items set ", *sql_continue = ","; - int evaltype; lld_filter_t filter; zabbix_log(LOG_LEVEL_DEBUG, "In %s() itemid:" ZBX_FS_UI64, __function_name, lld_ruleid); @@ -554,8 +525,8 @@ void lld_process_discovery_rule(zbx_uint64_t lld_ruleid, char *value, zbx_timesp ZBX_STR2UINT64(hostid, row[0]); discovery_key = zbx_strdup(discovery_key, row[1]); state = (unsigned char)atoi(row[2]); - evaltype = atoi(row[3]); - formula = zbx_strdup(formula, row[4]); + filter.evaltype = atoi(row[3]); + filter.expression = zbx_strdup(NULL, row[4]); db_error = zbx_strdup(db_error, row[5]); lifetime_str = zbx_strdup(NULL, row[6]); @@ -577,7 +548,7 @@ void lld_process_discovery_rule(zbx_uint64_t lld_ruleid, char *value, zbx_timesp if (0 == hostid) goto clean; - if (SUCCEED != lld_filter_load(&filter, lld_ruleid, evaltype, formula, &error)) + if (SUCCEED != lld_filter_load(&filter, lld_ruleid, &error)) goto error; if (SUCCEED != lld_rows_get(value, &filter, &lld_rows, &error)) @@ -626,7 +597,6 @@ error: clean: zbx_free(error); zbx_free(db_error); - zbx_free(formula); zbx_free(discovery_key); zbx_free(sql); |