zabbix-agent2/src/zabbix_server/lld/lld_item.c
/******************************************************************************
 *                                                                            *
 * Function: lld_item_dependencies_get                                        *
 *                                                                            *
 * Purpose: recursively get dependencies with dependent items taking into     *
 *          account item prototypes                                           *
 *                                                                            *
 * Parameters: item_prototypes   - [IN] item prototypes                       *
 *             item_dependencies - [OUT] list of dependencies                 *
 *                                                                            *
 ******************************************************************************/
lld_item_dependencies_get

Purpose: recursively get dependencies with dependent items taking into
account item prototypes

Parameters: item_prototypes   - [IN] item prototypes
item_dependencies - [OUT] list of dependencies
lld_item_dependencies_get Purpose : recursively get dependencies with dependent items taking into account item prototypes Parameters : item_prototypes - [ IN ] item prototypes item_dependencies - [ OUT ] list of dependencies
static void	lld_item_dependencies_get(const zbx_vector_ptr_t *item_prototypes, zbx_vector_ptr_t *item_dependencies)
{
#define NEXT_CHECK_BY_ITEM_IDS		0
#define NEXT_CHECK_BY_MASTERITEM_IDS	1
	int			i, check_type;
	zbx_vector_uint64_t	processed_masterid, processed_itemid, next_check_itemids, next_check_masterids,
				*check_ids;
	char			*sql = NULL;
	size_t			sql_alloc = 0, sql_offset;
	DB_RESULT		result;
	DB_ROW			row;
	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
	zbx_vector_uint64_create(&processed_masterid);
	zbx_vector_uint64_create(&processed_itemid);
	zbx_vector_uint64_create(&next_check_itemids);
	zbx_vector_uint64_create(&next_check_masterids);
	for (i = 0; i < item_prototypes->values_num; i++)
	{
		const zbx_lld_item_prototype_t	*item_prototype;
		item_prototype = (const zbx_lld_item_prototype_t *)item_prototypes->values[i];
		if (0 != item_prototype->master_itemid)
		{
			lld_item_dependence_add(item_dependencies, item_prototype->itemid,
					item_prototype->master_itemid, ZBX_FLAG_DISCOVERY_PROTOTYPE);
			zbx_vector_uint64_append(&next_check_itemids, item_prototype->master_itemid);
			zbx_vector_uint64_append(&next_check_masterids, item_prototype->master_itemid);
		}
	}
	while (0 < next_check_itemids.values_num || 0 < next_check_masterids.values_num)
	{
		if (0 < next_check_itemids.values_num)
		{
			check_type = NEXT_CHECK_BY_ITEM_IDS;
			check_ids = &next_check_itemids;
		}
		else
		{
			check_type = NEXT_CHECK_BY_MASTERITEM_IDS;
			check_ids = &next_check_masterids;
		}
		sql_offset = 0;
		zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "select itemid,master_itemid,flags from items where");
		DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset,
				NEXT_CHECK_BY_ITEM_IDS == check_type ? "itemid" : "master_itemid",
				check_ids->values, check_ids->values_num);
		if (NEXT_CHECK_BY_ITEM_IDS == check_type)
			zbx_vector_uint64_append_array(&processed_itemid, check_ids->values, check_ids->values_num);
		else
			zbx_vector_uint64_append_array(&processed_masterid, check_ids->values, check_ids->values_num);
		zbx_vector_uint64_clear(check_ids);
		result = DBselect("%s", sql);
		while (NULL != (row = DBfetch(result)))
		{
			int			dependence_found = 0;
			zbx_item_dependence_t	*dependence = NULL;
			zbx_uint64_t		itemid, master_itemid;
			unsigned int		item_flags;
			ZBX_STR2UINT64(itemid, row[0]);
			ZBX_DBROW2UINT64(master_itemid, row[1]);
			ZBX_STR2UCHAR(item_flags, row[2]);
			for (i = 0; i < item_dependencies->values_num; i++)
			{
				dependence = (zbx_item_dependence_t *)item_dependencies->values[i];
				if (dependence->itemid == itemid && dependence->master_itemid == master_itemid)
				{
					dependence_found = 1;
					break;
				}
			}
			if (0 == dependence_found)
			{
				dependence = lld_item_dependence_add(item_dependencies, itemid, master_itemid,
						item_flags);
			}
			if (FAIL == zbx_vector_uint64_search(&processed_masterid, dependence->itemid,
					ZBX_DEFAULT_UINT64_COMPARE_FUNC))
			{
				zbx_vector_uint64_append(&next_check_masterids, dependence->itemid);
			}
			if (NEXT_CHECK_BY_ITEM_IDS != check_type || 0 == dependence->master_itemid)
				continue;
			if (FAIL == zbx_vector_uint64_search(&processed_itemid, dependence->master_itemid,
					ZBX_DEFAULT_UINT64_COMPARE_FUNC))
			{
				zbx_vector_uint64_append(&next_check_itemids, dependence->master_itemid);
			}
		}
		DBfree_result(result);
	}
	zbx_free(sql);
	zbx_vector_uint64_destroy(&processed_masterid);
	zbx_vector_uint64_destroy(&processed_itemid);
	zbx_vector_uint64_destroy(&next_check_itemids);
	zbx_vector_uint64_destroy(&next_check_masterids);
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
#undef NEXT_CHECK_BY_ITEM_IDS
#undef NEXT_CHECK_BY_MASTERITEM_IDS
}
static void lld_item_dependencies_get ( const zbx_vector_ptr_t * item_prototypes , zbx_vector_ptr_t * item_dependencies ) { #define NEXT_CHECK_BY_ITEM_IDS 		0
 #define NEXT_CHECK_BY_MASTERITEM_IDS 	1
 int i , check_type ; zbx_vector_uint64_t processed_masterid , processed_itemid , next_check_itemids , next_check_masterids , * check_ids ; char * sql = NULL ; size_t sql_alloc = 0 , sql_offset ; DB_RESULT result ; DB_ROW row ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ ) ; zbx_vector_uint64_create ( & processed_masterid ) ; zbx_vector_uint64_create ( & processed_itemid ) ; zbx_vector_uint64_create ( & next_check_itemids ) ; zbx_vector_uint64_create ( & next_check_masterids ) ; for ( i = 0 ; i < item_prototypes -> values_num ; i ++ ) { const zbx_lld_item_prototype_t * item_prototype ; item_prototype = ( const zbx_lld_item_prototype_t * ) item_prototypes -> values [ i ] ; if ( 0 != item_prototype -> master_itemid ) { lld_item_dependence_add ( item_dependencies , item_prototype -> itemid , item_prototype -> master_itemid , ZBX_FLAG_DISCOVERY_PROTOTYPE ) ; zbx_vector_uint64_append ( & next_check_itemids , item_prototype -> master_itemid ) ; zbx_vector_uint64_append ( & next_check_masterids , item_prototype -> master_itemid ) ; } } while ( 0 < next_check_itemids . values_num || 0 < next_check_masterids . values_num ) { if ( 0 < next_check_itemids . values_num ) { check_type = NEXT_CHECK_BY_ITEM_IDS ; check_ids = & next_check_itemids ; } else { check_type = NEXT_CHECK_BY_MASTERITEM_IDS ; check_ids = & next_check_masterids ; } sql_offset = 0 ; zbx_strcpy_alloc ( & sql , & sql_alloc , & sql_offset , " " ) ; DBadd_condition_alloc ( & sql , & sql_alloc , & sql_offset , NEXT_CHECK_BY_ITEM_IDS == check_type ? " " : " " , check_ids -> values , check_ids -> values_num ) ; if ( NEXT_CHECK_BY_ITEM_IDS == check_type ) zbx_vector_uint64_append_array ( & processed_itemid , check_ids -> values , check_ids -> values_num ) ; else zbx_vector_uint64_append_array ( & processed_masterid , check_ids -> values , check_ids -> values_num ) ; zbx_vector_uint64_clear ( check_ids ) ; result = DBselect ( " " , sql ) ; while ( NULL != ( row = DBfetch ( result ) ) ) { int dependence_found = 0 ; zbx_item_dependence_t * dependence = NULL ; zbx_uint64_t itemid , master_itemid ; unsigned int item_flags ; ZBX_STR2UINT64 ( itemid , row [ 0 ] ) ; ZBX_DBROW2UINT64 ( master_itemid , row [ 1 ] ) ; ZBX_STR2UCHAR ( item_flags , row [ 2 ] ) ; for ( i = 0 ; i < item_dependencies -> values_num ; i ++ ) { dependence = ( zbx_item_dependence_t * ) item_dependencies -> values [ i ] ; if ( dependence -> itemid == itemid && dependence -> master_itemid == master_itemid ) { dependence_found = 1 ; break ; } } if ( 0 == dependence_found ) { dependence = lld_item_dependence_add ( item_dependencies , itemid , master_itemid , item_flags ) ; } if ( FAIL == zbx_vector_uint64_search ( & processed_masterid , dependence -> itemid , ZBX_DEFAULT_UINT64_COMPARE_FUNC ) ) { zbx_vector_uint64_append ( & next_check_masterids , dependence -> itemid ) ; } if ( NEXT_CHECK_BY_ITEM_IDS != check_type || 0 == dependence -> master_itemid ) continue ; if ( FAIL == zbx_vector_uint64_search ( & processed_itemid , dependence -> master_itemid , ZBX_DEFAULT_UINT64_COMPARE_FUNC ) ) { zbx_vector_uint64_append ( & next_check_itemids , dependence -> master_itemid ) ; } } DBfree_result ( result ) ; } zbx_free ( sql ) ; zbx_vector_uint64_destroy ( & processed_masterid ) ; zbx_vector_uint64_destroy ( & processed_itemid ) ; zbx_vector_uint64_destroy ( & next_check_itemids ) ; zbx_vector_uint64_destroy ( & next_check_masterids ) ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ ) ; #undef  NEXT_CHECK_BY_ITEM_IDS
 #undef  NEXT_CHECK_BY_MASTERITEM_IDS
 }
Function: lld_item_dependencies_get                                        
                                                                            
 Purpose: recursively get dependencies with dependent items taking into     
          account item prototypes                                           
                                                                            
 Parameters: item_prototypes   - [IN] item prototypes                       
             item_dependencies - [OUT] list of dependencies
Function : lld_item_dependencies_get Purpose : recursively get dependencies with dependent items taking into account item prototypes Parameters : item_prototypes - [ IN ] item prototypes item_dependencies - [ OUT ] list of dependencies
/* collect the item id of prototypes for searching dependencies into database */ /* search dependency in two directions (masteritem_id->itemid and itemid->masteritem_id) */