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

Purpose: compares hosts table with cached configuration data
and populates the changeset

Parameter: sync - [OUT] the changeset

Return value: SUCCEED - the changeset was successfully calculated
FAIL    - otherwise
zbx_dbsync_compare_hosts Purpose : compares hosts table with cached configuration data and populates the changeset Parameter : sync - [ OUT ] the changeset Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise
int	zbx_dbsync_compare_hosts(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_HOST		*host;
#if defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
	if (NULL == (result = DBselect(
			"select hostid,proxy_hostid,host,ipmi_authtype,ipmi_privilege,ipmi_username,"
				"ipmi_password,maintenance_status,maintenance_type,maintenance_from,"
				"status,name,lastaccess,tls_connect,tls_accept,tls_issuer,tls_subject,"
				"tls_psk_identity,tls_psk,proxy_address,auto_compress,maintenanceid"
			" from hosts"
			" where status in (%d,%d,%d,%d)"
				" and flags<>%d",
			HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED,
			HOST_STATUS_PROXY_ACTIVE, HOST_STATUS_PROXY_PASSIVE,
			ZBX_FLAG_DISCOVERY_PROTOTYPE)))
	{
		return FAIL;
	}
	dbsync_prepare(sync, 22, NULL);
#else
	if (NULL == (result = DBselect(
			"select hostid,proxy_hostid,host,ipmi_authtype,ipmi_privilege,ipmi_username,"
				"ipmi_password,maintenance_status,maintenance_type,maintenance_from,"
				"status,name,lastaccess,tls_connect,tls_accept,"
				"proxy_address,auto_compress,maintenanceid"
			" from hosts"
			" where status in (%d,%d,%d,%d)"
				" and flags<>%d",
			HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED,
			HOST_STATUS_PROXY_ACTIVE, HOST_STATUS_PROXY_PASSIVE,
			ZBX_FLAG_DISCOVERY_PROTOTYPE)))
	{
		return FAIL;
	}
	dbsync_prepare(sync, 18, NULL);
#endif
	if (ZBX_DBSYNC_INIT == sync->mode)
	{
		sync->dbresult = result;
		return SUCCEED;
	}
	zbx_hashset_create(&ids, dbsync_env.cache->hosts.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));
		if (NULL == (host = (ZBX_DC_HOST *)zbx_hashset_search(&dbsync_env.cache->hosts, &rowid)))
			tag = ZBX_DBSYNC_ROW_ADD;
		else if (FAIL == dbsync_compare_host(host, dbrow))
			tag = ZBX_DBSYNC_ROW_UPDATE;
		if (ZBX_DBSYNC_ROW_NONE != tag)
			dbsync_add_row(sync, rowid, tag, dbrow);
	}
	zbx_hashset_iter_reset(&dbsync_env.cache->hosts, &iter);
	while (NULL != (host = (ZBX_DC_HOST *)zbx_hashset_iter_next(&iter)))
	{
		if (NULL == zbx_hashset_search(&ids, &host->hostid))
			dbsync_add_row(sync, host->hostid, ZBX_DBSYNC_ROW_REMOVE, NULL);
	}
	zbx_hashset_destroy(&ids);
	DBfree_result(result);
	return SUCCEED;
}
int zbx_dbsync_compare_hosts ( 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_HOST * host ; #if defined ( HAVE_GNUTLS ) || defined ( HAVE_OPENSSL ) if ( NULL == ( result = DBselect ( " " " " " " " " " " " " " " , HOST_STATUS_MONITORED , HOST_STATUS_NOT_MONITORED , HOST_STATUS_PROXY_ACTIVE , HOST_STATUS_PROXY_PASSIVE , ZBX_FLAG_DISCOVERY_PROTOTYPE ) ) ) { return FAIL ; } dbsync_prepare ( sync , 22 , NULL ) ; #else if ( NULL == ( result = DBselect ( " " " " " " " " " " " " " " , HOST_STATUS_MONITORED , HOST_STATUS_NOT_MONITORED , HOST_STATUS_PROXY_ACTIVE , HOST_STATUS_PROXY_PASSIVE , ZBX_FLAG_DISCOVERY_PROTOTYPE ) ) ) { return FAIL ; } dbsync_prepare ( sync , 18 , NULL ) ; #endif if ( ZBX_DBSYNC_INIT == sync -> mode ) { sync -> dbresult = result ; return SUCCEED ; } zbx_hashset_create ( & ids , dbsync_env . cache -> hosts . 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 ) ) ; if ( NULL == ( host = ( ZBX_DC_HOST * ) zbx_hashset_search ( & dbsync_env . cache -> hosts , & rowid ) ) ) tag = ZBX_DBSYNC_ROW_ADD ; else if ( FAIL == dbsync_compare_host ( host , dbrow ) ) tag = ZBX_DBSYNC_ROW_UPDATE ; if ( ZBX_DBSYNC_ROW_NONE != tag ) dbsync_add_row ( sync , rowid , tag , dbrow ) ; } zbx_hashset_iter_reset ( & dbsync_env . cache -> hosts , & iter ) ; while ( NULL != ( host = ( ZBX_DC_HOST * ) zbx_hashset_iter_next ( & iter ) ) ) { if ( NULL == zbx_hashset_search ( & ids , & host -> hostid ) ) dbsync_add_row ( sync , host -> hostid , ZBX_DBSYNC_ROW_REMOVE , NULL ) ; } zbx_hashset_destroy ( & ids ) ; DBfree_result ( result ) ; return SUCCEED ; }
Function: zbx_dbsync_compare_hosts                                         
                                                                            
 Purpose: compares hosts table with cached configuration data               
          and populates the changeset                                       
                                                                            
 Parameter: sync - [OUT] the changeset                                      
                                                                            
 Return value: SUCCEED - the changeset was successfully calculated          
               FAIL    - otherwise
Function : zbx_dbsync_compare_hosts Purpose : compares hosts table with cached configuration data and populates the changeset Parameter : sync - [ OUT ] the changeset Return value : SUCCEED - the changeset was successfully calculated FAIL - otherwise