3 extern void *cgroups_main(void *ptr);
5 void netdata_cleanup_and_exit(int ret) {
8 error_log_limit_unlimited();
10 debug(D_EXIT, "Called: netdata_cleanup_and_exit()");
17 if(unlink(pidfile) != 0)
18 error("Cannot unlink pidfile '%s'.", pidfile);
21 #ifdef NETDATA_INTERNAL_CHECKS
30 info("netdata exiting. Bye bye...");
34 struct netdata_static_thread static_threads[] = {
35 #ifdef INTERNAL_PLUGIN_NFACCT
36 // nfacct requires root access
37 // so, we build it as an external plugin with setuid to root
38 {"nfacct", CONFIG_SECTION_PLUGINS, "nfacct", 1, NULL, NULL, nfacct_main},
41 {"tc", CONFIG_SECTION_PLUGINS, "tc", 1, NULL, NULL, tc_main},
42 {"idlejitter", CONFIG_SECTION_PLUGINS, "idlejitter", 1, NULL, NULL, cpuidlejitter_main},
43 #if defined(__FreeBSD__)
44 {"freebsd", CONFIG_SECTION_PLUGINS, "freebsd", 1, NULL, NULL, freebsd_main},
45 #elif defined(__APPLE__)
46 {"macos", CONFIG_SECTION_PLUGINS, "macos", 1, NULL, NULL, macos_main},
48 {"proc", CONFIG_SECTION_PLUGINS, "proc", 1, NULL, NULL, proc_main},
49 {"diskspace", CONFIG_SECTION_PLUGINS, "diskspace", 1, NULL, NULL, proc_diskspace_main},
50 {"cgroups", CONFIG_SECTION_PLUGINS, "cgroups", 1, NULL, NULL, cgroups_main},
51 #endif /* __FreeBSD__, __APPLE__*/
52 {"check", CONFIG_SECTION_PLUGINS, "checks", 0, NULL, NULL, checks_main},
53 {"backends", NULL, NULL, 1, NULL, NULL, backends_main},
54 {"health", NULL, NULL, 1, NULL, NULL, health_main},
55 {"plugins.d", NULL, NULL, 1, NULL, NULL, pluginsd_main},
56 {"web", NULL, NULL, 1, NULL, NULL, socket_listen_main_multi_threaded},
57 {"web-single-threaded", NULL, NULL, 0, NULL, NULL, socket_listen_main_single_threaded},
58 {"push-metrics", NULL, NULL, 0, NULL, NULL, rrdpush_sender_thread},
59 {NULL, NULL, NULL, 0, NULL, NULL, NULL}
62 void web_server_threading_selection(void) {
63 web_server_mode = web_server_mode_id(config_get(CONFIG_SECTION_API, "mode", web_server_mode_name(web_server_mode)));
65 int multi_threaded = (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED);
66 int single_threaded = (web_server_mode == WEB_SERVER_MODE_SINGLE_THREADED);
69 for(i = 0; static_threads[i].name ; i++) {
70 if(static_threads[i].start_routine == socket_listen_main_multi_threaded)
71 static_threads[i].enabled = multi_threaded;
73 if(static_threads[i].start_routine == socket_listen_main_single_threaded)
74 static_threads[i].enabled = single_threaded;
77 web_client_timeout = (int) config_get_number(CONFIG_SECTION_API, "disconnect idle clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
79 respect_web_browser_do_not_track_policy = config_get_boolean(CONFIG_SECTION_API, "respect do not track policy", respect_web_browser_do_not_track_policy);
80 web_x_frame_options = config_get(CONFIG_SECTION_API, "x-frame-options header", "");
81 if(!*web_x_frame_options) web_x_frame_options = NULL;
83 #ifdef NETDATA_WITH_ZLIB
84 web_enable_gzip = config_get_boolean(CONFIG_SECTION_API, "gzip compression", web_enable_gzip);
86 char *s = config_get(CONFIG_SECTION_API, "compression strategy", "default");
87 if(!strcmp(s, "default"))
88 web_gzip_strategy = Z_DEFAULT_STRATEGY;
89 else if(!strcmp(s, "filtered"))
90 web_gzip_strategy = Z_FILTERED;
91 else if(!strcmp(s, "huffman only"))
92 web_gzip_strategy = Z_HUFFMAN_ONLY;
93 else if(!strcmp(s, "rle"))
94 web_gzip_strategy = Z_RLE;
95 else if(!strcmp(s, "fixed"))
96 web_gzip_strategy = Z_FIXED;
98 error("Invalid compression strategy '%s'. Valid strategies are 'default', 'filtered', 'huffman only', 'rle' and 'fixed'. Proceeding with 'default'.", s);
99 web_gzip_strategy = Z_DEFAULT_STRATEGY;
102 web_gzip_level = (int)config_get_number(CONFIG_SECTION_API, "compression level", 3);
103 if(web_gzip_level < 1) {
104 error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 1 (fastest compression).", web_gzip_level);
107 else if(web_gzip_level > 9) {
108 error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 9 (best compression).", web_gzip_level);
111 #endif /* NETDATA_WITH_ZLIB */
115 int killpid(pid_t pid, int sig)
118 debug(D_EXIT, "Request to kill pid %d", pid);
121 if(kill(pid, 0) == -1) {
124 error("Request to kill pid %d, but it is not running.", pid);
128 error("Request to kill pid %d, but I do not have enough permissions.", pid);
132 error("Request to kill pid %d, but I received an error.", pid);
138 ret = kill(pid, sig);
142 error("Cannot kill pid %d, but it is not running.", pid);
146 error("Cannot kill pid %d, but I do not have enough permissions.", pid);
150 error("Cannot kill pid %d, but I received an error.", pid);
161 error_log_limit_unlimited();
165 struct web_client *w;
166 for(w = web_clients; w ; w = w->next) {
167 info("Stopping web client %s", w->client_ip);
168 pthread_cancel(w->thread);
170 // pthread_join(w->thread, NULL);
176 for (i = 0; static_threads[i].name != NULL ; i++) {
177 if(static_threads[i].enabled) {
178 info("Stopping %s thread", static_threads[i].name);
179 pthread_cancel(*static_threads[i].thread);
181 // pthread_join(*static_threads[i].thread, NULL);
183 static_threads[i].enabled = 0;
188 info("Killing tc-qos-helper process %d", tc_child_pid);
189 if(killpid(tc_child_pid, SIGTERM) != -1)
190 waitid(P_PID, (id_t) tc_child_pid, &info, WEXITED);
196 for(cd = pluginsd_root ; cd ; cd = cd->next) {
197 if(cd->enabled && !cd->obsolete) {
198 info("Stopping %s plugin thread", cd->id);
199 pthread_cancel(cd->thread);
202 info("killing %s plugin child process pid %d", cd->id, cd->pid);
203 if(killpid(cd->pid, SIGTERM) != -1)
204 waitid(P_PID, (id_t) cd->pid, &info, WEXITED);
213 // if, for any reason there is any child exited
215 info("Cleaning up an other children");
216 waitid(P_PID, 0, &info, WEXITED|WNOHANG);
218 info("All threads/childs stopped.");
221 struct option_def options[] = {
222 // opt description arg name default value
223 { 'c', "Configuration file to load.", "filename", CONFIG_DIR "/" CONFIG_FILENAME},
224 { 'D', "Do not fork. Run in the foreground.", NULL, "run in the background"},
225 { 'h', "Display this help message.", NULL, NULL},
226 { 'P', "File to save a pid while running.", "filename", "do not save pid to a file"},
227 { 'i', "The IP address to listen to.", "IP", "all IP addresses IPv4 and IPv6"},
228 { 'k', "Check health configuration and exit.", NULL, NULL},
229 { 'p', "API/Web port to use.", "port", "19999"},
230 { 's', "Prefix for /proc and /sys (for containers).", "path", "no prefix"},
231 { 't', "The internal clock of netdata.", "seconds", "1"},
232 { 'u', "Run as user.", "username", "netdata"},
233 { 'v', "Print netdata version and exit.", NULL, NULL},
234 { 'W', "See Advanced options below.", "options", NULL},
237 void help(int exitcode) {
244 int num_opts = sizeof(options) / sizeof(struct option_def);
248 // Compute maximum argument length
249 for( i = 0; i < num_opts; i++ ) {
250 if(options[i].arg_name) {
251 int len_arg = (int)strlen(options[i].arg_name);
252 if(len_arg > max_len_arg) max_len_arg = len_arg;
256 if(max_len_arg > 30) max_len_arg = 30;
257 if(max_len_arg < 20) max_len_arg = 20;
259 fprintf(stream, "%s", "\n"
261 " |.-. .-. .-. .-. . netdata \n"
262 " | '-' '-' '-' '-' real-time performance monitoring, done right! \n"
263 " +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->\n"
265 " Copyright (C) 2016-2017, Costa Tsaousis <costa@tsaousis.gr>\n"
266 " Released under GNU General Public License v3 or later.\n"
267 " All rights reserved.\n"
269 " Home Page : https://my-netdata.io\n"
270 " Source Code: https://github.com/firehol/netdata\n"
271 " Wiki / Docs: https://github.com/firehol/netdata/wiki\n"
272 " Support : https://github.com/firehol/netdata/issues\n"
273 " License : https://github.com/firehol/netdata/blob/master/LICENSE.md\n"
275 " Twitter : https://twitter.com/linuxnetdata\n"
276 " Facebook : https://www.facebook.com/linuxnetdata/\n"
278 " netdata is a https://firehol.org project.\n"
283 fprintf(stream, " SYNOPSIS: netdata [options]\n");
284 fprintf(stream, "\n");
285 fprintf(stream, " Options:\n\n");
287 // Output options description.
288 for( i = 0; i < num_opts; i++ ) {
289 fprintf(stream, " -%c %-*s %s", options[i].val, max_len_arg, options[i].arg_name ? options[i].arg_name : "", options[i].description);
290 if(options[i].default_value) {
291 fprintf(stream, "\n %c %-*s Default: %s\n", ' ', max_len_arg, "", options[i].default_value);
293 fprintf(stream, "\n");
295 fprintf(stream, "\n");
298 fprintf(stream, "\n Advanced options:\n\n"
299 " -W stacksize=N Set the stacksize (in bytes).\n\n"
300 " -W debug_flags=N Set runtime tracing to debug.log.\n\n"
301 " -W unittest Run internal unittests and exit.\n\n"
302 " -W simple-pattern pattern string\n"
303 " Check if string matches pattern and exit.\n\n"
306 fprintf(stream, "\n Signals netdata handles:\n\n"
307 " - HUP Close and reopen log files.\n"
308 " - USR1 Save internal DB to disk.\n"
309 " - USR2 Reload health configuration.\n"
317 // TODO: Remove this function with the nix major release.
318 void remove_option(int opt_index, int *argc, char **argv) {
320 // remove the options.
323 for(i = opt_index; i < *argc; i++) {
327 } while(argv[i][0] != '-' && opt_index >= *argc);
330 static const char *verify_required_directory(const char *dir) {
332 fatal("Cannot cd to directory '%s'", dir);
334 DIR *d = opendir(dir);
336 fatal("Cannot examine the contents of directory '%s'", dir);
342 void log_init(void) {
343 char filename[FILENAME_MAX + 1];
344 snprintfz(filename, FILENAME_MAX, "%s/debug.log", netdata_configured_log_dir);
345 stdout_filename = config_get(CONFIG_SECTION_GLOBAL, "debug log", filename);
347 snprintfz(filename, FILENAME_MAX, "%s/error.log", netdata_configured_log_dir);
348 stderr_filename = config_get(CONFIG_SECTION_GLOBAL, "error log", filename);
350 snprintfz(filename, FILENAME_MAX, "%s/access.log", netdata_configured_log_dir);
351 stdaccess_filename = config_get(CONFIG_SECTION_GLOBAL, "access log", filename);
353 error_log_throttle_period_backup =
354 error_log_throttle_period = config_get_number(CONFIG_SECTION_GLOBAL, "errors flood protection period", error_log_throttle_period);
355 error_log_errors_per_period = (unsigned long)config_get_number(CONFIG_SECTION_GLOBAL, "errors to trigger flood protection", (long long int)error_log_errors_per_period);
357 setenv("NETDATA_ERRORS_THROTTLE_PERIOD", config_get(CONFIG_SECTION_GLOBAL, "errors flood protection period" , ""), 1);
358 setenv("NETDATA_ERRORS_PER_PERIOD", config_get(CONFIG_SECTION_GLOBAL, "errors to trigger flood protection", ""), 1);
361 static void backwards_compatible_config() {
362 // allow existing configurations to work with the current version of netdata
364 if(config_exists(CONFIG_SECTION_GLOBAL, "multi threaded web server")) {
365 int mode = config_get_boolean(CONFIG_SECTION_GLOBAL, "multi threaded web server", 1);
366 web_server_mode = (mode)?WEB_SERVER_MODE_MULTI_THREADED:WEB_SERVER_MODE_SINGLE_THREADED;
369 if(config_exists(CONFIG_SECTION_GLOBAL, "bind socket to IP") && !config_exists(CONFIG_SECTION_API, "bind to"))
370 config_move(CONFIG_SECTION_GLOBAL, "bind socket to IP", CONFIG_SECTION_API, "bind to");
372 if(config_exists(CONFIG_SECTION_GLOBAL, "bind to") && !config_exists(CONFIG_SECTION_API, "bind to"))
373 config_move(CONFIG_SECTION_GLOBAL, "bind to", CONFIG_SECTION_API, "bind to");
375 if(config_exists(CONFIG_SECTION_GLOBAL, "port") && !config_exists(CONFIG_SECTION_API, "default port"))
376 config_move(CONFIG_SECTION_GLOBAL, "port", CONFIG_SECTION_API, "default port");
378 if(config_exists(CONFIG_SECTION_GLOBAL, "default port") && !config_exists(CONFIG_SECTION_API, "default port"))
379 config_move(CONFIG_SECTION_GLOBAL, "default port", CONFIG_SECTION_API, "default port");
381 if(config_exists(CONFIG_SECTION_GLOBAL, "disconnect idle clients after seconds") && !config_exists(CONFIG_SECTION_API, "disconnect idle clients after seconds"))
382 config_move(CONFIG_SECTION_GLOBAL, "disconnect idle clients after seconds", CONFIG_SECTION_API, "disconnect idle clients after seconds");
384 if(config_exists(CONFIG_SECTION_GLOBAL, "respect web browser do not track policy") && !config_exists(CONFIG_SECTION_API, "respect do not track policy"))
385 config_move(CONFIG_SECTION_GLOBAL, "respect web browser do not track policy", CONFIG_SECTION_API, "respect do not track policy");
387 if(config_exists(CONFIG_SECTION_GLOBAL, "web x-frame-options header") && !config_exists(CONFIG_SECTION_API, "x-frame-options header"))
388 config_move(CONFIG_SECTION_GLOBAL, "web x-frame-options header", CONFIG_SECTION_API, "x-frame-options header");
390 if(config_exists(CONFIG_SECTION_GLOBAL, "enable web responses gzip compression") && !config_exists(CONFIG_SECTION_API, "gzip compression"))
391 config_move(CONFIG_SECTION_GLOBAL, "enable web responses gzip compression", CONFIG_SECTION_API, "gzip compression");
393 if(config_exists(CONFIG_SECTION_GLOBAL, "web compression strategy") && !config_exists(CONFIG_SECTION_API, "compression strategy"))
394 config_move(CONFIG_SECTION_GLOBAL, "web compression strategy", CONFIG_SECTION_API, "compression strategy");
396 if(config_exists(CONFIG_SECTION_GLOBAL, "web compression level") && !config_exists(CONFIG_SECTION_API, "compression level"))
397 config_move(CONFIG_SECTION_GLOBAL, "web compression level", CONFIG_SECTION_API, "compression level");
399 if(config_exists(CONFIG_SECTION_GLOBAL, "web files owner") && !config_exists(CONFIG_SECTION_API, "web files owner"))
400 config_move(CONFIG_SECTION_GLOBAL, "web files owner", CONFIG_SECTION_API, "web files owner");
402 if(config_exists(CONFIG_SECTION_GLOBAL, "web files group") && !config_exists(CONFIG_SECTION_API, "web files group"))
403 config_move(CONFIG_SECTION_GLOBAL, "web files group", CONFIG_SECTION_API, "web files group");
406 static void get_netdata_configured_variables() {
407 backwards_compatible_config();
409 // ------------------------------------------------------------------------
412 char buf[HOSTNAME_MAX + 1];
413 if(gethostname(buf, HOSTNAME_MAX) == -1)
414 error("WARNING: Cannot get machine hostname.");
416 netdata_configured_hostname = config_get(CONFIG_SECTION_GLOBAL, "hostname", buf);
417 debug(D_OPTIONS, "hostname set to '%s'", netdata_configured_hostname);
419 netdata_configured_hostname = config_get(CONFIG_SECTION_GLOBAL, "hostname", CONFIG_DIR);
421 // ------------------------------------------------------------------------
422 // get default database size
424 default_rrd_history_entries = (int) config_get_number(CONFIG_SECTION_GLOBAL, "history", align_entries_to_pagesize(default_rrd_memory_mode, RRD_DEFAULT_HISTORY_ENTRIES));
426 long h = align_entries_to_pagesize(default_rrd_memory_mode, default_rrd_history_entries);
427 if(h != default_rrd_history_entries) {
428 config_set_number(CONFIG_SECTION_GLOBAL, "history", h);
429 default_rrd_history_entries = (int)h;
432 if(default_rrd_history_entries < 5 || default_rrd_history_entries > RRD_HISTORY_ENTRIES_MAX) {
433 error("Invalid history entries %d given. Defaulting to %d.", default_rrd_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
434 default_rrd_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
437 // ------------------------------------------------------------------------
438 // get default database update frequency
440 default_rrd_update_every = (int) config_get_number(CONFIG_SECTION_GLOBAL, "update every", UPDATE_EVERY);
441 if(default_rrd_update_every < 1 || default_rrd_update_every > 600) {
442 error("Invalid data collection frequency (update every) %d given. Defaulting to %d.", default_rrd_update_every, UPDATE_EVERY_MAX);
443 default_rrd_update_every = UPDATE_EVERY;
446 // ------------------------------------------------------------------------
447 // let the plugins know the min update_every
450 netdata_configured_config_dir = config_get(CONFIG_SECTION_GLOBAL, "config directory", CONFIG_DIR);
451 netdata_configured_log_dir = config_get(CONFIG_SECTION_GLOBAL, "log directory", LOG_DIR);
452 netdata_configured_plugins_dir = config_get(CONFIG_SECTION_GLOBAL, "plugins directory", PLUGINS_DIR);
453 netdata_configured_web_dir = config_get(CONFIG_SECTION_GLOBAL, "web files directory", WEB_DIR);
454 netdata_configured_cache_dir = config_get(CONFIG_SECTION_GLOBAL, "cache directory", CACHE_DIR);
455 netdata_configured_varlib_dir = config_get(CONFIG_SECTION_GLOBAL, "lib directory", VARLIB_DIR);
456 netdata_configured_home_dir = config_get(CONFIG_SECTION_GLOBAL, "home directory", CACHE_DIR);
458 // ------------------------------------------------------------------------
459 // get default memory mode for the database
461 default_rrd_memory_mode = rrd_memory_mode_id(config_get(CONFIG_SECTION_GLOBAL, "memory mode", rrd_memory_mode_name(default_rrd_memory_mode)));
463 // ------------------------------------------------------------------------
465 netdata_configured_host_prefix = config_get(CONFIG_SECTION_GLOBAL, "host access prefix", "");
467 // --------------------------------------------------------------------
470 #ifdef MADV_MERGEABLE
471 enable_ksm = config_get_boolean(CONFIG_SECTION_GLOBAL, "memory deduplication (ksm)", enable_ksm);
474 // --------------------------------------------------------------------
475 // get various system parameters
479 get_system_pid_max();
482 void set_global_environment() {
485 snprintfz(b, 15, "%d", default_rrd_update_every);
486 setenv("NETDATA_UPDATE_EVERY", b, 1);
489 setenv("NETDATA_HOSTNAME" , netdata_configured_hostname, 1);
490 setenv("NETDATA_CONFIG_DIR" , verify_required_directory(netdata_configured_config_dir), 1);
491 setenv("NETDATA_PLUGINS_DIR", verify_required_directory(netdata_configured_plugins_dir), 1);
492 setenv("NETDATA_WEB_DIR" , verify_required_directory(netdata_configured_web_dir), 1);
493 setenv("NETDATA_CACHE_DIR" , verify_required_directory(netdata_configured_cache_dir), 1);
494 setenv("NETDATA_LIB_DIR" , verify_required_directory(netdata_configured_varlib_dir), 1);
495 setenv("NETDATA_LOG_DIR" , verify_required_directory(netdata_configured_log_dir), 1);
496 setenv("HOME" , verify_required_directory(netdata_configured_home_dir), 1);
497 setenv("NETDATA_HOST_PREFIX", netdata_configured_host_prefix, 1);
499 // avoid flood calls to stat(/etc/localtime)
500 // http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
501 setenv("TZ", ":/etc/localtime", 0);
503 // set the path we need
504 char path[1024 + 1], *p = getenv("PATH");
505 if(!p) p = "/bin:/usr/bin";
506 snprintfz(path, 1024, "%s:%s", p, "/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
507 setenv("PATH", config_get(CONFIG_SECTION_PLUGINS, "PATH environment variable", path), 1);
510 p = getenv("PYTHONPATH");
512 setenv("PYTHONPATH", config_get(CONFIG_SECTION_PLUGINS, "PYTHONPATH environment variable", p), 1);
514 // disable buffering for python plugins
515 setenv("PYTHONUNBUFFERED", "1", 1);
518 int main(int argc, char **argv) {
519 int i, check_config = 0;
520 int config_loaded = 0;
522 size_t wanted_stacksize = 0, stacksize = 0;
525 // set the name for logging
526 program_name = "netdata";
528 // parse depercated options
529 // TODO: Remove this block with the next major release.
533 if(strcmp(argv[i], "-pidfile") == 0 && (i+1) < argc) {
534 strncpyz(pidfile, argv[i+1], FILENAME_MAX);
535 fprintf(stderr, "%s: deprecated option -- %s -- please use -P instead.\n", argv[0], argv[i]);
536 remove_option(i, &argc, argv);
538 else if(strcmp(argv[i], "-nodaemon") == 0 || strcmp(argv[i], "-nd") == 0) {
540 fprintf(stderr, "%s: deprecated option -- %s -- please use -D instead.\n ", argv[0], argv[i]);
541 remove_option(i, &argc, argv);
543 else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) {
544 config_set(CONFIG_SECTION_GLOBAL, "host access prefix", argv[i+1]);
545 fprintf(stderr, "%s: deprecated option -- %s -- please use -s instead.\n", argv[0], argv[i]);
546 remove_option(i, &argc, argv);
548 else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) {
549 config_set(CONFIG_SECTION_GLOBAL, "history", argv[i+1]);
550 fprintf(stderr, "%s: deprecated option -- %s -- This option will be removed with V2.*.\n", argv[0], argv[i]);
551 remove_option(i, &argc, argv);
559 int num_opts = sizeof(options) / sizeof(struct option_def);
560 char optstring[(num_opts * 2) + 1];
563 for( i = 0; i < num_opts; i++ ) {
564 optstring[string_i] = options[i].val;
566 if(options[i].arg_name) {
567 optstring[string_i] = ':';
571 // terminate optstring
572 optstring[string_i] ='\0';
573 optstring[(num_opts *2)] ='\0';
576 while( (opt = getopt(argc, argv, optstring)) != -1 ) {
579 if(config_load(optarg, 1) != 1) {
580 error("Cannot load configuration file %s.", optarg);
584 debug(D_OPTIONS, "Configuration loaded from %s.", optarg);
595 config_set(CONFIG_SECTION_API, "bind to", optarg);
602 strncpy(pidfile, optarg, FILENAME_MAX);
603 pidfile[FILENAME_MAX] = '\0';
606 config_set(CONFIG_SECTION_GLOBAL, "default port", optarg);
609 config_set(CONFIG_SECTION_GLOBAL, "host access prefix", optarg);
612 config_set(CONFIG_SECTION_GLOBAL, "update every", optarg);
615 config_set(CONFIG_SECTION_GLOBAL, "run as user", optarg);
618 printf("%s %s\n", program_name, program_version);
622 char* stacksize_string = "stacksize=";
623 char* debug_flags_string = "debug_flags=";
624 if(strcmp(optarg, "unittest") == 0) {
625 default_rrd_update_every = 1;
626 if(!config_loaded) config_load(NULL, 0);
627 get_netdata_configured_variables();
629 rrd_init("unittest");
630 if(run_all_mockup_tests()) exit(1);
631 if(unit_test_storage()) exit(1);
632 fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
635 else if(strcmp(optarg, "simple-pattern") == 0) {
636 if(optind + 2 > argc) {
637 fprintf(stderr, "%s", "\nUSAGE: -W simple-pattern 'pattern' 'string'\n\n"
638 " Checks if 'pattern' matches the given 'string'.\n"
639 " - 'pattern' can be one or more space separated words.\n"
640 " - each 'word' can contain one or more asterisks.\n"
641 " - words starting with '!' give negative matches.\n"
642 " - words are processed left to right\n"
646 " > match all veth interfaces, except veth0:\n"
648 " -W simple-pattern '!veth0 veth*' 'veth12'\n"
651 " > match all *.ext files directly in /path/:\n"
652 " (this will not match *.ext files in a subdir of /path/)\n"
654 " -W simple-pattern '!/path/*/*.ext /path/*.ext' '/path/test.ext'\n"
660 const char *heystack = argv[optind];
661 const char *needle = argv[optind + 1];
663 SIMPLE_PATTERN *p = simple_pattern_create(heystack
664 , SIMPLE_PATTERN_EXACT);
665 int ret = simple_pattern_matches(p, needle);
666 simple_pattern_free(p);
669 fprintf(stdout, "RESULT: MATCHED - pattern '%s' matches '%s'\n", heystack, needle);
673 fprintf(stdout, "RESULT: NOT MATCHED - pattern '%s' does not match '%s'\n", heystack, needle);
677 else if(strncmp(optarg, stacksize_string, strlen(stacksize_string)) == 0) {
678 optarg += strlen(stacksize_string);
679 config_set(CONFIG_SECTION_GLOBAL, "pthread stack size", optarg);
681 else if(strncmp(optarg, debug_flags_string, strlen(debug_flags_string)) == 0) {
682 optarg += strlen(debug_flags_string);
683 config_set(CONFIG_SECTION_GLOBAL, "debug flags", optarg);
684 debug_flags = strtoull(optarg, NULL, 0);
696 // close all open file descriptors, except the standard ones
697 // the caller may have left open files (lxc-attach has this issue)
700 for(fd = (int) (sysconf(_SC_OPEN_MAX) - 1); fd > 2; fd--)
701 if(fd_is_valid(fd)) close(fd);
706 config_load(NULL, 0);
709 char *pmax = config_get(CONFIG_SECTION_GLOBAL, "glibc malloc arena max for plugins", "1");
711 setenv("MALLOC_ARENA_MAX", pmax, 1);
713 #if defined(HAVE_C_MALLOPT)
714 i = (int)config_get_number(CONFIG_SECTION_GLOBAL, "glibc malloc arena max for netdata", 1);
716 mallopt(M_ARENA_MAX, 1);
719 // prepare configuration environment variables for the plugins
721 get_netdata_configured_variables();
722 set_global_environment();
724 // work while we are cd into config_dir
725 // to allow the plugins refer to their config
726 // files using relative filenames
727 if(chdir(netdata_configured_config_dir) == -1)
728 fatal("Cannot cd to '%s'", netdata_configured_config_dir);
734 // --------------------------------------------------------------------
735 // get the debugging flags from the configuration file
737 char *flags = config_get(CONFIG_SECTION_GLOBAL, "debug flags", "0x0000000000000000");
738 setenv("NETDATA_DEBUG_FLAGS", flags, 1);
740 debug_flags = strtoull(flags, NULL, 0);
741 debug(D_OPTIONS, "Debug flags set to '0x%" PRIX64 "'.", debug_flags);
743 if(debug_flags != 0) {
744 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
745 if(setrlimit(RLIMIT_CORE, &rl) != 0)
746 error("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
748 #ifdef HAVE_SYS_PRCTL_H
749 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
754 // --------------------------------------------------------------------
755 // get log filenames and settings
759 stdout_filename = stderr_filename = stdaccess_filename = "system";
760 error_log_throttle_period = 0;
761 error_log_errors_per_period = 0;
763 error_log_limit_unlimited();
765 // --------------------------------------------------------------------
766 // setup process signals
768 // block signals while initializing threads.
769 // this causes the threads to block signals.
772 if(pthread_sigmask(SIG_BLOCK, &sigset, NULL) == -1)
773 error("Could not block signals for threads");
775 // Catch signals which we want to use
779 // ingore all signals while we run in a signal handler
780 sigfillset(&sa.sa_mask);
782 // INFO: If we add signals here we have to unblock them
783 // at popen.c when running a external plugin.
785 // Ignore SIGPIPE completely.
786 sa.sa_handler = SIG_IGN;
787 if(sigaction(SIGPIPE, &sa, NULL) == -1)
788 error("Failed to change signal handler for SIGPIPE");
790 sa.sa_handler = sig_handler_exit;
791 if(sigaction(SIGINT, &sa, NULL) == -1)
792 error("Failed to change signal handler for SIGINT");
794 sa.sa_handler = sig_handler_exit;
795 if(sigaction(SIGTERM, &sa, NULL) == -1)
796 error("Failed to change signal handler for SIGTERM");
798 sa.sa_handler = sig_handler_logrotate;
799 if(sigaction(SIGHUP, &sa, NULL) == -1)
800 error("Failed to change signal handler for SIGHUP");
802 // save database on SIGUSR1
803 sa.sa_handler = sig_handler_save;
804 if(sigaction(SIGUSR1, &sa, NULL) == -1)
805 error("Failed to change signal handler for SIGUSR1");
807 // reload health configuration on SIGUSR2
808 sa.sa_handler = sig_handler_reload_health;
809 if(sigaction(SIGUSR2, &sa, NULL) == -1)
810 error("Failed to change signal handler for SIGUSR2");
813 // --------------------------------------------------------------------
814 // get the required stack size of the threads of netdata
816 i = pthread_attr_init(&attr);
818 fatal("pthread_attr_init() failed with code %d.", i);
820 i = pthread_attr_getstacksize(&attr, &stacksize);
822 fatal("pthread_attr_getstacksize() failed with code %d.", i);
824 debug(D_OPTIONS, "initial pthread stack size is %zu bytes", stacksize);
826 wanted_stacksize = (size_t)config_get_number(CONFIG_SECTION_GLOBAL, "pthread stack size", (long)stacksize);
829 // --------------------------------------------------------------------
830 // check which threads are enabled and initialize them
832 for (i = 0; static_threads[i].name != NULL ; i++) {
833 struct netdata_static_thread *st = &static_threads[i];
836 st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
838 if(st->enabled && st->init_routine)
843 // --------------------------------------------------------------------
844 // get the user we should run
846 // IMPORTANT: this is required before web_files_uid()
847 user = config_get(CONFIG_SECTION_GLOBAL, "run as user" , (getuid() == 0)?NETDATA_USER:"");
849 // IMPORTANT: these have to run once, while single threaded
850 web_files_uid(); // IMPORTANT: web_files_uid() before web_files_gid()
854 // --------------------------------------------------------------------
855 // create the listening sockets
857 if(!check_config && web_server_mode != WEB_SERVER_MODE_NONE)
858 create_listen_sockets();
861 // --------------------------------------------------------------------
862 // load the aggregated host configuration file
864 char filename[FILENAME_MAX + 1];
865 snprintfz(filename, FILENAME_MAX, "%s/aggregated_hosts.conf", netdata_configured_config_dir);
866 appconfig_load(&stream_config, filename, 0);
870 // initialize the log files
871 open_all_log_files();
873 #ifdef NETDATA_INTERNAL_CHECKS
874 if(debug_flags != 0) {
875 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
876 if(setrlimit(RLIMIT_CORE, &rl) != 0)
877 error("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
878 #ifdef HAVE_SYS_PRCTL_H
879 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
882 #endif /* NETDATA_INTERNAL_CHECKS */
885 // fork, switch user, create pid file, set process priority
886 if(become_daemon(dont_fork, user) == -1)
887 fatal("Cannot daemonize myself.");
889 info("netdata started on pid %d.", getpid());
892 // ------------------------------------------------------------------------
893 // set default pthread stack size - after we have forked
895 if(stacksize < wanted_stacksize) {
896 i = pthread_attr_setstacksize(&attr, wanted_stacksize);
898 fatal("pthread_attr_setstacksize() to %zu bytes, failed with code %d.", wanted_stacksize, i);
900 debug(D_SYSTEM, "Successfully set pthread stacksize to %zu bytes", wanted_stacksize);
904 // --------------------------------------------------------------------
905 // find we need to send data to another netdata
910 // ------------------------------------------------------------------------
911 // initialize health monitoring
916 // ------------------------------------------------------------------------
917 // initialize the registry
922 // ------------------------------------------------------------------------
923 // initialize rrd host
925 rrd_init(netdata_configured_hostname);
932 // ------------------------------------------------------------------------
933 // enable log flood protection
935 error_log_limit_reset();
938 // ------------------------------------------------------------------------
941 web_server_threading_selection();
943 for (i = 0; static_threads[i].name != NULL ; i++) {
944 struct netdata_static_thread *st = &static_threads[i];
947 st->thread = mallocz(sizeof(pthread_t));
949 debug(D_SYSTEM, "Starting thread %s.", st->name);
951 if(pthread_create(st->thread, &attr, st->start_routine, st))
952 error("failed to create new thread for %s.", st->name);
954 else if(pthread_detach(*st->thread))
955 error("Cannot request detach of newly created %s thread.", st->name);
957 else debug(D_SYSTEM, "Not starting thread %s.", st->name);
960 info("netdata initialization completed. Enjoy real-time performance monitoring!");
963 // ------------------------------------------------------------------------
964 // block signals while initializing threads.
968 if(pthread_sigmask(SIG_UNBLOCK, &sigset, NULL) == -1) {
969 error("Could not unblock signals for threads");
972 // Handle flags set in the signal handler.
976 debug(D_EXIT, "Exit main loop of netdata.");
977 netdata_cleanup_and_exit(0);