zabbix-agent2/src/libs/zbxdbhigh/trigger_dep_linking.c
/**********************************************************************************************************
 *                                                                                                        *
 * Function: prepare_trigger_dependencies_updates_and_deletes                                             *
 *                                                                                                        *
 * Purpose: takes a list of pending trigger dependencies (links) and excludes entries that are            *
 *          already present on the target host to generate a new list (links_processed). Also, prepare    *
 *          the list of the trigger dependencies (trigger_dep_ids_del) that need to be deleted on the     *
 *          target host, since they are not present on the template trigger.                              *
 *                                                                                                        *
 * Parameters: trids               - [IN] vector of trigger identifiers from database                     *
 *             links               - [OUT] pairs of trigger dependencies, list of links_up and links_down *
 *                                         links that we want to be present on the target host            *
 *             links_processed     - [OUT] processed links with entries that are already present excluded *
 *             trigger_dep_ids_del - [OUT] list of triggers dependencies that need to be deleted          *
 *                                                                                                        *
 * Return value: upon successful completion return SUCCEED, or FAIL on DB error                           *
 *                                                                                                        *
 *********************************************************************************************************/
prepare_trigger_dependencies_updates_and_deletes

Purpose: takes a list of pending trigger dependencies (links) and excludes entries that are
already present on the target host to generate a new list (links_processed). Also, prepare
the list of the trigger dependencies (trigger_dep_ids_del) that need to be deleted on the
target host, since they are not present on the template trigger.

Parameters: trids               - [IN] vector of trigger identifiers from database
links               - [OUT] pairs of trigger dependencies, list of links_up and links_down
links that we want to be present on the target host
links_processed     - [OUT] processed links with entries that are already present excluded
trigger_dep_ids_del - [OUT] list of triggers dependencies that need to be deleted

Return value: upon successful completion return SUCCEED, or FAIL on DB error
prepare_trigger_dependencies_updates_and_deletes Purpose : takes a list of pending trigger dependencies ( links ) and excludes entries that are already present on the target host to generate a new list ( links_processed ) . Also prepare the list of the trigger dependencies ( trigger_dep_ids_del ) that need to be deleted on the target host since they are not present on the template trigger . Parameters : trids - [ IN ] vector of trigger identifiers from database links - [ OUT ] pairs of trigger dependencies list of links_up and links_down links that we want to be present on the target host links_processed - [ OUT ] processed links with entries that are already present excluded trigger_dep_ids_del - [ OUT ] list of triggers dependencies that need to be deleted Return value : upon successful completion return SUCCEED or FAIL on DB error
static int	prepare_trigger_dependencies_updates_and_deletes(const zbx_vector_uint64_t *trids,
		zbx_vector_uint64_pair_t *links, zbx_vector_uint64_pair_t *links_processed,
		zbx_vector_uint64_t *trigger_dep_ids_del)
{
	char			*sql = NULL;
	int			i, res = SUCCEED;
	size_t			sql_alloc = 256, sql_offset = 0;
	DB_RESULT		result;
	DB_ROW			row;
	zbx_hashset_t		h;
	zbx_hashset_iter_t	iter;
	zbx_trigger_dep_entry_t	*found;
	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
#define	TRIGGER_FUNCS_HASHSET_DEF_SIZE	100
	zbx_hashset_create(&h, TRIGGER_FUNCS_HASHSET_DEF_SIZE, zbx_trigger_dep_entries_hash_func,
			zbx_trigger_dep_entries_compare_func);
#undef TRIGGER_FUNCS_HASHSET_DEF_SIZE
	zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
			"select td.triggerdepid,td.triggerid_down,td.triggerid_up,t.flags"
			" from trigger_depends td,triggers t "
			" where t.triggerid=td.triggerid_down"
			" and");
	DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "td.triggerid_down", trids->values, trids->values_num);
	if (NULL == (result = DBselect("%s", sql)))
	{
		res = FAIL;
		goto clean;
	}
	while (NULL != (row = DBfetch(result)))
	{
		int				flags;
		zbx_uint64_t			trigger_dep_id, trigger_id_down, trigger_id_up;
		zbx_trigger_dep_entry_t		temp_t;
		zbx_trigger_dep_vec_entry_t	*s;
		ZBX_STR2UINT64(trigger_dep_id, row[0]);
		ZBX_STR2UINT64(trigger_id_down, row[1]);
		ZBX_STR2UINT64(trigger_id_up, row[2]);
		flags = atoi(row[3]);
		temp_t.trigger_down_id = trigger_id_down;
		s = (zbx_trigger_dep_vec_entry_t *)zbx_malloc(NULL, sizeof(zbx_trigger_dep_vec_entry_t));
		s->trigger_dep_id = trigger_dep_id;
		s->trigger_down_id = trigger_id_down;
		s->trigger_up_id = trigger_id_up;
		s->status = 0;
		s->flags = flags;
		if (NULL != (found = (zbx_trigger_dep_entry_t *)zbx_hashset_search(&h, &temp_t)))
		{
			zbx_vector_trigger_up_entries_append(&(found->v), s);
		}
		else
		{
			zbx_trigger_dep_entry_t local_temp_t;
			zbx_vector_trigger_up_entries_create(&(local_temp_t.v));
			zbx_vector_trigger_up_entries_append(&(local_temp_t.v), s);
			local_temp_t.trigger_down_id = trigger_id_down;
			zbx_hashset_insert(&h, &local_temp_t, sizeof(local_temp_t));
		}
	}
	DBfree_result(result);
	for (i = 0; i < links->values_num; i++)
	{
		zbx_trigger_dep_entry_t	temp_t;
		temp_t.trigger_down_id = links->values[i].first;
		if (NULL != (found = (zbx_trigger_dep_entry_t *) zbx_hashset_search(&h, &temp_t)))
		{
			int	j;
			int	found_trigger_up = 0;
			for (j = 0; j < found->v.values_num; j++)
			{
				if (links->values[i].second == found->v.values[j]->trigger_up_id)
				{
					found_trigger_up = 1;
					found->v.values[j]->status = 1;
					break;
				}
			}
			if (0 == found_trigger_up)
			{
				zbx_uint64_pair_t	x;
				x.first = links->values[i].first;
				x.second = links->values[i].second;
				zbx_vector_uint64_pair_append(links_processed, x);
			}
		}
		else
		{
			zbx_uint64_pair_t	x;
			x.first = links->values[i].first;
			x.second = links->values[i].second;
			zbx_vector_uint64_pair_append(links_processed, x);
		}
	}
	zbx_hashset_iter_reset(&h, &iter);
	while (NULL != (found = (zbx_trigger_dep_entry_t *)zbx_hashset_iter_next(&iter)))
	{
		for (i = 0; i < found->v.values_num; i++)
		{
			if (0 == found->v.values[i]->status)
			{
				zbx_vector_uint64_append(trigger_dep_ids_del,
						found->v.values[i]->trigger_dep_id);
				zbx_audit_trigger_update_json_remove_dependency(found->v.values[i]->flags,
						found->v.values[i]->trigger_dep_id,
						found->v.values[i]->trigger_down_id);
			}
		}
	}
clean:
	zbx_triggers_dep_entries_clean(&h);
	zbx_free(sql);
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(res));
	return res;
}
static int prepare_trigger_dependencies_updates_and_deletes ( const zbx_vector_uint64_t * trids , zbx_vector_uint64_pair_t * links , zbx_vector_uint64_pair_t * links_processed , zbx_vector_uint64_t * trigger_dep_ids_del ) { char * sql = NULL ; int i , res = SUCCEED ; size_t sql_alloc = 256 , sql_offset = 0 ; DB_RESULT result ; DB_ROW row ; zbx_hashset_t h ; zbx_hashset_iter_t iter ; zbx_trigger_dep_entry_t * found ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ ) ; #define TRIGGER_FUNCS_HASHSET_DEF_SIZE 	100
 zbx_hashset_create ( & h , TRIGGER_FUNCS_HASHSET_DEF_SIZE , zbx_trigger_dep_entries_hash_func , zbx_trigger_dep_entries_compare_func ) ; #undef  TRIGGER_FUNCS_HASHSET_DEF_SIZE
 zbx_snprintf_alloc ( & sql , & sql_alloc , & sql_offset , " " " " " " " " ) ; DBadd_condition_alloc ( & sql , & sql_alloc , & sql_offset , " " , trids -> values , trids -> values_num ) ; if ( NULL == ( result = DBselect ( " " , sql ) ) ) { res = FAIL ; goto clean ; } while ( NULL != ( row = DBfetch ( result ) ) ) { int flags ; zbx_uint64_t trigger_dep_id , trigger_id_down , trigger_id_up ; zbx_trigger_dep_entry_t temp_t ; zbx_trigger_dep_vec_entry_t * s ; ZBX_STR2UINT64 ( trigger_dep_id , row [ 0 ] ) ; ZBX_STR2UINT64 ( trigger_id_down , row [ 1 ] ) ; ZBX_STR2UINT64 ( trigger_id_up , row [ 2 ] ) ; flags = atoi ( row [ 3 ] ) ; temp_t . trigger_down_id = trigger_id_down ; s = ( zbx_trigger_dep_vec_entry_t * ) zbx_malloc ( NULL , sizeof ( zbx_trigger_dep_vec_entry_t ) ) ; s -> trigger_dep_id = trigger_dep_id ; s -> trigger_down_id = trigger_id_down ; s -> trigger_up_id = trigger_id_up ; s -> status = 0 ; s -> flags = flags ; if ( NULL != ( found = ( zbx_trigger_dep_entry_t * ) zbx_hashset_search ( & h , & temp_t ) ) ) { zbx_vector_trigger_up_entries_append ( & ( found -> v ) , s ) ; } else { zbx_trigger_dep_entry_t local_temp_t ; zbx_vector_trigger_up_entries_create ( & ( local_temp_t . v ) ) ; zbx_vector_trigger_up_entries_append ( & ( local_temp_t . v ) , s ) ; local_temp_t . trigger_down_id = trigger_id_down ; zbx_hashset_insert ( & h , & local_temp_t , sizeof ( local_temp_t ) ) ; } } DBfree_result ( result ) ; for ( i = 0 ; i < links -> values_num ; i ++ ) { zbx_trigger_dep_entry_t temp_t ; temp_t . trigger_down_id = links -> values [ i ] . first ; if ( NULL != ( found = ( zbx_trigger_dep_entry_t * ) zbx_hashset_search ( & h , & temp_t ) ) ) { int j ; int found_trigger_up = 0 ; for ( j = 0 ; j < found -> v . values_num ; j ++ ) { if ( links -> values [ i ] . second == found -> v . values [ j ] -> trigger_up_id ) { found_trigger_up = 1 ; found -> v . values [ j ] -> status = 1 ; break ; } } if ( 0 == found_trigger_up ) { zbx_uint64_pair_t x ; x . first = links -> values [ i ] . first ; x . second = links -> values [ i ] . second ; zbx_vector_uint64_pair_append ( links_processed , x ) ; } } else { zbx_uint64_pair_t x ; x . first = links -> values [ i ] . first ; x . second = links -> values [ i ] . second ; zbx_vector_uint64_pair_append ( links_processed , x ) ; } } zbx_hashset_iter_reset ( & h , & iter ) ; while ( NULL != ( found = ( zbx_trigger_dep_entry_t * ) zbx_hashset_iter_next ( & iter ) ) ) { for ( i = 0 ; i < found -> v . values_num ; i ++ ) { if ( 0 == found -> v . values [ i ] -> status ) { zbx_vector_uint64_append ( trigger_dep_ids_del , found -> v . values [ i ] -> trigger_dep_id ) ; zbx_audit_trigger_update_json_remove_dependency ( found -> v . values [ i ] -> flags , found -> v . values [ i ] -> trigger_dep_id , found -> v . values [ i ] -> trigger_down_id ) ; } } } clean : zbx_triggers_dep_entries_clean ( & h ) ; zbx_free ( sql ) ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ , zbx_result_string ( res ) ) ; return res ; }
Function: prepare_trigger_dependencies_updates_and_deletes                                             
                                                                                                        
 Purpose: takes a list of pending trigger dependencies (links) and excludes entries that are            
          already present on the target host to generate a new list (links_processed).
Function : prepare_trigger_dependencies_updates_and_deletes Purpose : takes a list of pending trigger dependencies ( links ) and excludes entries that are already present on the target host to generate a new list ( links_processed ) .
/* trigger_up are equal */ /* mark it as 'need to preserve' */