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