12 #include <sys/resource.h>
17 #include "web_server.h"
20 #include "web_client.h"
24 #include "unit_test.h"
26 #include "plugins_d.h"
27 #include "plugin_idlejitter.h"
28 #include "plugin_tc.h"
29 #include "plugin_checks.h"
30 #include "plugin_proc.h"
31 #include "plugin_nfacct.h"
35 struct netdata_static_thread {
45 void (*init_routine) (void);
46 void *(*start_routine) (void *);
49 struct netdata_static_thread static_threads[] = {
50 {"tc", "plugins", "tc", 1, NULL, NULL, tc_main},
51 {"idlejitter", "plugins", "idlejitter", 1, NULL, NULL, cpuidlejitter_main},
52 {"proc", "plugins", "proc", 1, NULL, NULL, proc_main},
54 #ifdef INTERNAL_PLUGIN_NFACCT
55 // nfacct requires root access
56 // so, we build it as an external plugin with setuid to root
57 {"nfacct", "plugins", "nfacct", 1, NULL, NULL, nfacct_main},
60 {"plugins.d", NULL, NULL, 1, NULL, NULL, pluginsd_main},
61 {"check", "plugins", "checks", 0, NULL, NULL, checks_main},
62 {"web", NULL, NULL, 1, NULL, NULL, socket_listen_main},
63 {NULL, NULL, NULL, 0, NULL, NULL, NULL}
66 int killpid(pid_t pid, int sig)
69 debug(D_EXIT, "Request to kill pid %d", pid);
72 if(kill(pid, 0) == -1) {
75 error("Request to kill pid %d, but it is not running.", pid);
79 error("Request to kill pid %d, but I do not have enough permissions.", pid);
83 error("Request to kill pid %d, but I received an error.", pid);
91 old = signal(sig, SIG_IGN);
93 error("Cannot overwrite signal handler for signal %d", sig);
99 if(signal(sig, old) == SIG_ERR)
100 error("Cannot restore signal handler for signal %d", sig);
105 error("Cannot kill pid %d, but it is not running.", pid);
109 error("Cannot kill pid %d, but I do not have enough permissions.", pid);
113 error("Cannot kill pid %d, but I received an error.", pid);
126 struct web_client *w;
127 for(w = web_clients; w ; w = w->next) {
128 debug(D_EXIT, "Stopping web client %s", w->client_ip);
129 pthread_cancel(w->thread);
130 pthread_join(w->thread, NULL);
134 for (i = 0; static_threads[i].name != NULL ; i++) {
135 if(static_threads[i].thread) {
136 debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
137 pthread_cancel(*static_threads[i].thread);
138 pthread_join(*static_threads[i].thread, NULL);
139 static_threads[i].thread = NULL;
144 info("Killing tc-qos-helper procees");
145 if(killpid(tc_child_pid, SIGTERM) != -1)
146 waitid(tc_child_pid, 0, &info, WEXITED);
151 for(cd = pluginsd_root ; cd ; cd = cd->next) {
152 debug(D_EXIT, "Stopping %s plugin thread", cd->id);
153 pthread_cancel(cd->thread);
154 pthread_join(cd->thread, NULL);
156 if(cd->pid && !cd->obsolete) {
157 debug(D_EXIT, "killing %s plugin process", cd->id);
158 if(killpid(cd->pid, SIGTERM) != -1)
159 waitid(cd->pid, 0, &info, WEXITED);
163 debug(D_EXIT, "All threads/childs stopped.");
167 int main(int argc, char **argv)
170 int config_loaded = 0;
173 // global initialization
176 // set the name for logging
177 program_name = "netdata";
179 // parse the arguments
180 for(i = 1; i < argc ; i++) {
181 if(strcmp(argv[i], "-c") == 0 && (i+1) < argc) {
182 if(load_config(argv[i+1], 1) != 1) {
183 fprintf(stderr, "Cannot load configuration file %s. Reason: %s\n", argv[i+1], strerror(errno));
187 debug(D_OPTIONS, "Configuration loaded from %s.", argv[i+1]);
192 else if(strcmp(argv[i], "-df") == 0 && (i+1) < argc) { config_set("global", "debug flags", argv[i+1]); i++; }
193 else if(strcmp(argv[i], "-p") == 0 && (i+1) < argc) { config_set("global", "port", argv[i+1]); i++; }
194 else if(strcmp(argv[i], "-u") == 0 && (i+1) < argc) { config_set("global", "run as user", argv[i+1]); i++; }
195 else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) { config_set("global", "history", argv[i+1]); i++; }
196 else if(strcmp(argv[i], "-t") == 0 && (i+1) < argc) { config_set("global", "update every", argv[i+1]); i++; }
197 else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) { config_set("global", "host access prefix", argv[i+1]); i++; }
198 else if(strcmp(argv[i], "-nodeamon") == 0 || strcmp(argv[i], "-nd") == 0) dont_fork = 1;
199 else if(strcmp(argv[i], "--unittest") == 0) {
200 if(unit_test_storage()) exit(1);
202 rrd_update_every = 1;
203 if(unit_test(1000000, 0)) exit(1);
204 if(unit_test(1000000, 500000)) exit(1);
205 if(unit_test(1000000, 100000)) exit(1);
206 if(unit_test(1000000, 900000)) exit(1);
207 if(unit_test(2000000, 0)) exit(1);
208 if(unit_test(2000000, 500000)) exit(1);
209 if(unit_test(2000000, 100000)) exit(1);
210 if(unit_test(2000000, 900000)) exit(1);
211 if(unit_test(500000, 500000)) exit(1);
212 //if(unit_test(500000, 100000)) exit(1); // FIXME: the expected numbers are wrong
213 //if(unit_test(500000, 900000)) exit(1); // FIXME: the expected numbers are wrong
214 if(unit_test(500000, 0)) exit(1);
215 fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
219 fprintf(stderr, "Cannot understand option '%s'.\n", argv[i]);
220 fprintf(stderr, "\nUSAGE: %s [-d] [-l LINES_TO_SAVE] [-u UPDATE_TIMER] [-p LISTEN_PORT] [-dl debug log file] [-df debug flags].\n\n", argv[0]);
221 fprintf(stderr, " -c CONFIG FILE the configuration file to load. Default: %s.\n", CONFIG_DIR "/" CONFIG_FILENAME);
222 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);
223 fprintf(stderr, " -t UPDATE_TIMER can be from 1 to %d seconds. Default: %d.\n", UPDATE_EVERY_MAX, UPDATE_EVERY);
224 fprintf(stderr, " -p LISTEN_PORT can be from 1 to %d. Default: %d.\n", 65535, LISTEN_PORT);
225 fprintf(stderr, " -u USERNAME can be any system username to run as. Default: none.\n");
226 fprintf(stderr, " -ch path to access host /proc and /sys when running in a container. Default: empty.\n");
227 fprintf(stderr, " -nd or -nodeamon to disable forking in the background. Default: unset.\n");
228 fprintf(stderr, " -df FLAGS debug options. Default: 0x%08llx.\n", debug_flags);
233 if(!config_loaded) load_config(NULL, 0);
235 char *input_log_file = NULL;
236 char *output_log_file = NULL;
237 char *error_log_file = NULL;
238 char *access_log_file = NULL;
242 // --------------------------------------------------------------------
244 sprintf(buffer, "0x%08llx", 0ULL);
245 char *flags = config_get("global", "debug flags", buffer);
246 debug_flags = strtoull(flags, NULL, 0);
247 debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
249 if(debug_flags != 0) {
250 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
251 if(setrlimit(RLIMIT_CORE, &rl) != 0)
252 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
255 // --------------------------------------------------------------------
257 global_host_prefix = config_get("global", "host access prefix", "");
259 // --------------------------------------------------------------------
261 output_log_file = config_get("global", "debug log", LOG_DIR "/debug.log");
262 if(strcmp(output_log_file, "syslog") == 0) {
263 output_log_syslog = 1;
264 output_log_file = NULL;
266 else if(strcmp(output_log_file, "none") == 0) {
267 output_log_syslog = 0;
268 output_log_file = NULL;
270 else output_log_syslog = 0;
272 // --------------------------------------------------------------------
275 error_log_file = config_get("global", "error log", LOG_DIR "/error.log");
276 if(strcmp(error_log_file, "syslog") == 0) {
277 error_log_syslog = 1;
278 error_log_file = NULL;
280 else if(strcmp(error_log_file, "none") == 0) {
281 error_log_syslog = 0;
282 error_log_file = NULL;
283 silent = 1; // optimization - do not even generate debug log entries
285 else error_log_syslog = 0;
287 // --------------------------------------------------------------------
289 access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
290 if(strcmp(access_log_file, "syslog") == 0) {
291 access_log_syslog = 1;
292 access_log_file = NULL;
294 else if(strcmp(access_log_file, "none") == 0) {
295 access_log_syslog = 0;
296 access_log_file = NULL;
298 else access_log_syslog = 0;
300 // --------------------------------------------------------------------
302 rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
304 // --------------------------------------------------------------------
306 if(gethostname(buffer, HOSTNAME_MAX) == -1)
307 error("WARNING: Cannot get machine hostname.");
308 hostname = config_get("global", "hostname", buffer);
309 debug(D_OPTIONS, "hostname set to '%s'", hostname);
311 // --------------------------------------------------------------------
313 rrd_default_history_entries = config_get_number("global", "history", RRD_DEFAULT_HISTORY_ENTRIES);
314 if(rrd_default_history_entries < 5 || rrd_default_history_entries > RRD_HISTORY_ENTRIES_MAX) {
315 fprintf(stderr, "Invalid save lines %d given. Defaulting to %d.\n", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
316 rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
319 debug(D_OPTIONS, "save lines set to %d.", rrd_default_history_entries);
322 // --------------------------------------------------------------------
324 rrd_update_every = config_get_number("global", "update every", UPDATE_EVERY);
325 if(rrd_update_every < 1 || rrd_update_every > 600) {
326 fprintf(stderr, "Invalid update timer %d given. Defaulting to %d.\n", rrd_update_every, UPDATE_EVERY_MAX);
327 rrd_update_every = UPDATE_EVERY;
329 else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
331 // --------------------------------------------------------------------
333 for (i = 0; static_threads[i].name != NULL ; i++) {
334 struct netdata_static_thread *st = &static_threads[i];
336 if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
337 if(st->enabled && st->init_routine) st->init_routine();
340 // --------------------------------------------------------------------
343 char *user = config_get("global", "run as user", (getuid() == 0)?"nobody":"");
345 if(become_user(user) != 0) {
346 fprintf(stderr, "Cannot become user %s.\n", user);
349 else debug(D_OPTIONS, "Successfully became user %s.", user);
352 // --------------------------------------------------------------------
354 listen_backlog = config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
356 listen_port = config_get_number("global", "port", LISTEN_PORT);
357 if(listen_port < 1 || listen_port > 65535) {
358 fprintf(stderr, "Invalid listen port %d given. Defaulting to %d.\n", listen_port, LISTEN_PORT);
359 listen_port = LISTEN_PORT;
361 else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
364 char *ipv = config_get("global", "ip version", "any");
365 if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
366 else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
367 else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
368 else fprintf(stderr, "Cannot understand ip version '%s'. Assumming 'any'.", ipv);
370 if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(listen_port, listen_backlog);
372 listen_fd = create_listen_socket4(listen_port, listen_backlog);
373 if(listen_fd >= 0 && ip != 4) fprintf(stderr, "Managed to open an IPv4 socket on port %d.", listen_port);
376 if(listen_fd < 0) fatal("Cannot listen socket.");
379 // never become a problem
380 if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
382 #ifndef NETDATA_NO_DAEMON
383 if(become_daemon(dont_fork, 0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
384 fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno));
389 if(output_log_syslog || error_log_syslog || access_log_syslog)
390 openlog("netdata", LOG_PID, LOG_DAEMON);
392 info("NetData started on pid %d", getpid());
396 for (i = 1 ; i < 65 ;i++) {
405 signal(i, sig_handler);
410 for (i = 0; static_threads[i].name != NULL ; i++) {
411 struct netdata_static_thread *st = &static_threads[i];
414 st->thread = malloc(sizeof(pthread_t));
416 info("Starting thread %s.", st->name);
418 if(pthread_create(st->thread, NULL, st->start_routine, NULL))
419 error("failed to create new thread for %s.", st->name);
421 else if(pthread_detach(*st->thread))
422 error("Cannot request detach of newly created %s thread.", st->name);
424 else info("Not starting thread %s.", st->name);
427 // for future use - the main thread