zabbix-agent2/src/zabbix_server/lld/lld_host.c
/******************************************************************************
 *                                                                            *
 * Function: lld_host_update_tags                                             *
 *                                                                            *
 * Purpose: update host tags                                                  *
 *                                                                            *
 * Parameters: host       - [IN] a host with existing tags, sorted by tag +   *
 *                               value.                                       *
 *                        - [OUT] a host with updated tags, sorted by tagid   *
 *             tags       - [IN] the new tags, sorted by tag + value          *
 *             lld_macros - [IN] a vector of LLD macros                       *
 *             info       - [OUT] error information                           *
 *                                                                            *
 * Comments: No database changes are made if host tags are equal to the new   *
 *           tags. Otherwise the tags are updated starting with the first not *
 *           matching tag. If there are more host tags than new tags then the *
 *           extra host tags are marked for removal. If there are more new    *
 *           tags than host tags, then new tags are appended to the host tags.*
 *                                                                            *
 ******************************************************************************/
lld_host_update_tags

Purpose: update host tags

Parameters: host       - [IN] a host with existing tags, sorted by tag +
value.
- [OUT] a host with updated tags, sorted by tagid
tags       - [IN] the new tags, sorted by tag + value
lld_macros - [IN] a vector of LLD macros
info       - [OUT] error information

Comments: No database changes are made if host tags are equal to the new
tags. Otherwise the tags are updated starting with the first not
matching tag. If there are more host tags than new tags then the
extra host tags are marked for removal. If there are more new
tags than host tags, then new tags are appended to the host tags.
lld_host_update_tags Purpose : update host tags Parameters : host - [ IN ] a host with existing tags sorted by tag + value . - [ OUT ] a host with updated tags sorted by tagid tags - [ IN ] the new tags sorted by tag + value lld_macros - [ IN ] a vector of LLD macros info - [ OUT ] error information Comments : No database changes are made if host tags are equal to the new tags . Otherwise the tags are updated starting with the first not matching tag . If there are more host tags than new tags then the extra host tags are marked for removal . If there are more new tags than host tags then new tags are appended to the host tags .
static void	lld_host_update_tags(zbx_lld_host_t *host, const zbx_vector_db_tag_ptr_t *tags,
		const zbx_vector_ptr_t *lld_macros, char **info)
{
	int			i;
	zbx_db_tag_t		*host_tag, *proto_tag;
	zbx_vector_db_tag_ptr_t	new_tags;
	char			*tag = NULL, *value = NULL;
	zbx_vector_db_tag_ptr_create(&new_tags);
	for (i = 0; i < tags->values_num; i++)
	{
		proto_tag = (zbx_db_tag_t *)tags->values[i];
		tag = zbx_strdup(tag, proto_tag->tag);
		value = zbx_strdup(value, proto_tag->value);
		substitute_lld_macros(&tag, host->jp_row, lld_macros, ZBX_MACRO_FUNC, NULL, 0);
		substitute_lld_macros(&value, host->jp_row, lld_macros, ZBX_MACRO_FUNC, NULL, 0);
		if (SUCCEED != lld_tag_validate(new_tags.values, new_tags.values_num, tag, value, info))
			continue;
		proto_tag = zbx_db_tag_create(tag, value);
		zbx_vector_db_tag_ptr_append(&new_tags, proto_tag);
		zbx_free(tag);
		zbx_free(value);
	}
	zbx_vector_db_tag_ptr_sort(&new_tags, zbx_db_tag_compare_func);
	zbx_vector_db_tag_ptr_reserve(&host->tags, (size_t)new_tags.values_num);
	for (i = 0; i < host->tags.values_num; i++)
	{
		host_tag = (zbx_db_tag_t *)host->tags.values[i];
		if (i < new_tags.values_num)
		{
			proto_tag = (zbx_db_tag_t *)new_tags.values[i];
			if (0 != strcmp(host_tag->tag, proto_tag->tag))
			{
				host_tag->tag_orig = zbx_strdup(NULL, host_tag->tag);
				host_tag->tag = zbx_strdup(host_tag->tag, proto_tag->tag);
				host_tag->flags |= ZBX_FLAG_DB_TAG_UPDATE_TAG;
			}
			if (0 != strcmp(host_tag->value, proto_tag->value))
			{
				host_tag->value_orig = zbx_strdup(NULL, host_tag->value);
				host_tag->value = zbx_strdup(host_tag->value, proto_tag->value);
				host_tag->flags |= ZBX_FLAG_DB_TAG_UPDATE_VALUE;
			}
		}
		else
			host_tag->flags = ZBX_FLAG_DB_TAG_REMOVE;
	}
	if (i < new_tags.values_num)
	{
		int	j;
		for (j = i; j < new_tags.values_num; j++)
		{
			proto_tag = (zbx_db_tag_t *)new_tags.values[j];
			proto_tag->tagid = 0;
			proto_tag->flags = 0;
		}
		zbx_vector_db_tag_ptr_append_array(&host->tags, new_tags.values + i, new_tags.values_num - i);
		new_tags.values_num = i;
	}
	zbx_free(tag);
	zbx_free(value);
	zbx_vector_db_tag_ptr_sort(&host->tags, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC);
	zbx_vector_db_tag_ptr_clear_ext(&new_tags, zbx_db_tag_free);
	zbx_vector_db_tag_ptr_destroy(&new_tags);
}
static void lld_host_update_tags ( zbx_lld_host_t * host , const zbx_vector_db_tag_ptr_t * tags , const zbx_vector_ptr_t * lld_macros , char * * info ) { int i ; zbx_db_tag_t * host_tag , * proto_tag ; zbx_vector_db_tag_ptr_t new_tags ; char * tag = NULL , * value = NULL ; zbx_vector_db_tag_ptr_create ( & new_tags ) ; for ( i = 0 ; i < tags -> values_num ; i ++ ) { proto_tag = ( zbx_db_tag_t * ) tags -> values [ i ] ; tag = zbx_strdup ( tag , proto_tag -> tag ) ; value = zbx_strdup ( value , proto_tag -> value ) ; substitute_lld_macros ( & tag , host -> jp_row , lld_macros , ZBX_MACRO_FUNC , NULL , 0 ) ; substitute_lld_macros ( & value , host -> jp_row , lld_macros , ZBX_MACRO_FUNC , NULL , 0 ) ; if ( SUCCEED != lld_tag_validate ( new_tags . values , new_tags . values_num , tag , value , info ) ) continue ; proto_tag = zbx_db_tag_create ( tag , value ) ; zbx_vector_db_tag_ptr_append ( & new_tags , proto_tag ) ; zbx_free ( tag ) ; zbx_free ( value ) ; } zbx_vector_db_tag_ptr_sort ( & new_tags , zbx_db_tag_compare_func ) ; zbx_vector_db_tag_ptr_reserve ( & host -> tags , ( size_t ) new_tags . values_num ) ; for ( i = 0 ; i < host -> tags . values_num ; i ++ ) { host_tag = ( zbx_db_tag_t * ) host -> tags . values [ i ] ; if ( i < new_tags . values_num ) { proto_tag = ( zbx_db_tag_t * ) new_tags . values [ i ] ; if ( 0 != strcmp ( host_tag -> tag , proto_tag -> tag ) ) { host_tag -> tag_orig = zbx_strdup ( NULL , host_tag -> tag ) ; host_tag -> tag = zbx_strdup ( host_tag -> tag , proto_tag -> tag ) ; host_tag -> flags |= ZBX_FLAG_DB_TAG_UPDATE_TAG ; } if ( 0 != strcmp ( host_tag -> value , proto_tag -> value ) ) { host_tag -> value_orig = zbx_strdup ( NULL , host_tag -> value ) ; host_tag -> value = zbx_strdup ( host_tag -> value , proto_tag -> value ) ; host_tag -> flags |= ZBX_FLAG_DB_TAG_UPDATE_VALUE ; } } else host_tag -> flags = ZBX_FLAG_DB_TAG_REMOVE ; } if ( i < new_tags . values_num ) { int j ; for ( j = i ; j < new_tags . values_num ; j ++ ) { proto_tag = ( zbx_db_tag_t * ) new_tags . values [ j ] ; proto_tag -> tagid = 0 ; proto_tag -> flags = 0 ; } zbx_vector_db_tag_ptr_append_array ( & host -> tags , new_tags . values + i , new_tags . values_num - i ) ; new_tags . values_num = i ; } zbx_free ( tag ) ; zbx_free ( value ) ; zbx_vector_db_tag_ptr_sort ( & host -> tags , ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC ) ; zbx_vector_db_tag_ptr_clear_ext ( & new_tags , zbx_db_tag_free ) ; zbx_vector_db_tag_ptr_destroy ( & new_tags ) ; }
Function: lld_host_update_tags                                             
                                                                            
 Purpose: update host tags                                                  
                                                                            
 Parameters: host       - [IN] a host with existing tags, sorted by tag +   
                               value.
Function : lld_host_update_tags Purpose : update host tags Parameters : host - [ IN ] a host with existing tags sorted by tag + value .
/* create vector of new tags with expanded lld macros */ /* update host tags or flag them for removal */ /* add missing tags */ /* set uninitialized properties of new tags that will be moved to host */ /* sort existing tags by their ids for update operations */