zabbix-agent2/src/libs/zbxwin32/perfmon.c
/******************************************************************************
 *                                                                            *
 * Function: init_builtin_counter_indexes                                     *
 *                                                                            *
 * Purpose: Scans registry key with all performance counter English names     *
 *          and obtains system-dependent PDH counter indexes for further      *
 *          use by corresponding items.                                       *
 *                                                                            *
 * Return value: SUCCEED/FAIL                                                 *
 *                                                                            *
 * Comments: This function should be normally called during agent             *
 *           initialization from init_perf_collector().                       *
 *                                                                            *
 ******************************************************************************/
init_builtin_counter_indexes

Purpose: Scans registry key with all performance counter English names
and obtains system-dependent PDH counter indexes for further
use by corresponding items.

Return value: SUCCEED/FAIL

Comments: This function should be normally called during agent
initialization from init_perf_collector().
init_builtin_counter_indexes Purpose : Scans registry key with all performance counter English names and obtains system - dependent PDH counter indexes for further use by corresponding items . Return value : SUCCEED / FAIL Comments : This function should be normally called during agent initialization from init_perf_collector () .
int	init_builtin_counter_indexes(void)
{
	int 				ret = SUCCEED, i;
	wchar_t 			*counter_text, *eng_names, *counter_base;
	DWORD				counter_index;
	static const OSVERSIONINFOEX	*vi = NULL;
	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
	if (NULL == vi && NULL == (vi = zbx_win_getversion()))
	{
		zabbix_log(LOG_LEVEL_ERR, "Failed to get windows version");
		ret = FAIL;
		goto out;
	}
	if (NULL == (counter_base = eng_names = get_all_counter_names(HKEY_PERFORMANCE_TEXT, L"Counter")))
	{
		ret = FAIL;
		goto out;
	}
	counter_base += wcslen(counter_base) + 1;
	counter_base += wcslen(counter_base) + 1;
	for (counter_text = counter_base; 0 != *counter_text; counter_text += wcslen(counter_text) + 1)
	{
		counter_index = (DWORD)_wtoi(counter_text);
		counter_text += wcslen(counter_text) + 1;
		for (i = 0; i < ARRSIZE(builtin_object_map); i++)
		{
			if (0 == builtin_object_map[i].pdhIndex && vi->dwMajorVersion >=
					builtin_object_map[i].minSupported_dwMajorVersion && vi->dwMinorVersion >=
					builtin_object_map[i].minSupported_dwMinorVersion && 0 ==
					wcscmp(builtin_object_map[i].eng_name, counter_text))
			{
				builtin_object_map[i].pdhIndex = counter_index;
				break;
			}
		}
	}
	for (counter_text = counter_base; 0 != *counter_text; counter_text += wcslen(counter_text) + 1)
	{
		counter_index = (DWORD)_wtoi(counter_text);
		counter_text += wcslen(counter_text) + 1;
		for (i = 0; i < ARRSIZE(builtin_counter_map); i++)
		{
			if (0 == builtin_counter_map[i].pdhIndex && vi->dwMajorVersion >=
					builtin_counter_map[i].minSupported_dwMajorVersion && vi->dwMinorVersion >=
					builtin_counter_map[i].minSupported_dwMinorVersion && 0 ==
					wcscmp(builtin_counter_map[i].eng_name, counter_text) && SUCCEED ==
					validate_object_counter(get_builtin_object_index(i), counter_index))
			{
				builtin_counter_map[i].pdhIndex = counter_index;
				break;
			}
		}
	}
	zbx_free(eng_names);
#define CHECK_COUNTER_INDICES(index_map)								\
	for (i = 0; i < ARRSIZE(index_map); i++)							\
	{												\
		if (0 == index_map[i].pdhIndex && vi->dwMajorVersion >=					\
				index_map[i].minSupported_dwMajorVersion && vi->dwMinorVersion >=       \
				builtin_counter_map[i].minSupported_dwMinorVersion)                     \
		{											\
			char	*counter;								\
													\
			counter = zbx_unicode_to_utf8(index_map[i].eng_name);				\
			zabbix_log(LOG_LEVEL_ERR, "Failed to initialize builtin counter: %s", counter);	\
			zbx_free(counter);								\
		}											\
	}
	CHECK_COUNTER_INDICES(builtin_object_map);
	CHECK_COUNTER_INDICES(builtin_counter_map);
#undef CHECK_COUNTER_INDICES
out:
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
	return ret;
}
int init_builtin_counter_indexes ( void ) { int ret = SUCCEED , i ; wchar_t * counter_text , * eng_names , * counter_base ; DWORD counter_index ; static const OSVERSIONINFOEX * vi = NULL ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ ) ; if ( NULL == vi && NULL == ( vi = zbx_win_getversion ( ) ) ) { zabbix_log ( LOG_LEVEL_ERR , " " ) ; ret = FAIL ; goto out ; } if ( NULL == ( counter_base = eng_names = get_all_counter_names ( HKEY_PERFORMANCE_TEXT , L" " ) ) ) { ret = FAIL ; goto out ; } counter_base += wcslen ( counter_base ) + 1 ; counter_base += wcslen ( counter_base ) + 1 ; for ( counter_text = counter_base ; 0 != * counter_text ; counter_text += wcslen ( counter_text ) + 1 ) { counter_index = ( DWORD ) _wtoi ( counter_text ) ; counter_text += wcslen ( counter_text ) + 1 ; for ( i = 0 ; i < ARRSIZE ( builtin_object_map ) ; i ++ ) { if ( 0 == builtin_object_map [ i ] . pdhIndex && vi -> dwMajorVersion >= builtin_object_map [ i ] . minSupported_dwMajorVersion && vi -> dwMinorVersion >= builtin_object_map [ i ] . minSupported_dwMinorVersion && 0 == wcscmp ( builtin_object_map [ i ] . eng_name , counter_text ) ) { builtin_object_map [ i ] . pdhIndex = counter_index ; break ; } } } for ( counter_text = counter_base ; 0 != * counter_text ; counter_text += wcslen ( counter_text ) + 1 ) { counter_index = ( DWORD ) _wtoi ( counter_text ) ; counter_text += wcslen ( counter_text ) + 1 ; for ( i = 0 ; i < ARRSIZE ( builtin_counter_map ) ; i ++ ) { if ( 0 == builtin_counter_map [ i ] . pdhIndex && vi -> dwMajorVersion >= builtin_counter_map [ i ] . minSupported_dwMajorVersion && vi -> dwMinorVersion >= builtin_counter_map [ i ] . minSupported_dwMinorVersion && 0 == wcscmp ( builtin_counter_map [ i ] . eng_name , counter_text ) && SUCCEED == validate_object_counter ( get_builtin_object_index ( i ) , counter_index ) ) { builtin_counter_map [ i ] . pdhIndex = counter_index ; break ; } } } zbx_free ( eng_names ) ; #define CHECK_COUNTER_INDICES ( index_map ) 								\
	for (i = 0; i < ARRSIZE(index_map); i++)							\
	{												\
		if (0 == index_map[i].pdhIndex && vi->dwMajorVersion >=					\
				index_map[i].minSupported_dwMajorVersion && vi->dwMinorVersion >=       \
				builtin_counter_map[i].minSupported_dwMinorVersion)                     \
		{											\
			char	*counter;								\
													\
			counter = zbx_unicode_to_utf8(index_map[i].eng_name);				\
			zabbix_log(LOG_LEVEL_ERR, "Failed to initialize builtin counter: %s", counter);	\
			zbx_free(counter);								\
		}											\
	}
 CHECK_COUNTER_INDICES ( builtin_object_map ) ; CHECK_COUNTER_INDICES ( builtin_counter_map ) ; #undef  CHECK_COUNTER_INDICES
 out : zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ , zbx_result_string ( ret ) ) ; return ret ; }
Function: init_builtin_counter_indexes                                     
                                                                            
 Purpose: Scans registry key with all performance counter English names     
          and obtains system-dependent PDH counter indexes for further      
          use by corresponding items.
Function : init_builtin_counter_indexes Purpose : Scans registry key with all performance counter English names and obtains system - dependent PDH counter indexes for further use by corresponding items .
/* Get buffer holding a list of performance counter indexes and English counter names. */ /* L"Counter" stores names, L"Help" stores descriptions ("Help" is not used).          */ /* bypass first pair of counter data elements - these contain number of records */ /* get builtin object names */ /* Get builtin counter names. There may be counter name duplicates. */ /* Validate them in combination with parent object.                 */ /* check if all builtin counter indices are filled */