zabbix-agent2/src/zabbix_agent/stats.c
/******************************************************************************
 *                                                                            *
 * Function: init_collector_data                                              *
 *                                                                            *
 * Purpose: Allocate memory for collector                                     *
 *                                                                            *
 * Author: Eugene Grigorjev                                                   *
 *                                                                            *
 * Comments: Unix version allocates memory as shared.                         *
 *                                                                            *
 ******************************************************************************/
init_collector_data

Purpose: Allocate memory for collector

Author: Eugene Grigorjev

Comments: Unix version allocates memory as shared.
init_collector_data Purpose : Allocate memory for collector Author : Eugene Grigorjev Comments : Unix version allocates memory as shared .
int	init_collector_data(char **error)
{
	int	cpu_count, ret = FAIL;
	size_t	sz, sz_cpu, sz_cpu_phys_util = 0;
	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
	cpu_count = zbx_get_cpu_num();
	sz = ZBX_SIZE_T_ALIGN8(sizeof(ZBX_COLLECTOR_DATA));
#ifdef _WINDOWS
	ZBX_UNUSED(error);
	sz_cpu = sizeof(zbx_perf_counter_data_t *) * (cpu_count + 1);
	collector = zbx_malloc(collector, sz + sz_cpu);
	memset(collector, 0, sz + sz_cpu);
	collector->cpus.cpu_counter = (zbx_perf_counter_data_t **)((char *)collector + sz);
	collector->cpus.count = cpu_count;
#else
	sz_cpu = sizeof(ZBX_SINGLE_CPU_STAT_DATA) * (cpu_count + 1);
#ifdef _AIX
	sz_cpu = ZBX_SIZE_T_ALIGN8(sz_cpu);
	sz_cpu_phys_util = ZBX_SIZE_T_ALIGN8(sizeof(ZBX_CPU_UTIL_PCT_AIX)) * MAX_COLLECTOR_HISTORY * (cpu_count + 1);
#endif
	if (-1 == (shm_id = zbx_shm_create(sz + sz_cpu + sz_cpu_phys_util)))
	{
		*error = zbx_strdup(*error, "cannot allocate shared memory for collector");
		goto out;
	}
	if ((void *)(-1) == (collector = (ZBX_COLLECTOR_DATA *)shmat(shm_id, NULL, 0)))
	{
		*error = zbx_dsprintf(*error, "cannot attach shared memory for collector: %s", zbx_strerror(errno));
		goto out;
	}
	if (-1 == zbx_shm_destroy(shm_id))
	{
		*error = zbx_strdup(*error, "cannot mark the new shared memory for destruction.");
		goto out;
	}
	collector->cpus.cpu = (ZBX_SINGLE_CPU_STAT_DATA *)((char *)collector + sz);
	collector->cpus.count = cpu_count;
	collector->diskstat_shmid = ZBX_NONEXISTENT_SHMID;
#ifdef _AIX
	collector->cpus_phys_util.counters = (ZBX_CPU_UTIL_PCT_AIX *)((char *)collector + sz + sz_cpu);
	collector->cpus_phys_util.row_num = MAX_COLLECTOR_HISTORY;
	collector->cpus_phys_util.column_num = cpu_count + 1;	
	collector->cpus_phys_util.h_latest = 0;
	collector->cpus_phys_util.h_count = 0;
#endif
#ifdef ZBX_PROCSTAT_COLLECTOR
	zbx_procstat_init();
#endif
	if (SUCCEED != zbx_mutex_create(&diskstats_lock, ZBX_MUTEX_DISKSTATS, error))
		goto out;
#endif
#ifdef _AIX
	memset(&collector->vmstat, 0, sizeof(collector->vmstat));
#endif
#if defined(HAVE_KSTAT_H) && defined(HAVE_VMINFO_T_UPDATES)
	if (SUCCEED != zbx_kstat_init(&collector->kstat, error))
		goto out;
#endif
	ret = SUCCEED;
#ifndef _WINDOWS
out:
#endif
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
	return ret;
}
int init_collector_data ( char * * error ) { int cpu_count , ret = FAIL ; size_t sz , sz_cpu , sz_cpu_phys_util = 0 ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ ) ; cpu_count = zbx_get_cpu_num ( ) ; sz = ZBX_SIZE_T_ALIGN8 ( sizeof ( ZBX_COLLECTOR_DATA ) ) ; #ifdef _WINDOWS ZBX_UNUSED ( error ) ; sz_cpu = sizeof ( zbx_perf_counter_data_t * ) * ( cpu_count + 1 ) ; collector = zbx_malloc ( collector , sz + sz_cpu ) ; memset ( collector , 0 , sz + sz_cpu ) ; collector -> cpus . cpu_counter = ( zbx_perf_counter_data_t * * ) ( ( char * ) collector + sz ) ; collector -> cpus . count = cpu_count ; #else sz_cpu = sizeof ( ZBX_SINGLE_CPU_STAT_DATA ) * ( cpu_count + 1 ) ; #ifdef _AIX sz_cpu = ZBX_SIZE_T_ALIGN8 ( sz_cpu ) ; sz_cpu_phys_util = ZBX_SIZE_T_ALIGN8 ( sizeof ( ZBX_CPU_UTIL_PCT_AIX ) ) * MAX_COLLECTOR_HISTORY * ( cpu_count + 1 ) ; #endif if ( -1 == ( shm_id = zbx_shm_create ( sz + sz_cpu + sz_cpu_phys_util ) ) ) { * error = zbx_strdup ( * error , " " ) ; goto out ; } if ( ( void * ) ( -1 ) == ( collector = ( ZBX_COLLECTOR_DATA * ) shmat ( shm_id , NULL , 0 ) ) ) { * error = zbx_dsprintf ( * error , " " , zbx_strerror ( errno ) ) ; goto out ; } if ( -1 == zbx_shm_destroy ( shm_id ) ) { * error = zbx_strdup ( * error , " " ) ; goto out ; } collector -> cpus . cpu = ( ZBX_SINGLE_CPU_STAT_DATA * ) ( ( char * ) collector + sz ) ; collector -> cpus . count = cpu_count ; collector -> diskstat_shmid = ZBX_NONEXISTENT_SHMID ; #ifdef _AIX collector -> cpus_phys_util . counters = ( ZBX_CPU_UTIL_PCT_AIX * ) ( ( char * ) collector + sz + sz_cpu ) ; collector -> cpus_phys_util . row_num = MAX_COLLECTOR_HISTORY ; collector -> cpus_phys_util . column_num = cpu_count + 1 ; collector -> cpus_phys_util . h_latest = 0 ; collector -> cpus_phys_util . h_count = 0 ; #endif #ifdef ZBX_PROCSTAT_COLLECTOR zbx_procstat_init ( ) ; #endif if ( SUCCEED != zbx_mutex_create ( & diskstats_lock , ZBX_MUTEX_DISKSTATS , error ) ) goto out ; #endif #ifdef _AIX memset ( & collector -> vmstat , 0 , sizeof ( collector -> vmstat ) ) ; #endif #if defined ( HAVE_KSTAT_H ) && defined ( HAVE_VMINFO_T_UPDATES ) if ( SUCCEED != zbx_kstat_init ( & collector -> kstat , error ) ) goto out ; #endif ret = SUCCEED ; #ifndef _WINDOWS out : #endif zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ ) ; return ret ; }
Function: init_collector_data                                              
                                                                            
 Purpose: Allocate memory for collector                                     
                                                                            
 Author: Eugene Grigorjev                                                   
                                                                            
 Comments: Unix version allocates memory as shared.
Function : init_collector_data Purpose : Allocate memory for collector Author : Eugene Grigorjev Comments : Unix version allocates memory as shared .
/* Immediately mark the new shared memory for destruction after attaching to it */ /* each CPU + total for all CPUs */