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
|
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "stats.h"
#include "log.h"
#include "zbxconf.h"
#ifndef _WINDOWS
# include "diskdevices.h"
#endif
#include "cfg.h"
#include "mutexs.h"
#ifdef _WINDOWS
# include "service.h"
# include "perfstat.h"
/* defined in sysinfo lib */
extern int get_cpu_num_win32(void);
#else
# include "daemon.h"
# include "ipc.h"
#endif
ZBX_COLLECTOR_DATA *collector = NULL;
extern ZBX_THREAD_LOCAL unsigned char process_type;
extern ZBX_THREAD_LOCAL int server_num, process_num;
#ifndef _WINDOWS
static int shm_id;
int my_diskstat_shmid = ZBX_NONEXISTENT_SHMID;
ZBX_DISKDEVICES_DATA *diskdevices = NULL;
zbx_mutex_t diskstats_lock = ZBX_MUTEX_NULL;
#endif
/******************************************************************************
* *
* Function: zbx_get_cpu_num *
* *
* Purpose: returns the number of processors which are currently online *
* (i.e., available). *
* *
* Return value: number of CPUs *
* *
* Author: Eugene Grigorjev *
* *
******************************************************************************/
static int zbx_get_cpu_num(void)
{
#if defined(_WINDOWS)
return get_cpu_num_win32();
#elif defined(HAVE_SYS_PSTAT_H)
struct pst_dynamic psd;
if (-1 == pstat_getdynamic(&psd, sizeof(struct pst_dynamic), 1, 0))
goto return_one;
return (int)psd.psd_proc_cnt;
#elif defined(_SC_NPROCESSORS_CONF)
/* FreeBSD 7.0 x86 */
/* Solaris 10 x86 */
/* AIX 6.1 */
int ncpu;
if (-1 == (ncpu = sysconf(_SC_NPROCESSORS_CONF)))
goto return_one;
return ncpu;
#elif defined(HAVE_FUNCTION_SYSCTL_HW_NCPU)
/* FreeBSD 6.2 x86; FreeBSD 7.0 x86 */
/* NetBSD 3.1 x86; NetBSD 4.0 x86 */
/* OpenBSD 4.2 x86 */
size_t len;
int mib[] = {CTL_HW, HW_NCPU}, ncpu;
len = sizeof(ncpu);
if (0 != sysctl(mib, 2, &ncpu, &len, NULL, 0))
goto return_one;
return ncpu;
#elif defined(HAVE_PROC_CPUINFO)
FILE *f = NULL;
int ncpu = 0;
if (NULL == (file = fopen("/proc/cpuinfo", "r")))
goto return_one;
while (NULL != fgets(line, 1024, file))
{
if (NULL == strstr(line, "processor"))
continue;
ncpu++;
}
zbx_fclose(file);
if (0 == ncpu)
goto return_one;
return ncpu;
#endif
#ifndef _WINDOWS
return_one:
zabbix_log(LOG_LEVEL_WARNING, "cannot determine number of CPUs, assuming 1");
return 1;
#endif
}
/******************************************************************************
* *
* Function: 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;
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);
if (-1 == (shm_id = zbx_shm_create(sz + sz_cpu)))
{
*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;
}
/* Immediately mark the new shared memory for destruction after attaching to it */
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 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
ret = SUCCEED;
#ifndef _WINDOWS
out:
#endif
zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
return ret;
}
/******************************************************************************
* *
* Function: free_collector_data *
* *
* Purpose: Free memory allocated for collector *
* *
* Author: Eugene Grigorjev *
* *
* Comments: Unix version allocated memory as shared. *
* *
******************************************************************************/
void free_collector_data(void)
{
#ifdef _WINDOWS
zbx_free(collector);
#else
if (NULL == collector)
return;
#ifdef ZBX_PROCSTAT_COLLECTOR
zbx_procstat_destroy();
#endif
if (ZBX_NONEXISTENT_SHMID != collector->diskstat_shmid)
{
if (-1 == shmctl(collector->diskstat_shmid, IPC_RMID, 0))
zabbix_log(LOG_LEVEL_WARNING, "cannot remove shared memory for disk statistics collector: %s",
zbx_strerror(errno));
diskdevices = NULL;
collector->diskstat_shmid = ZBX_NONEXISTENT_SHMID;
}
if (-1 == shmctl(shm_id, IPC_RMID, 0))
zabbix_log(LOG_LEVEL_WARNING, "cannot remove shared memory for collector: %s", zbx_strerror(errno));
zbx_mutex_destroy(&diskstats_lock);
#endif
collector = NULL;
}
/******************************************************************************
* *
* Function: diskstat_shm_init *
* *
* Purpose: Allocate shared memory for collecting disk statistics *
* *
******************************************************************************/
void diskstat_shm_init(void)
{
#ifndef _WINDOWS
size_t shm_size;
/* initially allocate memory for collecting statistics for only 1 disk */
shm_size = sizeof(ZBX_DISKDEVICES_DATA);
if (-1 == (collector->diskstat_shmid = zbx_shm_create(shm_size)))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot allocate shared memory for disk statistics collector");
exit(EXIT_FAILURE);
}
if ((void *)(-1) == (diskdevices = (ZBX_DISKDEVICES_DATA *)shmat(collector->diskstat_shmid, NULL, 0)))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot attach shared memory for disk statistics collector: %s",
zbx_strerror(errno));
exit(EXIT_FAILURE);
}
diskdevices->count = 0;
diskdevices->max_diskdev = 1;
my_diskstat_shmid = collector->diskstat_shmid;
zabbix_log(LOG_LEVEL_DEBUG, "diskstat_shm_init() allocated initial shm segment id:%d"
" for disk statistics collector", collector->diskstat_shmid);
#endif
}
/******************************************************************************
* *
* Function: diskstat_shm_reattach *
* *
* Purpose: If necessary, reattach to disk statistics shared memory segment. *
* *
******************************************************************************/
void diskstat_shm_reattach(void)
{
#ifndef _WINDOWS
if (my_diskstat_shmid != collector->diskstat_shmid)
{
int old_shmid;
old_shmid = my_diskstat_shmid;
if (ZBX_NONEXISTENT_SHMID != my_diskstat_shmid)
{
if (-1 == shmdt((void *) diskdevices))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot detach from disk statistics collector shared"
" memory: %s", zbx_strerror(errno));
exit(EXIT_FAILURE);
}
diskdevices = NULL;
my_diskstat_shmid = ZBX_NONEXISTENT_SHMID;
}
if ((void *)(-1) == (diskdevices = (ZBX_DISKDEVICES_DATA *)shmat(collector->diskstat_shmid, NULL, 0)))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot attach shared memory for disk statistics collector: %s",
zbx_strerror(errno));
exit(EXIT_FAILURE);
}
my_diskstat_shmid = collector->diskstat_shmid;
zabbix_log(LOG_LEVEL_DEBUG, "diskstat_shm_reattach() switched shm id from %d to %d",
old_shmid, my_diskstat_shmid);
}
#endif
}
/******************************************************************************
* *
* Function: diskstat_shm_extend *
* *
* Purpose: create a new, larger disk statistics shared memory segment and *
* copy data from the old one. *
* *
******************************************************************************/
void diskstat_shm_extend(void)
{
#ifndef _WINDOWS
size_t old_shm_size, new_shm_size;
int old_shmid, new_shmid, old_max, new_max;
ZBX_DISKDEVICES_DATA *new_diskdevices;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
/* calculate the size of the new shared memory segment */
old_max = diskdevices->max_diskdev;
if (old_max < 4)
new_max = old_max + 1;
else if (old_max < 256)
new_max = old_max * 2;
else
new_max = old_max + 256;
old_shm_size = sizeof(ZBX_DISKDEVICES_DATA) + sizeof(ZBX_SINGLE_DISKDEVICE_DATA) * (old_max - 1);
new_shm_size = sizeof(ZBX_DISKDEVICES_DATA) + sizeof(ZBX_SINGLE_DISKDEVICE_DATA) * (new_max - 1);
if (-1 == (new_shmid = zbx_shm_create(new_shm_size)))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot allocate shared memory for extending disk statistics collector");
exit(EXIT_FAILURE);
}
if ((void *)(-1) == (new_diskdevices = (ZBX_DISKDEVICES_DATA *)shmat(new_shmid, NULL, 0)))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot attach shared memory for extending disk statistics collector: %s",
zbx_strerror(errno));
exit(EXIT_FAILURE);
}
/* copy data from the old segment */
memcpy(new_diskdevices, diskdevices, old_shm_size);
new_diskdevices->max_diskdev = new_max;
/* delete the old segment */
if (-1 == shmdt((void *) diskdevices))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot detach from disk statistics collector shared memory");
exit(EXIT_FAILURE);
}
if (-1 == zbx_shm_destroy(collector->diskstat_shmid))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot destroy old disk statistics collector shared memory");
exit(EXIT_FAILURE);
}
/* switch to the new segment */
old_shmid = collector->diskstat_shmid;
collector->diskstat_shmid = new_shmid;
my_diskstat_shmid = collector->diskstat_shmid;
diskdevices = new_diskdevices;
zabbix_log(LOG_LEVEL_DEBUG, "End of %s() extended diskstat shared memory: old_max:%d new_max:%d old_size:"
ZBX_FS_SIZE_T " new_size:" ZBX_FS_SIZE_T " old_shmid:%d new_shmid:%d", __func__, old_max,
new_max, (zbx_fs_size_t)old_shm_size, (zbx_fs_size_t)new_shm_size, old_shmid,
collector->diskstat_shmid);
#endif
}
/******************************************************************************
* *
* Function: collector_thread *
* *
* Purpose: Collect system information *
* *
* Author: Eugene Grigorjev *
* *
******************************************************************************/
ZBX_THREAD_ENTRY(collector_thread, args)
{
assert(args);
process_type = ((zbx_thread_args_t *)args)->process_type;
server_num = ((zbx_thread_args_t *)args)->server_num;
process_num = ((zbx_thread_args_t *)args)->process_num;
zabbix_log(LOG_LEVEL_INFORMATION, "agent #%d started [collector]", server_num);
zbx_free(args);
#ifdef _AIX
/* Initialize collecting of vmstat data early. This helps getting the real values on the */
/* first request. Also on the first request collector is starting to update vmstat data. */
collect_vmstat_data(&collector->vmstat);
#endif
if (SUCCEED != init_cpu_collector(&(collector->cpus)))
free_cpu_collector(&(collector->cpus));
while (ZBX_IS_RUNNING())
{
zbx_update_env(zbx_time());
zbx_setproctitle("collector [processing data]");
#ifdef _WINDOWS
collect_perfstat();
#else
if (0 != CPU_COLLECTOR_STARTED(collector))
collect_cpustat(&(collector->cpus));
if (0 != DISKDEVICE_COLLECTOR_STARTED(collector))
collect_stats_diskdevices();
#ifdef ZBX_PROCSTAT_COLLECTOR
zbx_procstat_collect();
#endif
#endif
#ifdef _AIX
if (1 == collector->vmstat.enabled)
collect_vmstat_data(&collector->vmstat);
#endif
zbx_setproctitle("collector [idle 1 sec]");
zbx_sleep(1);
}
#ifdef _WINDOWS
if (0 != CPU_COLLECTOR_STARTED(collector))
free_cpu_collector(&(collector->cpus));
ZBX_DO_EXIT();
zbx_thread_exit(EXIT_SUCCESS);
#else
zbx_setproctitle("%s #%d [terminated]", get_process_type_string(process_type), process_num);
while (1)
zbx_sleep(SEC_PER_MIN);
#endif
}
|