]> arthur.barton.de Git - netdata.git/blob - src/main.c
build: migrate to autotools
[netdata.git] / src / main.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <signal.h>
8 #include <syslog.h>
9 #include <pthread.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <sys/time.h>
15 #include <sys/resource.h>
16
17 #include "common.h"
18 #include "log.h"
19 #include "daemon.h"
20 #include "web_server.h"
21 #include "popen.h"
22 #include "appconfig.h"
23 #include "web_client.h"
24 #include "rrd.h"
25 #include "rrd2json.h"
26
27 #include "unit_test.h"
28
29 #include "plugins_d.h"
30 #include "plugin_idlejitter.h"
31 #include "plugin_tc.h"
32 #include "plugin_checks.h"
33 #include "plugin_proc.h"
34 #include "plugin_nfacct.h"
35
36 #include "main.h"
37
38 struct netdata_static_thread {
39         char *name;
40
41         char *config_section;
42         char *config_name;
43
44         int enabled;
45
46         pthread_t *thread;
47
48         void (*init_routine) (void);
49         void *(*start_routine) (void *);
50 };
51
52 struct netdata_static_thread static_threads[] = {
53         {"tc",                  "plugins",      "tc",                   1, NULL, NULL,  tc_main},
54         {"idlejitter",  "plugins",      "idlejitter",   1, NULL, NULL,  cpuidlejitter_main},
55         {"proc",                "plugins",      "proc",                 1, NULL, NULL,  proc_main},
56
57 #ifdef INTERNAL_PLUGIN_NFACCT
58         // nfacct requires root access
59         // so, we build it as an external plugin with setuid to root
60         {"nfacct",              "plugins",      "nfacct",               1, NULL, NULL,  nfacct_main},
61 #endif
62
63         {"plugins.d",   NULL,           NULL,                   1, NULL, NULL,  pluginsd_main},
64         {"check",               "plugins",      "checks",               0, NULL, NULL,  checks_main},
65         {"web",                 NULL,           NULL,                   1, NULL, NULL,  socket_listen_main},
66         {NULL,                  NULL,           NULL,                   0, NULL, NULL,  NULL}
67 };
68
69 int killpid(pid_t pid, int sig)
70 {
71         int ret = -1;
72         debug(D_EXIT, "Request to kill pid %d", pid);
73
74         errno = 0;
75         if(kill(pid, 0) == -1) {
76                 switch(errno) {
77                         case ESRCH: 
78                                 error("Request to kill pid %d, but it is not running.", pid);
79                                 break;
80
81                         case EPERM:
82                                 error("Request to kill pid %d, but I do not have enough permissions.", pid);
83                                 break;
84
85                         default:
86                                 error("Request to kill pid %d, but I received an error.", pid);
87                                 break;
88                 }
89         }
90         else {
91                 errno = 0;
92                 
93                 void (*old)(int);               
94                 old = signal(sig, SIG_IGN);
95                 if(old == SIG_ERR) {
96                         error("Cannot overwrite signal handler for signal %d", sig);
97                         old = sig_handler;
98                 }
99
100                 ret = kill(pid, sig);
101
102                 if(signal(sig, old) == SIG_ERR)
103                         error("Cannot restore signal handler for signal %d", sig);
104
105                 if(ret == -1) {
106                         switch(errno) {
107                                 case ESRCH: 
108                                         error("Cannot kill pid %d, but it is not running.", pid);
109                                         break;
110
111                                 case EPERM:
112                                         error("Cannot kill pid %d, but I do not have enough permissions.", pid);
113                                         break;
114
115                                 default:
116                                         error("Cannot kill pid %d, but I received an error.", pid);
117                                         break;
118                         }
119                 }
120         }
121
122         return ret;
123 }
124
125 void kill_childs()
126 {
127         siginfo_t info;
128
129         struct web_client *w;
130         for(w = web_clients; w ; w = w->next) {
131                 debug(D_EXIT, "Stopping web client %s", w->client_ip);
132                 pthread_cancel(w->thread);
133                 pthread_join(w->thread, NULL);
134         }
135
136         int i;  
137         for (i = 0; static_threads[i].name != NULL ; i++) {
138                 if(static_threads[i].thread) {
139                         debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
140                         pthread_cancel(*static_threads[i].thread);
141                         pthread_join(*static_threads[i].thread, NULL);
142                         static_threads[i].thread = NULL;
143                 }
144         }
145
146         if(tc_child_pid) {
147                 info("Killing tc-qos-helper procees");
148                 if(killpid(tc_child_pid, SIGTERM) != -1)
149                         waitid(tc_child_pid, 0, &info, WEXITED);
150         }
151         tc_child_pid = 0;
152
153         struct plugind *cd;
154         for(cd = pluginsd_root ; cd ; cd = cd->next) {
155                 debug(D_EXIT, "Stopping %s plugin thread", cd->id);
156                 pthread_cancel(cd->thread);
157                 pthread_join(cd->thread, NULL);
158
159                 if(cd->pid && !cd->obsolete) {
160                         debug(D_EXIT, "killing %s plugin process", cd->id);
161                         if(killpid(cd->pid, SIGTERM) != -1)
162                                 waitid(cd->pid, 0, &info, WEXITED);
163                 }
164         }
165
166         debug(D_EXIT, "All threads/childs stopped.");
167 }
168
169
170 int main(int argc, char **argv)
171 {
172         int i;
173         int config_loaded = 0;
174         int dont_fork = 0;
175
176         // global initialization
177         get_HZ();
178
179         // set the name for logging
180         program_name = "netdata";
181
182         // parse  the arguments
183         for(i = 1; i < argc ; i++) {
184                 if(strcmp(argv[i], "-c") == 0 && (i+1) < argc) {
185                         if(load_config(argv[i+1], 1) != 1) {
186                                 fprintf(stderr, "Cannot load configuration file %s. Reason: %s\n", argv[i+1], strerror(errno));
187                                 exit(1);
188                         }
189                         else {
190                                 debug(D_OPTIONS, "Configuration loaded from %s.", argv[i+1]);
191                                 config_loaded = 1;
192                         }
193                         i++;
194                 }
195                 else if(strcmp(argv[i], "-df") == 0 && (i+1) < argc) { config_set("global", "debug flags",  argv[i+1]); i++; }
196                 else if(strcmp(argv[i], "-p")  == 0 && (i+1) < argc) { config_set("global", "port",         argv[i+1]); i++; }
197                 else if(strcmp(argv[i], "-u")  == 0 && (i+1) < argc) { config_set("global", "run as user",  argv[i+1]); i++; }
198                 else if(strcmp(argv[i], "-l")  == 0 && (i+1) < argc) { config_set("global", "history",      argv[i+1]); i++; }
199                 else if(strcmp(argv[i], "-t")  == 0 && (i+1) < argc) { config_set("global", "update every", argv[i+1]); i++; }
200                 else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) { config_set("global", "host access prefix", argv[i+1]); i++; }
201                 else if(strcmp(argv[i], "-nodeamon") == 0 || strcmp(argv[i], "-nd") == 0) dont_fork = 1;
202                 else if(strcmp(argv[i], "--unittest")  == 0) {
203                         if(unit_test_storage()) exit(1);
204                         exit(0);
205                         rrd_update_every = 1;
206                         if(unit_test(1000000, 0)) exit(1);
207                         if(unit_test(1000000, 500000)) exit(1);
208                         if(unit_test(1000000, 100000)) exit(1);
209                         if(unit_test(1000000, 900000)) exit(1);
210                         if(unit_test(2000000, 0)) exit(1);
211                         if(unit_test(2000000, 500000)) exit(1);
212                         if(unit_test(2000000, 100000)) exit(1);
213                         if(unit_test(2000000, 900000)) exit(1);
214                         if(unit_test(500000, 500000)) exit(1);
215                         //if(unit_test(500000, 100000)) exit(1); // FIXME: the expected numbers are wrong
216                         //if(unit_test(500000, 900000)) exit(1); // FIXME: the expected numbers are wrong
217                         if(unit_test(500000, 0)) exit(1);
218                         fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
219                         exit(0);
220                 }
221                 else {
222                         fprintf(stderr, "Cannot understand option '%s'.\n", argv[i]);
223                         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]);
224                         fprintf(stderr, "  -c CONFIG FILE the configuration file to load. Default: %s.\n", CONFIG_DIR "/" CONFIG_FILENAME);
225                         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);
226                         fprintf(stderr, "  -t UPDATE_TIMER can be from 1 to %d seconds. Default: %d.\n", UPDATE_EVERY_MAX, UPDATE_EVERY);
227                         fprintf(stderr, "  -p LISTEN_PORT can be from 1 to %d. Default: %d.\n", 65535, LISTEN_PORT);
228                         fprintf(stderr, "  -u USERNAME can be any system username to run as. Default: none.\n");
229                         fprintf(stderr, "  -ch path to access host /proc and /sys when running in a container. Default: empty.\n");
230                         fprintf(stderr, "  -nd or -nodeamon to disable forking in the background. Default: unset.\n");
231                         fprintf(stderr, "  -df FLAGS debug options. Default: 0x%08llx.\n", debug_flags);
232                         exit(1);
233                 }
234         }
235
236         if(!config_loaded) load_config(NULL, 0);
237
238         char *input_log_file = NULL;
239         char *output_log_file = NULL;
240         char *error_log_file = NULL;
241         char *access_log_file = NULL;
242         {
243                 char buffer[1024];
244
245                 // --------------------------------------------------------------------
246
247                 sprintf(buffer, "0x%08llx", 0ULL);
248                 char *flags = config_get("global", "debug flags", buffer);
249                 debug_flags = strtoull(flags, NULL, 0);
250                 debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
251
252                 if(debug_flags != 0) {
253                         struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
254                         if(setrlimit(RLIMIT_CORE, &rl) != 0)
255                                 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
256                 }
257
258                 // --------------------------------------------------------------------
259
260                 global_host_prefix = config_get("global", "host access prefix", "");
261
262                 // --------------------------------------------------------------------
263
264                 output_log_file = config_get("global", "debug log", LOG_DIR "/debug.log");
265                 if(strcmp(output_log_file, "syslog") == 0) {
266                         output_log_syslog = 1;
267                         output_log_file = NULL;
268                 }
269                 else if(strcmp(output_log_file, "none") == 0) {
270                         output_log_syslog = 0;
271                         output_log_file = NULL;
272                 }
273                 else output_log_syslog = 0;
274
275                 // --------------------------------------------------------------------
276
277                 silent = 0;
278                 error_log_file = config_get("global", "error log", LOG_DIR "/error.log");
279                 if(strcmp(error_log_file, "syslog") == 0) {
280                         error_log_syslog = 1;
281                         error_log_file = NULL;
282                 }
283                 else if(strcmp(error_log_file, "none") == 0) {
284                         error_log_syslog = 0;
285                         error_log_file = NULL;
286                         silent = 1; // optimization - do not even generate debug log entries
287                 }
288                 else error_log_syslog = 0;
289
290                 // --------------------------------------------------------------------
291
292                 access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
293                 if(strcmp(access_log_file, "syslog") == 0) {
294                         access_log_syslog = 1;
295                         access_log_file = NULL;
296                 }
297                 else if(strcmp(access_log_file, "none") == 0) {
298                         access_log_syslog = 0;
299                         access_log_file = NULL;
300                 }
301                 else access_log_syslog = 0;
302
303                 // --------------------------------------------------------------------
304
305                 rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
306
307                 // --------------------------------------------------------------------
308
309                 if(gethostname(buffer, HOSTNAME_MAX) == -1)
310                         error("WARNING: Cannot get machine hostname.");
311                 hostname = config_get("global", "hostname", buffer);
312                 debug(D_OPTIONS, "hostname set to '%s'", hostname);
313
314                 // --------------------------------------------------------------------
315
316                 rrd_default_history_entries = config_get_number("global", "history", RRD_DEFAULT_HISTORY_ENTRIES);
317                 if(rrd_default_history_entries < 5 || rrd_default_history_entries > RRD_HISTORY_ENTRIES_MAX) {
318                         fprintf(stderr, "Invalid save lines %d given. Defaulting to %d.\n", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
319                         rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
320                 }
321                 else {
322                         debug(D_OPTIONS, "save lines set to %d.", rrd_default_history_entries);
323                 }
324
325                 // --------------------------------------------------------------------
326
327                 rrd_update_every = config_get_number("global", "update every", UPDATE_EVERY);
328                 if(rrd_update_every < 1 || rrd_update_every > 600) {
329                         fprintf(stderr, "Invalid update timer %d given. Defaulting to %d.\n", rrd_update_every, UPDATE_EVERY_MAX);
330                         rrd_update_every = UPDATE_EVERY;
331                 }
332                 else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
333
334                 // --------------------------------------------------------------------
335                 
336                 for (i = 0; static_threads[i].name != NULL ; i++) {
337                         struct netdata_static_thread *st = &static_threads[i];
338
339                         if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
340                         if(st->enabled && st->init_routine) st->init_routine();
341                 }
342
343                 // --------------------------------------------------------------------
344
345                 prepare_rundir();
346                 char *user = config_get("global", "run as user", (getuid() == 0)?NETDATA_USER:"");
347                 if(*user) {
348                         if(become_user(user) != 0) {
349                                 fprintf(stderr, "Cannot become user %s.\n", user);
350                                 exit(1);
351                         }
352                         else debug(D_OPTIONS, "Successfully became user %s.", user);
353                 }
354
355                 // --------------------------------------------------------------------
356
357                 listen_backlog = config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
358
359                 listen_port = config_get_number("global", "port", LISTEN_PORT);
360                 if(listen_port < 1 || listen_port > 65535) {
361                         fprintf(stderr, "Invalid listen port %d given. Defaulting to %d.\n", listen_port, LISTEN_PORT);
362                         listen_port = LISTEN_PORT;
363                 }
364                 else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
365
366                 int ip = 0;
367                 char *ipv = config_get("global", "ip version", "any");
368                 if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
369                 else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
370                 else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
371                 else fprintf(stderr, "Cannot understand ip version '%s'. Assumming 'any'.", ipv);
372
373                 if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(listen_port, listen_backlog);
374                 if(listen_fd < 0) {
375                         listen_fd = create_listen_socket4(listen_port, listen_backlog);
376                         if(listen_fd >= 0 && ip != 4) fprintf(stderr, "Managed to open an IPv4 socket on port %d.", listen_port);
377                 }
378
379                 if(listen_fd < 0) fatal("Cannot listen socket.");
380         }
381
382         // never become a problem
383         if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
384
385 #ifdef NETDATA_DAEMON
386         if(become_daemon(dont_fork, 0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
387                 fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno));
388                 exit(1);
389         }
390 #endif
391
392         if(output_log_syslog || error_log_syslog || access_log_syslog)
393                 openlog("netdata", LOG_PID, LOG_DAEMON);
394
395         info("NetData started on pid %d", getpid());
396
397
398         // catch all signals
399         for (i = 1 ; i < 65 ;i++) {
400                 switch(i) {
401                         case SIGSEGV:
402                         case SIGFPE:
403                         case SIGCHLD:
404                                 signal(i, SIG_DFL);
405                                 break;
406
407                         default:
408                                 signal(i,  sig_handler);
409                                 break;
410                 }
411         }
412
413         for (i = 0; static_threads[i].name != NULL ; i++) {
414                 struct netdata_static_thread *st = &static_threads[i];
415
416                 if(st->enabled) {
417                         st->thread = malloc(sizeof(pthread_t));
418
419                         info("Starting thread %s.", st->name);
420
421                         if(pthread_create(st->thread, NULL, st->start_routine, NULL))
422                                 error("failed to create new thread for %s.", st->name);
423
424                         else if(pthread_detach(*st->thread))
425                                 error("Cannot request detach of newly created %s thread.", st->name);
426                 }
427                 else info("Not starting thread %s.", st->name);
428         }
429
430         // for future use - the main thread
431         while(1) sleep(60);
432
433         exit(0);
434 }