]> arthur.barton.de Git - netdata.git/blob - src/main.c
log: now is logging program name too; tc: huge optimizations; procfile: optimized...
[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 void kill_childs()
64 {
65         siginfo_t info;
66
67         struct web_client *w;
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);
72         }
73
74         int i;  
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;
81                 }
82         }
83
84         if(tc_child_pid) {
85                 debug(D_EXIT, "Killing tc-qos-helper procees");
86                 kill(tc_child_pid, SIGTERM);
87                 waitid(tc_child_pid, 0, &info, WEXITED);
88         }
89         tc_child_pid = 0;
90
91         struct plugind *cd;
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);
96
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);
101                 }
102         }
103
104         debug(D_EXIT, "All threads/childs stopped.");
105 }
106
107
108 int main(int argc, char **argv)
109 {
110         int i;
111         int config_loaded = 0;
112
113         // set the name for logging
114         program_name = "netdata";
115
116         // parse  the arguments
117         for(i = 1; i < argc ; i++) {
118                 if(strcmp(argv[i], "-c") == 0 && (i+1) < argc) {
119                         if(load_config(argv[i+1], 1) != 1) {
120                                 fprintf(stderr, "Cannot load configuration file %s. Reason: %s\n", argv[i+1], strerror(errno));
121                                 exit(1);
122                         }
123                         else {
124                                 debug(D_OPTIONS, "Configuration loaded from %s.", argv[i+1]);
125                                 config_loaded = 1;
126                         }
127                         i++;
128                 }
129                 else if(strcmp(argv[i], "-df") == 0 && (i+1) < argc) { config_set("global", "debug flags",  argv[i+1]); i++; }
130                 else if(strcmp(argv[i], "-p")  == 0 && (i+1) < argc) { config_set("global", "port",         argv[i+1]); i++; }
131                 else if(strcmp(argv[i], "-u")  == 0 && (i+1) < argc) { config_set("global", "run as user",  argv[i+1]); i++; }
132                 else if(strcmp(argv[i], "-l")  == 0 && (i+1) < argc) { config_set("global", "history",      argv[i+1]); i++; }
133                 else if(strcmp(argv[i], "-t")  == 0 && (i+1) < argc) { config_set("global", "update every", argv[i+1]); i++; }
134                 else if(strcmp(argv[i], "--unittest")  == 0) {
135                         if(unit_test_storage()) exit(1);
136                         exit(0);
137                         rrd_update_every = 1;
138                         if(unit_test(1000000, 0)) exit(1);
139                         if(unit_test(1000000, 500000)) exit(1);
140                         if(unit_test(1000000, 100000)) exit(1);
141                         if(unit_test(1000000, 900000)) exit(1);
142                         if(unit_test(2000000, 0)) exit(1);
143                         if(unit_test(2000000, 500000)) exit(1);
144                         if(unit_test(2000000, 100000)) exit(1);
145                         if(unit_test(2000000, 900000)) exit(1);
146                         if(unit_test(500000, 500000)) exit(1);
147                         //if(unit_test(500000, 100000)) exit(1); // FIXME: the expected numbers are wrong
148                         //if(unit_test(500000, 900000)) exit(1); // FIXME: the expected numbers are wrong
149                         if(unit_test(500000, 0)) exit(1);
150                         fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
151                         exit(0);
152                 }
153                 else {
154                         fprintf(stderr, "Cannot understand option '%s'.\n", argv[i]);
155                         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]);
156                         fprintf(stderr, "  -c CONFIG FILE the configuration file to load. Default: %s.\n", CONFIG_DIR "/" CONFIG_FILENAME);
157                         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);
158                         fprintf(stderr, "  -t UPDATE_TIMER can be from 1 to %d seconds. Default: %d.\n", UPDATE_EVERY_MAX, UPDATE_EVERY);
159                         fprintf(stderr, "  -p LISTEN_PORT can be from 1 to %d. Default: %d.\n", 65535, LISTEN_PORT);
160                         fprintf(stderr, "  -u USERNAME can be any system username to run as. Default: none.\n");
161                         fprintf(stderr, "  -df FLAGS debug options. Default: 0x%8llx.\n", debug_flags);
162                         exit(1);
163                 }
164         }
165
166         if(!config_loaded) load_config(NULL, 0);
167
168         char *input_log_file = NULL;
169         char *output_log_file = NULL;
170         char *error_log_file = NULL;
171         char *access_log_file = NULL;
172         {
173                 char buffer[1024];
174
175                 // --------------------------------------------------------------------
176
177                 sprintf(buffer, "0x%08llx", 0ULL);
178                 char *flags = config_get("global", "debug flags", buffer);
179                 debug_flags = strtoull(flags, NULL, 0);
180                 debug(D_OPTIONS, "Debug flags set to '0x%8llx'.", debug_flags);
181
182                 // --------------------------------------------------------------------
183
184                 output_log_file = config_get("global", "debug log", LOG_DIR "/debug.log");
185                 if(strcmp(output_log_file, "syslog") == 0) {
186                         output_log_syslog = 1;
187                         output_log_file = NULL;
188                 }
189                 else if(strcmp(output_log_file, "none") == 0) {
190                         output_log_syslog = 0;
191                         output_log_file = NULL;
192                 }
193                 else output_log_syslog = 0;
194
195                 // --------------------------------------------------------------------
196
197                 silent = 0;
198                 error_log_file = config_get("global", "error log", LOG_DIR "/error.log");
199                 if(strcmp(error_log_file, "syslog") == 0) {
200                         error_log_syslog = 1;
201                         error_log_file = NULL;
202                 }
203                 else if(strcmp(error_log_file, "none") == 0) {
204                         error_log_syslog = 0;
205                         error_log_file = NULL;
206                         silent = 1; // optimization - do not even generate debug log entries
207                 }
208                 else error_log_syslog = 0;
209
210                 // --------------------------------------------------------------------
211
212                 access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
213                 if(strcmp(access_log_file, "syslog") == 0) {
214                         access_log_syslog = 1;
215                         access_log_file = NULL;
216                 }
217                 else if(strcmp(access_log_file, "none") == 0) {
218                         access_log_syslog = 0;
219                         access_log_file = NULL;
220                 }
221                 else access_log_syslog = 0;
222
223                 // --------------------------------------------------------------------
224
225                 rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
226
227                 // --------------------------------------------------------------------
228
229                 if(gethostname(buffer, HOSTNAME_MAX) == -1)
230                         error("WARNING: Cannot get machine hostname.");
231                 hostname = config_get("global", "hostname", buffer);
232                 debug(D_OPTIONS, "hostname set to '%s'", hostname);
233
234                 // --------------------------------------------------------------------
235
236                 rrd_default_history_entries = config_get_number("global", "history", RRD_DEFAULT_HISTORY_ENTRIES);
237                 if(rrd_default_history_entries < 5 || rrd_default_history_entries > RRD_HISTORY_ENTRIES_MAX) {
238                         fprintf(stderr, "Invalid save lines %d given. Defaulting to %d.\n", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
239                         rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
240                 }
241                 else {
242                         debug(D_OPTIONS, "save lines set to %d.", rrd_default_history_entries);
243                 }
244
245                 // --------------------------------------------------------------------
246
247                 rrd_update_every = config_get_number("global", "update every", UPDATE_EVERY);
248                 if(rrd_update_every < 1 || rrd_update_every > 600) {
249                         fprintf(stderr, "Invalid update timer %d given. Defaulting to %d.\n", rrd_update_every, UPDATE_EVERY_MAX);
250                         rrd_update_every = UPDATE_EVERY;
251                 }
252                 else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
253
254                 // --------------------------------------------------------------------
255                 
256                 for (i = 0; static_threads[i].name != NULL ; i++) {
257                         struct netdata_static_thread *st = &static_threads[i];
258
259                         if(st->config_name) st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
260                         if(st->enabled && st->init_routine) st->init_routine();
261                 }
262
263                 // --------------------------------------------------------------------
264
265                 prepare_rundir();
266                 char *user = config_get("global", "run as user", (getuid() == 0)?"nobody":"");
267                 if(*user) {
268                         if(become_user(user) != 0) {
269                                 fprintf(stderr, "Cannot become user %s.\n", user);
270                                 exit(1);
271                         }
272                         else debug(D_OPTIONS, "Successfully became user %s.", user);
273                 }
274
275                 // --------------------------------------------------------------------
276
277                 listen_backlog = config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
278
279                 listen_port = config_get_number("global", "port", LISTEN_PORT);
280                 if(listen_port < 1 || listen_port > 65535) {
281                         fprintf(stderr, "Invalid listen port %d given. Defaulting to %d.\n", listen_port, LISTEN_PORT);
282                         listen_port = LISTEN_PORT;
283                 }
284                 else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
285
286                 int ip = 0;
287                 char *ipv = config_get("global", "ip version", "any");
288                 if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
289                 else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
290                 else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
291                 else fprintf(stderr, "Cannot understand ip version '%s'. Assumming 'any'.", ipv);
292
293                 if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(listen_port, listen_backlog);
294                 if(listen_fd < 0) {
295                         listen_fd = create_listen_socket4(listen_port, listen_backlog);
296                         if(listen_fd >= 0 && ip != 4) fprintf(stderr, "Managed to open an IPv4 socket on port %d.", listen_port);
297                 }
298
299                 if(listen_fd < 0) fatal("Cannot listen socket.");
300         }
301
302         // never become a problem
303         if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
304
305 #ifndef NETDATA_NO_DAEMON
306         if(become_daemon(0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
307                 fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno));
308                 exit(1);
309         }
310 #endif
311
312         if(output_log_syslog || error_log_syslog || access_log_syslog)
313                 openlog("netdata", LOG_PID, LOG_DAEMON);
314
315         info("NetData started on pid %d", getpid());
316
317
318         // catch all signals
319         for (i = 1 ; i < 65 ;i++) if(i != SIGSEGV && i != SIGFPE) signal(i,  sig_handler);
320         
321         for (i = 0; static_threads[i].name != NULL ; i++) {
322                 struct netdata_static_thread *st = &static_threads[i];
323
324                 if(st->enabled) {
325                         st->thread = malloc(sizeof(pthread_t));
326
327                         info("Starting thread %s.", st->name);
328
329                         if(pthread_create(st->thread, NULL, st->start_routine, NULL))
330                                 error("failed to create new thread for %s.", st->name);
331
332                         else if(pthread_detach(*st->thread))
333                                 error("Cannot request detach of newly created %s thread.", st->name);
334                 }
335                 else info("Not starting thread %s.", st->name);
336         }
337
338         // the main process - the web server listener
339         // this never ends
340         // socket_listen_main(NULL);
341         while(1) process_childs(1);
342
343         exit(0);
344 }