/****************************************************************************** * * * Function: procstat_get_monitored_pids * * * * Purpose: creates a list of unique pids that are monitored by current data * * gathering cycle * * * * Parameters: pids - [OUT] a sorted vector of unique pids * * queries - [IN] local, working copy of queries * * pids_num - [IN] the total number of pids monitored by queries * * (might contain duplicated pids) * * * ******************************************************************************/ static void procstat_get_monitored_pids(zbx_vector_uint64_t *pids, const zbx_vector_ptr_t *queries, int pids_num) { zbx_procstat_query_data_t *qdata; int i; zbx_vector_uint64_reserve(pids, pids_num); for (i = 0; i < queries->values_num; i++) { qdata = (zbx_procstat_query_data_t *)queries->values[i]; if (SUCCEED != qdata->error) continue; memcpy(pids->values + pids->values_num, qdata->pids.values, sizeof(zbx_uint64_t) * qdata->pids.values_num); pids->values_num += qdata->pids.values_num; } zbx_vector_uint64_sort(pids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_vector_uint64_uniq(pids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); }
/****************************************************************************** * * * Function: db_get_query_functions * * * * Purpose: get event query functionids from database * * * ******************************************************************************/ static void db_get_query_functions(zbx_vector_ptr_t *event_queries) { DB_ROW row; DB_RESULT result; int i; zbx_vector_uint64_t triggerids; zbx_hashset_t triggers; zbx_hashset_iter_t iter; char *sql = NULL; size_t sql_alloc = 0, sql_offset = 0; zbx_trigger_functions_t *trigger = NULL, trigger_local; zbx_uint64_t triggerid, functionid; zbx_event_suppress_query_t *query; /* cache functionids by triggerids */ zbx_hashset_create(&triggers, 100, ZBX_DEFAULT_UINT64_HASH_FUNC, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_vector_uint64_create(&triggerids); for (i = 0; i < event_queries->values_num; i++) { query = (zbx_event_suppress_query_t *)event_queries->values[i]; zbx_vector_uint64_append(&triggerids, query->triggerid); } zbx_vector_uint64_sort(&triggerids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_vector_uint64_uniq(&triggerids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "select functionid,triggerid from functions where"); DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "triggerid", triggerids.values, triggerids.values_num); zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, " order by triggerid"); result = DBselect("%s", sql); zbx_free(sql); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(functionid, row[0]); ZBX_STR2UINT64(triggerid, row[1]); if (NULL == trigger || trigger->triggerid != triggerid) { trigger_local.triggerid = triggerid; trigger = (zbx_trigger_functions_t *)zbx_hashset_insert(&triggers, &trigger_local, sizeof(trigger_local)); zbx_vector_uint64_create(&trigger->functionids); } zbx_vector_uint64_append(&trigger->functionids, functionid); } DBfree_result(result); /* copy functionids to event queries */ for (i = 0; i < event_queries->values_num; i++) { query = (zbx_event_suppress_query_t *)event_queries->values[i]; if (NULL == (trigger = (zbx_trigger_functions_t *)zbx_hashset_search(&triggers, &query->triggerid))) continue; zbx_vector_uint64_append_array(&query->functionids, trigger->functionids.values, trigger->functionids.values_num); } zbx_hashset_iter_reset(&triggers, &iter); while (NULL != (trigger = (zbx_trigger_functions_t *)zbx_hashset_iter_next(&iter))) zbx_vector_uint64_destroy(&trigger->functionids); zbx_hashset_destroy(&triggers); zbx_vector_uint64_destroy(&triggerids); }
/****************************************************************************** * * * Function: process_record * * * * Purpose: process record update * * * * Parameters: * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int process_record(char **sql, size_t *sql_alloc, size_t *sql_offset, int sender_nodeid, int nodeid, const ZBX_TABLE *table, const char *record, int lastrecord, int acknowledges, zbx_vector_uint64_t *ack_eventids) { const char *r; int f, res = FAIL; char *value_esc; zabbix_log(LOG_LEVEL_DEBUG, "In process_record()"); if (0 == *sql_offset) { DBbegin_multiple_update(sql, sql_alloc, sql_offset); #ifdef HAVE_MULTIROW_INSERT begin_history_sql(sql, sql_alloc, sql_offset, table); #endif } #if !defined(HAVE_MULTIROW_INSERT) begin_history_sql(sql, sql_alloc, sql_offset, table); #endif zbx_chrcpy_alloc(sql, sql_alloc, sql_offset, '('); if (0 != (table->flags & ZBX_HISTORY_SYNC)) zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%d,", nodeid); for (r = record, f = 0; table->fields[f].name != 0; f++) { if (0 != (table->flags & ZBX_HISTORY_SYNC) && 0 == (table->fields[f].flags & ZBX_HISTORY_SYNC)) continue; if (NULL == r) goto error; zbx_get_next_field(&r, &buffer, &buffer_alloc, ZBX_DM_DELIMITER); if (0 != acknowledges && 0 == strcmp(table->fields[f].name, "eventid")) { zbx_uint64_t eventid; ZBX_STR2UINT64(eventid, buffer); zbx_vector_uint64_append(ack_eventids, eventid); } if (table->fields[f].type == ZBX_TYPE_INT || table->fields[f].type == ZBX_TYPE_UINT || table->fields[f].type == ZBX_TYPE_ID || table->fields[f].type == ZBX_TYPE_FLOAT) { zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s,", buffer); } else if (table->fields[f].type == ZBX_TYPE_BLOB) { if ('\0' == *buffer) zbx_strcpy_alloc(sql, sql_alloc, sql_offset, "'',"); else { #ifdef HAVE_POSTGRESQL size_t len; len = zbx_hex2binary(buffer); zbx_pg_escape_bytea((u_char *)buffer, len, &tmp, &tmp_alloc); zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "'%s',", tmp); #else zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "0x%s,", buffer); #endif } } else /* ZBX_TYPE_TEXT, ZBX_TYPE_CHAR */ { zbx_hex2binary(buffer); value_esc = DBdyn_escape_string(buffer); zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "'%s',", value_esc); zbx_free(value_esc); } } (*sql_offset)--; #ifdef HAVE_MULTIROW_INSERT zbx_strcpy_alloc(sql, sql_alloc, sql_offset, "),"); #else zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ");\n"); #endif if (0 != lastrecord || *sql_offset > ZBX_MAX_SQL_SIZE) { #ifdef HAVE_MULTIROW_INSERT (*sql_offset)--; zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ";\n"); #endif DBend_multiple_update(sql, sql_alloc, sql_offset); if (ZBX_DB_OK <= DBexecute("%s", *sql)) res = SUCCEED; *sql_offset = 0; if (SUCCEED == res && 0 != lastrecord && 0 != acknowledges && 0 != ack_eventids->values_num) { zbx_vector_uint64_sort(ack_eventids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_vector_uint64_uniq(ack_eventids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_strcpy_alloc(sql, sql_alloc, sql_offset, "update events" " set acknowledged=1" " where"); DBadd_condition_alloc(sql, sql_alloc, sql_offset, "eventid", ack_eventids->values, ack_eventids->values_num); if (ZBX_DB_OK > DBexecute("%s", *sql)) res = FAIL; *sql_offset = 0; } } else res = SUCCEED; return res; error: zabbix_log(LOG_LEVEL_ERR, "NODE %d: Received invalid record from node %d for node %d [%s]", CONFIG_NODEID, sender_nodeid, nodeid, record); return FAIL; }
/****************************************************************************** * * * Function: db_get_query_events * * * * Purpose: get open, recently resolved and resolved problems with suppress * * data from database and prepare event query, event data structures * * * ******************************************************************************/ static void db_get_query_events(zbx_vector_ptr_t *event_queries, zbx_vector_ptr_t *event_data) { DB_ROW row; DB_RESULT result; zbx_event_suppress_query_t *query; zbx_event_suppress_data_t *data = NULL; zbx_uint64_t eventid; zbx_uint64_pair_t pair; zbx_vector_uint64_t eventids; /* get open or recently closed problems */ result = DBselect("select eventid,objectid,r_eventid" " from problem" " where source=%d" " and object=%d" " and " ZBX_SQL_MOD(eventid, %d) "=%d" " order by eventid", EVENT_SOURCE_TRIGGERS, EVENT_OBJECT_TRIGGER, CONFIG_TIMER_FORKS, process_num - 1); while (NULL != (row = DBfetch(result))) { query = (zbx_event_suppress_query_t *)zbx_malloc(NULL, sizeof(zbx_event_suppress_query_t)); ZBX_STR2UINT64(query->eventid, row[0]); ZBX_STR2UINT64(query->triggerid, row[1]); ZBX_DBROW2UINT64(query->r_eventid, row[2]); zbx_vector_uint64_create(&query->functionids); zbx_vector_ptr_create(&query->tags); zbx_vector_uint64_pair_create(&query->maintenances); zbx_vector_ptr_append(event_queries, query); } DBfree_result(result); /* get event suppress data */ zbx_vector_uint64_create(&eventids); result = DBselect("select eventid,maintenanceid,suppress_until" " from event_suppress" " where " ZBX_SQL_MOD(eventid, %d) "=%d" " order by eventid", CONFIG_TIMER_FORKS, process_num - 1); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(eventid, row[0]); if (FAIL == zbx_vector_ptr_bsearch(event_queries, &eventid, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC)) zbx_vector_uint64_append(&eventids, eventid); if (NULL == data || data->eventid != eventid) { data = (zbx_event_suppress_data_t *)zbx_malloc(NULL, sizeof(zbx_event_suppress_data_t)); data->eventid = eventid; zbx_vector_uint64_pair_create(&data->maintenances); zbx_vector_ptr_append(event_data, data); } ZBX_DBROW2UINT64(pair.first, row[1]); pair.second = atoi(row[2]); zbx_vector_uint64_pair_append(&data->maintenances, pair); } DBfree_result(result); /* get missing event data */ if (0 != eventids.values_num) { char *sql = NULL; size_t sql_alloc = 0, sql_offset = 0; zbx_vector_uint64_uniq(&eventids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "select e.eventid,e.objectid,er.r_eventid" " from events e" " left join event_recovery er" " on e.eventid=er.eventid" " where"); DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "e.eventid", eventids.values, eventids.values_num); result = DBselect("%s", sql); zbx_free(sql); while (NULL != (row = DBfetch(result))) { query = (zbx_event_suppress_query_t *)zbx_malloc(NULL, sizeof(zbx_event_suppress_query_t)); ZBX_STR2UINT64(query->eventid, row[0]); ZBX_STR2UINT64(query->triggerid, row[1]); ZBX_DBROW2UINT64(query->r_eventid, row[2]); zbx_vector_uint64_create(&query->functionids); zbx_vector_ptr_create(&query->tags); zbx_vector_uint64_pair_create(&query->maintenances); zbx_vector_ptr_append(event_queries, query); } DBfree_result(result); zbx_vector_ptr_sort(event_queries, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); } zbx_vector_uint64_destroy(&eventids); }