zabbix-agent2/src/libs/zbxdbcache/dbsync.c
int	zbx_dbsync_compare_items(zbx_dbsync_t *sync)
{
	DB_ROW			dbrow;
	DB_RESULT		result;
	zbx_hashset_t		ids;
	zbx_hashset_iter_t	iter;
	zbx_uint64_t		rowid;
	ZBX_DC_ITEM		*item;
	char			**row;

	if (NULL == (result = DBselect(
			"select i.itemid,i.hostid,i.status,i.type,i.value_type,i.key_,i.snmp_oid,i.ipmi_sensor,i.delay,"
				"i.trapper_hosts,i.logtimefmt,i.params,ir.state,i.authtype,i.username,i.password,"
				"i.publickey,i.privatekey,i.flags,i.interfaceid,ir.lastlogsize,ir.mtime,"
				"i.history,i.trends,i.inventory_link,i.valuemapid,i.units,ir.error,i.jmx_endpoint,"
				"i.master_itemid,i.timeout,i.url,i.query_fields,i.posts,i.status_codes,"
				"i.follow_redirects,i.post_type,i.http_proxy,i.headers,i.retrieve_mode,"
				"i.request_method,i.output_format,i.ssl_cert_file,i.ssl_key_file,i.ssl_key_password,"
				"i.verify_peer,i.verify_host,i.allow_traps,i.templateid,id.parent_itemid,null"
			" from items i"
			" inner join hosts h on i.hostid=h.hostid"
			" left join item_discovery id on i.itemid=id.itemid"
			" join item_rtdata ir on i.itemid=ir.itemid"
			" where h.status in (%d,%d) and i.flags<>%d",
			HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED, ZBX_FLAG_DISCOVERY_PROTOTYPE)))

	{
		return FAIL;
	}

	dbsync_prepare(sync, 51, dbsync_item_preproc_row);

	if (ZBX_DBSYNC_INIT == sync->mode)
	{
		sync->dbresult = result;
		return SUCCEED;
	}

	zbx_hashset_create(&ids, dbsync_env.cache->items.num_data, ZBX_DEFAULT_UINT64_HASH_FUNC,
			ZBX_DEFAULT_UINT64_COMPARE_FUNC);

	while (NULL != (dbrow = DBfetch(result)))
	{
		unsigned char	tag = ZBX_DBSYNC_ROW_NONE;

		ZBX_STR2UINT64(rowid, dbrow[0]);
		zbx_hashset_insert(&ids, &rowid, sizeof(rowid));

		row = dbsync_preproc_row(sync, dbrow);

		if (NULL == (item = (ZBX_DC_ITEM *)zbx_hashset_search(&dbsync_env.cache->items, &rowid)))
			tag = ZBX_DBSYNC_ROW_ADD;
		else if (FAIL == dbsync_compare_item(item, row))
			tag = ZBX_DBSYNC_ROW_UPDATE;

		if (ZBX_DBSYNC_ROW_NONE != tag)
			dbsync_add_row(sync, rowid, tag, row);
	}

	zbx_hashset_iter_reset(&dbsync_env.cache->items, &iter);
	while (NULL != (item = (ZBX_DC_ITEM *)zbx_hashset_iter_next(&iter)))
	{
		if (NULL == zbx_hashset_search(&ids, &item->itemid))
			dbsync_add_row(sync, item->itemid, ZBX_DBSYNC_ROW_REMOVE, NULL);
	}

	zbx_hashset_destroy(&ids);
	DBfree_result(result);

	return SUCCEED;
}
/******************************************************************************
 *                                                                            *
 * Function: zbx_dbsync_compare_items                                         *
 *                                                                            *
 * Purpose: compares items table with cached configuration data               *
 *                                                                            *
 * Return value: SUCCEED - the changeset was successfully calculated          *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 ******************************************************************************/
zbx_dbsync_compare_items

Purpose: compares items table with cached configuration data

Return value: SUCCEED - the changeset was successfully calculated
FAIL    - otherwise
zbx_dbsync_compare_items Purpose : compares items table with cached configuration data Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise
int	zbx_dbsync_compare_items(zbx_dbsync_t *sync)
{
	DB_ROW			dbrow;
	DB_RESULT		result;
	zbx_hashset_t		ids;
	zbx_hashset_iter_t	iter;
	zbx_uint64_t		rowid;
	ZBX_DC_ITEM		*item;
	char			**row;
	if (NULL == (result = DBselect(
			"select i.itemid,i.hostid,i.status,i.type,i.value_type,i.key_,i.snmp_oid,i.ipmi_sensor,i.delay,"
				"i.trapper_hosts,i.logtimefmt,i.params,ir.state,i.authtype,i.username,i.password,"
				"i.publickey,i.privatekey,i.flags,i.interfaceid,ir.lastlogsize,ir.mtime,"
				"i.history,i.trends,i.inventory_link,i.valuemapid,i.units,ir.error,i.jmx_endpoint,"
				"i.master_itemid,i.timeout,i.url,i.query_fields,i.posts,i.status_codes,"
				"i.follow_redirects,i.post_type,i.http_proxy,i.headers,i.retrieve_mode,"
				"i.request_method,i.output_format,i.ssl_cert_file,i.ssl_key_file,i.ssl_key_password,"
				"i.verify_peer,i.verify_host,i.allow_traps,i.templateid,id.parent_itemid,null"
			" from items i"
			" inner join hosts h on i.hostid=h.hostid"
			" left join item_discovery id on i.itemid=id.itemid"
			" join item_rtdata ir on i.itemid=ir.itemid"
			" where h.status in (%d,%d) and i.flags<>%d",
			HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED, ZBX_FLAG_DISCOVERY_PROTOTYPE)))
	{
		return FAIL;
	}
	dbsync_prepare(sync, 51, dbsync_item_preproc_row);
	if (ZBX_DBSYNC_INIT == sync->mode)
	{
		sync->dbresult = result;
		return SUCCEED;
	}
	zbx_hashset_create(&ids, dbsync_env.cache->items.num_data, ZBX_DEFAULT_UINT64_HASH_FUNC,
			ZBX_DEFAULT_UINT64_COMPARE_FUNC);
	while (NULL != (dbrow = DBfetch(result)))
	{
		unsigned char	tag = ZBX_DBSYNC_ROW_NONE;
		ZBX_STR2UINT64(rowid, dbrow[0]);
		zbx_hashset_insert(&ids, &rowid, sizeof(rowid));
		row = dbsync_preproc_row(sync, dbrow);
		if (NULL == (item = (ZBX_DC_ITEM *)zbx_hashset_search(&dbsync_env.cache->items, &rowid)))
			tag = ZBX_DBSYNC_ROW_ADD;
		else if (FAIL == dbsync_compare_item(item, row))
			tag = ZBX_DBSYNC_ROW_UPDATE;
		if (ZBX_DBSYNC_ROW_NONE != tag)
			dbsync_add_row(sync, rowid, tag, row);
	}
	zbx_hashset_iter_reset(&dbsync_env.cache->items, &iter);
	while (NULL != (item = (ZBX_DC_ITEM *)zbx_hashset_iter_next(&iter)))
	{
		if (NULL == zbx_hashset_search(&ids, &item->itemid))
			dbsync_add_row(sync, item->itemid, ZBX_DBSYNC_ROW_REMOVE, NULL);
	}
	zbx_hashset_destroy(&ids);
	DBfree_result(result);
	return SUCCEED;
}
int zbx_dbsync_compare_items ( zbx_dbsync_t * sync ) { DB_ROW dbrow ; DB_RESULT result ; zbx_hashset_t ids ; zbx_hashset_iter_t iter ; zbx_uint64_t rowid ; ZBX_DC_ITEM * item ; char * * row ; if ( NULL == ( result = DBselect ( " " " " " " " " " " " " " " " " " " " " " " " " " " , HOST_STATUS_MONITORED , HOST_STATUS_NOT_MONITORED , ZBX_FLAG_DISCOVERY_PROTOTYPE ) ) ) { return FAIL ; } dbsync_prepare ( sync , 51 , dbsync_item_preproc_row ) ; if ( ZBX_DBSYNC_INIT == sync -> mode ) { sync -> dbresult = result ; return SUCCEED ; } zbx_hashset_create ( & ids , dbsync_env . cache -> items . num_data , ZBX_DEFAULT_UINT64_HASH_FUNC , ZBX_DEFAULT_UINT64_COMPARE_FUNC ) ; while ( NULL != ( dbrow = DBfetch ( result ) ) ) { unsigned char tag = ZBX_DBSYNC_ROW_NONE ; ZBX_STR2UINT64 ( rowid , dbrow [ 0 ] ) ; zbx_hashset_insert ( & ids , & rowid , sizeof ( rowid ) ) ; row = dbsync_preproc_row ( sync , dbrow ) ; if ( NULL == ( item = ( ZBX_DC_ITEM * ) zbx_hashset_search ( & dbsync_env . cache -> items , & rowid ) ) ) tag = ZBX_DBSYNC_ROW_ADD ; else if ( FAIL == dbsync_compare_item ( item , row ) ) tag = ZBX_DBSYNC_ROW_UPDATE ; if ( ZBX_DBSYNC_ROW_NONE != tag ) dbsync_add_row ( sync , rowid , tag , row ) ; } zbx_hashset_iter_reset ( & dbsync_env . cache -> items , & iter ) ; while ( NULL != ( item = ( ZBX_DC_ITEM * ) zbx_hashset_iter_next ( & iter ) ) ) { if ( NULL == zbx_hashset_search ( & ids , & item -> itemid ) ) dbsync_add_row ( sync , item -> itemid , ZBX_DBSYNC_ROW_REMOVE , NULL ) ; } zbx_hashset_destroy ( & ids ) ; DBfree_result ( result ) ; return SUCCEED ; }
Function: zbx_dbsync_compare_items                                         
                                                                            
 Purpose: compares items table with cached configuration data               
                                                                            
 Return value: SUCCEED - the changeset was successfully calculated          
               FAIL    - otherwise
Function : zbx_dbsync_compare_items Purpose : compares items table with cached configuration data Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise