summaryrefslogtreecommitdiffstats
path: root/src/zabbix_agent/cpustat.c
blob: d27b72818b553b069874b88afa356a498a18ca50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* 
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/

#include "common.h"
#include "cpustat.h"

#include "log.h"

#ifdef WIN32

	#include "perfmon.h"

#else /* not WIN32 */

	static int	get_cpustat(
		int *now,
		float *cpu_user,
		float *cpu_system,
		float *cpu_nice,
		float *cpu_idle
		);

	static void	apply_cpustat(
		ZBX_CPUS_STAT_DATA *pcpus,
		int now, 
		float cpu_user, 
		float cpu_system,
		float cpu_nice,
		float cpu_idle
		);

#endif /* WIN32 */


/******************************************************************************
 *                                                                            *
 * Function: init_cpu_collector                                               *
 *                                                                            *
 * Purpose: Initialize statistic structure and prepare state                  *
 *          for data calculation                                              *
 *                                                                            *
 * Parameters:  pcpus - pointer to the structure                              *
 *                      of ZBX_CPUS_STAT_DATA type                            *
 *                                                                            *
 * Return value: If the function succeeds, the return 0,                      *
 *               great than 0 on an error                                     *
 *                                                                            *
 * Author: Eugene Grigorjev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/

int	init_cpu_collector(ZBX_CPUS_STAT_DATA *pcpus)
{
#ifdef WIN32

	SYSTEM_INFO	sysInfo;
	PDH_STATUS	status;
	int	i;

	char counter_path[MAX_COUNTER_PATH];

	GetSystemInfo(&sysInfo);

	pcpus->count = sysInfo.dwNumberOfProcessors;

	memset(pcpus, 0, sizeof(ZBX_CPUS_STAT_DATA));

	if (PdhOpenQuery(NULL,0,&pcpus->pdh_query)!=ERROR_SUCCESS)
	{
		zabbix_log( LOG_LEVEL_ERR, "Call to PdhOpenQuery() failed: %s", strerror_from_system(GetLastError()));
		return 1;
	}

	zbx_snprintf(counter_path, sizeof(counter_path), "\\%s(_Total)\\%s",GetCounterName(PCI_PROCESSOR),GetCounterName(PCI_PROCESSOR_TIME));

	if (ERROR_SUCCESS != (status = PdhAddCounter(
		pcpus->pdh_query, 
		counter_path, 0, 
		&pcpus->cpu[0].usage_couter)))
	{
		zabbix_log( LOG_LEVEL_ERR, "Unable to add performance counter \"%s\" to query: %s", strerror_from_module(status,"PDH.DLL"));
		return 2;
	}

	for(i=1 /* 0 - is Total cpus */; i <= pcpus->count /* "<=" instead of  "+ 1" */; i++)
	{
		zbx_snprintf(counter_path, sizeof(counter_path),"\\%s(%d)\\%s", GetCounterName(PCI_PROCESSOR), i-1, GetCounterName(PCI_PROCESSOR_TIME));

		if (ERROR_SUCCESS != (status = PdhAddCounter(
			pcpus->pdh_query, 
			counter_path,0,
			&pcpus->cpu[i].usage_couter)))
		{
			zabbix_log( LOG_LEVEL_ERR, "Unable to add performance counter \"%s\" to query: %s", strerror_from_module(status,"PDH.DLL"));
			return 2;
		}
	}

	if (ERROR_SUCCESS != (status = PdhCollectQueryData(pcpus->pdh_query)))
	{
		zabbix_log( LOG_LEVEL_ERR, "Call to PdhCollectQueryData() failed: %s", strerror_from_module(status,"PDH.DLL"));
		return 3;
	}

	for(i = 1; i <= pcpus->count; i++)
	{
		PdhGetRawCounterValue(pcpus->cpu[i].usage_couter, NULL, &pcpus->cpu[i].usage_old);
	}


	zbx_snprintf(counter_path, sizeof(counter_path), "\\%s\\%s", GetCounterName(PCI_SYSTEM), GetCounterName(PCI_PROCESSOR_QUEUE_LENGTH));

	// Prepare for CPU execution queue usage collection
	if (ERROR_SUCCESS != (status = PdhAddCounter(pcpus->pdh_query, counter_path, 0, &pcpus->queue_counter)))
	{
		zabbix_log( LOG_LEVEL_ERR, "Unable to add performance counter \"%s\" to query: %s", strerror_from_module(status,"PDH.DLL"));
		return 2;
	}

#else /* not WIN32 */

	memset(pcpus, 0, sizeof(ZBX_CPUS_STAT_DATA));


#endif /* WIN32 */

	return 0;
}

/******************************************************************************
 *                                                                            *
 * Function: close_cpu_collector                                              *
 *                                                                            *
 * Purpose: Cleare state of data calculation                                  *
 *                                                                            *
 * Parameters:  pcpus - pointer to the structure                              *
 *                      of ZBX_CPUS_STAT_DATA type                            *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Author: Eugene Grigorjev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/

void	close_cpu_collector(ZBX_CPUS_STAT_DATA *pcpus)
{
#ifdef WIN32

	int i;

	if(pcpus->queue_counter)
	{
		PdhRemoveCounter(pcpus->queue_counter);
		pcpus->queue_counter = NULL;
	}

	for(i=0; i < MAX_CPU; i++)
	{
		if(pcpus->cpu[i].usage_couter)
		{
			PdhRemoveCounter(pcpus->cpu[i].usage_couter);
			pcpus->cpu[i].usage_couter = NULL;
		}
	}
	
	if(pcpus->pdh_query)
	{
		PdhCloseQuery(pcpus->pdh_query);
		pcpus->pdh_query = NULL;
	}

#endif /* WIN32 */

}

void	collect_cpustat(ZBX_CPUS_STAT_DATA *pcpus)
{
#ifdef WIN32

	PDH_FMT_COUNTERVALUE 
		value;
	PDH_STATUS	
		status;
	LONG	sum;
	int	i,
		j,
		n;

	if(!pcpus->queue_counter) return;

	if ((status = PdhCollectQueryData(pcpus->pdh_query)) != ERROR_SUCCESS)
	{
		zabbix_log( LOG_LEVEL_ERR, "Call to PdhCollectQueryData() failed: %s", strerror_from_module(status,"PDH.DLL"));
		return;
	}

	// Process CPU utilization data
	for(i=0; i <= pcpus->count; i++)
	{
		if(!pcpus->cpu[i].usage_couter)
			continue;

		PdhGetRawCounterValue(
			pcpus->cpu[i].usage_couter, 
			NULL, 
			&pcpus->cpu[i].usage);

		PdhCalculateCounterFromRawValue(
			pcpus->cpu[i].usage_couter,
			PDH_FMT_LONG,
			&pcpus->cpu[i].usage,
			&pcpus->cpu[i].usage_old, 
			&value);

		pcpus->cpu[i].h_usage[pcpus->cpu[i].h_usage_index] = value.longValue;
		pcpus->cpu[i].usage_old = pcpus->cpu[i].usage;

		// Calculate average cpu usage
		for(n = pcpus->cpu[i].h_usage_index, j = 0, sum = 0; j < MAX_CPU_HISTORY; j++, n--)
		{
			if(n < 0) n = MAX_CPU_HISTORY - 1;

			sum += pcpus->cpu[i].h_usage[n];

			if(j == 60) /* cpu usage for last minute */
			{
				pcpus->cpu[i].util1 = ((double)sum)/(double)j;
			}
			else if(j == 300) /* cpu usage for last five minutes */
			{
				pcpus->cpu[i].util5 = ((double)sum)/(double)j;
			}
		}

		/* cpu usage for last fifteen minutes */
		pcpus->cpu[i].util15 = ((double)sum)/(double)MAX_CPU_HISTORY;
		
		pcpus->cpu[i].h_usage_index++;
		if (pcpus->cpu[i].h_usage_index == MAX_CPU_HISTORY)
			pcpus->cpu[i].h_usage_index = 0;
	}


	if(pcpus->queue_counter)
	{
		// Process CPU queue length data
		PdhGetRawCounterValue(
			pcpus->queue_counter,
			NULL,
			&pcpus->queue);

		PdhCalculateCounterFromRawValue(
			pcpus->queue_counter,
			PDH_FMT_LONG,
			&pcpus->queue,
			NULL,
			&value);

		pcpus->h_queue[pcpus->h_queue_index] = value.longValue;

		// Calculate average cpu usage
		for(n = pcpus->h_queue_index, j = 0, sum = 0; j < MAX_CPU_HISTORY; j++, n--)
		{
			if(n < 0) n = MAX_CPU_HISTORY - 1;

			sum += pcpus->h_queue[n];

			if(j == 60) /* processor(s) load for last minute */
			{
				pcpus->load1 = ((double)sum)/(double)j;
			}
			else if(j == 300) /* processor(s) load for last five minutes */
			{
				pcpus->load5 = ((double)sum)/(double)j;
			}
		}

		/* cpu usage for last fifteen minutes */
		pcpus->load15 = ((double)sum)/(double)MAX_CPU_HISTORY;

		pcpus->h_queue_index++;

		if (pcpus->h_queue_index == MAX_CPU_HISTORY)
			pcpus->h_queue_index = 0;
	}
#else /* not WIN32 */

	int	now = 0;
	float	cpu_user, cpu_nice, cpu_system, cpu_idle;


	if(0 != get_cpustat(&now, &cpu_user, &cpu_system, &cpu_nice, &cpu_idle))
		return;

	apply_cpustat(pcpus, now, cpu_user, cpu_system, cpu_nice, cpu_idle);

#endif /* WIN32 */
}

#if !defined(WIN32)

static int	get_cpustat(int *now,float *cpu_user,float *cpu_system,float *cpu_nice,float *cpu_idle)
{
    #if defined(HAVE_PROC_STAT)
	
	FILE	*file;
	char	line[MAX_STRING_LEN];
	
    #elif defined(HAVE_SYS_PSTAT_H) /* not HAVE_PROC_STAT */
	
	struct pst_dynamic stats;
	
    #else /* not HAVE_SYS_PSTAT_H */

	return 1;
	
    #endif /* HAVE_PROC_STAT */

	*now = time(NULL);

    #if defined(HAVE_PROC_STAT)
	
	if(NULL == (file = fopen("/proc/stat","r") ))
	{
		zbx_error("Cannot open [%s] [%s]\n","/proc/stat", strerror(errno));
		return 1;
	}

	*cpu_user = *cpu_nice = *cpu_system = *cpu_idle = -1;

	while(fgets(line,1024,file) != NULL)
	{
		if(strstr(line,"cpu ") == NULL) continue;

		sscanf(line, "cpu %f %f %f %f", cpu_user, cpu_nice, cpu_system, cpu_idle);
		break;
	}
	zbx_fclose(file);

	if(*cpu_user < 0) 
		return 1;
	
    #elif defined(HAVE_SYS_PSTAT_H) /* HAVE_PROC_STAT */

	pstat_getdynamic(&stats, sizeof( struct pst_dynamic ), 1, 0 );
	*cpu_user 	= (float)stats.psd_cpu_time[CP_USER];
	*cpu_nice 	= (float)stats.psd_cpu_time[CP_SYS];
	*cpu_system 	= (float)stats.psd_cpu_time[CP_NICE];
	*cpu_idle 	= (float)stats.psd_cpu_time[CP_IDLE];
	
    #endif /* HAVE_SYS_PSTAT_H */
	return 0;
}


#define CALC_CPU_LOAD(now_val, tim_val, now_all_val, tim_all_val)                             \
	if((now_val) - (tim_val) > 0 && (now_all_val) - (tim_all_val) > 0)                    \
	{                                                                                     \
		tim_val = 100 * (float)((now_val) - (tim_val)/(now_all_val) - (tim_all_val)); \
	}                                                                                     \
	else                                                                                  \
	{                                                                                     \
		tim_val = 0;                                                                  \
	}

static void	apply_cpustat(
	ZBX_CPUS_STAT_DATA *pcpus,
	int now, 
	float cpu_user, 
	float cpu_system,
	float cpu_nice,
	float cpu_idle
	)
{
	int	i	= 0,
		time	= 0,
		time1	= 0,
		time5	= 0,
		time15	= 0;

	for(i=0; i < MAX_CPU_HISTORY; i++)
	{
		if(pcpus->clock[i] < now - MAX_CPU_HISTORY)
		{
			pcpus->clock[i]	= now;

			pcpus->user	= pcpus->h_user[i]	= cpu_user;
			pcpus->system	= pcpus->h_system[i]	= cpu_system;
			pcpus->nice	= pcpus->h_nice[i]	= cpu_nice;
			pcpus->idle	= pcpus->h_idle[i]	= cpu_idle;

			pcpus->all	= cpu_idle + cpu_user + cpu_nice + cpu_system;
			break;
		}
	}

	time = time1 = time5 = time15 = now+1;

	for(i=0; i < MAX_CPU_HISTORY; i++)
	{
		if(0 == pcpus->clock[i])	continue;

		if(pcpus->clock[i] == now)
		{
			pcpus->idle	= pcpus->h_idle[i];
			pcpus->user	= pcpus->h_user[i];
			pcpus->nice	= pcpus->h_nice[i];
			pcpus->system	= pcpus->h_system[i];
			pcpus->all	= pcpus->idle + pcpus->user + pcpus->nice + pcpus->system;
		}

		if((pcpus->clock[i] >= (now - 60)) && (time1 > pcpus->clock[i]))
		{
			time1		= pcpus->clock[i];
			pcpus->idle1	= pcpus->h_idle[i];
			pcpus->user1	= pcpus->h_user[i];
			pcpus->nice1	= pcpus->h_nice[i];
			pcpus->system1	= pcpus->h_system[i];
			pcpus->all1	= pcpus->idle1 + pcpus->user1 + pcpus->nice1 + pcpus->system1;
		}
		if((pcpus->clock[i] >= (now - (5*60))) && (time5 > pcpus->clock[i]))
		{
			time5		= pcpus->clock[i];
			pcpus->idle5	= pcpus->h_idle[i];
			pcpus->user5	= pcpus->h_user[i];
			pcpus->nice5	= pcpus->h_nice[i];
			pcpus->system5	= pcpus->h_system[i];
			pcpus->all5	= pcpus->idle5 + pcpus->user5 + pcpus->nice5 + pcpus->system5;
		}
		if((pcpus->clock[i] >= (now - (15*60))) && (time15 > pcpus->clock[i]))
		{
			time15		= pcpus->clock[i];
			pcpus->idle15	= pcpus->h_idle[i];
			pcpus->user15	= pcpus->h_user[i];
			pcpus->nice15	= pcpus->h_nice[i];
			pcpus->system15	= pcpus->h_system[i];
			pcpus->all15	= pcpus->idle15 + pcpus->user15 + pcpus->nice15 + pcpus->system15;
		}
	}

	CALC_CPU_LOAD(pcpus->idle, pcpus->idle1,	pcpus->all, pcpus->all1);
	CALC_CPU_LOAD(pcpus->idle, pcpus->idle5,	pcpus->all, pcpus->all5);
	CALC_CPU_LOAD(pcpus->idle, pcpus->idle15,	pcpus->all, pcpus->all15);

	CALC_CPU_LOAD(pcpus->user, pcpus->user1,	pcpus->all, pcpus->all1);
	CALC_CPU_LOAD(pcpus->user, pcpus->user5,	pcpus->all, pcpus->all5);
	CALC_CPU_LOAD(pcpus->user, pcpus->user15,	pcpus->all, pcpus->all15);

	CALC_CPU_LOAD(pcpus->nice, pcpus->nice1,	pcpus->all, pcpus->all1);
	CALC_CPU_LOAD(pcpus->nice, pcpus->nice5,	pcpus->all, pcpus->all5);
	CALC_CPU_LOAD(pcpus->nice, pcpus->nice15,	pcpus->all, pcpus->all15);

	CALC_CPU_LOAD(pcpus->system, pcpus->system1,	pcpus->all, pcpus->all1);
	CALC_CPU_LOAD(pcpus->system, pcpus->system5,	pcpus->all, pcpus->all5);
	CALC_CPU_LOAD(pcpus->system, pcpus->system15,	pcpus->all, pcpus->all15);
}

#endif /* not WIN32 */