zabbix-agent2/src/libs/zbxdbcache/dbsync.c
/******************************************************************************
 *                                                                            *
 * Function: zbx_dbsync_compare_trigger_dependency                            *
 *                                                                            *
 * Purpose: compares trigger_depends table with cached configuration data     *
 *                                                                            *
 * Parameter: sync - [OUT] the changeset                                      *
 *                                                                            *
 * Return value: SUCCEED - the changeset was successfully calculated          *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 ******************************************************************************/
zbx_dbsync_compare_trigger_dependency

Purpose: compares trigger_depends table with cached configuration data

Parameter: sync - [OUT] the changeset

Return value: SUCCEED - the changeset was successfully calculated
FAIL    - otherwise
zbx_dbsync_compare_trigger_dependency Purpose : compares trigger_depends table with cached configuration data Parameter : sync - [ OUT ] the changeset Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise
int	zbx_dbsync_compare_trigger_dependency(zbx_dbsync_t *sync)
{
	DB_ROW			dbrow;
	DB_RESULT		result;
	zbx_hashset_t		deps;
	zbx_hashset_iter_t	iter;
	ZBX_DC_TRIGGER_DEPLIST	*dep_down, *dep_up;
	zbx_uint64_pair_t	*dep, dep_local;
	char			down_s[MAX_ID_LEN + 1], up_s[MAX_ID_LEN + 1];
	char			*del_row[2] = {down_s, up_s};
	int			i;
	if (NULL == (result = DBselect(
			"select distinct d.triggerid_down,d.triggerid_up"
			" from trigger_depends d,triggers t,hosts h,items i,functions f"
			" where t.triggerid=d.triggerid_down"
				" and t.flags<>%d"
				" and h.hostid=i.hostid"
				" and i.itemid=f.itemid"
				" and f.triggerid=d.triggerid_down"
				" and h.status in (%d,%d)",
				ZBX_FLAG_DISCOVERY_PROTOTYPE, HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED)))
	{
		return FAIL;
	}
	dbsync_prepare(sync, 2, NULL);
	if (ZBX_DBSYNC_INIT == sync->mode)
	{
		sync->dbresult = result;
		return SUCCEED;
	}
	zbx_hashset_create(&deps, 100, ZBX_DEFAULT_UINT64_PAIR_HASH_FUNC, ZBX_DEFAULT_UINT64_PAIR_COMPARE_FUNC);
	zbx_hashset_iter_reset(&dbsync_env.cache->trigdeps, &iter);
	while (NULL != (dep_down = (ZBX_DC_TRIGGER_DEPLIST *)zbx_hashset_iter_next(&iter)))
	{
		dep_local.first = dep_down->triggerid;
		for (i = 0; i < dep_down->dependencies.values_num; i++)
		{
			dep_up = (ZBX_DC_TRIGGER_DEPLIST *)dep_down->dependencies.values[i];
			dep_local.second = dep_up->triggerid;
			zbx_hashset_insert(&deps, &dep_local, sizeof(dep_local));
		}
	}
	while (NULL != (dbrow = DBfetch(result)))
	{
		ZBX_STR2UINT64(dep_local.first, dbrow[0]);
		ZBX_STR2UINT64(dep_local.second, dbrow[1]);
		if (NULL == (dep = (zbx_uint64_pair_t *)zbx_hashset_search(&deps, &dep_local)))
			dbsync_add_row(sync, 0, ZBX_DBSYNC_ROW_ADD, dbrow);
		else
			zbx_hashset_remove_direct(&deps, dep);
	}
	zbx_hashset_iter_reset(&deps, &iter);
	while (NULL != (dep = (zbx_uint64_pair_t *)zbx_hashset_iter_next(&iter)))
	{
		zbx_snprintf(down_s, sizeof(down_s), ZBX_FS_UI64, dep->first);
		zbx_snprintf(up_s, sizeof(up_s), ZBX_FS_UI64, dep->second);
		dbsync_add_row(sync, 0, ZBX_DBSYNC_ROW_REMOVE, del_row);
	}
	DBfree_result(result);
	zbx_hashset_destroy(&deps);
	return SUCCEED;
}
int zbx_dbsync_compare_trigger_dependency ( zbx_dbsync_t * sync ) { DB_ROW dbrow ; DB_RESULT result ; zbx_hashset_t deps ; zbx_hashset_iter_t iter ; ZBX_DC_TRIGGER_DEPLIST * dep_down , * dep_up ; zbx_uint64_pair_t * dep , dep_local ; char down_s [ MAX_ID_LEN + 1 ] , up_s [ MAX_ID_LEN + 1 ] ; char * del_row [ 2 ] = { down_s , up_s } ; int i ; if ( NULL == ( result = DBselect ( " " " " " " " " " " " " " " " " , ZBX_FLAG_DISCOVERY_PROTOTYPE , HOST_STATUS_MONITORED , HOST_STATUS_NOT_MONITORED ) ) ) { return FAIL ; } dbsync_prepare ( sync , 2 , NULL ) ; if ( ZBX_DBSYNC_INIT == sync -> mode ) { sync -> dbresult = result ; return SUCCEED ; } zbx_hashset_create ( & deps , 100 , ZBX_DEFAULT_UINT64_PAIR_HASH_FUNC , ZBX_DEFAULT_UINT64_PAIR_COMPARE_FUNC ) ; zbx_hashset_iter_reset ( & dbsync_env . cache -> trigdeps , & iter ) ; while ( NULL != ( dep_down = ( ZBX_DC_TRIGGER_DEPLIST * ) zbx_hashset_iter_next ( & iter ) ) ) { dep_local . first = dep_down -> triggerid ; for ( i = 0 ; i < dep_down -> dependencies . values_num ; i ++ ) { dep_up = ( ZBX_DC_TRIGGER_DEPLIST * ) dep_down -> dependencies . values [ i ] ; dep_local . second = dep_up -> triggerid ; zbx_hashset_insert ( & deps , & dep_local , sizeof ( dep_local ) ) ; } } while ( NULL != ( dbrow = DBfetch ( result ) ) ) { ZBX_STR2UINT64 ( dep_local . first , dbrow [ 0 ] ) ; ZBX_STR2UINT64 ( dep_local . second , dbrow [ 1 ] ) ; if ( NULL == ( dep = ( zbx_uint64_pair_t * ) zbx_hashset_search ( & deps , & dep_local ) ) ) dbsync_add_row ( sync , 0 , ZBX_DBSYNC_ROW_ADD , dbrow ) ; else zbx_hashset_remove_direct ( & deps , dep ) ; } zbx_hashset_iter_reset ( & deps , & iter ) ; while ( NULL != ( dep = ( zbx_uint64_pair_t * ) zbx_hashset_iter_next ( & iter ) ) ) { zbx_snprintf ( down_s , sizeof ( down_s ) , ZBX_FS_UI64 , dep -> first ) ; zbx_snprintf ( up_s , sizeof ( up_s ) , ZBX_FS_UI64 , dep -> second ) ; dbsync_add_row ( sync , 0 , ZBX_DBSYNC_ROW_REMOVE , del_row ) ; } DBfree_result ( result ) ; zbx_hashset_destroy ( & deps ) ; return SUCCEED ; }
Function: zbx_dbsync_compare_trigger_dependency                            
                                                                            
 Purpose: compares trigger_depends table with cached configuration data     
                                                                            
 Parameter: sync - [OUT] the changeset                                      
                                                                            
 Return value: SUCCEED - the changeset was successfully calculated          
               FAIL    - otherwise
Function : zbx_dbsync_compare_trigger_dependency Purpose : compares trigger_depends table with cached configuration data Parameter : sync - [ OUT ] the changeset Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise
/* index all host->template links */ /* add new rows, remove existing rows from index */ /* add removed rows */