14 #include "web_server.h"
17 #include "web_client.h"
21 #include "unit_test.h"
23 #include "plugins_d.h"
24 #include "plugin_idlejitter.h"
25 #include "plugin_tc.h"
26 #include "plugin_checks.h"
27 #include "plugin_proc.h"
28 #include "plugin_nfacct.h"
32 struct netdata_static_thread {
42 void (*init_routine) (void);
43 void *(*start_routine) (void *);
46 struct netdata_static_thread static_threads[] = {
47 {"tc", "plugins", "tc", 1, NULL, NULL, tc_main},
48 {"idlejitter", "plugins", "idlejitter", 1, NULL, NULL, cpuidlejitter_main},
49 {"proc", "plugins", "proc", 1, NULL, NULL, proc_main},
51 #ifdef INTERNAL_PLUGIN_NFACCT
52 // nfacct requires root access
53 // so, we build it as an external plugin with setuid to root
54 {"nfacct", "plugins", "nfacct", 1, NULL, NULL, nfacct_main},
57 {"plugins.d", NULL, NULL, 1, NULL, NULL, pluginsd_main},
58 {"check", "plugins", "checks", 0, NULL, NULL, checks_main},
59 {"web", NULL, NULL, 1, NULL, NULL, socket_listen_main},
60 {NULL, NULL, NULL, 0, NULL, NULL, NULL}
68 for(w = web_clients; w ; w = w->next) {
69 debug(D_EXIT, "Stopping web client %s", w->client_ip);
70 pthread_cancel(w->thread);
71 pthread_join(w->thread, NULL);
75 for (i = 0; static_threads[i].name != NULL ; i++) {
76 if(static_threads[i].thread) {
77 debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
78 pthread_cancel(*static_threads[i].thread);
79 pthread_join(*static_threads[i].thread, NULL);
80 static_threads[i].thread = NULL;
85 debug(D_EXIT, "Killing tc-qos-helper procees");
86 kill(tc_child_pid, SIGTERM);
87 waitid(tc_child_pid, 0, &info, WEXITED);
92 for(cd = pluginsd_root ; cd ; cd = cd->next) {
93 debug(D_EXIT, "Stopping %s plugin thread", cd->id);
94 pthread_cancel(cd->thread);
95 pthread_join(cd->thread, NULL);
97 if(cd->pid && !cd->obsolete) {
98 debug(D_EXIT, "killing %s plugin process", cd->id);
99 kill(cd->pid, SIGTERM);
100 waitid(cd->pid, 0, &info, WEXITED);
104 debug(D_EXIT, "All threads/childs stopped.");
108 int main(int argc, char **argv)
111 int config_loaded = 0;
113 // parse the arguments
114 for(i = 1; i < argc ; i++) {
115 if(strcmp(argv[i], "-c") == 0 && (i+1) < argc) {
116 if(load_config(argv[i+1], 1) != 1) {
117 fprintf(stderr, "Cannot load configuration file %s. Reason: %s\n", argv[i+1], strerror(errno));
121 debug(D_OPTIONS, "Configuration loaded from %s.", argv[i+1]);
126 else if(strcmp(argv[i], "-df") == 0 && (i+1) < argc) { config_set("global", "debug flags", argv[i+1]); i++; }
127 else if(strcmp(argv[i], "-p") == 0 && (i+1) < argc) { config_set("global", "port", argv[i+1]); i++; }
128 else if(strcmp(argv[i], "-u") == 0 && (i+1) < argc) { config_set("global", "run as user", argv[i+1]); i++; }
129 else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) { config_set("global", "history", argv[i+1]); i++; }
130 else if(strcmp(argv[i], "-t") == 0 && (i+1) < argc) { config_set("global", "update every", argv[i+1]); i++; }
131 else if(strcmp(argv[i], "--unittest") == 0) {
132 if(unit_test_storage()) exit(1);
135 if(unit_test(1000000, 0)) exit(1);
136 if(unit_test(1000000, 500000)) exit(1);
137 if(unit_test(1000000, 100000)) exit(1);
138 if(unit_test(1000000, 900000)) exit(1);
139 if(unit_test(2000000, 0)) exit(1);
140 if(unit_test(2000000, 500000)) exit(1);
141 if(unit_test(2000000, 100000)) exit(1);
142 if(unit_test(2000000, 900000)) exit(1);
143 if(unit_test(500000, 500000)) exit(1);
144 //if(unit_test(500000, 100000)) exit(1); // FIXME: the expected numbers are wrong
145 //if(unit_test(500000, 900000)) exit(1); // FIXME: the expected numbers are wrong
146 if(unit_test(500000, 0)) exit(1);
147 fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
151 fprintf(stderr, "Cannot understand option '%s'.\n", argv[i]);
152 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]);
153 fprintf(stderr, " -c CONFIG FILE the configuration file to load. Default: %s.\n", CONFIG_DIR "/" CONFIG_FILENAME);
154 fprintf(stderr, " -l LINES_TO_SAVE can be from 5 to %d lines in JSON data. Default: %d.\n", HISTORY_MAX, HISTORY);
155 fprintf(stderr, " -t UPDATE_TIMER can be from 1 to %d seconds. Default: %d.\n", UPDATE_EVERY_MAX, UPDATE_EVERY);
156 fprintf(stderr, " -p LISTEN_PORT can be from 1 to %d. Default: %d.\n", 65535, LISTEN_PORT);
157 fprintf(stderr, " -u USERNAME can be any system username to run as. Default: none.\n");
158 fprintf(stderr, " -df FLAGS debug options. Default: 0x%8llx.\n", debug_flags);
163 if(!config_loaded) load_config(NULL, 0);
165 char *input_log_file = NULL;
166 char *output_log_file = NULL;
167 char *error_log_file = NULL;
168 char *access_log_file = NULL;
172 // --------------------------------------------------------------------
174 sprintf(buffer, "0x%08llx", 0ULL);
175 char *flags = config_get("global", "debug flags", buffer);
176 debug_flags = strtoull(flags, NULL, 0);
177 debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
179 // --------------------------------------------------------------------
181 output_log_file = config_get("global", "debug log", LOG_DIR "/debug.log");
182 if(strcmp(output_log_file, "syslog") == 0) {
183 output_log_syslog = 1;
184 output_log_file = NULL;
186 else if(strcmp(output_log_file, "none") == 0) {
187 output_log_syslog = 0;
188 output_log_file = NULL;
190 else output_log_syslog = 0;
192 // --------------------------------------------------------------------
195 error_log_file = config_get("global", "error log", LOG_DIR "/error.log");
196 if(strcmp(error_log_file, "syslog") == 0) {
197 error_log_syslog = 1;
198 error_log_file = NULL;
200 else if(strcmp(error_log_file, "none") == 0) {
201 error_log_syslog = 0;
202 error_log_file = NULL;
203 silent = 1; // optimization - do not even generate debug log entries
205 else error_log_syslog = 0;
207 // --------------------------------------------------------------------
209 access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
210 if(strcmp(access_log_file, "syslog") == 0) {
211 access_log_syslog = 1;
212 access_log_file = NULL;
214 else if(strcmp(access_log_file, "none") == 0) {
215 access_log_syslog = 0;
216 access_log_file = NULL;
218 else access_log_syslog = 0;
220 // --------------------------------------------------------------------
222 memory_mode = memory_mode_id(config_get("global", "memory mode", memory_mode_name(memory_mode)));
224 // --------------------------------------------------------------------
226 if(gethostname(buffer, HOSTNAME_MAX) == -1)
227 error("WARNING: Cannot get machine hostname.");
228 hostname = config_get("global", "hostname", buffer);
229 debug(D_OPTIONS, "hostname set to '%s'", hostname);
231 // --------------------------------------------------------------------
233 save_history = config_get_number("global", "history", HISTORY);
234 if(save_history < 5 || save_history > HISTORY_MAX) {
235 fprintf(stderr, "Invalid save lines %d given. Defaulting to %d.\n", save_history, HISTORY);
236 save_history = HISTORY;
239 debug(D_OPTIONS, "save lines set to %d.", save_history);
242 // --------------------------------------------------------------------
244 update_every = config_get_number("global", "update every", UPDATE_EVERY);
245 if(update_every < 1 || update_every > 600) {
246 fprintf(stderr, "Invalid update timer %d given. Defaulting to %d.\n", update_every, UPDATE_EVERY_MAX);
247 update_every = UPDATE_EVERY;
249 else debug(D_OPTIONS, "update timer set to %d.", update_every);
251 // --------------------------------------------------------------------
253 for (i = 0; static_threads[i].name != NULL ; i++) {
254 struct netdata_static_thread *st = &static_threads[i];
256 if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
257 if(st->enabled && st->init_routine) st->init_routine();
260 // --------------------------------------------------------------------
263 char *user = config_get("global", "run as user", (getuid() == 0)?"nobody":"");
265 if(become_user(user) != 0) {
266 fprintf(stderr, "Cannot become user %s.\n", user);
269 else debug(D_OPTIONS, "Successfully became user %s.", user);
272 // --------------------------------------------------------------------
274 listen_port = config_get_number("global", "port", LISTEN_PORT);
275 if(listen_port < 1 || listen_port > 65535) {
276 fprintf(stderr, "Invalid listen port %d given. Defaulting to %d.\n", listen_port, LISTEN_PORT);
277 listen_port = LISTEN_PORT;
279 else debug(D_OPTIONS, "listen port set to %d.", listen_port);
281 listen_fd = create_listen_socket(listen_port);
284 // never become a problem
286 fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
289 if(become_daemon(0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
290 fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno));
295 if(output_log_syslog || error_log_syslog || access_log_syslog)
296 openlog("netdata", LOG_PID, LOG_DAEMON);
298 info("NetData started on pid %d", getpid());
302 for (i = 1 ; i < 65 ;i++) if(i != SIGSEGV && i != SIGFPE) signal(i, sig_handler);
304 for (i = 0; static_threads[i].name != NULL ; i++) {
305 struct netdata_static_thread *st = &static_threads[i];
308 st->thread = malloc(sizeof(pthread_t));
310 info("Starting thread %s.", st->name);
312 if(pthread_create(st->thread, NULL, st->start_routine, NULL))
313 error("failed to create new thread for %s.", st->name);
315 else if(pthread_detach(*st->thread))
316 error("Cannot request detach of newly created %s thread.", st->name);
318 else info("Not starting thread %s.", st->name);
321 // the main process - the web server listener
323 // socket_listen_main(NULL);
324 while(1) process_childs(1);