]> arthur.barton.de Git - netdata.git/blobdiff - src/main.c
cgroups are now on their own thread; cgroup names are controlled by plugins.d/cgroup...
[netdata.git] / src / main.c
old mode 100755 (executable)
new mode 100644 (file)
index 3d8edcf..ba02371
@@ -37,6 +37,8 @@
 
 #include "main.h"
 
+extern void *cgroups_main(void *ptr);
+
 int netdata_exit = 0;
 
 void netdata_cleanup_and_exit(int ret)
@@ -44,7 +46,23 @@ void netdata_cleanup_and_exit(int ret)
        netdata_exit = 1;
        rrdset_save_all();
        // kill_childs();
-       unlink(RUN_DIR "/netdata.pid");
+
+       // let it log a few more error messages
+       error_log_limit_reset();
+
+       if(pidfd != -1) {
+               if(ftruncate(pidfd, 0) != 0)
+                       error("Cannot truncate pidfile '%s'.", pidfile);
+
+               close(pidfd);
+               pidfd = -1;
+       }
+
+       if(pidfile[0]) {
+               if(unlink(pidfile) != 0)
+                       error("Cannot unlink pidfile '%s'.", pidfile);
+       }
+
        info("NetData exiting. Bye bye...");
        exit(ret);
 }
@@ -67,6 +85,7 @@ struct netdata_static_thread static_threads[] = {
        {"tc",                  "plugins",      "tc",                   1, NULL, NULL,  tc_main},
        {"idlejitter",  "plugins",      "idlejitter",   1, NULL, NULL,  cpuidlejitter_main},
        {"proc",                "plugins",      "proc",                 1, NULL, NULL,  proc_main},
+       {"cgroups",             "plugins",      "cgroups",              1, NULL, NULL,  cgroups_main},
 
 #ifdef INTERNAL_PLUGIN_NFACCT
        // nfacct requires root access
@@ -220,6 +239,11 @@ int main(int argc, char **argv)
                else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) { config_set("global", "host access prefix", argv[i+1]); i++; }
                else if(strcmp(argv[i], "-stacksize") == 0 && (i+1) < argc) { config_set("global", "pthread stack size", argv[i+1]); i++; }
                else if(strcmp(argv[i], "-nodaemon") == 0 || strcmp(argv[i], "-nd") == 0) dont_fork = 1;
+               else if(strcmp(argv[i], "-pidfile") == 0 && (i+1) < argc) {
+                       i++;
+                       strncpy(pidfile, argv[i], FILENAME_MAX);
+                       pidfile[FILENAME_MAX] = '\0';
+               }
                else if(strcmp(argv[i], "--unittest")  == 0) {
                        rrd_update_every = 1;
                        if(run_all_mockup_tests()) exit(1);
@@ -239,6 +263,7 @@ int main(int argc, char **argv)
                        fprintf(stderr, "  -nd or -nodeamon to disable forking in the background. Default: unset.\n");
                        fprintf(stderr, "  -df FLAGS debug options. Default: 0x%08llx.\n", debug_flags);
                        fprintf(stderr, "  -stacksize BYTES to overwrite the pthread stack size.\n");
+                       fprintf(stderr, "  -pidfile FILENAME to save a pid while running.\n");
                        exit(1);
                }
        }
@@ -325,6 +350,12 @@ int main(int argc, char **argv)
                }
                else error_log_syslog = 0;
 
+               error_log_throttle_period = config_get_number("global", "errors flood protection period", error_log_throttle_period);
+               setenv("NETDATA_ERRORS_THROTTLE_PERIOD", config_get("global", "errors flood protection period"    , ""), 1);
+
+               error_log_errors_per_period = config_get_number("global", "errors to trigger flood protection", error_log_errors_per_period);
+               setenv("NETDATA_ERRORS_PER_PERIOD"     , config_get("global", "errors to trigger flood protection", ""), 1);
+
                // --------------------------------------------------------------------
 
                access_log_file = config_get("global", "access log", LOG_DIR "/access.log");
@@ -353,7 +384,7 @@ int main(int argc, char **argv)
 
                rrd_default_history_entries = (int) config_get_number("global", "history", RRD_DEFAULT_HISTORY_ENTRIES);
                if(rrd_default_history_entries < 5 || rrd_default_history_entries > RRD_HISTORY_ENTRIES_MAX) {
-                       fprintf(stderr, "Invalid save lines %d given. Defaulting to %d.\n", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
+                       info("Invalid save lines %d given. Defaulting to %d.", rrd_default_history_entries, RRD_DEFAULT_HISTORY_ENTRIES);
                        rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
                }
                else {
@@ -364,7 +395,7 @@ int main(int argc, char **argv)
 
                rrd_update_every = (int) config_get_number("global", "update every", UPDATE_EVERY);
                if(rrd_update_every < 1 || rrd_update_every > 600) {
-                       fprintf(stderr, "Invalid update timer %d given. Defaulting to %d.\n", rrd_update_every, UPDATE_EVERY_MAX);
+                       info("Invalid update timer %d given. Defaulting to %d.", rrd_update_every, UPDATE_EVERY_MAX);
                        rrd_update_every = UPDATE_EVERY;
                }
                else debug(D_OPTIONS, "update timer set to %d.", rrd_update_every);
@@ -401,9 +432,13 @@ int main(int argc, char **argv)
 
                // --------------------------------------------------------------------
 
-               prepare_rundir();
+               // get the user we should run
+               // IMPORTANT: this is required before web_files_uid()
                user = config_get("global", "run as user"    , (getuid() == 0)?NETDATA_USER:"");
-               web_files_uid();
+
+               // IMPORTANT: these have to run once, while single threaded
+               web_files_uid(); // IMPORTANT: web_files_uid() before web_files_gid()
+               web_files_gid();
 
                // --------------------------------------------------------------------
 
@@ -411,7 +446,7 @@ int main(int argc, char **argv)
 
                listen_port = (int) config_get_number("global", "port", LISTEN_PORT);
                if(listen_port < 1 || listen_port > 65535) {
-                       fprintf(stderr, "Invalid listen port %d given. Defaulting to %d.\n", listen_port, LISTEN_PORT);
+                       info("Invalid listen port %d given. Defaulting to %d.", listen_port, LISTEN_PORT);
                        listen_port = LISTEN_PORT;
                }
                else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
@@ -421,12 +456,12 @@ int main(int argc, char **argv)
                if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
                else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
                else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
-               else fprintf(stderr, "Cannot understand ip version '%s'. Assumming 'any'.", ipv);
+               else info("Cannot understand ip version '%s'. Assuming 'any'.", ipv);
 
-               if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(listen_port, listen_backlog);
+               if(ip == 0 || ip == 6) listen_fd = create_listen_socket6(config_get("global", "bind socket to IP", "*"), listen_port, listen_backlog);
                if(listen_fd < 0) {
-                       listen_fd = create_listen_socket4(listen_port, listen_backlog);
-                       if(listen_fd >= 0 && ip != 4) fprintf(stderr, "Managed to open an IPv4 socket on port %d.", listen_port);
+                       listen_fd = create_listen_socket4(config_get("global", "bind socket to IP", "*"), listen_port, listen_backlog);
+                       if(listen_fd >= 0 && ip != 4) info("Managed to open an IPv4 socket on port %d.", listen_port);
                }
 
                if(listen_fd < 0) fatal("Cannot listen socket.");