]> arthur.barton.de Git - netdata.git/blob - src/main.c
6a341917d936c17c5d359663b7c26fbabd133144
[netdata.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <syslog.h>
6 #include <pthread.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11 #include <sys/time.h>
12 #include <sys/resource.h>
13
14 #include "common.h"
15 #include "log.h"
16 #include "daemon.h"
17 #include "web_server.h"
18 #include "popen.h"
19 #include "config.h"
20 #include "web_client.h"
21 #include "rrd.h"
22 #include "rrd2json.h"
23
24 #include "unit_test.h"
25
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"
32
33 #include "main.h"
34
35 struct netdata_static_thread {
36         char *name;
37
38         char *config_section;
39         char *config_name;
40
41         int enabled;
42
43         pthread_t *thread;
44
45         void (*init_routine) (void);
46         void *(*start_routine) (void *);
47 };
48
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},
53
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},
58 #endif
59
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}
64 };
65
66 int killpid(pid_t pid, int sig)
67 {
68         int ret = -1;
69         debug(D_EXIT, "Request to kill pid %d", pid);
70
71         errno = 0;
72         if(kill(pid, 0) == -1) {
73                 switch(errno) {
74                         case ESRCH: 
75                                 error("Request to kill pid %d, but it is not running.", pid);
76                                 break;
77
78                         case EPERM:
79                                 error("Request to kill pid %d, but I do not have enough permissions.", pid);
80                                 break;
81
82                         default:
83                                 error("Request to kill pid %d, but I received an error.", pid);
84                                 break;
85                 }
86         }
87         else {
88                 errno = 0;
89                 
90                 void (*old)(int);               
91                 old = signal(sig, SIG_IGN);
92                 if(old == SIG_ERR) {
93                         error("Cannot overwrite signal handler for signal %d", sig);
94                         old = sig_handler;
95                 }
96
97                 ret = kill(pid, sig);
98
99                 if(signal(sig, old) == SIG_ERR)
100                         error("Cannot restore signal handler for signal %d", sig);
101
102                 if(ret == -1) {
103                         switch(errno) {
104                                 case ESRCH: 
105                                         error("Cannot kill pid %d, but it is not running.", pid);
106                                         break;
107
108                                 case EPERM:
109                                         error("Cannot kill pid %d, but I do not have enough permissions.", pid);
110                                         break;
111
112                                 default:
113                                         error("Cannot kill pid %d, but I received an error.", pid);
114                                         break;
115                         }
116                 }
117         }
118
119         return ret;
120 }
121
122 void kill_childs()
123 {
124         siginfo_t info;
125
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);
131         }
132
133         int i;  
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;
140                 }
141         }
142
143         if(tc_child_pid) {
144                 info("Killing tc-qos-helper procees");
145                 if(killpid(tc_child_pid, SIGTERM) != -1)
146                         waitid(tc_child_pid, 0, &info, WEXITED);
147         }
148         tc_child_pid = 0;
149
150         struct plugind *cd;
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);
155
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);
160                 }
161         }
162
163         debug(D_EXIT, "All threads/childs stopped.");
164 }
165
166
167 int main(int argc, char **argv)
168 {
169         int i;
170         int config_loaded = 0;
171         int dont_fork = 0;
172
173         // global initialization
174         get_HZ();
175
176         // set the name for logging
177         program_name = "netdata";
178
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));
184                                 exit(1);
185                         }
186                         else {
187                                 debug(D_OPTIONS, "Configuration loaded from %s.", argv[i+1]);
188                                 config_loaded = 1;
189                         }
190                         i++;
191                 }
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);
201                         exit(0);
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");
216                         exit(0);
217                 }
218                 else {
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);
229                         exit(1);
230                 }
231         }
232
233         if(!config_loaded) load_config(NULL, 0);
234
235         char *input_log_file = NULL;
236         char *output_log_file = NULL;
237         char *error_log_file = NULL;
238         char *access_log_file = NULL;
239         {
240                 char buffer[1024];
241
242                 // --------------------------------------------------------------------
243
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);
248
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...");
253                 }
254
255                 // --------------------------------------------------------------------
256
257                 global_host_prefix = config_get("global", "host access prefix", "");
258
259                 // --------------------------------------------------------------------
260
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;
265                 }
266                 else if(strcmp(output_log_file, "none") == 0) {
267                         output_log_syslog = 0;
268                         output_log_file = NULL;
269                 }
270                 else output_log_syslog = 0;
271
272                 // --------------------------------------------------------------------
273
274                 silent = 0;
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;
279                 }
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
284                 }
285                 else error_log_syslog = 0;
286
287                 // --------------------------------------------------------------------
288
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;
293                 }
294                 else if(strcmp(access_log_file, "none") == 0) {
295                         access_log_syslog = 0;
296                         access_log_file = NULL;
297                 }
298                 else access_log_syslog = 0;
299
300                 // --------------------------------------------------------------------
301
302                 rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
303
304                 // --------------------------------------------------------------------
305
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);
310
311                 // --------------------------------------------------------------------
312
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;
317                 }
318                 else {
319                         debug(D_OPTIONS, "save lines set to %d.", rrd_default_history_entries);
320                 }
321
322                 // --------------------------------------------------------------------
323
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;
328                 }
329                 else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
330
331                 // --------------------------------------------------------------------
332                 
333                 for (i = 0; static_threads[i].name != NULL ; i++) {
334                         struct netdata_static_thread *st = &static_threads[i];
335
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();
338                 }
339
340                 // --------------------------------------------------------------------
341
342                 prepare_rundir();
343                 char *user = config_get("global", "run as user", (getuid() == 0)?"nobody":"");
344                 if(*user) {
345                         if(become_user(user) != 0) {
346                                 fprintf(stderr, "Cannot become user %s.\n", user);
347                                 exit(1);
348                         }
349                         else debug(D_OPTIONS, "Successfully became user %s.", user);
350                 }
351
352                 // --------------------------------------------------------------------
353
354                 listen_backlog = config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
355
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;
360                 }
361                 else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
362
363                 int ip = 0;
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);
369
370                 if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(listen_port, listen_backlog);
371                 if(listen_fd < 0) {
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);
374                 }
375
376                 if(listen_fd < 0) fatal("Cannot listen socket.");
377         }
378
379         // never become a problem
380         if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
381
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));
385                 exit(1);
386         }
387 #endif
388
389         if(output_log_syslog || error_log_syslog || access_log_syslog)
390                 openlog("netdata", LOG_PID, LOG_DAEMON);
391
392         info("NetData started on pid %d", getpid());
393
394
395         // catch all signals
396         for (i = 1 ; i < 65 ;i++) {
397                 switch(i) {
398                         case SIGSEGV:
399                         case SIGFPE:
400                         case SIGCHLD:
401                                 signal(i, SIG_DFL);
402                                 break;
403
404                         default:
405                                 signal(i,  sig_handler);
406                                 break;
407                 }
408         }
409
410         for (i = 0; static_threads[i].name != NULL ; i++) {
411                 struct netdata_static_thread *st = &static_threads[i];
412
413                 if(st->enabled) {
414                         st->thread = malloc(sizeof(pthread_t));
415
416                         info("Starting thread %s.", st->name);
417
418                         if(pthread_create(st->thread, NULL, st->start_routine, NULL))
419                                 error("failed to create new thread for %s.", st->name);
420
421                         else if(pthread_detach(*st->thread))
422                                 error("Cannot request detach of newly created %s thread.", st->name);
423                 }
424                 else info("Not starting thread %s.", st->name);
425         }
426
427         // for future use - the main thread
428         while(1) sleep(60);
429
430         exit(0);
431 }