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