12 #include <sys/types.h>
15 #include <sys/resource.h>
17 #include <sys/prctl.h>
22 #include "web_server.h"
24 #include "appconfig.h"
25 #include "web_client.h"
29 #include "unit_test.h"
31 #include "plugins_d.h"
32 #include "plugin_idlejitter.h"
33 #include "plugin_tc.h"
34 #include "plugin_checks.h"
35 #include "plugin_proc.h"
36 #include "plugin_nfacct.h"
42 void netdata_cleanup_and_exit(int ret)
47 unlink("/var/run/netdata.pid");
48 info("NetData exiting. Bye bye...");
52 struct netdata_static_thread {
62 void (*init_routine) (void);
63 void *(*start_routine) (void *);
66 struct netdata_static_thread static_threads[] = {
67 {"tc", "plugins", "tc", 1, NULL, NULL, tc_main},
68 {"idlejitter", "plugins", "idlejitter", 1, NULL, NULL, cpuidlejitter_main},
69 {"proc", "plugins", "proc", 1, NULL, NULL, proc_main},
71 #ifdef INTERNAL_PLUGIN_NFACCT
72 // nfacct requires root access
73 // so, we build it as an external plugin with setuid to root
74 {"nfacct", "plugins", "nfacct", 1, NULL, NULL, nfacct_main},
77 {"plugins.d", NULL, NULL, 1, NULL, NULL, pluginsd_main},
78 {"check", "plugins", "checks", 0, NULL, NULL, checks_main},
79 {"web", NULL, NULL, 1, NULL, NULL, socket_listen_main},
80 {NULL, NULL, NULL, 0, NULL, NULL, NULL}
83 int killpid(pid_t pid, int sig)
86 debug(D_EXIT, "Request to kill pid %d", pid);
89 if(kill(pid, 0) == -1) {
92 error("Request to kill pid %d, but it is not running.", pid);
96 error("Request to kill pid %d, but I do not have enough permissions.", pid);
100 error("Request to kill pid %d, but I received an error.", pid);
108 old = signal(sig, SIG_IGN);
110 error("Cannot overwrite signal handler for signal %d", sig);
114 ret = kill(pid, sig);
116 if(signal(sig, old) == SIG_ERR)
117 error("Cannot restore signal handler for signal %d", sig);
122 error("Cannot kill pid %d, but it is not running.", pid);
126 error("Cannot kill pid %d, but I do not have enough permissions.", pid);
130 error("Cannot kill pid %d, but I received an error.", pid);
143 struct web_client *w;
144 for(w = web_clients; w ; w = w->next) {
145 debug(D_EXIT, "Stopping web client %s", w->client_ip);
146 pthread_cancel(w->thread);
147 pthread_join(w->thread, NULL);
151 for (i = 0; static_threads[i].name != NULL ; i++) {
152 if(static_threads[i].thread) {
153 debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
154 pthread_cancel(*static_threads[i].thread);
155 pthread_join(*static_threads[i].thread, NULL);
156 static_threads[i].thread = NULL;
161 info("Killing tc-qos-helper procees");
162 if(killpid(tc_child_pid, SIGTERM) != -1)
163 waitid(P_PID, (id_t) tc_child_pid, &info, WEXITED);
168 for(cd = pluginsd_root ; cd ; cd = cd->next) {
169 debug(D_EXIT, "Stopping %s plugin thread", cd->id);
170 pthread_cancel(cd->thread);
171 pthread_join(cd->thread, NULL);
173 if(cd->pid && !cd->obsolete) {
174 debug(D_EXIT, "killing %s plugin process", cd->id);
175 if(killpid(cd->pid, SIGTERM) != -1)
176 waitid(P_PID, (id_t) cd->pid, &info, WEXITED);
180 // if, for any reason there is any child exited
182 waitid(P_PID, 0, &info, WEXITED|WNOHANG);
184 debug(D_EXIT, "All threads/childs stopped.");
188 int main(int argc, char **argv)
191 int config_loaded = 0;
194 // global initialization
197 // set the name for logging
198 program_name = "netdata";
200 // parse the arguments
201 for(i = 1; i < argc ; i++) {
202 if(strcmp(argv[i], "-c") == 0 && (i+1) < argc) {
203 if(load_config(argv[i+1], 1) != 1) {
204 error("Cannot load configuration file %s.", argv[i+1]);
208 debug(D_OPTIONS, "Configuration loaded from %s.", argv[i+1]);
213 else if(strcmp(argv[i], "-df") == 0 && (i+1) < argc) { config_set("global", "debug flags", argv[i+1]); debug_flags = strtoull(argv[i+1], NULL, 0); i++; }
214 else if(strcmp(argv[i], "-p") == 0 && (i+1) < argc) { config_set("global", "port", argv[i+1]); i++; }
215 else if(strcmp(argv[i], "-u") == 0 && (i+1) < argc) { config_set("global", "run as user", argv[i+1]); i++; }
216 else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) { config_set("global", "history", argv[i+1]); i++; }
217 else if(strcmp(argv[i], "-t") == 0 && (i+1) < argc) { config_set("global", "update every", argv[i+1]); i++; }
218 else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) { config_set("global", "host access prefix", argv[i+1]); i++; }
219 else if(strcmp(argv[i], "-nodaemon") == 0 || strcmp(argv[i], "-nd") == 0) dont_fork = 1;
220 else if(strcmp(argv[i], "--unittest") == 0) {
221 rrd_update_every = 1;
222 if(run_all_mockup_tests()) exit(1);
223 if(unit_test_storage()) exit(1);
224 fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
228 fprintf(stderr, "Cannot understand option '%s'.\n", argv[i]);
229 fprintf(stderr, "\nUSAGE: %s [-d] [-l LINES_TO_SAVE] [-u UPDATE_TIMER] [-p LISTEN_PORT] [-df debug flags].\n\n", argv[0]);
230 fprintf(stderr, " -c CONFIG FILE the configuration file to load. Default: %s.\n", CONFIG_DIR "/" CONFIG_FILENAME);
231 fprintf(stderr, " -l LINES_TO_SAVE can be from 5 to %d lines in JSON data. Default: %d.\n", RRD_HISTORY_ENTRIES_MAX, RRD_DEFAULT_HISTORY_ENTRIES);
232 fprintf(stderr, " -t UPDATE_TIMER can be from 1 to %d seconds. Default: %d.\n", UPDATE_EVERY_MAX, UPDATE_EVERY);
233 fprintf(stderr, " -p LISTEN_PORT can be from 1 to %d. Default: %d.\n", 65535, LISTEN_PORT);
234 fprintf(stderr, " -u USERNAME can be any system username to run as. Default: none.\n");
235 fprintf(stderr, " -ch path to access host /proc and /sys when running in a container. Default: empty.\n");
236 fprintf(stderr, " -nd or -nodeamon to disable forking in the background. Default: unset.\n");
237 fprintf(stderr, " -df FLAGS debug options. Default: 0x%08llx.\n", debug_flags);
242 if(!config_loaded) load_config(NULL, 0);
244 // prepare configuration environment variables for the plugins
245 setenv("NETDATA_CONFIG_DIR" , config_get("global", "config directory" , CONFIG_DIR) , 1);
246 setenv("NETDATA_PLUGINS_DIR", config_get("global", "plugins directory" , PLUGINS_DIR), 1);
247 setenv("NETDATA_WEB_DIR" , config_get("global", "web files directory", WEB_DIR) , 1);
248 setenv("NETDATA_CACHE_DIR" , config_get("global", "cache directory" , CACHE_DIR) , 1);
249 setenv("NETDATA_LOG_DIR" , config_get("global", "log directory" , LOG_DIR) , 1);
250 setenv("NETDATA_HOST_PREFIX", config_get("global", "host access prefix" , "") , 1);
252 // avoid extended to stat(/etc/localtime)
253 // http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
254 setenv("TZ", ":/etc/localtime", 0);
256 // cd to /tmp to avoid any plugins writing files at random places
257 if(chdir("/tmp")) error("netdata: ERROR: Cannot cd to /tmp");
259 char *input_log_file = NULL;
260 char *output_log_file = NULL;
261 char *error_log_file = NULL;
262 char *access_log_file = NULL;
267 // --------------------------------------------------------------------
269 sprintf(buffer, "0x%08llx", 0ULL);
270 char *flags = config_get("global", "debug flags", buffer);
271 setenv("NETDATA_DEBUG_FLAGS", flags, 1);
273 debug_flags = strtoull(flags, NULL, 0);
274 debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
276 if(debug_flags != 0) {
277 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
278 if(setrlimit(RLIMIT_CORE, &rl) != 0)
279 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
280 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
283 // --------------------------------------------------------------------
285 #ifdef MADV_MERGEABLE
286 enable_ksm = config_get_boolean("global", "memory deduplication (ksm)", enable_ksm);
288 #warning "Kernel memory deduplication (KSM) is not available"
291 // --------------------------------------------------------------------
294 global_host_prefix = config_get("global", "host access prefix", "");
295 setenv("NETDATA_HOST_PREFIX", global_host_prefix, 1);
297 // --------------------------------------------------------------------
299 output_log_file = config_get("global", "debug log", LOG_DIR "/debug.log");
300 if(strcmp(output_log_file, "syslog") == 0) {
301 output_log_syslog = 1;
302 output_log_file = NULL;
304 else if(strcmp(output_log_file, "none") == 0) {
305 output_log_syslog = 0;
306 output_log_file = NULL;
308 else output_log_syslog = 0;
310 // --------------------------------------------------------------------
312 error_log_file = config_get("global", "error log", LOG_DIR "/error.log");
313 if(strcmp(error_log_file, "syslog") == 0) {
314 error_log_syslog = 1;
315 error_log_file = NULL;
317 else if(strcmp(error_log_file, "none") == 0) {
318 error_log_syslog = 0;
319 error_log_file = NULL;
320 // optimization - do not even generate debug log entries
322 else error_log_syslog = 0;
324 // --------------------------------------------------------------------
326 access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
327 if(strcmp(access_log_file, "syslog") == 0) {
328 access_log_syslog = 1;
329 access_log_file = NULL;
331 else if(strcmp(access_log_file, "none") == 0) {
332 access_log_syslog = 0;
333 access_log_file = NULL;
335 else access_log_syslog = 0;
337 // --------------------------------------------------------------------
339 rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
341 // --------------------------------------------------------------------
343 if(gethostname(buffer, HOSTNAME_MAX) == -1)
344 error("WARNING: Cannot get machine hostname.");
345 hostname = config_get("global", "hostname", buffer);
346 debug(D_OPTIONS, "hostname set to '%s'", hostname);
348 // --------------------------------------------------------------------
350 rrd_default_history_entries = (int) config_get_number("global", "history", RRD_DEFAULT_HISTORY_ENTRIES);
351 if(rrd_default_history_entries < 5 || rrd_default_history_entries > RRD_HISTORY_ENTRIES_MAX) {
352 fprintf(stderr, "Invalid save lines %d given. Defaulting to %d.\n", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
353 rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
356 debug(D_OPTIONS, "save lines set to %d.", rrd_default_history_entries);
359 // --------------------------------------------------------------------
361 rrd_update_every = (int) config_get_number("global", "update every", UPDATE_EVERY);
362 if(rrd_update_every < 1 || rrd_update_every > 600) {
363 fprintf(stderr, "Invalid update timer %d given. Defaulting to %d.\n", rrd_update_every, UPDATE_EVERY_MAX);
364 rrd_update_every = UPDATE_EVERY;
366 else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
368 // let the plugins know the min update_every
371 snprintf(buf, 50, "%d", rrd_update_every);
372 setenv("NETDATA_UPDATE_EVERY", buf, 1);
375 // --------------------------------------------------------------------
377 for (i = 0; static_threads[i].name != NULL ; i++) {
378 struct netdata_static_thread *st = &static_threads[i];
380 if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
381 if(st->enabled && st->init_routine) st->init_routine();
384 // --------------------------------------------------------------------
387 user = config_get("global", "run as user" , (getuid() == 0)?NETDATA_USER:"");
390 // --------------------------------------------------------------------
392 listen_backlog = (int) config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
394 listen_port = (int) config_get_number("global", "port", LISTEN_PORT);
395 if(listen_port < 1 || listen_port > 65535) {
396 fprintf(stderr, "Invalid listen port %d given. Defaulting to %d.\n", listen_port, LISTEN_PORT);
397 listen_port = LISTEN_PORT;
399 else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
402 char *ipv = config_get("global", "ip version", "any");
403 if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
404 else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
405 else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
406 else fprintf(stderr, "Cannot understand ip version '%s'. Assumming 'any'.", ipv);
408 if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(listen_port, listen_backlog);
410 listen_fd = create_listen_socket4(listen_port, listen_backlog);
411 if(listen_fd >= 0 && ip != 4) fprintf(stderr, "Managed to open an IPv4 socket on port %d.", listen_port);
414 if(listen_fd < 0) fatal("Cannot listen socket.");
417 // never become a problem
418 if(nice(20) == -1) error("Cannot lower my CPU priority.");
420 if(become_daemon(dont_fork, 0, user, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
421 fatal("Cannot demonize myself.");
425 if(debug_flags != 0) {
426 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
427 if(setrlimit(RLIMIT_CORE, &rl) != 0)
428 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
430 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
433 if(output_log_syslog || error_log_syslog || access_log_syslog)
434 openlog("netdata", LOG_PID, LOG_DAEMON);
436 info("NetData started on pid %d", getpid());
440 for (i = 1 ; i < 65 ;i++) {
442 case SIGKILL: // not catchable
443 case SIGSTOP: // not catchable
453 signal(i, sig_handler);
458 for (i = 0; static_threads[i].name != NULL ; i++) {
459 struct netdata_static_thread *st = &static_threads[i];
462 st->thread = malloc(sizeof(pthread_t));
464 fatal("Cannot allocate pthread_t memory");
466 info("Starting thread %s.", st->name);
468 if(pthread_create(st->thread, NULL, st->start_routine, NULL))
469 error("failed to create new thread for %s.", st->name);
471 else if(pthread_detach(*st->thread))
472 error("Cannot request detach of newly created %s thread.", st->name);
474 else info("Not starting thread %s.", st->name);
477 // for future use - the main thread
479 if(netdata_exit != 0) {
482 if(netdata_exit > 5) {
483 netdata_cleanup_and_exit(0);