3 extern void *cgroups_main(void *ptr);
5 void netdata_cleanup_and_exit(int ret) {
8 error_log_limit_unlimited();
10 info("Called: netdata_cleanup_and_exit()");
11 #ifdef NETDATA_INTERNAL_CHECKS
19 if(unlink(pidfile) != 0)
20 error("Cannot unlink pidfile '%s'.", pidfile);
23 info("NetData exiting. Bye bye...");
27 struct netdata_static_thread {
37 void (*init_routine) (void);
38 void *(*start_routine) (void *);
39 } static_threads[] = {
40 #ifdef INTERNAL_PLUGIN_NFACCT
41 // nfacct requires root access
42 // so, we build it as an external plugin with setuid to root
43 {"nfacct", "plugins", "nfacct", 1, NULL, NULL, nfacct_main},
46 {"tc", "plugins", "tc", 1, NULL, NULL, tc_main},
47 {"idlejitter", "plugins", "idlejitter", 1, NULL, NULL, cpuidlejitter_main},
48 {"proc", "plugins", "proc", 1, NULL, NULL, proc_main},
49 {"cgroups", "plugins", "cgroups", 1, NULL, NULL, cgroups_main},
50 {"plugins.d", NULL, NULL, 1, NULL, NULL, pluginsd_main},
51 {"check", "plugins", "checks", 0, NULL, NULL, checks_main},
52 {"web", NULL, NULL, 1, NULL, NULL, socket_listen_main_multi_threaded},
53 {"web-single-threaded", NULL, NULL, 0, NULL, NULL, socket_listen_main_single_threaded},
54 {NULL, NULL, NULL, 0, NULL, NULL, NULL}
57 void web_server_threading_selection(void) {
58 int threaded = config_get_boolean("global", "multi threaded web server", 1);
61 for(i = 0; static_threads[i].name ; i++) {
62 if(static_threads[i].start_routine == socket_listen_main_multi_threaded)
63 static_threads[i].enabled = threaded?1:0;
65 if(static_threads[i].start_routine == socket_listen_main_single_threaded)
66 static_threads[i].enabled = threaded?0:1;
69 web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
71 web_donotrack_comply = config_get_boolean("global", "respect web browser do not track policy", web_donotrack_comply);
73 #ifdef NETDATA_WITH_ZLIB
74 web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
76 char *s = config_get("global", "web compression strategy", "default");
77 if(!strcmp(s, "default"))
78 web_gzip_strategy = Z_DEFAULT_STRATEGY;
79 else if(!strcmp(s, "filtered"))
80 web_gzip_strategy = Z_FILTERED;
81 else if(!strcmp(s, "huffman only"))
82 web_gzip_strategy = Z_HUFFMAN_ONLY;
83 else if(!strcmp(s, "rle"))
84 web_gzip_strategy = Z_RLE;
85 else if(!strcmp(s, "fixed"))
86 web_gzip_strategy = Z_FIXED;
88 error("Invalid compression strategy '%s'. Valid strategies are 'default', 'filtered', 'huffman only', 'rle' and 'fixed'. Proceeding with 'default'.", s);
89 web_gzip_strategy = Z_DEFAULT_STRATEGY;
92 web_gzip_level = (int)config_get_number("global", "web compression level", 3);
93 if(web_gzip_level < 1) {
94 error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 1 (fastest compression).", web_gzip_level);
97 else if(web_gzip_level > 9) {
98 error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 9 (best compression).", web_gzip_level);
101 #endif /* NETDATA_WITH_ZLIB */
105 int killpid(pid_t pid, int sig)
108 debug(D_EXIT, "Request to kill pid %d", pid);
111 if(kill(pid, 0) == -1) {
114 error("Request to kill pid %d, but it is not running.", pid);
118 error("Request to kill pid %d, but I do not have enough permissions.", pid);
122 error("Request to kill pid %d, but I received an error.", pid);
128 ret = kill(pid, sig);
132 error("Cannot kill pid %d, but it is not running.", pid);
136 error("Cannot kill pid %d, but I do not have enough permissions.", pid);
140 error("Cannot kill pid %d, but I received an error.", pid);
153 struct web_client *w;
154 for(w = web_clients; w ; w = w->next) {
155 debug(D_EXIT, "Stopping web client %s", w->client_ip);
156 pthread_cancel(w->thread);
157 pthread_join(w->thread, NULL);
161 for (i = 0; static_threads[i].name != NULL ; i++) {
162 if(static_threads[i].thread) {
163 debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
164 pthread_cancel(*static_threads[i].thread);
165 pthread_join(*static_threads[i].thread, NULL);
166 static_threads[i].thread = NULL;
171 info("Killing tc-qos-helper procees");
172 if(killpid(tc_child_pid, SIGTERM) != -1)
173 waitid(P_PID, (id_t) tc_child_pid, &info, WEXITED);
178 for(cd = pluginsd_root ; cd ; cd = cd->next) {
179 debug(D_EXIT, "Stopping %s plugin thread", cd->id);
180 pthread_cancel(cd->thread);
181 pthread_join(cd->thread, NULL);
183 if(cd->pid && !cd->obsolete) {
184 debug(D_EXIT, "killing %s plugin process", cd->id);
185 if(killpid(cd->pid, SIGTERM) != -1)
186 waitid(P_PID, (id_t) cd->pid, &info, WEXITED);
190 // if, for any reason there is any child exited
192 waitid(P_PID, 0, &info, WEXITED|WNOHANG);
194 debug(D_EXIT, "All threads/childs stopped.");
197 struct option_def options[] = {
198 // opt description arg name default value
199 {'c', "Load alternate configuration file", "config_file", CONFIG_DIR "/" CONFIG_FILENAME},
200 {'D', "Disable fork into background", NULL, NULL},
201 {'h', "Display help message", NULL, NULL},
202 {'P', "File to save a pid while running", "FILE", NULL},
203 {'i', "The IP address to listen to.", "address", "All addresses"},
204 {'p', "Port to listen. Can be from 1 to 65535.", "port_number", "19999"},
205 {'s', "Path to access host /proc and /sys when running in a container.", "PATH", NULL},
206 {'t', "The frequency in seconds, for data collection. \
207 Same as 'update every' config file option.", "seconds", "1"},
208 {'u', "System username to run as.", "username", "netdata"},
209 {'v', "Version of the program", NULL, NULL},
210 {'W', "vendor options.", "stacksize=<size>|unittest|debug_flag", NULL},
213 void help(int exitcode) {
220 int num_opts = sizeof(options) / sizeof(struct option_def);
224 // Compute maximum argument length
225 for( i = 0; i < num_opts; i++ ) {
226 if(options[i].arg_name) {
227 int len_arg = strlen(options[i].arg_name);
228 if(len_arg > max_len_arg) max_len_arg = len_arg;
232 fprintf(stream, "SYNOPSIS: netdata [options]\n");
233 fprintf(stream, "\n");
234 fprintf(stream, "Options:\n");
236 // Output options description.
237 for( i = 0; i < num_opts; i++ ) {
238 fprintf(stream, " -%c %-*s %s", options[i].val, max_len_arg, options[i].arg_name ? options[i].arg_name : "", options[i].description);
239 if(options[i].default_value) {
240 fprintf(stream, " Default: %s\n", options[i].default_value);
242 fprintf(stream, "\n");
250 // TODO: Remove this function with the nix major release.
251 void remove_option(int opt_index, int *argc, char **argv) {
253 // remove the options.
256 for(i = opt_index; i < *argc; i++) {
260 } while(argv[i][0] != '-' && opt_index >= *argc);
264 int main(int argc, char **argv)
267 int config_loaded = 0;
269 size_t wanted_stacksize = 0, stacksize = 0;
272 // global initialization
275 // set the name for logging
276 program_name = "netdata";
278 // parse command line.
280 // parse depercated options
281 // TODO: Remove this block with the next major release.
285 if(strcmp(argv[i], "-pidfile") == 0 && (i+1) < argc) {
286 strncpyz(pidfile, argv[i+1], FILENAME_MAX);
287 fprintf(stderr, "%s: deprecated option -- %s -- please use -P instead.\n", argv[0], argv[i]);
288 remove_option(i, &argc, argv);
290 else if(strcmp(argv[i], "-nodaemon") == 0 || strcmp(argv[i], "-nd") == 0) {
292 fprintf(stderr, "%s: deprecated option -- %s -- please use -D instead.\n ", argv[0], argv[i]);
293 remove_option(i, &argc, argv);
295 else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) {
296 config_set("global", "host access prefix", argv[i+1]);
297 fprintf(stderr, "%s: deprecated option -- %s -- please use -s instead.\n", argv[0], argv[i]);
298 remove_option(i, &argc, argv);
300 else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) {
301 config_set("global", "history", argv[i+1]);
302 fprintf(stderr, "%s: deprecated option -- %s -- This option will be removed with V2.*.\n", argv[0], argv[i]);
303 remove_option(i, &argc, argv);
311 int num_opts = sizeof(options) / sizeof(struct option_def);
312 char optstring[(num_opts * 2) + 1];
315 for( i = 0; i < num_opts; i++ ) {
316 optstring[string_i] = options[i].val;
318 if(options[i].arg_name) {
319 optstring[string_i] = ':';
325 while( (opt = getopt(argc, argv, optstring)) != -1 ) {
328 if(load_config(optarg, 1) != 1) {
329 error("Cannot load configuration file %s.", optarg);
333 debug(D_OPTIONS, "Configuration loaded from %s.", optarg);
344 config_set("global", "bind to", optarg);
347 strncpy(pidfile, optarg, FILENAME_MAX);
348 pidfile[FILENAME_MAX] = '\0';
351 config_set("global", "default port", optarg);
354 config_set("global", "host access prefix", optarg);
357 config_set("global", "update every", optarg);
360 config_set("global", "run as user", optarg);
363 // TODO: Outsource version to makefile which can compute version from git.
364 printf("netdata 1.2.1_master\n");
369 char* stacksize = "stacksize=";
370 char* debug_flags_string = "debug_flags=";
371 if(strcmp(optarg, "unittest") == 0) {
372 rrd_update_every = 1;
373 if(run_all_mockup_tests()) exit(1);
374 if(unit_test_storage()) exit(1);
375 fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
377 } else if(strncmp(optarg, stacksize, strlen(stacksize)) == 0) {
378 optarg += strlen(stacksize);
379 config_set("global", "pthread stack size", optarg);
380 } else if(strncmp(optarg, debug_flags_string, strlen(debug_flags_string)) == 0) {
381 optarg += strlen(debug_flags_string);
382 config_set("global", "debug flags", optarg);
383 debug_flags = strtoull(optarg, NULL, 0);
394 if(!config_loaded) load_config(NULL, 0);
396 // prepare configuration environment variables for the plugins
397 setenv("NETDATA_CONFIG_DIR" , config_get("global", "config directory" , CONFIG_DIR) , 1);
398 setenv("NETDATA_PLUGINS_DIR", config_get("global", "plugins directory" , PLUGINS_DIR), 1);
399 setenv("NETDATA_WEB_DIR" , config_get("global", "web files directory", WEB_DIR) , 1);
400 setenv("NETDATA_CACHE_DIR" , config_get("global", "cache directory" , CACHE_DIR) , 1);
401 setenv("NETDATA_LIB_DIR" , config_get("global", "lib directory" , VARLIB_DIR) , 1);
402 setenv("NETDATA_LOG_DIR" , config_get("global", "log directory" , LOG_DIR) , 1);
403 setenv("NETDATA_HOST_PREFIX", config_get("global", "host access prefix" , "") , 1);
404 setenv("HOME" , config_get("global", "home directory" , CACHE_DIR) , 1);
406 // disable buffering for python plugins
407 setenv("PYTHONUNBUFFERED", "1", 1);
409 // avoid flood calls to stat(/etc/localtime)
410 // http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
411 setenv("TZ", ":/etc/localtime", 0);
414 char path[1024 + 1], *p = getenv("PATH");
415 if(!p) p = "/bin:/usr/bin";
416 snprintfz(path, 1024, "%s:%s", p, "/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
417 setenv("PATH", config_get("plugins", "PATH environment variable", path), 1);
420 // cd to /tmp to avoid any plugins writing files at random places
421 if(chdir("/tmp")) error("netdata: ERROR: Cannot cd to /tmp");
423 char *input_log_file = NULL;
424 char *output_log_file = NULL;
425 char *error_log_file = NULL;
426 char *access_log_file = NULL;
429 char *flags = config_get("global", "debug flags", "0x00000000");
430 setenv("NETDATA_DEBUG_FLAGS", flags, 1);
432 debug_flags = strtoull(flags, NULL, 0);
433 debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
435 if(debug_flags != 0) {
436 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
437 if(setrlimit(RLIMIT_CORE, &rl) != 0)
438 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
439 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
442 // --------------------------------------------------------------------
444 #ifdef MADV_MERGEABLE
445 enable_ksm = config_get_boolean("global", "memory deduplication (ksm)", enable_ksm);
447 #warning "Kernel memory deduplication (KSM) is not available"
450 // --------------------------------------------------------------------
453 global_host_prefix = config_get("global", "host access prefix", "");
454 setenv("NETDATA_HOST_PREFIX", global_host_prefix, 1);
456 // --------------------------------------------------------------------
458 output_log_file = config_get("global", "debug log", LOG_DIR "/debug.log");
459 if(strcmp(output_log_file, "syslog") == 0) {
460 output_log_syslog = 1;
461 output_log_file = NULL;
463 else if(strcmp(output_log_file, "none") == 0) {
464 output_log_syslog = 0;
465 output_log_file = NULL;
467 else output_log_syslog = 0;
469 // --------------------------------------------------------------------
471 error_log_file = config_get("global", "error log", LOG_DIR "/error.log");
472 if(strcmp(error_log_file, "syslog") == 0) {
473 error_log_syslog = 1;
474 error_log_file = NULL;
476 else if(strcmp(error_log_file, "none") == 0) {
477 error_log_syslog = 0;
478 error_log_file = NULL;
479 // optimization - do not even generate debug log entries
481 else error_log_syslog = 0;
483 error_log_throttle_period = config_get_number("global", "errors flood protection period", error_log_throttle_period);
484 setenv("NETDATA_ERRORS_THROTTLE_PERIOD", config_get("global", "errors flood protection period" , ""), 1);
486 error_log_errors_per_period = (unsigned long)config_get_number("global", "errors to trigger flood protection", error_log_errors_per_period);
487 setenv("NETDATA_ERRORS_PER_PERIOD" , config_get("global", "errors to trigger flood protection", ""), 1);
489 // --------------------------------------------------------------------
491 access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
492 if(strcmp(access_log_file, "syslog") == 0) {
493 access_log_syslog = 1;
494 access_log_file = NULL;
496 else if(strcmp(access_log_file, "none") == 0) {
497 access_log_syslog = 0;
498 access_log_file = NULL;
500 else access_log_syslog = 0;
502 // --------------------------------------------------------------------
504 rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
506 // --------------------------------------------------------------------
509 char hostnamebuf[HOSTNAME_MAX + 1];
510 if(gethostname(hostnamebuf, HOSTNAME_MAX) == -1)
511 error("WARNING: Cannot get machine hostname.");
512 hostname = config_get("global", "hostname", hostnamebuf);
513 debug(D_OPTIONS, "hostname set to '%s'", hostname);
516 // --------------------------------------------------------------------
518 rrd_default_history_entries = (int) config_get_number("global", "history", RRD_DEFAULT_HISTORY_ENTRIES);
519 if(rrd_default_history_entries < 5 || rrd_default_history_entries > RRD_HISTORY_ENTRIES_MAX) {
520 info("Invalid save lines %d given. Defaulting to %d.", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
521 rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
524 debug(D_OPTIONS, "save lines set to %d.", rrd_default_history_entries);
527 // --------------------------------------------------------------------
529 rrd_update_every = (int) config_get_number("global", "update every", UPDATE_EVERY);
530 if(rrd_update_every < 1 || rrd_update_every > 600) {
531 info("Invalid update timer %d given. Defaulting to %d.", rrd_update_every, UPDATE_EVERY_MAX);
532 rrd_update_every = UPDATE_EVERY;
534 else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
536 // let the plugins know the min update_every
539 snprintfz(buf, 15, "%d", rrd_update_every);
540 setenv("NETDATA_UPDATE_EVERY", buf, 1);
543 // --------------------------------------------------------------------
545 // block signals while initializing threads.
546 // this causes the threads to block signals.
550 if(pthread_sigmask(SIG_BLOCK, &sigset, NULL) == -1) {
551 error("Could not block signals for threads");
554 // Catch signals which we want to use to quit savely
556 sigemptyset(&sa.sa_mask);
557 sigaddset(&sa.sa_mask, SIGHUP);
558 sigaddset(&sa.sa_mask, SIGINT);
559 sigaddset(&sa.sa_mask, SIGTERM);
560 sa.sa_handler = sig_handler_exit;
562 if(sigaction(SIGHUP, &sa, NULL) == -1) {
563 error("Failed to change signal handler for SIGHUP");
565 if(sigaction(SIGINT, &sa, NULL) == -1) {
566 error("Failed to change signal handler for SIGINT");
568 if(sigaction(SIGTERM, &sa, NULL) == -1) {
569 error("Failed to change signal handler for SIGTERM");
572 // save database on SIGUSR1
573 sa.sa_handler = sig_handler_save;
574 if(sigaction(SIGUSR1, &sa, NULL) == -1) {
575 error("Failed to change signal handler for SIGUSR1");
578 // Ignore SIGPIPE completely.
579 // INFO: If we add signals here we have to unblock them
580 // at popen.c when running a external plugin.
581 sa.sa_handler = SIG_IGN;
582 if(sigaction(SIGPIPE, &sa, NULL) == -1) {
583 error("Failed to change signal handler for SIGPIPE");
586 // --------------------------------------------------------------------
588 i = pthread_attr_init(&attr);
590 fatal("pthread_attr_init() failed with code %d.", i);
592 i = pthread_attr_getstacksize(&attr, &stacksize);
594 fatal("pthread_attr_getstacksize() failed with code %d.", i);
596 debug(D_OPTIONS, "initial pthread stack size is %zu bytes", stacksize);
598 wanted_stacksize = config_get_number("global", "pthread stack size", stacksize);
600 // --------------------------------------------------------------------
602 for (i = 0; static_threads[i].name != NULL ; i++) {
603 struct netdata_static_thread *st = &static_threads[i];
605 if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
606 if(st->enabled && st->init_routine) st->init_routine();
609 // --------------------------------------------------------------------
611 // get the user we should run
612 // IMPORTANT: this is required before web_files_uid()
613 user = config_get("global", "run as user" , (getuid() == 0)?NETDATA_USER:"");
615 // IMPORTANT: these have to run once, while single threaded
616 web_files_uid(); // IMPORTANT: web_files_uid() before web_files_gid()
619 // --------------------------------------------------------------------
621 create_listen_sockets();
624 // never become a problem
625 if(nice(20) == -1) error("Cannot lower my CPU priority.");
627 if(become_daemon(dont_fork, 0, user, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1)
628 fatal("Cannot demonize myself.");
630 #ifdef NETDATA_INTERNAL_CHECKS
631 if(debug_flags != 0) {
632 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
633 if(setrlimit(RLIMIT_CORE, &rl) != 0)
634 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
635 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
637 #endif /* NETDATA_INTERNAL_CHECKS */
639 if(output_log_syslog || error_log_syslog || access_log_syslog)
640 openlog("netdata", LOG_PID, LOG_DAEMON);
642 info("NetData started on pid %d", getpid());
645 // ------------------------------------------------------------------------
646 // get default pthread stack size
648 if(stacksize < wanted_stacksize) {
649 i = pthread_attr_setstacksize(&attr, wanted_stacksize);
651 fatal("pthread_attr_setstacksize() to %zu bytes, failed with code %d.", wanted_stacksize, i);
653 info("Successfully set pthread stacksize to %zu bytes", wanted_stacksize);
656 // --------------------------------------------------------------------
657 // initialize the registry
661 // ------------------------------------------------------------------------
664 web_server_threading_selection();
666 for (i = 0; static_threads[i].name != NULL ; i++) {
667 struct netdata_static_thread *st = &static_threads[i];
670 st->thread = mallocz(sizeof(pthread_t));
672 info("Starting thread %s.", st->name);
674 if(pthread_create(st->thread, &attr, st->start_routine, NULL))
675 error("failed to create new thread for %s.", st->name);
677 else if(pthread_detach(*st->thread))
678 error("Cannot request detach of newly created %s thread.", st->name);
680 else info("Not starting thread %s.", st->name);
683 // ------------------------------------------------------------------------
684 // block signals while initializing threads.
688 if(pthread_sigmask(SIG_UNBLOCK, &sigset, NULL) == -1) {
689 error("Could not unblock signals for threads");
692 // Handle flags set in the signal handler.
696 info("Exit main loop of netdata.");
697 netdata_cleanup_and_exit(0);