/****************************************************************************** * * * Function: zbx_translate_message_params * * * * Purpose: translates message by replacing parameters %%<id> with translated * * values * * * * Parameters: message - [IN/OUT] the message to translate * * hLib - [IN] the parameter message file handle * * * ******************************************************************************/ static void zbx_translate_message_params(char **message, HINSTANCE hLib) { char *param, *pstart, *pend; int dwMessageId; size_t offset = 0; while (1) { if (NULL == (pstart = strstr(*message + offset, "%%"))) break; pend = pstart + 2; dwMessageId = atoi(pend); while ('\0' != *pend && 0 != isdigit(*pend)) pend++; offset = pend - *message - 1; if (NULL != (param = zbx_format_message(hLib, dwMessageId, NULL))) { zbx_replace_string(message, pstart - *message, &offset, param); zbx_free(param); } } }
/****************************************************************************** * * * Function: update_template_lld_rule_formulas * * * * Purpose: translate template item condition identifiers in expression type * * discovery rule formulas to refer the host item condition * * identifiers instead. * * * * Parameters: items - [IN] the template items * * rules - [IN] the ldd rule mapping * * * ******************************************************************************/ static void update_template_lld_rule_formulas(zbx_vector_ptr_t *items, zbx_vector_ptr_t *rules) { zbx_lld_rule_map_t *rule; int i, j, index; char *formula; zbx_uint64_t conditionid; for (i = 0; i < items->values_num; i++) { zbx_template_item_t *item = items->values[i]; if (0 == (ZBX_FLAG_DISCOVERY_RULE & item->flags) || CONDITION_EVAL_TYPE_EXPRESSION != item->evaltype) continue; index = zbx_vector_ptr_bsearch(rules, &item->templateid, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); if (FAIL == index) { THIS_SHOULD_NEVER_HAPPEN; continue; } rule = rules->values[index]; formula = zbx_strdup(NULL, item->formula); conditionid = rule->conditionid; for (j = 0; j < rule->conditions.values_num; j++) { zbx_uint64_t id; char srcid[64], dstid[64], *ptr; size_t pos = 0, len; zbx_lld_rule_condition_t *condition = rule->conditions.values[j]; if (j < rule->conditionids.values_num) id = rule->conditionids.values[j]; else id = conditionid++; zbx_snprintf(srcid, sizeof(srcid), "{" ZBX_FS_UI64 "}", condition->item_conditionid); zbx_snprintf(dstid, sizeof(dstid), "{" ZBX_FS_UI64 "}", id); len = strlen(srcid); while (NULL != (ptr = strstr(formula + pos, srcid))) { pos = ptr - formula + len - 1; zbx_replace_string(&formula, ptr - formula, &pos, dstid); } } zbx_free(item->formula); item->formula = formula; } }
/****************************************************************************** * * * Function: lld_expression_create * * * * Purpose: transforms the simple trigger expression to the DB format * * * * Example: * * * * "{1} > 5" => "{84756} > 5" * * ^ ^ * * | functionid from the database * * internal function index * * * ******************************************************************************/ static void lld_expression_create(char **expression, zbx_vector_ptr_t *functions) { const char *__function_name = "lld_expression_create"; size_t l, r; int i; zbx_uint64_t function_index; zbx_lld_function_t *function; char buffer[ZBX_MAX_UINT64_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In %s() expression:'%s'", __function_name, *expression); for (l = 0; '\0' != (*expression)[l]; l++) { if ('{' != (*expression)[l]) continue; for (r = l + 1; '\0' != (*expression)[r] && '}' != (*expression)[r]; r++) ; if ('}' != (*expression)[r]) continue; /* ... > 0 | {1} + ... */ /* l r */ if (SUCCEED != is_uint64_n(*expression + l + 1, r - l - 1, &function_index)) continue; for (i = 0; i < functions->values_num; i++) { function = (zbx_lld_function_t *)functions->values[i]; if (function->index != function_index) continue; zbx_snprintf(buffer, sizeof(buffer), ZBX_FS_UI64, function->functionid); r--; zbx_replace_string(expression, l + 1, &r, buffer); r++; break; } l = r; } zabbix_log(LOG_LEVEL_DEBUG, "End of %s() expression:'%s'", __function_name, *expression); }
static void lld_expression_simplify(char **expression, zbx_vector_ptr_t *functions) { const char *__function_name = "lld_expression_simplify"; size_t l, r; int index; zbx_uint64_t functionid, function_index = 0; zbx_lld_function_t *function; char buffer[ZBX_MAX_UINT64_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In %s() expression:'%s'", __function_name, *expression); for (l = 0; '\0' != (*expression)[l]; l++) { if ('{' != (*expression)[l]) continue; for (r = l + 1; '\0' != (*expression)[r] && '}' != (*expression)[r]; r++) ; if ('}' != (*expression)[r]) continue; /* ... > 0 | {12345} + ... */ /* l r */ if (SUCCEED != is_uint64_n(*expression + l + 1, r - l - 1, &functionid)) continue; if (FAIL != (index = zbx_vector_ptr_bsearch(functions, &functionid, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC))) { function = (zbx_lld_function_t *)functions->values[index]; if (0 == function->index) function->index = ++function_index; zbx_snprintf(buffer, sizeof(buffer), ZBX_FS_UI64, function->index); r--; zbx_replace_string(expression, l + 1, &r, buffer); r++; } l = r; } zabbix_log(LOG_LEVEL_DEBUG, "End of %s() expression:'%s'", __function_name, *expression); }
/****************************************************************************** * * * Function: http_substitute_macros * * * * Purpose: substitute macros in input string by value from http test config * * * * Parameters: macros - [IN] macros from httptest * * data - [IN\OUT] string to substitute macros * * * * Author: Alexei Vladishev * * * ******************************************************************************/ void http_substitute_macros(const char *macros, char **data) { const char *__function_name = "http_substitute_macros"; char c, *replace_to = NULL; size_t l, r, replace_to_alloc = 64; int rc; zabbix_log(LOG_LEVEL_DEBUG, "In %s() data:'%s'", __function_name, *data); for (l = 0; '\0' != (*data)[l]; l++) { if ('{' != (*data)[l]) continue; for (r = l + 1; '\0' != (*data)[r] && '}' != (*data)[r]; r++) ; if ('}' != (*data)[r]) break; if (NULL == replace_to) replace_to = zbx_malloc(replace_to, replace_to_alloc); c = (*data)[r + 1]; (*data)[r + 1] = '\0'; rc = http_get_macro_value(macros, &(*data)[l], &replace_to, &replace_to_alloc); (*data)[r + 1] = c; if (SUCCEED != rc) continue; zbx_replace_string(data, l, &r, replace_to); l = r; } zbx_free(replace_to); zabbix_log(LOG_LEVEL_DEBUG, "End of %s() data:'%s'", __function_name, *data); }
/****************************************************************************** * * * Function: http_substitute_variables * * * * Purpose: substitute variables in input string with their values from http * * test config * * * * Parameters: httptest - [IN] the http test data * * data - [IN/OUT] string to substitute macros in * * * * Author: Alexei Vladishev, Andris Zeila * * * ******************************************************************************/ void http_substitute_variables(zbx_httptest_t *httptest, char **data) { const char *__function_name = "http_substitute_variables"; char replace_char; size_t left, right; int index; zbx_ptr_pair_t pair; zabbix_log(LOG_LEVEL_DEBUG, "In %s() data:'%s'", __function_name, *data); for (left = 0; '\0' != (*data)[left]; left++) { if ('{' != (*data)[left]) continue; for (right = left + 1; '\0' != (*data)[right] && '}' != (*data)[right]; right++) ; if ('}' != (*data)[right]) break; replace_char = (*data)[right + 1]; (*data)[right + 1] = '\0'; pair.first = *data + left; index = zbx_vector_ptr_pair_search(&httptest->macros, pair, httpmacro_cmp_func); (*data)[right + 1] = replace_char; if (FAIL == index) continue; zbx_replace_string(data, left, &right, (char*)httptest->macros.values[index].second); left = right; } zabbix_log(LOG_LEVEL_DEBUG, "End of %s() data:'%s'", __function_name, *data); }