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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
|
/*
** Zabbix
** Copyright (C) 2001-2014 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 "sysinfo.h"
#include "cfg.h"
#include "log.h"
#include "zbxconf.h"
#include "zbxgetopt.h"
#include "zbxself.h"
#ifndef _WINDOWS
# include "../libs/zbxnix/control.h"
#endif
#ifndef _WINDOWS
# include "zbxmodules.h"
#endif
#include "comms.h"
#include "alias.h"
#include "stats.h"
#ifdef _WINDOWS
# include "perfstat.h"
#else
# include "sighandler.h"
#endif
#include "active.h"
#include "listener.h"
#include "symbols.h"
#if defined(ZABBIX_SERVICE)
# include "service.h"
#elif defined(ZABBIX_DAEMON)
# include "daemon.h"
#endif
#include "setproctitle.h"
const char *progname = NULL;
/* default config file location */
#ifdef _WINDOWS
static char DEFAULT_CONFIG_FILE[] = "C:\\zabbix_agentd.conf";
#else
static char DEFAULT_CONFIG_FILE[] = SYSCONFDIR "/zabbix_agentd.conf";
#endif
/* application TITLE */
const char title_message[] = APPLICATION_NAME
#if defined(_WIN64)
" Win64"
#elif defined(WIN32)
" Win32"
#endif
#if defined(ZABBIX_SERVICE)
" (service)"
#elif defined(ZABBIX_DAEMON)
" (daemon)"
#endif
;
/* end of application TITLE */
const char syslog_app_name[] = "zabbix_agentd";
/* application USAGE message */
const char usage_message[] =
#ifndef _WINDOWS
"[-VRhp]"
#endif
#ifdef _WINDOWS
"[-Vhp] [-idsx] [-m]"
#endif
" [-c <config-file>] [-t <item key>]";
/* end of application USAGE message */
/* application HELP message */
const char *help_message[] = {
"Options:",
"",
" -c --config <config-file> Absolute path to the configuration file",
" -p --print Print known items and exit",
" -t --test <item key> Test specified item and exit",
" -h --help Display help information",
" -V --version Display version number",
#ifndef _WINDOWS
" -R --runtime-control <option> Perform administrative functions",
"",
"Runtime control options:",
" " ZBX_LOG_LEVEL_INCREASE "=<target> Increase log level, affect all processes if target is not specified",
" " ZBX_LOG_LEVEL_DECREASE "=<target> Decrease log level, affect all processes if target is not specified",
"",
"Log level control targets:",
" <pid> Process identifier",
" <process type> All processes of specified type (e.g. poller)",
" <process type,N> Process type and number (e.g. poller,3)",
#endif
#ifdef _WINDOWS
"",
"Functions:",
"",
" -i --install Install Zabbix agent as service",
" -d --uninstall Uninstall Zabbix agent from service",
" -s --start Start Zabbix agent service",
" -x --stop Stop Zabbix agent service",
" -m --multiple-agents Service name will include hostname",
#endif
NULL /* end of text */
};
/* end of application HELP message */
/* COMMAND LINE OPTIONS */
static struct zbx_option longopts[] =
{
{"config", 1, NULL, 'c'},
{"runtime-control", 1, NULL, 'R'},
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'V'},
{"print", 0, NULL, 'p'},
{"test", 1, NULL, 't'},
#ifdef _WINDOWS
{"install", 0, NULL, 'i'},
{"uninstall", 0, NULL, 'd'},
{"start", 0, NULL, 's'},
{"stop", 0, NULL, 'x'},
{"multiple-agents", 0, NULL, 'm'},
#endif
{NULL}
};
static char shortopts[] =
"c:hVR:pt:"
#ifdef _WINDOWS
"idsxm"
#endif
;
/* end of COMMAND LINE OPTIONS */
static char *TEST_METRIC = NULL;
int threads_num = 0;
ZBX_THREAD_HANDLE *threads = NULL;
unsigned char daemon_type = ZBX_DAEMON_TYPE_AGENT;
ZBX_THREAD_LOCAL unsigned char process_type = 255; /* ZBX_PROCESS_TYPE_UNKNOWN */
ZBX_THREAD_LOCAL int process_num;
ZBX_THREAD_LOCAL int server_num = 0;
ZBX_THREAD_ACTIVECHK_ARGS *CONFIG_ACTIVE_ARGS = NULL;
int CONFIG_ALERTER_FORKS = 0;
int CONFIG_DISCOVERER_FORKS = 0;
int CONFIG_HOUSEKEEPER_FORKS = 0;
int CONFIG_PINGER_FORKS = 0;
int CONFIG_POLLER_FORKS = 0;
int CONFIG_UNREACHABLE_POLLER_FORKS = 0;
int CONFIG_HTTPPOLLER_FORKS = 0;
int CONFIG_IPMIPOLLER_FORKS = 0;
int CONFIG_TIMER_FORKS = 0;
int CONFIG_TRAPPER_FORKS = 0;
int CONFIG_SNMPTRAPPER_FORKS = 0;
int CONFIG_JAVAPOLLER_FORKS = 0;
int CONFIG_ESCALATOR_FORKS = 0;
int CONFIG_SELFMON_FORKS = 0;
int CONFIG_WATCHDOG_FORKS = 0;
int CONFIG_DATASENDER_FORKS = 0;
int CONFIG_HEARTBEAT_FORKS = 0;
int CONFIG_PROXYPOLLER_FORKS = 0;
int CONFIG_HISTSYNCER_FORKS = 0;
int CONFIG_CONFSYNCER_FORKS = 0;
int CONFIG_VMWARE_FORKS = 0;
int CONFIG_COLLECTOR_FORKS = 1;
int CONFIG_ACTIVE_FORKS = 0;
int CONFIG_PASSIVE_FORKS = 3; /* number of listeners for processing passive checks */
char *opt = NULL;
#ifdef _WINDOWS
void zbx_co_uninitialize();
#endif
int get_process_info_by_thread(int server_num, unsigned char *process_type, int *process_num)
{
int server_count = 0;
if (0 == server_num)
{
/* fail if the main process is queried */
return FAIL;
}
else if (server_num <= (server_count += CONFIG_COLLECTOR_FORKS))
{
*process_type = ZBX_PROCESS_TYPE_COLLECTOR;
*process_num = server_num - server_count + CONFIG_COLLECTOR_FORKS;
}
else if (server_num <= (server_count += CONFIG_PASSIVE_FORKS))
{
*process_type = ZBX_PROCESS_TYPE_LISTENER;
*process_num = server_num - server_count + CONFIG_PASSIVE_FORKS;
}
else if (server_num <= (server_count += CONFIG_ACTIVE_FORKS))
{
*process_type = ZBX_PROCESS_TYPE_ACTIVE_CHECKS;
*process_num = server_num - server_count + CONFIG_ACTIVE_FORKS;
}
else
{
return FAIL;
}
return SUCCEED;
}
static void parse_commandline(int argc, char **argv, ZBX_TASK_EX *t)
{
char ch = '\0';
t->task = ZBX_TASK_START;
/* parse the command-line */
while ((char)EOF != (ch = (char)zbx_getopt_long(argc, argv, shortopts, longopts, NULL)))
{
switch (ch)
{
case 'c':
CONFIG_FILE = strdup(zbx_optarg);
break;
case 'R':
#ifndef _WINDOWS
if (0 == strncmp(zbx_optarg, ZBX_LOG_LEVEL_INCREASE, strlen(ZBX_LOG_LEVEL_INCREASE)))
{
if (SUCCEED != get_log_level_message(zbx_optarg + strlen(ZBX_LOG_LEVEL_INCREASE),
ZBX_RTC_LOG_LEVEL_INCREASE, &t->flags))
{
exit(EXIT_FAILURE);
}
}
else if (0 == strncmp(zbx_optarg, ZBX_LOG_LEVEL_DECREASE,
strlen(ZBX_LOG_LEVEL_DECREASE)))
{
if (SUCCEED != get_log_level_message(zbx_optarg + strlen(ZBX_LOG_LEVEL_DECREASE),
ZBX_RTC_LOG_LEVEL_DECREASE, &t->flags))
{
exit(EXIT_FAILURE);
}
}
else
{
zbx_error("invalid runtime control option: %s", zbx_optarg);
exit(EXIT_FAILURE);
}
t->task = ZBX_TASK_RUNTIME_CONTROL;
#else
zbx_error("process runtime control is not supported on Windows platform");
exit(EXIT_FAILURE);
#endif
break;
case 'h':
help();
exit(EXIT_SUCCESS);
break;
case 'V':
version();
#ifdef _AIX
tl_version();
#endif
exit(EXIT_SUCCESS);
break;
case 'p':
if (ZBX_TASK_START == t->task)
t->task = ZBX_TASK_PRINT_SUPPORTED;
break;
case 't':
if (ZBX_TASK_START == t->task)
{
t->task = ZBX_TASK_TEST_METRIC;
TEST_METRIC = strdup(zbx_optarg);
}
break;
#ifdef _WINDOWS
case 'i':
t->task = ZBX_TASK_INSTALL_SERVICE;
break;
case 'd':
t->task = ZBX_TASK_UNINSTALL_SERVICE;
break;
case 's':
t->task = ZBX_TASK_START_SERVICE;
break;
case 'x':
t->task = ZBX_TASK_STOP_SERVICE;
break;
case 'm':
t->flags = ZBX_TASK_FLAG_MULTIPLE_AGENTS;
break;
#endif
default:
t->task = ZBX_TASK_SHOW_USAGE;
break;
}
}
if (NULL == CONFIG_FILE)
CONFIG_FILE = DEFAULT_CONFIG_FILE;
}
/******************************************************************************
* *
* Function: set_defaults *
* *
* Purpose: set configuration defaults *
* *
* Author: Vladimir Levijev, Rudolfs Kreicbergs *
* *
******************************************************************************/
static void set_defaults(void)
{
AGENT_RESULT result;
char **value = NULL;
if (NULL == CONFIG_HOSTNAME)
{
if (NULL == CONFIG_HOSTNAME_ITEM)
CONFIG_HOSTNAME_ITEM = zbx_strdup(CONFIG_HOSTNAME_ITEM, "system.hostname");
init_result(&result);
if (SUCCEED == process(CONFIG_HOSTNAME_ITEM, PROCESS_LOCAL_COMMAND, &result) &&
NULL != (value = GET_STR_RESULT(&result)))
{
assert(*value);
if (MAX_ZBX_HOSTNAME_LEN < strlen(*value))
{
(*value)[MAX_ZBX_HOSTNAME_LEN] = '\0';
zabbix_log(LOG_LEVEL_WARNING, "hostname truncated to [%s])", *value);
}
CONFIG_HOSTNAME = zbx_strdup(CONFIG_HOSTNAME, *value);
}
else
zabbix_log(LOG_LEVEL_WARNING, "failed to get system hostname from [%s])", CONFIG_HOSTNAME_ITEM);
free_result(&result);
}
else if (NULL != CONFIG_HOSTNAME_ITEM)
zabbix_log(LOG_LEVEL_WARNING, "both Hostname and HostnameItem defined, using [%s]", CONFIG_HOSTNAME);
if (NULL != CONFIG_HOST_METADATA && NULL != CONFIG_HOST_METADATA_ITEM)
{
zabbix_log(LOG_LEVEL_WARNING, "both HostMetadata and HostMetadataItem defined, using [%s]",
CONFIG_HOST_METADATA);
}
#ifndef _WINDOWS
if (NULL == CONFIG_LOAD_MODULE_PATH)
CONFIG_LOAD_MODULE_PATH = zbx_strdup(CONFIG_LOAD_MODULE_PATH, LIBDIR "/modules");
if (NULL == CONFIG_PID_FILE)
CONFIG_PID_FILE = "/tmp/zabbix_agentd.pid";
#endif
}
/******************************************************************************
* *
* Function: zbx_validate_config *
* *
* Purpose: validate configuration parameters *
* *
* Author: Vladimir Levijev *
* *
******************************************************************************/
static void zbx_validate_config(void)
{
char *ch_error;
if (NULL == CONFIG_HOSTNAME)
{
zabbix_log(LOG_LEVEL_CRIT, "\"Hostname\" configuration parameter is not defined");
exit(EXIT_FAILURE);
}
if (FAIL == zbx_check_hostname(CONFIG_HOSTNAME, &ch_error))
{
zabbix_log(LOG_LEVEL_CRIT, "invalid \"Hostname\" configuration parameter: '%s': %s", CONFIG_HOSTNAME,
ch_error);
zbx_free(ch_error);
exit(EXIT_FAILURE);
}
if (NULL != CONFIG_HOST_METADATA && HOST_METADATA_LEN < zbx_strlen_utf8(CONFIG_HOST_METADATA))
{
zabbix_log(LOG_LEVEL_CRIT, "the value of \"HostMetadata\" configuration parameter cannot be longer than"
" %d characters", HOST_METADATA_LEN);
exit(EXIT_FAILURE);
}
/* make sure active or passive check is enabled */
if (0 == CONFIG_ACTIVE_FORKS && 0 == CONFIG_PASSIVE_FORKS)
{
zabbix_log(LOG_LEVEL_CRIT, "either active or passive checks must be enabled");
exit(EXIT_FAILURE);
}
if (NULL != CONFIG_SOURCE_IP && ('\0' == *CONFIG_SOURCE_IP || SUCCEED != is_ip(CONFIG_SOURCE_IP)))
{
zabbix_log(LOG_LEVEL_CRIT, "invalid \"SourceIP\" configuration parameter: '%s'", CONFIG_SOURCE_IP);
exit(EXIT_FAILURE);
}
}
static int add_activechk_host(const char *host, unsigned short port)
{
int i;
for (i = 0; i < CONFIG_ACTIVE_FORKS; i++)
{
if (0 == strcmp(CONFIG_ACTIVE_ARGS[i].host, host) && CONFIG_ACTIVE_ARGS[i].port == port)
return FAIL;
}
CONFIG_ACTIVE_FORKS++;
CONFIG_ACTIVE_ARGS = zbx_realloc(CONFIG_ACTIVE_ARGS, sizeof(ZBX_THREAD_ACTIVECHK_ARGS) * CONFIG_ACTIVE_FORKS);
CONFIG_ACTIVE_ARGS[CONFIG_ACTIVE_FORKS - 1].host = zbx_strdup(NULL, host);
CONFIG_ACTIVE_ARGS[CONFIG_ACTIVE_FORKS - 1].port = port;
return SUCCEED;
}
/******************************************************************************
* *
* Function: get_serveractive_hosts *
* *
* Purpose: parse string like IP<:port>,[IPv6]<:port> *
* *
******************************************************************************/
static void get_serveractive_hosts(char *active_hosts)
{
char *l = active_hosts, *r;
int rc = SUCCEED;
do
{
char *host = NULL;
unsigned short port;
if (NULL != (r = strchr(l, ',')))
*r = '\0';
if (SUCCEED != parse_serveractive_element(l, &host, &port, (unsigned short)ZBX_DEFAULT_SERVER_PORT))
goto fail;
rc = add_activechk_host(host, port);
zbx_free(host);
if (SUCCEED != rc)
goto fail;
if (NULL != r)
{
*r = ',';
l = r + 1;
}
}
while (NULL != r);
return;
fail:
if (SUCCEED != rc)
zbx_error("error parsing a \"ServerActive\" option: address \"%s\" specified more than once", l);
else
zbx_error("error parsing a \"ServerActive\" option: address \"%s\" is invalid", l);
if (NULL != r)
*r = ',';
exit(EXIT_FAILURE);
}
/******************************************************************************
* *
* Function: zbx_load_config *
* *
* Purpose: load configuration from config file *
* *
* Parameters: requirement - produce error if config file missing or not *
* *
******************************************************************************/
static void zbx_load_config(int requirement)
{
char *active_hosts = NULL;
struct cfg_line cfg[] =
{
/* PARAMETER, VAR, TYPE,
MANDATORY, MIN, MAX */
{"Server", &CONFIG_HOSTS_ALLOWED, TYPE_STRING_LIST,
PARM_OPT, 0, 0},
{"ServerActive", &active_hosts, TYPE_STRING_LIST,
PARM_OPT, 0, 0},
{"Hostname", &CONFIG_HOSTNAME, TYPE_STRING,
PARM_OPT, 0, 0},
{"HostnameItem", &CONFIG_HOSTNAME_ITEM, TYPE_STRING,
PARM_OPT, 0, 0},
{"HostMetadata", &CONFIG_HOST_METADATA, TYPE_STRING,
PARM_OPT, 0, 0},
{"HostMetadataItem", &CONFIG_HOST_METADATA_ITEM, TYPE_STRING,
PARM_OPT, 0, 0},
{"BufferSize", &CONFIG_BUFFER_SIZE, TYPE_INT,
PARM_OPT, 2, 65535},
{"BufferSend", &CONFIG_BUFFER_SEND, TYPE_INT,
PARM_OPT, 1, SEC_PER_HOUR},
#ifndef _WINDOWS
{"PidFile", &CONFIG_PID_FILE, TYPE_STRING,
PARM_OPT, 0, 0},
#endif
{"LogFile", &CONFIG_LOG_FILE, TYPE_STRING,
PARM_OPT, 0, 0},
{"LogFileSize", &CONFIG_LOG_FILE_SIZE, TYPE_INT,
PARM_OPT, 0, 1024},
{"Timeout", &CONFIG_TIMEOUT, TYPE_INT,
PARM_OPT, 1, 30},
{"ListenPort", &CONFIG_LISTEN_PORT, TYPE_INT,
PARM_OPT, 1024, 32767},
{"ListenIP", &CONFIG_LISTEN_IP, TYPE_STRING_LIST,
PARM_OPT, 0, 0},
{"SourceIP", &CONFIG_SOURCE_IP, TYPE_STRING,
PARM_OPT, 0, 0},
{"DebugLevel", &CONFIG_LOG_LEVEL, TYPE_INT,
PARM_OPT, 0, 4},
{"StartAgents", &CONFIG_PASSIVE_FORKS, TYPE_INT,
PARM_OPT, 0, 100},
{"RefreshActiveChecks", &CONFIG_REFRESH_ACTIVE_CHECKS, TYPE_INT,
PARM_OPT, SEC_PER_MIN, SEC_PER_HOUR},
{"MaxLinesPerSecond", &CONFIG_MAX_LINES_PER_SECOND, TYPE_INT,
PARM_OPT, 1, 1000},
{"EnableRemoteCommands", &CONFIG_ENABLE_REMOTE_COMMANDS, TYPE_INT,
PARM_OPT, 0, 1},
{"LogRemoteCommands", &CONFIG_LOG_REMOTE_COMMANDS, TYPE_INT,
PARM_OPT, 0, 1},
{"UnsafeUserParameters", &CONFIG_UNSAFE_USER_PARAMETERS, TYPE_INT,
PARM_OPT, 0, 1},
{"Alias", &CONFIG_ALIASES, TYPE_MULTISTRING,
PARM_OPT, 0, 0},
{"UserParameter", &CONFIG_USER_PARAMETERS, TYPE_MULTISTRING,
PARM_OPT, 0, 0},
#ifndef _WINDOWS
{"LoadModulePath", &CONFIG_LOAD_MODULE_PATH, TYPE_STRING,
PARM_OPT, 0, 0},
{"LoadModule", &CONFIG_LOAD_MODULE, TYPE_MULTISTRING,
PARM_OPT, 0, 0},
{"AllowRoot", &CONFIG_ALLOW_ROOT, TYPE_INT,
PARM_OPT, 0, 1},
{"User", &CONFIG_USER, TYPE_STRING,
PARM_OPT, 0, 0},
#endif
#ifdef _WINDOWS
{"PerfCounter", &CONFIG_PERF_COUNTERS, TYPE_MULTISTRING,
PARM_OPT, 0, 0},
#endif
{NULL}
};
/* initialize multistrings */
zbx_strarr_init(&CONFIG_ALIASES);
zbx_strarr_init(&CONFIG_USER_PARAMETERS);
#ifndef _WINDOWS
zbx_strarr_init(&CONFIG_LOAD_MODULE);
#endif
#ifdef _WINDOWS
zbx_strarr_init(&CONFIG_PERF_COUNTERS);
#endif
parse_cfg_file(CONFIG_FILE, cfg, requirement, ZBX_CFG_STRICT);
set_defaults();
if (ZBX_CFG_FILE_REQUIRED == requirement && NULL == CONFIG_HOSTS_ALLOWED && 0 != CONFIG_PASSIVE_FORKS)
{
zbx_error("StartAgents is not 0, parameter Server must be defined");
exit(EXIT_FAILURE);
}
if (NULL != active_hosts && '\0' != *active_hosts)
get_serveractive_hosts(active_hosts);
zbx_free(active_hosts);
if (ZBX_CFG_FILE_REQUIRED == requirement)
zbx_validate_config();
}
/******************************************************************************
* *
* Function: zbx_free_config *
* *
* Purpose: free configuration memory *
* *
* Author: Vladimir Levijev *
* *
******************************************************************************/
static void zbx_free_config(void)
{
zbx_strarr_free(CONFIG_ALIASES);
zbx_strarr_free(CONFIG_USER_PARAMETERS);
#ifndef _WINDOWS
zbx_strarr_free(CONFIG_LOAD_MODULE);
#endif
#ifdef _WINDOWS
zbx_strarr_free(CONFIG_PERF_COUNTERS);
#endif
}
#ifdef _WINDOWS
static int zbx_exec_service_task(const char *name, const ZBX_TASK_EX *t)
{
int ret;
switch (t->task)
{
case ZBX_TASK_INSTALL_SERVICE:
ret = ZabbixCreateService(name, t->flags & ZBX_TASK_FLAG_MULTIPLE_AGENTS);
break;
case ZBX_TASK_UNINSTALL_SERVICE:
ret = ZabbixRemoveService();
break;
case ZBX_TASK_START_SERVICE:
ret = ZabbixStartService();
break;
case ZBX_TASK_STOP_SERVICE:
ret = ZabbixStopService();
break;
default:
/* there can not be other choice */
assert(0);
}
return ret;
}
#endif /* _WINDOWS */
int MAIN_ZABBIX_ENTRY()
{
zbx_sock_t listen_sock;
int i, j = 0;
#ifdef _WINDOWS
DWORD res;
#endif
if (NULL == CONFIG_LOG_FILE || '\0' == *CONFIG_LOG_FILE)
zabbix_open_log(LOG_TYPE_SYSLOG, CONFIG_LOG_LEVEL, NULL);
else
zabbix_open_log(LOG_TYPE_FILE, CONFIG_LOG_LEVEL, CONFIG_LOG_FILE);
zabbix_log(LOG_LEVEL_INFORMATION, "Starting Zabbix Agent [%s]. Zabbix %s (revision %s).",
CONFIG_HOSTNAME, ZABBIX_VERSION, ZABBIX_REVISION);
zabbix_log(LOG_LEVEL_INFORMATION, "using configuration file: %s", CONFIG_FILE);
#ifndef _WINDOWS
if (FAIL == load_modules(CONFIG_LOAD_MODULE_PATH, CONFIG_LOAD_MODULE, CONFIG_TIMEOUT, 1))
{
zabbix_log(LOG_LEVEL_CRIT, "loading modules failed, exiting...");
exit(EXIT_FAILURE);
}
#endif
if (0 != CONFIG_PASSIVE_FORKS)
{
if (FAIL == zbx_tcp_listen(&listen_sock, CONFIG_LISTEN_IP, (unsigned short)CONFIG_LISTEN_PORT))
{
zabbix_log(LOG_LEVEL_CRIT, "listener failed: %s", zbx_tcp_strerror());
exit(EXIT_FAILURE);
}
}
init_collector_data();
#ifdef _WINDOWS
init_perf_collector(1);
load_perf_counters(CONFIG_PERF_COUNTERS);
#endif
zbx_free_config();
/* --- START THREADS ---*/
/* allocate memory for a collector, all listeners and an active check */
threads_num = CONFIG_COLLECTOR_FORKS + CONFIG_PASSIVE_FORKS + CONFIG_ACTIVE_FORKS;
#ifdef _WINDOWS
if (MAXIMUM_WAIT_OBJECTS < threads_num)
{
zabbix_log(LOG_LEVEL_CRIT, "Too many agent threads. Please reduce the StartAgents configuration"
" parameter or the number of active servers in ServerActive configuration parameter.");
exit(EXIT_FAILURE);
}
#endif
threads = zbx_calloc(threads, threads_num, sizeof(ZBX_THREAD_HANDLE));
for (i = 0; i < threads_num; i++)
{
zbx_thread_args_t *thread_args;
thread_args = (zbx_thread_args_t *)zbx_malloc(NULL, sizeof(zbx_thread_args_t));
if (FAIL == get_process_info_by_thread(i + 1, &thread_args->process_type, &thread_args->process_num))
{
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
thread_args->server_num = i + 1;
thread_args->args = NULL;
switch (thread_args->process_type)
{
case ZBX_PROCESS_TYPE_COLLECTOR:
threads[i] = zbx_thread_start(collector_thread, thread_args);
break;
case ZBX_PROCESS_TYPE_LISTENER:
thread_args->args = &listen_sock;
threads[i] = zbx_thread_start(listener_thread, thread_args);
break;
case ZBX_PROCESS_TYPE_ACTIVE_CHECKS:
thread_args->args = &CONFIG_ACTIVE_ARGS[j++];
threads[i] = zbx_thread_start(active_checks_thread, thread_args);
break;
}
}
#ifdef _WINDOWS
set_parent_signal_handler(); /* must be called after all threads are created */
/* wait for an exiting thread */
res = WaitForMultipleObjectsEx(threads_num, threads, FALSE, INFINITE, FALSE);
if (ZBX_IS_RUNNING())
{
/* Zabbix agent service should either be stopped by the user in ServiceCtrlHandler() or */
/* crash. If some thread has terminated normally, it means something is terribly wrong. */
zabbix_log(LOG_LEVEL_CRIT, "One thread has terminated unexpectedly (code:%lu). Exiting ...", res);
THIS_SHOULD_NEVER_HAPPEN;
/* notify other threads and allow them to terminate */
ZBX_DO_EXIT();
zbx_sleep(1);
}
else
{
/* wait for the service worker thread to terminate us */
zbx_sleep(3);
THIS_SHOULD_NEVER_HAPPEN;
}
#else
while (-1 == wait(&i)) /* wait for any child to exit */
{
if (EINTR != errno)
{
zabbix_log(LOG_LEVEL_ERR, "failed to wait on child processes: %s", zbx_strerror(errno));
break;
}
}
/* all exiting child processes should be caught by signal handlers */
THIS_SHOULD_NEVER_HAPPEN;
#endif
zbx_on_exit();
return SUCCEED;
}
/******************************************************************************
* *
* Function: zbx_free_service_resources *
* *
* Purpose: free service resources allocated by main thread *
* *
******************************************************************************/
void zbx_free_service_resources(void)
{
if (NULL != threads)
{
int i;
#if !defined(_WINDOWS)
sigset_t set;
/* ignore SIGCHLD signals in order for zbx_sleep() to work */
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, NULL);
#else
/* wait for threads to finish first. although listener threads will never end */
WaitForMultipleObjectsEx(threads_num, threads, TRUE, 1000, FALSE);
#endif
for (i = 0; i < threads_num; i++)
{
if (threads[i])
zbx_thread_kill(threads[i]);
}
for (i = 0; i < threads_num; i++)
{
if (threads[i])
zbx_thread_wait(threads[i]);
threads[i] = ZBX_THREAD_HANDLE_NULL;
}
zbx_free(threads);
}
zabbix_log(LOG_LEVEL_INFORMATION, "Zabbix Agent stopped. Zabbix %s (revision %s).",
ZABBIX_VERSION, ZABBIX_REVISION);
#ifndef _WINDOWS
unload_modules();
#endif
zabbix_close_log();
free_metrics();
alias_list_free();
free_collector_data();
#ifdef _WINDOWS
free_perf_collector();
zbx_co_uninitialize();
#endif
}
void zbx_on_exit(void)
{
zabbix_log(LOG_LEVEL_DEBUG, "zbx_on_exit() called");
zbx_free_service_resources();
#if defined(PS_OVERWRITE_ARGV)
setproctitle_free_env();
#endif
exit(EXIT_SUCCESS);
}
#if defined(HAVE_SIGQUEUE)
void zbx_sigusr_handler(int flags)
{
}
#endif
int main(int argc, char **argv)
{
ZBX_TASK_EX t = {ZBX_TASK_START};
#ifdef _WINDOWS
int ret;
/* Provide, so our process handles errors instead of the system itself. */
/* Attention!!! */
/* The system does not display the critical-error-handler message box. */
/* Instead, the system sends the error to the calling process.*/
SetErrorMode(SEM_FAILCRITICALERRORS);
#endif
#if defined(PS_OVERWRITE_ARGV) || defined(PS_PSTAT_ARGV)
argv = setproctitle_save_env(argc, argv);
#endif
progname = get_program_name(argv[0]);
parse_commandline(argc, argv, &t);
import_symbols();
/* this is needed to set default hostname in zbx_load_config() */
init_metrics();
switch (t.task)
{
case ZBX_TASK_SHOW_USAGE:
usage();
exit(EXIT_FAILURE);
break;
#ifndef _WINDOWS
case ZBX_TASK_RUNTIME_CONTROL:
zbx_load_config(ZBX_CFG_FILE_OPTIONAL);
exit(SUCCEED == zbx_sigusr_send(t.flags) ? EXIT_SUCCESS : EXIT_FAILURE);
break;
#else
case ZBX_TASK_INSTALL_SERVICE:
case ZBX_TASK_UNINSTALL_SERVICE:
case ZBX_TASK_START_SERVICE:
case ZBX_TASK_STOP_SERVICE:
if (t.flags & ZBX_TASK_FLAG_MULTIPLE_AGENTS)
{
zbx_load_config(ZBX_CFG_FILE_REQUIRED);
zbx_snprintf(ZABBIX_SERVICE_NAME, sizeof(ZABBIX_SERVICE_NAME), "%s [%s]",
APPLICATION_NAME, CONFIG_HOSTNAME);
zbx_snprintf(ZABBIX_EVENT_SOURCE, sizeof(ZABBIX_EVENT_SOURCE), "%s [%s]",
APPLICATION_NAME, CONFIG_HOSTNAME);
}
else
zbx_load_config(ZBX_CFG_FILE_OPTIONAL);
zbx_free_config();
ret = zbx_exec_service_task(argv[0], &t);
free_metrics();
exit(SUCCEED == ret ? EXIT_SUCCESS : EXIT_FAILURE);
break;
#endif
case ZBX_TASK_TEST_METRIC:
case ZBX_TASK_PRINT_SUPPORTED:
zbx_load_config(ZBX_CFG_FILE_OPTIONAL);
#ifdef _WINDOWS
init_perf_collector(0);
load_perf_counters(CONFIG_PERF_COUNTERS);
#else
zbx_set_common_signal_handlers();
#endif
#ifndef _WINDOWS
if (FAIL == load_modules(CONFIG_LOAD_MODULE_PATH, CONFIG_LOAD_MODULE, CONFIG_TIMEOUT, 0))
{
zabbix_log(LOG_LEVEL_CRIT, "loading modules failed, exiting...");
exit(EXIT_FAILURE);
}
#endif
load_user_parameters(CONFIG_USER_PARAMETERS);
load_aliases(CONFIG_ALIASES);
zbx_free_config();
if (ZBX_TASK_TEST_METRIC == t.task)
test_parameter(TEST_METRIC);
else
test_parameters();
#ifdef _WINDOWS
free_perf_collector(); /* cpu_collector must be freed before perf_collector is freed */
#endif
#ifndef _WINDOWS
unload_modules();
#endif
free_metrics();
alias_list_free();
exit(EXIT_SUCCESS);
break;
default:
zbx_load_config(ZBX_CFG_FILE_REQUIRED);
load_user_parameters(CONFIG_USER_PARAMETERS);
load_aliases(CONFIG_ALIASES);
break;
}
START_MAIN_ZABBIX_ENTRY(CONFIG_ALLOW_ROOT, CONFIG_USER);
exit(EXIT_SUCCESS);
}
|