zabbix-agent2/src/zabbix_agent/perfstat.c
/******************************************************************************
 *                                                                            *
 * Comments: if the specified counter exists or a new is successfully         *
 *           added, a pointer to that counter is returned, NULL otherwise     *
 *                                                                            *
 ******************************************************************************/
if the specified counter exists or a new is successfully
added, a pointer to that counter is returned, NULL otherwise
if the specified counter exists or a new is successfully added a pointer to that counter is returned NULL otherwise
zbx_perf_counter_data_t	*add_perf_counter(const char *name, const char *counterpath, int interval,
		zbx_perf_counter_lang_t lang, char **error)
{
	zbx_perf_counter_data_t	*cptr = NULL;
	PDH_STATUS		pdh_status;
	int			added = FAIL;
	zabbix_log(LOG_LEVEL_DEBUG, "In %s() counter:'%s' interval:%d", __func__, counterpath, interval);
	LOCK_PERFCOUNTERS;
	if (SUCCEED != perf_collector_started())
	{
		*error = zbx_strdup(*error, "Performance collector is not started.");
		goto out;
	}
	for (cptr = ppsd.pPerfCounterList; ; cptr = cptr->next)
	{
		if (NULL == cptr)
		{
			cptr = (zbx_perf_counter_data_t *)zbx_malloc(cptr, sizeof(zbx_perf_counter_data_t));
			memset(cptr, 0, sizeof(zbx_perf_counter_data_t));
			if (NULL != name)
				cptr->name = zbx_strdup(NULL, name);
			cptr->counterpath = zbx_strdup(NULL, counterpath);
			cptr->interval = interval;
			cptr->lang = lang;
			cptr->value_current = -1;
			cptr->value_array = (double *)zbx_malloc(cptr->value_array, sizeof(double) * interval);
			pdh_status = zbx_PdhAddCounter(__func__, cptr, ppsd.pdh_query, counterpath,
					lang, &cptr->handle);
			cptr->next = ppsd.pPerfCounterList;
			ppsd.pPerfCounterList = cptr;
			if (ERROR_SUCCESS != pdh_status && PDH_CSTATUS_NO_INSTANCE != pdh_status)
			{
				*error = zbx_dsprintf(*error, "Invalid performance counter format.");
				cptr = NULL;	
			}
			added = SUCCEED;
			break;
		}
		if (NULL != name)
		{
			if (0 == strcmp(cptr->name, name))
				break;
		}
		else if (0 == strcmp(cptr->counterpath, counterpath) &&
				cptr->interval == interval && cptr->lang == lang)
		{
			break;
		}
	}
	if (FAIL == added)
	{
		zabbix_log(LOG_LEVEL_DEBUG, "%s() counter '%s' already exists", __func__, counterpath);
	}
	else if (NULL != name && NULL != cptr)
	{
		char	*alias_name;
		alias_name = zbx_dsprintf(NULL, "__UserPerfCounter[%s]", name);
		add_alias(name, alias_name);
		zbx_free(alias_name);
	}
out:
	UNLOCK_PERFCOUNTERS;
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s(): %s", __func__, NULL == cptr ? "FAIL" : "SUCCEED");
	return cptr;
}
zbx_perf_counter_data_t * add_perf_counter ( const char * name , const char * counterpath , int interval , zbx_perf_counter_lang_t lang , char * * error ) { zbx_perf_counter_data_t * cptr = NULL ; PDH_STATUS pdh_status ; int added = FAIL ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ , counterpath , interval ) ; LOCK_PERFCOUNTERS ; if ( SUCCEED != perf_collector_started ( ) ) { * error = zbx_strdup ( * error , " " ) ; goto out ; } for ( cptr = ppsd . pPerfCounterList ; ; cptr = cptr -> next ) { if ( NULL == cptr ) { cptr = ( zbx_perf_counter_data_t * ) zbx_malloc ( cptr , sizeof ( zbx_perf_counter_data_t ) ) ; memset ( cptr , 0 , sizeof ( zbx_perf_counter_data_t ) ) ; if ( NULL != name ) cptr -> name = zbx_strdup ( NULL , name ) ; cptr -> counterpath = zbx_strdup ( NULL , counterpath ) ; cptr -> interval = interval ; cptr -> lang = lang ; cptr -> value_current = -1 ; cptr -> value_array = ( double * ) zbx_malloc ( cptr -> value_array , sizeof ( double ) * interval ) ; pdh_status = zbx_PdhAddCounter ( __func__ , cptr , ppsd . pdh_query , counterpath , lang , & cptr -> handle ) ; cptr -> next = ppsd . pPerfCounterList ; ppsd . pPerfCounterList = cptr ; if ( ERROR_SUCCESS != pdh_status && PDH_CSTATUS_NO_INSTANCE != pdh_status ) { * error = zbx_dsprintf ( * error , " " ) ; cptr = NULL ; } added = SUCCEED ; break ; } if ( NULL != name ) { if ( 0 == strcmp ( cptr -> name , name ) ) break ; } else if ( 0 == strcmp ( cptr -> counterpath , counterpath ) && cptr -> interval == interval && cptr -> lang == lang ) { break ; } } if ( FAIL == added ) { zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ , counterpath ) ; } else if ( NULL != name && NULL != cptr ) { char * alias_name ; alias_name = zbx_dsprintf ( NULL , " " , name ) ; add_alias ( name , alias_name ) ; zbx_free ( alias_name ) ; } out : UNLOCK_PERFCOUNTERS ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ , NULL == cptr ? " " : " " ) ; return cptr ; }
Comments: if the specified counter exists or a new is successfully         
           added, a pointer to that counter is returned, NULL otherwise
Comments : if the specified counter exists or a new is successfully added a pointer to that counter is returned NULL otherwise
/* add new parameters */ /* initialize the counter */ /* add the counter to the query */ /* indicate a failure */