static int lld_rows_get(char *value, char *filter, zbx_vector_ptr_t *lld_rows, char **error) { const char *__function_name = "lld_rows_get"; struct zbx_json_parse jp, jp_data, jp_row; char *f_macro = NULL, *f_regexp = NULL; const char *p; zbx_vector_ptr_t regexps; zbx_lld_row_t *lld_row; int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (SUCCEED != zbx_json_open(value, &jp)) { *error = zbx_strdup(*error, "Value should be a JSON object."); goto out; } /* {"data":[{"{#IFNAME}":"eth0"},{"{#IFNAME}":"lo"},...]} */ /* ^-------------------------------------------^ */ if (SUCCEED != zbx_json_brackets_by_name(&jp, ZBX_PROTO_TAG_DATA, &jp_data)) { *error = zbx_dsprintf(*error, "Cannot find the \"%s\" array in the received JSON object.", ZBX_PROTO_TAG_DATA); goto out; } zbx_vector_ptr_create(®exps); if (NULL != (f_regexp = strchr(filter, ':'))) { f_macro = filter; *f_regexp++ = '\0'; if ('@' == *f_regexp) DCget_expressions_by_name(®exps, f_regexp + 1); zabbix_log(LOG_LEVEL_DEBUG, "%s() f_macro:'%s' f_regexp:'%s'", __function_name, f_macro, f_regexp); } p = NULL; /* {"data":[{"{#IFNAME}":"eth0"},{"{#IFNAME}":"lo"},...]} */ /* ^ */ while (NULL != (p = zbx_json_next(&jp_data, p))) { /* {"data":[{"{#IFNAME}":"eth0"},{"{#IFNAME}":"lo"},...]} */ /* ^------------------^ */ if (FAIL == zbx_json_brackets_open(p, &jp_row)) continue; if (NULL != f_macro && SUCCEED != lld_check_record(&jp_row, f_macro, f_regexp, ®exps)) continue; lld_row = zbx_malloc(NULL, sizeof(zbx_lld_row_t)); memcpy(&lld_row->jp_row, &jp_row, sizeof(struct zbx_json_parse)); zbx_vector_ptr_create(&lld_row->item_links); zbx_vector_ptr_append(lld_rows, lld_row); } zbx_regexp_clean_expressions(®exps); zbx_vector_ptr_destroy(®exps); ret = SUCCEED; out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: process_trap_for_interface * * * * Purpose: add trap to all matching items for the specified interface * * * * Return value: SUCCEED - a matching item was found * * FAIL - no matching item was found (including fallback items) * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ static int process_trap_for_interface(zbx_uint64_t interfaceid, char *trap, zbx_timespec_t *ts) { DC_ITEM *items = NULL; const char *regex; char error[ITEM_ERROR_LEN_MAX]; size_t num, i; int ret = FAIL, fb = -1, *lastclocks = NULL, *errcodes = NULL; zbx_uint64_t *itemids = NULL; unsigned char *states = NULL; AGENT_RESULT *results = NULL; AGENT_REQUEST request; zbx_vector_ptr_t regexps; zbx_vector_ptr_create(®exps); num = DCconfig_get_snmp_items_by_interfaceid(interfaceid, &items); itemids = zbx_malloc(itemids, sizeof(zbx_uint64_t) * num); states = zbx_malloc(states, sizeof(unsigned char) * num); lastclocks = zbx_malloc(lastclocks, sizeof(int) * num); errcodes = zbx_malloc(errcodes, sizeof(int) * num); results = zbx_malloc(results, sizeof(AGENT_RESULT) * num); for (i = 0; i < num; i++) { init_result(&results[i]); errcodes[i] = FAIL; items[i].key = zbx_strdup(items[i].key, items[i].key_orig); if (SUCCEED != substitute_key_macros(&items[i].key, NULL, &items[i], NULL, MACRO_TYPE_ITEM_KEY, error, sizeof(error))) { SET_MSG_RESULT(&results[i], zbx_strdup(NULL, error)); errcodes[i] = NOTSUPPORTED; continue; } if (0 == strcmp(items[i].key, "snmptrap.fallback")) { fb = i; continue; } init_request(&request); if (SUCCEED != parse_item_key(items[i].key, &request)) goto next; if (0 != strcmp(get_rkey(&request), "snmptrap")) goto next; if (1 < get_rparams_num(&request)) goto next; if (NULL != (regex = get_rparam(&request, 0))) { if ('@' == *regex) { DCget_expressions_by_name(®exps, regex + 1); if (0 == regexps.values_num) { SET_MSG_RESULT(&results[i], zbx_dsprintf(NULL, "Global regular expression \"%s\" does not exist.", regex + 1)); errcodes[i] = NOTSUPPORTED; goto next; } } if (SUCCEED != regexp_match_ex(®exps, trap, regex, ZBX_CASE_SENSITIVE)) goto next; } if (SUCCEED == set_result_type(&results[i], items[i].value_type, items[i].data_type, trap)) errcodes[i] = SUCCEED; else errcodes[i] = NOTSUPPORTED; ret = SUCCEED; next: free_request(&request); } if (FAIL == ret && -1 != fb) { if (SUCCEED == set_result_type(&results[fb], items[fb].value_type, items[fb].data_type, trap)) errcodes[fb] = SUCCEED; else errcodes[fb] = NOTSUPPORTED; ret = SUCCEED; } for (i = 0; i < num; i++) { switch (errcodes[i]) { case SUCCEED: if (ITEM_VALUE_TYPE_LOG == items[i].value_type) { calc_timestamp(results[i].log->value, &results[i].log->timestamp, items[i].logtimefmt); } items[i].state = ITEM_STATE_NORMAL; dc_add_history(items[i].itemid, items[i].value_type, items[i].flags, &results[i], ts, items[i].state, NULL); itemids[i] = items[i].itemid; states[i] = items[i].state; lastclocks[i] = ts->sec; break; case NOTSUPPORTED: items[i].state = ITEM_STATE_NOTSUPPORTED; dc_add_history(items[i].itemid, items[i].value_type, items[i].flags, NULL, ts, items[i].state, results[i].msg); itemids[i] = items[i].itemid; states[i] = items[i].state; lastclocks[i] = ts->sec; break; } zbx_free(items[i].key); free_result(&results[i]); } zbx_free(results); DCrequeue_items(itemids, states, lastclocks, NULL, NULL, errcodes, num); zbx_free(errcodes); zbx_free(lastclocks); zbx_free(states); zbx_free(itemids); DCconfig_clean_items(items, NULL, num); zbx_free(items); zbx_regexp_clean_expressions(®exps); zbx_vector_ptr_destroy(®exps); dc_flush_history(); return ret; }
/****************************************************************************** * * * Function: lld_filter_load * * * * Purpose: loads lld filter data * * * * Parameters: filter - [IN] the lld filter * * lld_ruleid - [IN] the lld rule id * * error - [OUT] the error description * * * ******************************************************************************/ static int lld_filter_load(lld_filter_t *filter, zbx_uint64_t lld_ruleid, char **error) { DB_RESULT result; DB_ROW row; lld_condition_t *condition; DC_ITEM item; int errcode, ret = SUCCEED; DCconfig_get_items_by_itemids(&item, &lld_ruleid, &errcode, 1); if (SUCCEED != errcode) { *error = zbx_dsprintf(*error, "Invalid discovery rule ID [" ZBX_FS_UI64 "].", lld_ruleid); ret = FAIL; goto out; } result = DBselect( "select item_conditionid,macro,value" " from item_condition" " where itemid=" ZBX_FS_UI64, lld_ruleid); while (NULL != (row = DBfetch(result))) { condition = zbx_malloc(NULL, sizeof(lld_condition_t)); ZBX_STR2UINT64(condition->id, row[0]); condition->macro = zbx_strdup(NULL, row[1]); condition->regexp = zbx_strdup(NULL, row[2]); zbx_vector_ptr_create(&condition->regexps); zbx_vector_ptr_append(&filter->conditions, condition); if ('@' == *condition->regexp) { DCget_expressions_by_name(&condition->regexps, condition->regexp + 1); if (0 == condition->regexps.values_num) { *error = zbx_dsprintf(*error, "Global regular expression \"%s\" does not exist.", condition->regexp + 1); ret = FAIL; break; } } else { substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item, NULL, &condition->regexp, MACRO_TYPE_LLD_FILTER, NULL, 0); } } DBfree_result(result); if (SUCCEED != ret) lld_conditions_free(&filter->conditions); else if (CONDITION_EVAL_TYPE_AND_OR == filter->evaltype) zbx_vector_ptr_sort(&filter->conditions, lld_condition_compare_by_macro); out: DCconfig_clean_items(&item, &errcode, 1); return ret; }