15 #include "web_server.h"
18 #include "web_client.h"
22 #include "unit_test.h"
24 #include "plugins_d.h"
25 #include "plugin_idlejitter.h"
26 #include "plugin_tc.h"
27 #include "plugin_checks.h"
28 #include "plugin_proc.h"
29 #include "plugin_nfacct.h"
33 struct netdata_static_thread {
43 void (*init_routine) (void);
44 void *(*start_routine) (void *);
47 struct netdata_static_thread static_threads[] = {
48 {"tc", "plugins", "tc", 1, NULL, NULL, tc_main},
49 {"idlejitter", "plugins", "idlejitter", 1, NULL, NULL, cpuidlejitter_main},
50 {"proc", "plugins", "proc", 1, NULL, NULL, proc_main},
52 #ifdef INTERNAL_PLUGIN_NFACCT
53 // nfacct requires root access
54 // so, we build it as an external plugin with setuid to root
55 {"nfacct", "plugins", "nfacct", 1, NULL, NULL, nfacct_main},
58 {"plugins.d", NULL, NULL, 1, NULL, NULL, pluginsd_main},
59 {"check", "plugins", "checks", 0, NULL, NULL, checks_main},
60 {"web", NULL, NULL, 1, NULL, NULL, socket_listen_main},
61 {NULL, NULL, NULL, 0, NULL, NULL, NULL}
64 int killpid(pid_t pid, int sig)
67 debug(D_EXIT, "Request to kill pid %d", pid);
70 if(kill(pid, 0) == -1) {
73 error("Request to kill pid %d, but it is not running.", pid);
77 error("Request to kill pid %d, but I do not have enough permissions.", pid);
81 error("Request to kill pid %d, but I received an error.", pid);
89 old = signal(sig, SIG_IGN);
91 error("Cannot overwrite signal handler for signal %d", sig);
97 if(signal(sig, old) == SIG_ERR)
98 error("Cannot restore signal handler for signal %d", sig);
103 error("Cannot kill pid %d, but it is not running.", pid);
107 error("Cannot kill pid %d, but I do not have enough permissions.", pid);
111 error("Cannot kill pid %d, but I received an error.", pid);
124 struct web_client *w;
125 for(w = web_clients; w ; w = w->next) {
126 debug(D_EXIT, "Stopping web client %s", w->client_ip);
127 pthread_cancel(w->thread);
128 pthread_join(w->thread, NULL);
132 for (i = 0; static_threads[i].name != NULL ; i++) {
133 if(static_threads[i].thread) {
134 debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
135 pthread_cancel(*static_threads[i].thread);
136 pthread_join(*static_threads[i].thread, NULL);
137 static_threads[i].thread = NULL;
142 info("Killing tc-qos-helper procees");
143 if(killpid(tc_child_pid, SIGTERM) != -1)
144 waitid(tc_child_pid, 0, &info, WEXITED);
149 for(cd = pluginsd_root ; cd ; cd = cd->next) {
150 debug(D_EXIT, "Stopping %s plugin thread", cd->id);
151 pthread_cancel(cd->thread);
152 pthread_join(cd->thread, NULL);
154 if(cd->pid && !cd->obsolete) {
155 debug(D_EXIT, "killing %s plugin process", cd->id);
156 if(killpid(cd->pid, SIGTERM) != -1)
157 waitid(cd->pid, 0, &info, WEXITED);
161 debug(D_EXIT, "All threads/childs stopped.");
165 int main(int argc, char **argv)
168 int config_loaded = 0;
170 // set the name for logging
171 program_name = "netdata";
173 // parse the arguments
174 for(i = 1; i < argc ; i++) {
175 if(strcmp(argv[i], "-c") == 0 && (i+1) < argc) {
176 if(load_config(argv[i+1], 1) != 1) {
177 fprintf(stderr, "Cannot load configuration file %s. Reason: %s\n", argv[i+1], strerror(errno));
181 debug(D_OPTIONS, "Configuration loaded from %s.", argv[i+1]);
186 else if(strcmp(argv[i], "-df") == 0 && (i+1) < argc) { config_set("global", "debug flags", argv[i+1]); i++; }
187 else if(strcmp(argv[i], "-p") == 0 && (i+1) < argc) { config_set("global", "port", argv[i+1]); i++; }
188 else if(strcmp(argv[i], "-u") == 0 && (i+1) < argc) { config_set("global", "run as user", argv[i+1]); i++; }
189 else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) { config_set("global", "history", argv[i+1]); i++; }
190 else if(strcmp(argv[i], "-t") == 0 && (i+1) < argc) { config_set("global", "update every", argv[i+1]); i++; }
191 else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) { config_set("global", "host access prefix", argv[i+1]); i++; }
192 else if(strcmp(argv[i], "--unittest") == 0) {
193 if(unit_test_storage()) exit(1);
195 rrd_update_every = 1;
196 if(unit_test(1000000, 0)) exit(1);
197 if(unit_test(1000000, 500000)) exit(1);
198 if(unit_test(1000000, 100000)) exit(1);
199 if(unit_test(1000000, 900000)) exit(1);
200 if(unit_test(2000000, 0)) exit(1);
201 if(unit_test(2000000, 500000)) exit(1);
202 if(unit_test(2000000, 100000)) exit(1);
203 if(unit_test(2000000, 900000)) exit(1);
204 if(unit_test(500000, 500000)) exit(1);
205 //if(unit_test(500000, 100000)) exit(1); // FIXME: the expected numbers are wrong
206 //if(unit_test(500000, 900000)) exit(1); // FIXME: the expected numbers are wrong
207 if(unit_test(500000, 0)) exit(1);
208 fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
212 fprintf(stderr, "Cannot understand option '%s'.\n", argv[i]);
213 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]);
214 fprintf(stderr, " -c CONFIG FILE the configuration file to load. Default: %s.\n", CONFIG_DIR "/" CONFIG_FILENAME);
215 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);
216 fprintf(stderr, " -t UPDATE_TIMER can be from 1 to %d seconds. Default: %d.\n", UPDATE_EVERY_MAX, UPDATE_EVERY);
217 fprintf(stderr, " -p LISTEN_PORT can be from 1 to %d. Default: %d.\n", 65535, LISTEN_PORT);
218 fprintf(stderr, " -u USERNAME can be any system username to run as. Default: none.\n");
219 fprintf(stderr, " -ch path to access host /proc and /sys when running in a container. Default: empty.\n");
220 fprintf(stderr, " -df FLAGS debug options. Default: 0x%8llx.\n", debug_flags);
225 if(!config_loaded) load_config(NULL, 0);
227 char *input_log_file = NULL;
228 char *output_log_file = NULL;
229 char *error_log_file = NULL;
230 char *access_log_file = NULL;
234 // --------------------------------------------------------------------
236 sprintf(buffer, "0x%08llx", 0ULL);
237 char *flags = config_get("global", "debug flags", buffer);
238 debug_flags = strtoull(flags, NULL, 0);
239 debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
241 // --------------------------------------------------------------------
243 global_host_prefix = config_get("global", "host access prefix", "");
245 // --------------------------------------------------------------------
247 output_log_file = config_get("global", "debug log", LOG_DIR "/debug.log");
248 if(strcmp(output_log_file, "syslog") == 0) {
249 output_log_syslog = 1;
250 output_log_file = NULL;
252 else if(strcmp(output_log_file, "none") == 0) {
253 output_log_syslog = 0;
254 output_log_file = NULL;
256 else output_log_syslog = 0;
258 // --------------------------------------------------------------------
261 error_log_file = config_get("global", "error log", LOG_DIR "/error.log");
262 if(strcmp(error_log_file, "syslog") == 0) {
263 error_log_syslog = 1;
264 error_log_file = NULL;
266 else if(strcmp(error_log_file, "none") == 0) {
267 error_log_syslog = 0;
268 error_log_file = NULL;
269 silent = 1; // optimization - do not even generate debug log entries
271 else error_log_syslog = 0;
273 // --------------------------------------------------------------------
275 access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
276 if(strcmp(access_log_file, "syslog") == 0) {
277 access_log_syslog = 1;
278 access_log_file = NULL;
280 else if(strcmp(access_log_file, "none") == 0) {
281 access_log_syslog = 0;
282 access_log_file = NULL;
284 else access_log_syslog = 0;
286 // --------------------------------------------------------------------
288 rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
290 // --------------------------------------------------------------------
292 if(gethostname(buffer, HOSTNAME_MAX) == -1)
293 error("WARNING: Cannot get machine hostname.");
294 hostname = config_get("global", "hostname", buffer);
295 debug(D_OPTIONS, "hostname set to '%s'", hostname);
297 // --------------------------------------------------------------------
299 rrd_default_history_entries = config_get_number("global", "history", RRD_DEFAULT_HISTORY_ENTRIES);
300 if(rrd_default_history_entries < 5 || rrd_default_history_entries > RRD_HISTORY_ENTRIES_MAX) {
301 fprintf(stderr, "Invalid save lines %d given. Defaulting to %d.\n", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
302 rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
305 debug(D_OPTIONS, "save lines set to %d.", rrd_default_history_entries);
308 // --------------------------------------------------------------------
310 rrd_update_every = config_get_number("global", "update every", UPDATE_EVERY);
311 if(rrd_update_every < 1 || rrd_update_every > 600) {
312 fprintf(stderr, "Invalid update timer %d given. Defaulting to %d.\n", rrd_update_every, UPDATE_EVERY_MAX);
313 rrd_update_every = UPDATE_EVERY;
315 else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
317 // --------------------------------------------------------------------
319 for (i = 0; static_threads[i].name != NULL ; i++) {
320 struct netdata_static_thread *st = &static_threads[i];
322 if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
323 if(st->enabled && st->init_routine) st->init_routine();
326 // --------------------------------------------------------------------
329 char *user = config_get("global", "run as user", (getuid() == 0)?"nobody":"");
331 if(become_user(user) != 0) {
332 fprintf(stderr, "Cannot become user %s.\n", user);
335 else debug(D_OPTIONS, "Successfully became user %s.", user);
338 // --------------------------------------------------------------------
340 listen_backlog = config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
342 listen_port = config_get_number("global", "port", LISTEN_PORT);
343 if(listen_port < 1 || listen_port > 65535) {
344 fprintf(stderr, "Invalid listen port %d given. Defaulting to %d.\n", listen_port, LISTEN_PORT);
345 listen_port = LISTEN_PORT;
347 else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
350 char *ipv = config_get("global", "ip version", "any");
351 if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
352 else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
353 else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
354 else fprintf(stderr, "Cannot understand ip version '%s'. Assumming 'any'.", ipv);
356 if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(listen_port, listen_backlog);
358 listen_fd = create_listen_socket4(listen_port, listen_backlog);
359 if(listen_fd >= 0 && ip != 4) fprintf(stderr, "Managed to open an IPv4 socket on port %d.", listen_port);
362 if(listen_fd < 0) fatal("Cannot listen socket.");
365 // never become a problem
366 if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
368 #ifndef NETDATA_NO_DAEMON
369 if(become_daemon(0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
370 fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno));
375 if(output_log_syslog || error_log_syslog || access_log_syslog)
376 openlog("netdata", LOG_PID, LOG_DAEMON);
378 info("NetData started on pid %d", getpid());
382 for (i = 1 ; i < 65 ;i++) {
391 signal(i, sig_handler);
396 for (i = 0; static_threads[i].name != NULL ; i++) {
397 struct netdata_static_thread *st = &static_threads[i];
400 st->thread = malloc(sizeof(pthread_t));
402 info("Starting thread %s.", st->name);
404 if(pthread_create(st->thread, NULL, st->start_routine, NULL))
405 error("failed to create new thread for %s.", st->name);
407 else if(pthread_detach(*st->thread))
408 error("Cannot request detach of newly created %s thread.", st->name);
410 else info("Not starting thread %s.", st->name);
413 // for future use - the main thread