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

Purpose: compares hosts_templates table with cached configuration data

Parameter: sync - [OUT] the changeset

Return value: SUCCEED - the changeset was successfully calculated
FAIL    - otherwise
zbx_dbsync_compare_host_templates Purpose : compares hosts_templates table with cached configuration data Parameter : sync - [ OUT ] the changeset Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise
int	zbx_dbsync_compare_host_templates(zbx_dbsync_t *sync)
{
	DB_ROW			dbrow;
	DB_RESULT		result;
	zbx_hashset_iter_t	iter;
	ZBX_DC_HTMPL		*htmpl;
	zbx_hashset_t		htmpls;
	int			i;
	zbx_uint64_pair_t	ht_local, *ht;
	char			hostid_s[MAX_ID_LEN + 1], templateid_s[MAX_ID_LEN + 1];
	char			*del_row[2] = {hostid_s, templateid_s};
	if (NULL == (result = DBselect(
			"select hostid,templateid"
			" from hosts_templates"
			" order by hostid")))
	{
		return FAIL;
	}
	dbsync_prepare(sync, 2, NULL);
	if (ZBX_DBSYNC_INIT == sync->mode)
	{
		sync->dbresult = result;
		return SUCCEED;
	}
	zbx_hashset_create(&htmpls, 100, ZBX_DEFAULT_UINT64_PAIR_HASH_FUNC, ZBX_DEFAULT_UINT64_PAIR_COMPARE_FUNC);
	zbx_hashset_iter_reset(&dbsync_env.cache->htmpls, &iter);
	while (NULL != (htmpl = (ZBX_DC_HTMPL *)zbx_hashset_iter_next(&iter)))
	{
		ht_local.first = htmpl->hostid;
		for (i = 0; i < htmpl->templateids.values_num; i++)
		{
			ht_local.second = htmpl->templateids.values[i];
			zbx_hashset_insert(&htmpls, &ht_local, sizeof(ht_local));
		}
	}
	while (NULL != (dbrow = DBfetch(result)))
	{
		ZBX_STR2UINT64(ht_local.first, dbrow[0]);
		ZBX_STR2UINT64(ht_local.second, dbrow[1]);
		if (NULL == (ht = (zbx_uint64_pair_t *)zbx_hashset_search(&htmpls, &ht_local)))
			dbsync_add_row(sync, 0, ZBX_DBSYNC_ROW_ADD, dbrow);
		else
			zbx_hashset_remove_direct(&htmpls, ht);
	}
	zbx_hashset_iter_reset(&htmpls, &iter);
	while (NULL != (ht = (zbx_uint64_pair_t *)zbx_hashset_iter_next(&iter)))
	{
		zbx_snprintf(hostid_s, sizeof(hostid_s), ZBX_FS_UI64, ht->first);
		zbx_snprintf(templateid_s, sizeof(templateid_s), ZBX_FS_UI64, ht->second);
		dbsync_add_row(sync, 0, ZBX_DBSYNC_ROW_REMOVE, del_row);
	}
	DBfree_result(result);
	zbx_hashset_destroy(&htmpls);
	return SUCCEED;
}
int zbx_dbsync_compare_host_templates ( zbx_dbsync_t * sync ) { DB_ROW dbrow ; DB_RESULT result ; zbx_hashset_iter_t iter ; ZBX_DC_HTMPL * htmpl ; zbx_hashset_t htmpls ; int i ; zbx_uint64_pair_t ht_local , * ht ; char hostid_s [ MAX_ID_LEN + 1 ] , templateid_s [ MAX_ID_LEN + 1 ] ; char * del_row [ 2 ] = { hostid_s , templateid_s } ; if ( NULL == ( result = DBselect ( " " " " " " ) ) ) { return FAIL ; } dbsync_prepare ( sync , 2 , NULL ) ; if ( ZBX_DBSYNC_INIT == sync -> mode ) { sync -> dbresult = result ; return SUCCEED ; } zbx_hashset_create ( & htmpls , 100 , ZBX_DEFAULT_UINT64_PAIR_HASH_FUNC , ZBX_DEFAULT_UINT64_PAIR_COMPARE_FUNC ) ; zbx_hashset_iter_reset ( & dbsync_env . cache -> htmpls , & iter ) ; while ( NULL != ( htmpl = ( ZBX_DC_HTMPL * ) zbx_hashset_iter_next ( & iter ) ) ) { ht_local . first = htmpl -> hostid ; for ( i = 0 ; i < htmpl -> templateids . values_num ; i ++ ) { ht_local . second = htmpl -> templateids . values [ i ] ; zbx_hashset_insert ( & htmpls , & ht_local , sizeof ( ht_local ) ) ; } } while ( NULL != ( dbrow = DBfetch ( result ) ) ) { ZBX_STR2UINT64 ( ht_local . first , dbrow [ 0 ] ) ; ZBX_STR2UINT64 ( ht_local . second , dbrow [ 1 ] ) ; if ( NULL == ( ht = ( zbx_uint64_pair_t * ) zbx_hashset_search ( & htmpls , & ht_local ) ) ) dbsync_add_row ( sync , 0 , ZBX_DBSYNC_ROW_ADD , dbrow ) ; else zbx_hashset_remove_direct ( & htmpls , ht ) ; } zbx_hashset_iter_reset ( & htmpls , & iter ) ; while ( NULL != ( ht = ( zbx_uint64_pair_t * ) zbx_hashset_iter_next ( & iter ) ) ) { zbx_snprintf ( hostid_s , sizeof ( hostid_s ) , ZBX_FS_UI64 , ht -> first ) ; zbx_snprintf ( templateid_s , sizeof ( templateid_s ) , ZBX_FS_UI64 , ht -> second ) ; dbsync_add_row ( sync , 0 , ZBX_DBSYNC_ROW_REMOVE , del_row ) ; } DBfree_result ( result ) ; zbx_hashset_destroy ( & htmpls ) ; return SUCCEED ; }
Function: zbx_dbsync_compare_host_templates                                
                                                                            
 Purpose: compares hosts_templates table with cached configuration data     
                                                                            
 Parameter: sync - [OUT] the changeset                                      
                                                                            
 Return value: SUCCEED - the changeset was successfully calculated          
               FAIL    - otherwise
Function : zbx_dbsync_compare_host_templates Purpose : compares hosts_templates 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 */