main.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "common.h"
  3. #include "buildinfo.h"
  4. #include "static_threads.h"
  5. int netdata_zero_metrics_enabled;
  6. int netdata_anonymous_statistics_enabled;
  7. struct netdata_static_thread *static_threads;
  8. struct config netdata_config = {
  9. .first_section = NULL,
  10. .last_section = NULL,
  11. .mutex = NETDATA_MUTEX_INITIALIZER,
  12. .index = {
  13. .avl_tree = {
  14. .root = NULL,
  15. .compar = appconfig_section_compare
  16. },
  17. .rwlock = AVL_LOCK_INITIALIZER
  18. }
  19. };
  20. void netdata_cleanup_and_exit(int ret) {
  21. // enabling this, is wrong
  22. // because the threads will be cancelled while cleaning up
  23. // netdata_exit = 1;
  24. error_log_limit_unlimited();
  25. info("EXIT: netdata prepares to exit with code %d...", ret);
  26. send_statistics("EXIT", ret?"ERROR":"OK","-");
  27. char agent_crash_file[FILENAME_MAX + 1];
  28. char agent_incomplete_shutdown_file[FILENAME_MAX + 1];
  29. snprintfz(agent_crash_file, FILENAME_MAX, "%s/.agent_crash", netdata_configured_varlib_dir);
  30. snprintfz(agent_incomplete_shutdown_file, FILENAME_MAX, "%s/.agent_incomplete_shutdown", netdata_configured_varlib_dir);
  31. (void) rename(agent_crash_file, agent_incomplete_shutdown_file);
  32. // cleanup/save the database and exit
  33. info("EXIT: cleaning up the database...");
  34. rrdhost_cleanup_all();
  35. if(!ret) {
  36. // exit cleanly
  37. // stop everything
  38. info("EXIT: stopping static threads...");
  39. #ifdef ENABLE_ACLK
  40. aclk_sync_exit_all();
  41. #endif
  42. cancel_main_threads();
  43. // free the database
  44. info("EXIT: freeing database memory...");
  45. #ifdef ENABLE_DBENGINE
  46. for(int tier = 0; tier < storage_tiers ; tier++)
  47. rrdeng_prepare_exit(multidb_ctx[tier]);
  48. #endif
  49. rrdhost_free_all();
  50. #ifdef ENABLE_DBENGINE
  51. for(int tier = 0; tier < storage_tiers ; tier++)
  52. rrdeng_exit(multidb_ctx[tier]);
  53. #endif
  54. }
  55. sql_close_context_database();
  56. sql_close_database();
  57. // unlink the pid
  58. if(pidfile[0]) {
  59. info("EXIT: removing netdata PID file '%s'...", pidfile);
  60. if(unlink(pidfile) != 0)
  61. error("EXIT: cannot unlink pidfile '%s'.", pidfile);
  62. }
  63. #ifdef ENABLE_HTTPS
  64. security_clean_openssl();
  65. #endif
  66. info("EXIT: all done - netdata is now exiting - bye bye...");
  67. (void) unlink(agent_incomplete_shutdown_file);
  68. exit(ret);
  69. }
  70. void web_server_threading_selection(void) {
  71. web_server_mode = web_server_mode_id(config_get(CONFIG_SECTION_WEB, "mode", web_server_mode_name(web_server_mode)));
  72. int static_threaded = (web_server_mode == WEB_SERVER_MODE_STATIC_THREADED);
  73. int i;
  74. for (i = 0; static_threads[i].name; i++) {
  75. if (static_threads[i].start_routine == socket_listen_main_static_threaded)
  76. static_threads[i].enabled = static_threaded;
  77. }
  78. }
  79. int make_dns_decision(const char *section_name, const char *config_name, const char *default_value, SIMPLE_PATTERN *p)
  80. {
  81. char *value = config_get(section_name,config_name,default_value);
  82. if(!strcmp("yes",value))
  83. return 1;
  84. if(!strcmp("no",value))
  85. return 0;
  86. if(strcmp("heuristic",value))
  87. error("Invalid configuration option '%s' for '%s'/'%s'. Valid options are 'yes', 'no' and 'heuristic'. Proceeding with 'heuristic'",
  88. value, section_name, config_name);
  89. return simple_pattern_is_potential_name(p);
  90. }
  91. void web_server_config_options(void)
  92. {
  93. web_client_timeout =
  94. (int)config_get_number(CONFIG_SECTION_WEB, "disconnect idle clients after seconds", web_client_timeout);
  95. web_client_first_request_timeout =
  96. (int)config_get_number(CONFIG_SECTION_WEB, "timeout for first request", web_client_first_request_timeout);
  97. web_client_streaming_rate_t =
  98. config_get_number(CONFIG_SECTION_WEB, "accept a streaming request every seconds", web_client_streaming_rate_t);
  99. respect_web_browser_do_not_track_policy =
  100. config_get_boolean(CONFIG_SECTION_WEB, "respect do not track policy", respect_web_browser_do_not_track_policy);
  101. web_x_frame_options = config_get(CONFIG_SECTION_WEB, "x-frame-options response header", "");
  102. if(!*web_x_frame_options)
  103. web_x_frame_options = NULL;
  104. web_allow_connections_from =
  105. simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow connections from", "localhost *"),
  106. NULL, SIMPLE_PATTERN_EXACT);
  107. web_allow_connections_dns =
  108. make_dns_decision(CONFIG_SECTION_WEB, "allow connections by dns", "heuristic", web_allow_connections_from);
  109. web_allow_dashboard_from =
  110. simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow dashboard from", "localhost *"),
  111. NULL, SIMPLE_PATTERN_EXACT);
  112. web_allow_dashboard_dns =
  113. make_dns_decision(CONFIG_SECTION_WEB, "allow dashboard by dns", "heuristic", web_allow_dashboard_from);
  114. web_allow_badges_from =
  115. simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow badges from", "*"), NULL, SIMPLE_PATTERN_EXACT);
  116. web_allow_badges_dns =
  117. make_dns_decision(CONFIG_SECTION_WEB, "allow badges by dns", "heuristic", web_allow_badges_from);
  118. web_allow_registry_from =
  119. simple_pattern_create(config_get(CONFIG_SECTION_REGISTRY, "allow from", "*"), NULL, SIMPLE_PATTERN_EXACT);
  120. web_allow_registry_dns = make_dns_decision(CONFIG_SECTION_REGISTRY, "allow by dns", "heuristic",
  121. web_allow_registry_from);
  122. web_allow_streaming_from = simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow streaming from", "*"),
  123. NULL, SIMPLE_PATTERN_EXACT);
  124. web_allow_streaming_dns = make_dns_decision(CONFIG_SECTION_WEB, "allow streaming by dns", "heuristic",
  125. web_allow_streaming_from);
  126. // Note the default is not heuristic, the wildcards could match DNS but the intent is ip-addresses.
  127. web_allow_netdataconf_from = simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow netdata.conf from",
  128. "localhost fd* 10.* 192.168.* 172.16.* 172.17.* 172.18.*"
  129. " 172.19.* 172.20.* 172.21.* 172.22.* 172.23.* 172.24.*"
  130. " 172.25.* 172.26.* 172.27.* 172.28.* 172.29.* 172.30.*"
  131. " 172.31.* UNKNOWN"), NULL, SIMPLE_PATTERN_EXACT);
  132. web_allow_netdataconf_dns =
  133. make_dns_decision(CONFIG_SECTION_WEB, "allow netdata.conf by dns", "no", web_allow_netdataconf_from);
  134. web_allow_mgmt_from =
  135. simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow management from", "localhost"),
  136. NULL, SIMPLE_PATTERN_EXACT);
  137. web_allow_mgmt_dns =
  138. make_dns_decision(CONFIG_SECTION_WEB, "allow management by dns","heuristic",web_allow_mgmt_from);
  139. #ifdef NETDATA_WITH_ZLIB
  140. web_enable_gzip = config_get_boolean(CONFIG_SECTION_WEB, "enable gzip compression", web_enable_gzip);
  141. char *s = config_get(CONFIG_SECTION_WEB, "gzip compression strategy", "default");
  142. if(!strcmp(s, "default"))
  143. web_gzip_strategy = Z_DEFAULT_STRATEGY;
  144. else if(!strcmp(s, "filtered"))
  145. web_gzip_strategy = Z_FILTERED;
  146. else if(!strcmp(s, "huffman only"))
  147. web_gzip_strategy = Z_HUFFMAN_ONLY;
  148. else if(!strcmp(s, "rle"))
  149. web_gzip_strategy = Z_RLE;
  150. else if(!strcmp(s, "fixed"))
  151. web_gzip_strategy = Z_FIXED;
  152. else {
  153. error("Invalid compression strategy '%s'. Valid strategies are 'default', 'filtered', 'huffman only', 'rle' and 'fixed'. Proceeding with 'default'.", s);
  154. web_gzip_strategy = Z_DEFAULT_STRATEGY;
  155. }
  156. web_gzip_level = (int)config_get_number(CONFIG_SECTION_WEB, "gzip compression level", 3);
  157. if(web_gzip_level < 1) {
  158. error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 1 (fastest compression).", web_gzip_level);
  159. web_gzip_level = 1;
  160. }
  161. else if(web_gzip_level > 9) {
  162. error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 9 (best compression).", web_gzip_level);
  163. web_gzip_level = 9;
  164. }
  165. #endif /* NETDATA_WITH_ZLIB */
  166. }
  167. // killpid kills pid with SIGTERM.
  168. int killpid(pid_t pid) {
  169. int ret;
  170. debug(D_EXIT, "Request to kill pid %d", pid);
  171. errno = 0;
  172. ret = kill(pid, SIGTERM);
  173. if (ret == -1) {
  174. switch(errno) {
  175. case ESRCH:
  176. // We wanted the process to exit so just let the caller handle.
  177. return ret;
  178. case EPERM:
  179. error("Cannot kill pid %d, but I do not have enough permissions.", pid);
  180. break;
  181. default:
  182. error("Cannot kill pid %d, but I received an error.", pid);
  183. break;
  184. }
  185. }
  186. return ret;
  187. }
  188. void cancel_main_threads() {
  189. error_log_limit_unlimited();
  190. int i, found = 0;
  191. usec_t max = 5 * USEC_PER_SEC, step = 100000;
  192. for (i = 0; static_threads[i].name != NULL ; i++) {
  193. if(static_threads[i].enabled == NETDATA_MAIN_THREAD_RUNNING) {
  194. info("EXIT: Stopping main thread: %s", static_threads[i].name);
  195. netdata_thread_cancel(*static_threads[i].thread);
  196. found++;
  197. }
  198. }
  199. netdata_exit = 1;
  200. while(found && max > 0) {
  201. max -= step;
  202. info("Waiting %d threads to finish...", found);
  203. sleep_usec(step);
  204. found = 0;
  205. for (i = 0; static_threads[i].name != NULL ; i++) {
  206. if (static_threads[i].enabled != NETDATA_MAIN_THREAD_EXITED)
  207. found++;
  208. }
  209. }
  210. if(found) {
  211. for (i = 0; static_threads[i].name != NULL ; i++) {
  212. if (static_threads[i].enabled != NETDATA_MAIN_THREAD_EXITED)
  213. error("Main thread %s takes too long to exit. Giving up...", static_threads[i].name);
  214. }
  215. }
  216. else
  217. info("All threads finished.");
  218. for (i = 0; static_threads[i].name != NULL ; i++)
  219. freez(static_threads[i].thread);
  220. free(static_threads);
  221. }
  222. struct option_def option_definitions[] = {
  223. // opt description arg name default value
  224. { 'c', "Configuration file to load.", "filename", CONFIG_DIR "/" CONFIG_FILENAME},
  225. { 'D', "Do not fork. Run in the foreground.", NULL, "run in the background"},
  226. { 'd', "Fork. Run in the background.", NULL, "run in the background"},
  227. { 'h', "Display this help message.", NULL, NULL},
  228. { 'P', "File to save a pid while running.", "filename", "do not save pid to a file"},
  229. { 'i', "The IP address to listen to.", "IP", "all IP addresses IPv4 and IPv6"},
  230. { 'p', "API/Web port to use.", "port", "19999"},
  231. { 's', "Prefix for /proc and /sys (for containers).", "path", "no prefix"},
  232. { 't', "The internal clock of netdata.", "seconds", "1"},
  233. { 'u', "Run as user.", "username", "netdata"},
  234. { 'v', "Print netdata version and exit.", NULL, NULL},
  235. { 'V', "Print netdata version and exit.", NULL, NULL},
  236. { 'W', "See Advanced options below.", "options", NULL},
  237. };
  238. int help(int exitcode) {
  239. FILE *stream;
  240. if(exitcode == 0)
  241. stream = stdout;
  242. else
  243. stream = stderr;
  244. int num_opts = sizeof(option_definitions) / sizeof(struct option_def);
  245. int i;
  246. int max_len_arg = 0;
  247. // Compute maximum argument length
  248. for( i = 0; i < num_opts; i++ ) {
  249. if(option_definitions[i].arg_name) {
  250. int len_arg = (int)strlen(option_definitions[i].arg_name);
  251. if(len_arg > max_len_arg) max_len_arg = len_arg;
  252. }
  253. }
  254. if(max_len_arg > 30) max_len_arg = 30;
  255. if(max_len_arg < 20) max_len_arg = 20;
  256. fprintf(stream, "%s", "\n"
  257. " ^\n"
  258. " |.-. .-. .-. .-. . netdata \n"
  259. " | '-' '-' '-' '-' real-time performance monitoring, done right! \n"
  260. " +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->\n"
  261. "\n"
  262. " Copyright (C) 2016-2020, Netdata, Inc. <info@netdata.cloud>\n"
  263. " Released under GNU General Public License v3 or later.\n"
  264. " All rights reserved.\n"
  265. "\n"
  266. " Home Page : https://netdata.cloud\n"
  267. " Source Code: https://github.com/netdata/netdata\n"
  268. " Docs : https://learn.netdata.cloud\n"
  269. " Support : https://github.com/netdata/netdata/issues\n"
  270. " License : https://github.com/netdata/netdata/blob/master/LICENSE.md\n"
  271. "\n"
  272. " Twitter : https://twitter.com/linuxnetdata\n"
  273. " Facebook : https://www.facebook.com/linuxnetdata/\n"
  274. "\n"
  275. "\n"
  276. );
  277. fprintf(stream, " SYNOPSIS: netdata [options]\n");
  278. fprintf(stream, "\n");
  279. fprintf(stream, " Options:\n\n");
  280. // Output options description.
  281. for( i = 0; i < num_opts; i++ ) {
  282. fprintf(stream, " -%c %-*s %s", option_definitions[i].val, max_len_arg, option_definitions[i].arg_name ? option_definitions[i].arg_name : "", option_definitions[i].description);
  283. if(option_definitions[i].default_value) {
  284. fprintf(stream, "\n %c %-*s Default: %s\n", ' ', max_len_arg, "", option_definitions[i].default_value);
  285. } else {
  286. fprintf(stream, "\n");
  287. }
  288. fprintf(stream, "\n");
  289. }
  290. fprintf(stream, "\n Advanced options:\n\n"
  291. " -W stacksize=N Set the stacksize (in bytes).\n\n"
  292. " -W debug_flags=N Set runtime tracing to debug.log.\n\n"
  293. " -W unittest Run internal unittests and exit.\n\n"
  294. " -W sqlite-check Check metadata database integrity and exit.\n\n"
  295. " -W sqlite-fix Check metadata database integrity, fix if needed and exit.\n\n"
  296. " -W sqlite-compact Reclaim metadata database unused space and exit.\n\n"
  297. #ifdef ENABLE_DBENGINE
  298. " -W createdataset=N Create a DB engine dataset of N seconds and exit.\n\n"
  299. " -W stresstest=A,B,C,D,E,F,G\n"
  300. " Run a DB engine stress test for A seconds,\n"
  301. " with B writers and C readers, with a ramp up\n"
  302. " time of D seconds for writers, a page cache\n"
  303. " size of E MiB, an optional disk space limit\n"
  304. " of F MiB, G libuv workers (default 16) and exit.\n\n"
  305. #endif
  306. " -W set section option value\n"
  307. " set netdata.conf option from the command line.\n\n"
  308. " -W buildinfo Print the version, the configure options,\n"
  309. " a list of optional features, and whether they\n"
  310. " are enabled or not.\n\n"
  311. " -W buildinfojson Print the version, the configure options,\n"
  312. " a list of optional features, and whether they\n"
  313. " are enabled or not, in JSON format.\n\n"
  314. " -W simple-pattern pattern string\n"
  315. " Check if string matches pattern and exit.\n\n"
  316. " -W \"claim -token=TOKEN -rooms=ROOM1,ROOM2\"\n"
  317. " Claim the agent to the workspace rooms pointed to by TOKEN and ROOM*.\n\n"
  318. );
  319. fprintf(stream, "\n Signals netdata handles:\n\n"
  320. " - HUP Close and reopen log files.\n"
  321. " - USR1 Save internal DB to disk.\n"
  322. " - USR2 Reload health configuration.\n"
  323. "\n"
  324. );
  325. fflush(stream);
  326. return exitcode;
  327. }
  328. #ifdef ENABLE_HTTPS
  329. static void security_init(){
  330. char filename[FILENAME_MAX + 1];
  331. snprintfz(filename, FILENAME_MAX, "%s/ssl/key.pem",netdata_configured_user_config_dir);
  332. security_key = config_get(CONFIG_SECTION_WEB, "ssl key", filename);
  333. snprintfz(filename, FILENAME_MAX, "%s/ssl/cert.pem",netdata_configured_user_config_dir);
  334. security_cert = config_get(CONFIG_SECTION_WEB, "ssl certificate", filename);
  335. tls_version = config_get(CONFIG_SECTION_WEB, "tls version", "1.3");
  336. tls_ciphers = config_get(CONFIG_SECTION_WEB, "tls ciphers", "none");
  337. security_openssl_library();
  338. }
  339. #endif
  340. static void log_init(void) {
  341. char filename[FILENAME_MAX + 1];
  342. snprintfz(filename, FILENAME_MAX, "%s/debug.log", netdata_configured_log_dir);
  343. stdout_filename = config_get(CONFIG_SECTION_LOGS, "debug", filename);
  344. snprintfz(filename, FILENAME_MAX, "%s/error.log", netdata_configured_log_dir);
  345. stderr_filename = config_get(CONFIG_SECTION_LOGS, "error", filename);
  346. snprintfz(filename, FILENAME_MAX, "%s/access.log", netdata_configured_log_dir);
  347. stdaccess_filename = config_get(CONFIG_SECTION_LOGS, "access", filename);
  348. #ifdef ENABLE_ACLK
  349. aclklog_enabled = config_get_boolean(CONFIG_SECTION_CLOUD, "conversation log", CONFIG_BOOLEAN_NO);
  350. if (aclklog_enabled) {
  351. snprintfz(filename, FILENAME_MAX, "%s/aclk.log", netdata_configured_log_dir);
  352. aclklog_filename = config_get(CONFIG_SECTION_CLOUD, "conversation log file", filename);
  353. }
  354. #endif
  355. char deffacility[8];
  356. snprintfz(deffacility,7,"%s","daemon");
  357. facility_log = config_get(CONFIG_SECTION_LOGS, "facility", deffacility);
  358. error_log_throttle_period = config_get_number(CONFIG_SECTION_LOGS, "errors flood protection period", error_log_throttle_period);
  359. error_log_errors_per_period = (unsigned long)config_get_number(CONFIG_SECTION_LOGS, "errors to trigger flood protection", (long long int)error_log_errors_per_period);
  360. error_log_errors_per_period_backup = error_log_errors_per_period;
  361. setenv("NETDATA_ERRORS_THROTTLE_PERIOD", config_get(CONFIG_SECTION_LOGS, "errors flood protection period" , ""), 1);
  362. setenv("NETDATA_ERRORS_PER_PERIOD", config_get(CONFIG_SECTION_LOGS, "errors to trigger flood protection", ""), 1);
  363. }
  364. char *initialize_lock_directory_path(char *prefix)
  365. {
  366. char filename[FILENAME_MAX + 1];
  367. snprintfz(filename, FILENAME_MAX, "%s/lock", prefix);
  368. return config_get(CONFIG_SECTION_DIRECTORIES, "lock", filename);
  369. }
  370. static void backwards_compatible_config() {
  371. // move [global] options to the [web] section
  372. config_move(CONFIG_SECTION_GLOBAL, "http port listen backlog",
  373. CONFIG_SECTION_WEB, "listen backlog");
  374. config_move(CONFIG_SECTION_GLOBAL, "bind socket to IP",
  375. CONFIG_SECTION_WEB, "bind to");
  376. config_move(CONFIG_SECTION_GLOBAL, "bind to",
  377. CONFIG_SECTION_WEB, "bind to");
  378. config_move(CONFIG_SECTION_GLOBAL, "port",
  379. CONFIG_SECTION_WEB, "default port");
  380. config_move(CONFIG_SECTION_GLOBAL, "default port",
  381. CONFIG_SECTION_WEB, "default port");
  382. config_move(CONFIG_SECTION_GLOBAL, "disconnect idle web clients after seconds",
  383. CONFIG_SECTION_WEB, "disconnect idle clients after seconds");
  384. config_move(CONFIG_SECTION_GLOBAL, "respect web browser do not track policy",
  385. CONFIG_SECTION_WEB, "respect do not track policy");
  386. config_move(CONFIG_SECTION_GLOBAL, "web x-frame-options header",
  387. CONFIG_SECTION_WEB, "x-frame-options response header");
  388. config_move(CONFIG_SECTION_GLOBAL, "enable web responses gzip compression",
  389. CONFIG_SECTION_WEB, "enable gzip compression");
  390. config_move(CONFIG_SECTION_GLOBAL, "web compression strategy",
  391. CONFIG_SECTION_WEB, "gzip compression strategy");
  392. config_move(CONFIG_SECTION_GLOBAL, "web compression level",
  393. CONFIG_SECTION_WEB, "gzip compression level");
  394. config_move(CONFIG_SECTION_GLOBAL, "config directory",
  395. CONFIG_SECTION_DIRECTORIES, "config");
  396. config_move(CONFIG_SECTION_GLOBAL, "stock config directory",
  397. CONFIG_SECTION_DIRECTORIES, "stock config");
  398. config_move(CONFIG_SECTION_GLOBAL, "log directory",
  399. CONFIG_SECTION_DIRECTORIES, "log");
  400. config_move(CONFIG_SECTION_GLOBAL, "web files directory",
  401. CONFIG_SECTION_DIRECTORIES, "web");
  402. config_move(CONFIG_SECTION_GLOBAL, "cache directory",
  403. CONFIG_SECTION_DIRECTORIES, "cache");
  404. config_move(CONFIG_SECTION_GLOBAL, "lib directory",
  405. CONFIG_SECTION_DIRECTORIES, "lib");
  406. config_move(CONFIG_SECTION_GLOBAL, "home directory",
  407. CONFIG_SECTION_DIRECTORIES, "home");
  408. config_move(CONFIG_SECTION_GLOBAL, "lock directory",
  409. CONFIG_SECTION_DIRECTORIES, "lock");
  410. config_move(CONFIG_SECTION_GLOBAL, "plugins directory",
  411. CONFIG_SECTION_DIRECTORIES, "plugins");
  412. config_move(CONFIG_SECTION_HEALTH, "health configuration directory",
  413. CONFIG_SECTION_DIRECTORIES, "health config");
  414. config_move(CONFIG_SECTION_HEALTH, "stock health configuration directory",
  415. CONFIG_SECTION_DIRECTORIES, "stock health config");
  416. config_move(CONFIG_SECTION_REGISTRY, "registry db directory",
  417. CONFIG_SECTION_DIRECTORIES, "registry");
  418. config_move(CONFIG_SECTION_GLOBAL, "debug log",
  419. CONFIG_SECTION_LOGS, "debug");
  420. config_move(CONFIG_SECTION_GLOBAL, "error log",
  421. CONFIG_SECTION_LOGS, "error");
  422. config_move(CONFIG_SECTION_GLOBAL, "access log",
  423. CONFIG_SECTION_LOGS, "access");
  424. config_move(CONFIG_SECTION_GLOBAL, "facility log",
  425. CONFIG_SECTION_LOGS, "facility");
  426. config_move(CONFIG_SECTION_GLOBAL, "errors flood protection period",
  427. CONFIG_SECTION_LOGS, "errors flood protection period");
  428. config_move(CONFIG_SECTION_GLOBAL, "errors to trigger flood protection",
  429. CONFIG_SECTION_LOGS, "errors to trigger flood protection");
  430. config_move(CONFIG_SECTION_GLOBAL, "debug flags",
  431. CONFIG_SECTION_LOGS, "debug flags");
  432. config_move(CONFIG_SECTION_GLOBAL, "TZ environment variable",
  433. CONFIG_SECTION_ENV_VARS, "TZ");
  434. config_move(CONFIG_SECTION_PLUGINS, "PATH environment variable",
  435. CONFIG_SECTION_ENV_VARS, "PATH");
  436. config_move(CONFIG_SECTION_PLUGINS, "PYTHONPATH environment variable",
  437. CONFIG_SECTION_ENV_VARS, "PYTHONPATH");
  438. config_move(CONFIG_SECTION_STATSD, "enabled",
  439. CONFIG_SECTION_PLUGINS, "statsd");
  440. config_move(CONFIG_SECTION_GLOBAL, "memory mode",
  441. CONFIG_SECTION_DB, "mode");
  442. config_move(CONFIG_SECTION_GLOBAL, "history",
  443. CONFIG_SECTION_DB, "retention");
  444. config_move(CONFIG_SECTION_GLOBAL, "update every",
  445. CONFIG_SECTION_DB, "update every");
  446. config_move(CONFIG_SECTION_GLOBAL, "page cache size",
  447. CONFIG_SECTION_DB, "dbengine page cache size MB");
  448. config_move(CONFIG_SECTION_DB, "page cache size",
  449. CONFIG_SECTION_DB, "dbengine page cache size MB");
  450. config_move(CONFIG_SECTION_GLOBAL, "page cache uses malloc",
  451. CONFIG_SECTION_DB, "dbengine page cache with malloc");
  452. config_move(CONFIG_SECTION_DB, "page cache with malloc",
  453. CONFIG_SECTION_DB, "dbengine page cache with malloc");
  454. config_move(CONFIG_SECTION_GLOBAL, "dbengine disk space",
  455. CONFIG_SECTION_DB, "dbengine disk space MB");
  456. config_move(CONFIG_SECTION_GLOBAL, "dbengine multihost disk space",
  457. CONFIG_SECTION_DB, "dbengine multihost disk space MB");
  458. config_move(CONFIG_SECTION_GLOBAL, "memory deduplication (ksm)",
  459. CONFIG_SECTION_DB, "memory deduplication (ksm)");
  460. config_move(CONFIG_SECTION_GLOBAL, "dbengine page fetch timeout",
  461. CONFIG_SECTION_DB, "dbengine page fetch timeout secs");
  462. config_move(CONFIG_SECTION_GLOBAL, "dbengine page fetch retries",
  463. CONFIG_SECTION_DB, "dbengine page fetch retries");
  464. config_move(CONFIG_SECTION_GLOBAL, "dbengine extent pages",
  465. CONFIG_SECTION_DB, "dbengine pages per extent");
  466. config_move(CONFIG_SECTION_GLOBAL, "cleanup obsolete charts after seconds",
  467. CONFIG_SECTION_DB, "cleanup obsolete charts after secs");
  468. config_move(CONFIG_SECTION_GLOBAL, "gap when lost iterations above",
  469. CONFIG_SECTION_DB, "gap when lost iterations above");
  470. config_move(CONFIG_SECTION_GLOBAL, "cleanup orphan hosts after seconds",
  471. CONFIG_SECTION_DB, "cleanup orphan hosts after secs");
  472. config_move(CONFIG_SECTION_GLOBAL, "delete obsolete charts files",
  473. CONFIG_SECTION_DB, "delete obsolete charts files");
  474. config_move(CONFIG_SECTION_GLOBAL, "delete orphan hosts files",
  475. CONFIG_SECTION_DB, "delete orphan hosts files");
  476. config_move(CONFIG_SECTION_GLOBAL, "enable zero metrics",
  477. CONFIG_SECTION_DB, "enable zero metrics");
  478. }
  479. static void get_netdata_configured_variables() {
  480. backwards_compatible_config();
  481. // ------------------------------------------------------------------------
  482. // get the hostname
  483. char buf[HOSTNAME_MAX + 1];
  484. if(gethostname(buf, HOSTNAME_MAX) == -1){
  485. error("Cannot get machine hostname.");
  486. }
  487. netdata_configured_hostname = config_get(CONFIG_SECTION_GLOBAL, "hostname", buf);
  488. debug(D_OPTIONS, "hostname set to '%s'", netdata_configured_hostname);
  489. // ------------------------------------------------------------------------
  490. // get default database update frequency
  491. default_rrd_update_every = (int) config_get_number(CONFIG_SECTION_DB, "update every", UPDATE_EVERY);
  492. if(default_rrd_update_every < 1 || default_rrd_update_every > 600) {
  493. error("Invalid data collection frequency (update every) %d given. Defaulting to %d.", default_rrd_update_every, UPDATE_EVERY);
  494. default_rrd_update_every = UPDATE_EVERY;
  495. config_set_number(CONFIG_SECTION_DB, "update every", default_rrd_update_every);
  496. }
  497. // ------------------------------------------------------------------------
  498. // get default memory mode for the database
  499. {
  500. const char *mode = config_get(CONFIG_SECTION_DB, "mode", rrd_memory_mode_name(default_rrd_memory_mode));
  501. default_rrd_memory_mode = rrd_memory_mode_id(mode);
  502. if(strcmp(mode, rrd_memory_mode_name(default_rrd_memory_mode)) != 0) {
  503. error("Invalid memory mode '%s' given. Using '%s'", mode, rrd_memory_mode_name(default_rrd_memory_mode));
  504. config_set(CONFIG_SECTION_DB, "mode", rrd_memory_mode_name(default_rrd_memory_mode));
  505. }
  506. }
  507. // ------------------------------------------------------------------------
  508. // get default database size
  509. if(default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE && default_rrd_memory_mode != RRD_MEMORY_MODE_NONE) {
  510. default_rrd_history_entries = (int)config_get_number(
  511. CONFIG_SECTION_DB, "retention",
  512. align_entries_to_pagesize(default_rrd_memory_mode, RRD_DEFAULT_HISTORY_ENTRIES));
  513. long h = align_entries_to_pagesize(default_rrd_memory_mode, default_rrd_history_entries);
  514. if (h != default_rrd_history_entries) {
  515. config_set_number(CONFIG_SECTION_DB, "retention", h);
  516. default_rrd_history_entries = (int)h;
  517. }
  518. }
  519. // ------------------------------------------------------------------------
  520. // get system paths
  521. netdata_configured_user_config_dir = config_get(CONFIG_SECTION_DIRECTORIES, "config", netdata_configured_user_config_dir);
  522. netdata_configured_stock_config_dir = config_get(CONFIG_SECTION_DIRECTORIES, "stock config", netdata_configured_stock_config_dir);
  523. netdata_configured_log_dir = config_get(CONFIG_SECTION_DIRECTORIES, "log", netdata_configured_log_dir);
  524. netdata_configured_web_dir = config_get(CONFIG_SECTION_DIRECTORIES, "web", netdata_configured_web_dir);
  525. netdata_configured_cache_dir = config_get(CONFIG_SECTION_DIRECTORIES, "cache", netdata_configured_cache_dir);
  526. netdata_configured_varlib_dir = config_get(CONFIG_SECTION_DIRECTORIES, "lib", netdata_configured_varlib_dir);
  527. char *env_home=getenv("HOME");
  528. netdata_configured_home_dir = config_get(CONFIG_SECTION_DIRECTORIES, "home", env_home?env_home:netdata_configured_home_dir);
  529. netdata_configured_lock_dir = initialize_lock_directory_path(netdata_configured_varlib_dir);
  530. {
  531. pluginsd_initialize_plugin_directories();
  532. netdata_configured_primary_plugins_dir = plugin_directories[PLUGINSD_STOCK_PLUGINS_DIRECTORY_PATH];
  533. }
  534. #ifdef ENABLE_DBENGINE
  535. // ------------------------------------------------------------------------
  536. // get default Database Engine page cache size in MiB
  537. db_engine_use_malloc = config_get_boolean(CONFIG_SECTION_DB, "dbengine page cache with malloc", CONFIG_BOOLEAN_NO);
  538. default_rrdeng_page_cache_mb = (int) config_get_number(CONFIG_SECTION_DB, "dbengine page cache size MB", default_rrdeng_page_cache_mb);
  539. if(default_rrdeng_page_cache_mb < RRDENG_MIN_PAGE_CACHE_SIZE_MB) {
  540. error("Invalid page cache size %d given. Defaulting to %d.", default_rrdeng_page_cache_mb, RRDENG_MIN_PAGE_CACHE_SIZE_MB);
  541. default_rrdeng_page_cache_mb = RRDENG_MIN_PAGE_CACHE_SIZE_MB;
  542. config_set_number(CONFIG_SECTION_DB, "dbengine page cache size MB", default_rrdeng_page_cache_mb);
  543. }
  544. // ------------------------------------------------------------------------
  545. // get default Database Engine disk space quota in MiB
  546. default_rrdeng_disk_quota_mb = (int) config_get_number(CONFIG_SECTION_DB, "dbengine disk space MB", default_rrdeng_disk_quota_mb);
  547. if(default_rrdeng_disk_quota_mb < RRDENG_MIN_DISK_SPACE_MB) {
  548. error("Invalid dbengine disk space %d given. Defaulting to %d.", default_rrdeng_disk_quota_mb, RRDENG_MIN_DISK_SPACE_MB);
  549. default_rrdeng_disk_quota_mb = RRDENG_MIN_DISK_SPACE_MB;
  550. config_set_number(CONFIG_SECTION_DB, "dbengine disk space MB", default_rrdeng_disk_quota_mb);
  551. }
  552. default_multidb_disk_quota_mb = (int) config_get_number(CONFIG_SECTION_DB, "dbengine multihost disk space MB", compute_multidb_diskspace());
  553. if(default_multidb_disk_quota_mb < RRDENG_MIN_DISK_SPACE_MB) {
  554. error("Invalid multidb disk space %d given. Defaulting to %d.", default_multidb_disk_quota_mb, default_rrdeng_disk_quota_mb);
  555. default_multidb_disk_quota_mb = default_rrdeng_disk_quota_mb;
  556. config_set_number(CONFIG_SECTION_DB, "dbengine multihost disk space MB", default_multidb_disk_quota_mb);
  557. }
  558. #else
  559. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  560. error_report("RRD_MEMORY_MODE_DBENGINE is not supported in this platform. The agent will use db mode 'save' instead.");
  561. default_rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
  562. }
  563. #endif
  564. // ------------------------------------------------------------------------
  565. netdata_configured_host_prefix = config_get(CONFIG_SECTION_GLOBAL, "host access prefix", "");
  566. verify_netdata_host_prefix();
  567. // --------------------------------------------------------------------
  568. // get KSM settings
  569. #ifdef MADV_MERGEABLE
  570. enable_ksm = config_get_boolean(CONFIG_SECTION_DB, "memory deduplication (ksm)", enable_ksm);
  571. #endif
  572. // --------------------------------------------------------------------
  573. // metric correlations
  574. enable_metric_correlations = config_get_boolean(CONFIG_SECTION_GLOBAL, "enable metric correlations", enable_metric_correlations);
  575. default_metric_correlations_method = weights_string_to_method(config_get(
  576. CONFIG_SECTION_GLOBAL, "metric correlations method",
  577. weights_method_to_string(default_metric_correlations_method)));
  578. // --------------------------------------------------------------------
  579. rrdset_free_obsolete_time = config_get_number(CONFIG_SECTION_DB, "cleanup obsolete charts after secs", rrdset_free_obsolete_time);
  580. // Current chart locking and invalidation scheme doesn't prevent Netdata from segmentation faults if a short
  581. // cleanup delay is set. Extensive stress tests showed that 10 seconds is quite a safe delay. Look at
  582. // https://github.com/netdata/netdata/pull/11222#issuecomment-868367920 for more information.
  583. if (rrdset_free_obsolete_time < 10) {
  584. rrdset_free_obsolete_time = 10;
  585. info("The \"cleanup obsolete charts after seconds\" option was set to 10 seconds.");
  586. config_set_number(CONFIG_SECTION_DB, "cleanup obsolete charts after secs", rrdset_free_obsolete_time);
  587. }
  588. gap_when_lost_iterations_above = (int)config_get_number(CONFIG_SECTION_DB, "gap when lost iterations above", gap_when_lost_iterations_above);
  589. if (gap_when_lost_iterations_above < 1) {
  590. gap_when_lost_iterations_above = 1;
  591. config_set_number(CONFIG_SECTION_DB, "gap when lost iterations above", gap_when_lost_iterations_above);
  592. }
  593. // --------------------------------------------------------------------
  594. // rrdcontext
  595. rrdcontext_enabled = config_get_boolean(CONFIG_SECTION_CLOUD, "rrdcontexts", rrdcontext_enabled);
  596. // --------------------------------------------------------------------
  597. // get various system parameters
  598. get_system_HZ();
  599. get_system_cpus();
  600. get_system_pid_max();
  601. }
  602. int load_netdata_conf(char *filename, char overwrite_used) {
  603. errno = 0;
  604. int ret = 0;
  605. if(filename && *filename) {
  606. ret = config_load(filename, overwrite_used, NULL);
  607. if(!ret)
  608. error("CONFIG: cannot load config file '%s'.", filename);
  609. }
  610. else {
  611. filename = strdupz_path_subpath(netdata_configured_user_config_dir, "netdata.conf");
  612. ret = config_load(filename, overwrite_used, NULL);
  613. if(!ret) {
  614. info("CONFIG: cannot load user config '%s'. Will try the stock version.", filename);
  615. freez(filename);
  616. filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "netdata.conf");
  617. ret = config_load(filename, overwrite_used, NULL);
  618. if(!ret)
  619. info("CONFIG: cannot load stock config '%s'. Running with internal defaults.", filename);
  620. }
  621. freez(filename);
  622. }
  623. return ret;
  624. }
  625. // coverity[ +tainted_string_sanitize_content : arg-0 ]
  626. static inline void coverity_remove_taint(char *s)
  627. {
  628. (void)s;
  629. }
  630. int get_system_info(struct rrdhost_system_info *system_info) {
  631. char *script;
  632. script = mallocz(sizeof(char) * (strlen(netdata_configured_primary_plugins_dir) + strlen("system-info.sh") + 2));
  633. sprintf(script, "%s/%s", netdata_configured_primary_plugins_dir, "system-info.sh");
  634. if (unlikely(access(script, R_OK) != 0)) {
  635. info("System info script %s not found.",script);
  636. freez(script);
  637. return 1;
  638. }
  639. pid_t command_pid;
  640. info("Executing %s", script);
  641. FILE *fp = mypopen(script, &command_pid);
  642. if(fp) {
  643. char line[200 + 1];
  644. // Removed the double strlens, if the Coverity tainted string warning reappears I'll revert.
  645. // One time init code, but I'm curious about the warning...
  646. while (fgets(line, 200, fp) != NULL) {
  647. char *value=line;
  648. while (*value && *value != '=') value++;
  649. if (*value=='=') {
  650. *value='\0';
  651. value++;
  652. char *end = value;
  653. while (*end && *end != '\n') end++;
  654. *end = '\0'; // Overwrite newline if present
  655. coverity_remove_taint(line); // I/O is controlled result of system_info.sh - not tainted
  656. coverity_remove_taint(value);
  657. if(unlikely(rrdhost_set_system_info_variable(system_info, line, value))) {
  658. info("Unexpected environment variable %s=%s", line, value);
  659. }
  660. else {
  661. info("%s=%s", line, value);
  662. setenv(line, value, 1);
  663. }
  664. }
  665. }
  666. mypclose(fp, command_pid);
  667. }
  668. freez(script);
  669. return 0;
  670. }
  671. void set_silencers_filename() {
  672. char filename[FILENAME_MAX + 1];
  673. snprintfz(filename, FILENAME_MAX, "%s/health.silencers.json", netdata_configured_varlib_dir);
  674. silencers_filename = config_get(CONFIG_SECTION_HEALTH, "silencers file", filename);
  675. }
  676. /* Any config setting that can be accessed without a default value i.e. configget(...,...,NULL) *MUST*
  677. be set in this procedure to be called in all the relevant code paths.
  678. */
  679. void post_conf_load(char **user)
  680. {
  681. // --------------------------------------------------------------------
  682. // get the user we should run
  683. // IMPORTANT: this is required before web_files_uid()
  684. if(getuid() == 0) {
  685. *user = config_get(CONFIG_SECTION_GLOBAL, "run as user", NETDATA_USER);
  686. }
  687. else {
  688. struct passwd *passwd = getpwuid(getuid());
  689. *user = config_get(CONFIG_SECTION_GLOBAL, "run as user", (passwd && passwd->pw_name)?passwd->pw_name:"");
  690. }
  691. // --------------------------------------------------------------------
  692. // Check if the cloud is enabled
  693. #if defined( DISABLE_CLOUD ) || !defined( ENABLE_ACLK )
  694. netdata_cloud_setting = 0;
  695. #else
  696. netdata_cloud_setting = appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "enabled", 1);
  697. #endif
  698. // This must be set before any point in the code that accesses it. Do not move it from this function.
  699. appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "cloud base url", DEFAULT_CLOUD_BASE_URL);
  700. }
  701. int main(int argc, char **argv) {
  702. int i;
  703. int config_loaded = 0;
  704. int dont_fork = 0;
  705. bool close_open_fds = true;
  706. size_t default_stacksize;
  707. char *user = NULL;
  708. static_threads = static_threads_get();
  709. netdata_ready=0;
  710. // set the name for logging
  711. program_name = "netdata";
  712. if (argc > 1 && strcmp(argv[1], SPAWN_SERVER_COMMAND_LINE_ARGUMENT) == 0) {
  713. // don't run netdata, this is the spawn server
  714. spawn_server();
  715. exit(0);
  716. }
  717. // parse options
  718. {
  719. int num_opts = sizeof(option_definitions) / sizeof(struct option_def);
  720. char optstring[(num_opts * 2) + 1];
  721. int string_i = 0;
  722. for( i = 0; i < num_opts; i++ ) {
  723. optstring[string_i] = option_definitions[i].val;
  724. string_i++;
  725. if(option_definitions[i].arg_name) {
  726. optstring[string_i] = ':';
  727. string_i++;
  728. }
  729. }
  730. // terminate optstring
  731. optstring[string_i] ='\0';
  732. optstring[(num_opts *2)] ='\0';
  733. int opt;
  734. while( (opt = getopt(argc, argv, optstring)) != -1 ) {
  735. switch(opt) {
  736. case 'c':
  737. if(load_netdata_conf(optarg, 1) != 1) {
  738. error("Cannot load configuration file %s.", optarg);
  739. return 1;
  740. }
  741. else {
  742. debug(D_OPTIONS, "Configuration loaded from %s.", optarg);
  743. post_conf_load(&user);
  744. load_cloud_conf(1);
  745. config_loaded = 1;
  746. }
  747. break;
  748. case 'D':
  749. dont_fork = 1;
  750. break;
  751. case 'd':
  752. dont_fork = 0;
  753. break;
  754. case 'h':
  755. return help(0);
  756. case 'i':
  757. config_set(CONFIG_SECTION_WEB, "bind to", optarg);
  758. break;
  759. case 'P':
  760. strncpy(pidfile, optarg, FILENAME_MAX);
  761. pidfile[FILENAME_MAX] = '\0';
  762. break;
  763. case 'p':
  764. config_set(CONFIG_SECTION_GLOBAL, "default port", optarg);
  765. break;
  766. case 's':
  767. config_set(CONFIG_SECTION_GLOBAL, "host access prefix", optarg);
  768. break;
  769. case 't':
  770. config_set(CONFIG_SECTION_GLOBAL, "update every", optarg);
  771. break;
  772. case 'u':
  773. config_set(CONFIG_SECTION_GLOBAL, "run as user", optarg);
  774. break;
  775. case 'v':
  776. case 'V':
  777. printf("%s %s\n", program_name, program_version);
  778. return 0;
  779. case 'W':
  780. {
  781. char* stacksize_string = "stacksize=";
  782. char* debug_flags_string = "debug_flags=";
  783. char* claim_string = "claim";
  784. #ifdef ENABLE_DBENGINE
  785. char* createdataset_string = "createdataset=";
  786. char* stresstest_string = "stresstest=";
  787. #endif
  788. if(strcmp(optarg, "sqlite-check") == 0) {
  789. sql_init_database(DB_CHECK_INTEGRITY, 0);
  790. return 0;
  791. }
  792. if(strcmp(optarg, "sqlite-fix") == 0) {
  793. sql_init_database(DB_CHECK_FIX_DB, 0);
  794. return 0;
  795. }
  796. if(strcmp(optarg, "sqlite-compact") == 0) {
  797. sql_init_database(DB_CHECK_RECLAIM_SPACE, 0);
  798. return 0;
  799. }
  800. if(strcmp(optarg, "unittest") == 0) {
  801. if (unit_test_static_threads())
  802. return 1;
  803. if (unit_test_buffer())
  804. return 1;
  805. if (unit_test_str2ld())
  806. return 1;
  807. if (unit_test_bitmap256())
  808. return 1;
  809. // No call to load the config file on this code-path
  810. post_conf_load(&user);
  811. get_netdata_configured_variables();
  812. rrdcontext_enabled = CONFIG_BOOLEAN_NO;
  813. default_rrd_update_every = 1;
  814. default_rrd_memory_mode = RRD_MEMORY_MODE_RAM;
  815. default_health_enabled = 0;
  816. storage_tiers = 1;
  817. registry_init();
  818. if(rrd_init("unittest", NULL)) {
  819. fprintf(stderr, "rrd_init failed for unittest\n");
  820. return 1;
  821. }
  822. default_rrdpush_enabled = 0;
  823. if(run_all_mockup_tests()) return 1;
  824. if(unit_test_storage()) return 1;
  825. #ifdef ENABLE_DBENGINE
  826. if(test_dbengine()) return 1;
  827. #endif
  828. if(test_sqlite()) return 1;
  829. if (dictionary_unittest(10000))
  830. return 1;
  831. if (rrdlabels_unittest())
  832. return 1;
  833. if (ctx_unittest())
  834. return 1;
  835. fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
  836. return 0;
  837. }
  838. #ifdef ENABLE_ML_TESTS
  839. else if(strcmp(optarg, "mltest") == 0) {
  840. return test_ml(argc, argv);
  841. }
  842. #endif
  843. #ifdef ENABLE_DBENGINE
  844. else if(strcmp(optarg, "mctest") == 0) {
  845. return mc_unittest();
  846. }
  847. else if(strcmp(optarg, "ctxtest") == 0) {
  848. return ctx_unittest();
  849. }
  850. else if(strcmp(optarg, "dicttest") == 0) {
  851. return dictionary_unittest(10000);
  852. }
  853. else if(strcmp(optarg, "rrdlabelstest") == 0) {
  854. return rrdlabels_unittest();
  855. }
  856. else if(strncmp(optarg, createdataset_string, strlen(createdataset_string)) == 0) {
  857. optarg += strlen(createdataset_string);
  858. unsigned history_seconds = strtoul(optarg, NULL, 0);
  859. generate_dbengine_dataset(history_seconds);
  860. return 0;
  861. }
  862. else if(strncmp(optarg, stresstest_string, strlen(stresstest_string)) == 0) {
  863. char *endptr;
  864. unsigned test_duration_sec = 0, dset_charts = 0, query_threads = 0, ramp_up_seconds = 0,
  865. page_cache_mb = 0, disk_space_mb = 0, workers = 16;
  866. optarg += strlen(stresstest_string);
  867. test_duration_sec = (unsigned)strtoul(optarg, &endptr, 0);
  868. if (',' == *endptr)
  869. dset_charts = (unsigned)strtoul(endptr + 1, &endptr, 0);
  870. if (',' == *endptr)
  871. query_threads = (unsigned)strtoul(endptr + 1, &endptr, 0);
  872. if (',' == *endptr)
  873. ramp_up_seconds = (unsigned)strtoul(endptr + 1, &endptr, 0);
  874. if (',' == *endptr)
  875. page_cache_mb = (unsigned)strtoul(endptr + 1, &endptr, 0);
  876. if (',' == *endptr)
  877. disk_space_mb = (unsigned)strtoul(endptr + 1, &endptr, 0);
  878. if (',' == *endptr)
  879. workers = (unsigned)strtoul(endptr + 1, &endptr, 0);
  880. if (workers > 1024)
  881. workers = 1024;
  882. char workers_str[16];
  883. snprintf(workers_str, 15, "%u", workers);
  884. setenv("UV_THREADPOOL_SIZE", workers_str, 1);
  885. dbengine_stress_test(test_duration_sec, dset_charts, query_threads, ramp_up_seconds,
  886. page_cache_mb, disk_space_mb);
  887. return 0;
  888. }
  889. #endif
  890. else if(strcmp(optarg, "simple-pattern") == 0) {
  891. if(optind + 2 > argc) {
  892. fprintf(stderr, "%s", "\nUSAGE: -W simple-pattern 'pattern' 'string'\n\n"
  893. " Checks if 'pattern' matches the given 'string'.\n"
  894. " - 'pattern' can be one or more space separated words.\n"
  895. " - each 'word' can contain one or more asterisks.\n"
  896. " - words starting with '!' give negative matches.\n"
  897. " - words are processed left to right\n"
  898. "\n"
  899. "Examples:\n"
  900. "\n"
  901. " > match all veth interfaces, except veth0:\n"
  902. "\n"
  903. " -W simple-pattern '!veth0 veth*' 'veth12'\n"
  904. "\n"
  905. "\n"
  906. " > match all *.ext files directly in /path/:\n"
  907. " (this will not match *.ext files in a subdir of /path/)\n"
  908. "\n"
  909. " -W simple-pattern '!/path/*/*.ext /path/*.ext' '/path/test.ext'\n"
  910. "\n"
  911. );
  912. return 1;
  913. }
  914. const char *haystack = argv[optind];
  915. const char *needle = argv[optind + 1];
  916. size_t len = strlen(needle) + 1;
  917. char wildcarded[len];
  918. SIMPLE_PATTERN *p = simple_pattern_create(haystack, NULL, SIMPLE_PATTERN_EXACT);
  919. int ret = simple_pattern_matches_extract(p, needle, wildcarded, len);
  920. simple_pattern_free(p);
  921. if(ret) {
  922. fprintf(stdout, "RESULT: MATCHED - pattern '%s' matches '%s', wildcarded '%s'\n", haystack, needle, wildcarded);
  923. return 0;
  924. }
  925. else {
  926. fprintf(stdout, "RESULT: NOT MATCHED - pattern '%s' does not match '%s', wildcarded '%s'\n", haystack, needle, wildcarded);
  927. return 1;
  928. }
  929. }
  930. else if(strncmp(optarg, stacksize_string, strlen(stacksize_string)) == 0) {
  931. optarg += strlen(stacksize_string);
  932. config_set(CONFIG_SECTION_GLOBAL, "pthread stack size", optarg);
  933. }
  934. else if(strncmp(optarg, debug_flags_string, strlen(debug_flags_string)) == 0) {
  935. optarg += strlen(debug_flags_string);
  936. config_set(CONFIG_SECTION_LOGS, "debug flags", optarg);
  937. debug_flags = strtoull(optarg, NULL, 0);
  938. }
  939. else if(strcmp(optarg, "set") == 0) {
  940. if(optind + 3 > argc) {
  941. fprintf(stderr, "%s", "\nUSAGE: -W set 'section' 'key' 'value'\n\n"
  942. " Overwrites settings of netdata.conf.\n"
  943. "\n"
  944. " These options interact with: -c netdata.conf\n"
  945. " If -c netdata.conf is given on the command line,\n"
  946. " before -W set... the user may overwrite command\n"
  947. " line parameters at netdata.conf\n"
  948. " If -c netdata.conf is given after (or missing)\n"
  949. " -W set... the user cannot overwrite the command line\n"
  950. " parameters."
  951. "\n"
  952. );
  953. return 1;
  954. }
  955. const char *section = argv[optind];
  956. const char *key = argv[optind + 1];
  957. const char *value = argv[optind + 2];
  958. optind += 3;
  959. // set this one as the default
  960. // only if it is not already set in the config file
  961. // so the caller can use -c netdata.conf before or
  962. // after this parameter to prevent or allow overwriting
  963. // variables at netdata.conf
  964. config_set_default(section, key, value);
  965. // fprintf(stderr, "SET section '%s', key '%s', value '%s'\n", section, key, value);
  966. }
  967. else if(strcmp(optarg, "set2") == 0) {
  968. if(optind + 4 > argc) {
  969. fprintf(stderr, "%s", "\nUSAGE: -W set 'conf_file' 'section' 'key' 'value'\n\n"
  970. " Overwrites settings of netdata.conf or cloud.conf\n"
  971. "\n"
  972. " These options interact with: -c netdata.conf\n"
  973. " If -c netdata.conf is given on the command line,\n"
  974. " before -W set... the user may overwrite command\n"
  975. " line parameters at netdata.conf\n"
  976. " If -c netdata.conf is given after (or missing)\n"
  977. " -W set... the user cannot overwrite the command line\n"
  978. " parameters."
  979. " conf_file can be \"cloud\" or \"netdata\".\n"
  980. "\n"
  981. );
  982. return 1;
  983. }
  984. const char *conf_file = argv[optind]; /* "cloud" is cloud.conf, otherwise netdata.conf */
  985. struct config *tmp_config = strcmp(conf_file, "cloud") ? &netdata_config : &cloud_config;
  986. const char *section = argv[optind + 1];
  987. const char *key = argv[optind + 2];
  988. const char *value = argv[optind + 3];
  989. optind += 4;
  990. // set this one as the default
  991. // only if it is not already set in the config file
  992. // so the caller can use -c netdata.conf before or
  993. // after this parameter to prevent or allow overwriting
  994. // variables at netdata.conf
  995. appconfig_set_default(tmp_config, section, key, value);
  996. // fprintf(stderr, "SET section '%s', key '%s', value '%s'\n", section, key, value);
  997. }
  998. else if(strcmp(optarg, "get") == 0) {
  999. if(optind + 3 > argc) {
  1000. fprintf(stderr, "%s", "\nUSAGE: -W get 'section' 'key' 'value'\n\n"
  1001. " Prints settings of netdata.conf.\n"
  1002. "\n"
  1003. " These options interact with: -c netdata.conf\n"
  1004. " -c netdata.conf has to be given before -W get.\n"
  1005. "\n"
  1006. );
  1007. return 1;
  1008. }
  1009. if(!config_loaded) {
  1010. fprintf(stderr, "warning: no configuration file has been loaded. Use -c CONFIG_FILE, before -W get. Using default config.\n");
  1011. load_netdata_conf(NULL, 0);
  1012. post_conf_load(&user);
  1013. }
  1014. get_netdata_configured_variables();
  1015. const char *section = argv[optind];
  1016. const char *key = argv[optind + 1];
  1017. const char *def = argv[optind + 2];
  1018. const char *value = config_get(section, key, def);
  1019. printf("%s\n", value);
  1020. return 0;
  1021. }
  1022. else if(strcmp(optarg, "get2") == 0) {
  1023. if(optind + 4 > argc) {
  1024. fprintf(stderr, "%s", "\nUSAGE: -W get2 'conf_file' 'section' 'key' 'value'\n\n"
  1025. " Prints settings of netdata.conf or cloud.conf\n"
  1026. "\n"
  1027. " These options interact with: -c netdata.conf\n"
  1028. " -c netdata.conf has to be given before -W get2.\n"
  1029. " conf_file can be \"cloud\" or \"netdata\".\n"
  1030. "\n"
  1031. );
  1032. return 1;
  1033. }
  1034. if(!config_loaded) {
  1035. fprintf(stderr, "warning: no configuration file has been loaded. Use -c CONFIG_FILE, before -W get. Using default config.\n");
  1036. load_netdata_conf(NULL, 0);
  1037. post_conf_load(&user);
  1038. load_cloud_conf(1);
  1039. }
  1040. get_netdata_configured_variables();
  1041. const char *conf_file = argv[optind]; /* "cloud" is cloud.conf, otherwise netdata.conf */
  1042. struct config *tmp_config = strcmp(conf_file, "cloud") ? &netdata_config : &cloud_config;
  1043. const char *section = argv[optind + 1];
  1044. const char *key = argv[optind + 2];
  1045. const char *def = argv[optind + 3];
  1046. const char *value = appconfig_get(tmp_config, section, key, def);
  1047. printf("%s\n", value);
  1048. return 0;
  1049. }
  1050. else if(strncmp(optarg, claim_string, strlen(claim_string)) == 0) {
  1051. /* will trigger a claiming attempt when the agent is initialized */
  1052. claiming_pending_arguments = optarg + strlen(claim_string);
  1053. }
  1054. else if(strcmp(optarg, "buildinfo") == 0) {
  1055. printf("Version: %s %s\n", program_name, program_version);
  1056. print_build_info();
  1057. return 0;
  1058. }
  1059. else if(strcmp(optarg, "buildinfojson") == 0) {
  1060. print_build_info_json();
  1061. return 0;
  1062. }
  1063. else if(strcmp(optarg, "keepopenfds") == 0) {
  1064. // Internal dev option to skip closing inherited
  1065. // open FDs. Useful, when we want to run the agent
  1066. // under profiling tools that open/maintain their
  1067. // own FDs.
  1068. close_open_fds = false;
  1069. } else {
  1070. fprintf(stderr, "Unknown -W parameter '%s'\n", optarg);
  1071. return help(1);
  1072. }
  1073. }
  1074. break;
  1075. default: /* ? */
  1076. fprintf(stderr, "Unknown parameter '%c'\n", opt);
  1077. return help(1);
  1078. }
  1079. }
  1080. }
  1081. #ifdef _SC_OPEN_MAX
  1082. if (close_open_fds == true) {
  1083. // close all open file descriptors, except the standard ones
  1084. // the caller may have left open files (lxc-attach has this issue)
  1085. for(int fd = (int) (sysconf(_SC_OPEN_MAX) - 1); fd > 2; fd--)
  1086. if(fd_is_valid(fd))
  1087. close(fd);
  1088. }
  1089. #endif
  1090. if(!config_loaded)
  1091. {
  1092. load_netdata_conf(NULL, 0);
  1093. post_conf_load(&user);
  1094. load_cloud_conf(0);
  1095. }
  1096. // ------------------------------------------------------------------------
  1097. // initialize netdata
  1098. {
  1099. char *pmax = config_get(CONFIG_SECTION_GLOBAL, "glibc malloc arena max for plugins", "1");
  1100. if(pmax && *pmax)
  1101. setenv("MALLOC_ARENA_MAX", pmax, 1);
  1102. #if defined(HAVE_C_MALLOPT)
  1103. i = (int)config_get_number(CONFIG_SECTION_GLOBAL, "glibc malloc arena max for netdata", 1);
  1104. if(i > 0)
  1105. mallopt(M_ARENA_MAX, 1);
  1106. #endif
  1107. // initialize the system clocks
  1108. clocks_init();
  1109. // prepare configuration environment variables for the plugins
  1110. setenv("UV_THREADPOOL_SIZE", config_get(CONFIG_SECTION_GLOBAL, "libuv worker threads", "16"), 1);
  1111. get_netdata_configured_variables();
  1112. set_global_environment();
  1113. // work while we are cd into config_dir
  1114. // to allow the plugins refer to their config
  1115. // files using relative filenames
  1116. if(chdir(netdata_configured_user_config_dir) == -1)
  1117. fatal("Cannot cd to '%s'", netdata_configured_user_config_dir);
  1118. // Get execution path before switching user to avoid permission issues
  1119. get_netdata_execution_path();
  1120. }
  1121. {
  1122. // --------------------------------------------------------------------
  1123. // get the debugging flags from the configuration file
  1124. char *flags = config_get(CONFIG_SECTION_LOGS, "debug flags", "0x0000000000000000");
  1125. setenv("NETDATA_DEBUG_FLAGS", flags, 1);
  1126. debug_flags = strtoull(flags, NULL, 0);
  1127. debug(D_OPTIONS, "Debug flags set to '0x%" PRIX64 "'.", debug_flags);
  1128. if(debug_flags != 0) {
  1129. struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
  1130. if(setrlimit(RLIMIT_CORE, &rl) != 0)
  1131. error("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
  1132. #ifdef HAVE_SYS_PRCTL_H
  1133. prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
  1134. #endif
  1135. }
  1136. // --------------------------------------------------------------------
  1137. // get log filenames and settings
  1138. log_init();
  1139. error_log_limit_unlimited();
  1140. // initialize the log files
  1141. open_all_log_files();
  1142. get_system_timezone();
  1143. // --------------------------------------------------------------------
  1144. // get the certificate and start security
  1145. #ifdef ENABLE_HTTPS
  1146. security_init();
  1147. #endif
  1148. // --------------------------------------------------------------------
  1149. // This is the safest place to start the SILENCERS structure
  1150. set_silencers_filename();
  1151. health_initialize_global_silencers();
  1152. // --------------------------------------------------------------------
  1153. // Initialize ML configuration
  1154. ml_init();
  1155. // --------------------------------------------------------------------
  1156. // setup process signals
  1157. // block signals while initializing threads.
  1158. // this causes the threads to block signals.
  1159. signals_block();
  1160. // setup the signals we want to use
  1161. signals_init();
  1162. // setup threads configs
  1163. default_stacksize = netdata_threads_init();
  1164. // --------------------------------------------------------------------
  1165. // check which threads are enabled and initialize them
  1166. for (i = 0; static_threads[i].name != NULL ; i++) {
  1167. struct netdata_static_thread *st = &static_threads[i];
  1168. if(st->config_name)
  1169. st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
  1170. if(st->enabled && st->init_routine)
  1171. st->init_routine();
  1172. }
  1173. // --------------------------------------------------------------------
  1174. // create the listening sockets
  1175. web_client_api_v1_init();
  1176. web_server_threading_selection();
  1177. if(web_server_mode != WEB_SERVER_MODE_NONE)
  1178. api_listen_sockets_setup();
  1179. }
  1180. #ifdef NETDATA_INTERNAL_CHECKS
  1181. if(debug_flags != 0) {
  1182. struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
  1183. if(setrlimit(RLIMIT_CORE, &rl) != 0)
  1184. error("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
  1185. #ifdef HAVE_SYS_PRCTL_H
  1186. prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
  1187. #endif
  1188. }
  1189. #endif /* NETDATA_INTERNAL_CHECKS */
  1190. // get the max file limit
  1191. if(getrlimit(RLIMIT_NOFILE, &rlimit_nofile) != 0)
  1192. error("getrlimit(RLIMIT_NOFILE) failed");
  1193. else
  1194. info("resources control: allowed file descriptors: soft = %zu, max = %zu", (size_t)rlimit_nofile.rlim_cur, (size_t)rlimit_nofile.rlim_max);
  1195. // fork, switch user, create pid file, set process priority
  1196. if(become_daemon(dont_fork, user) == -1)
  1197. fatal("Cannot daemonize myself.");
  1198. info("netdata started on pid %d.", getpid());
  1199. netdata_threads_init_after_fork((size_t)config_get_number(CONFIG_SECTION_GLOBAL, "pthread stack size", (long)default_stacksize));
  1200. // initialize internal registry
  1201. registry_init();
  1202. // fork the spawn server
  1203. spawn_init();
  1204. /*
  1205. * Libuv uv_spawn() uses SIGCHLD internally:
  1206. * https://github.com/libuv/libuv/blob/cc51217a317e96510fbb284721d5e6bc2af31e33/src/unix/process.c#L485
  1207. * and inadvertently replaces the netdata signal handler which was setup during initialization.
  1208. * Thusly, we must explicitly restore the signal handler for SIGCHLD.
  1209. * Warning: extreme care is needed when mixing and matching POSIX and libuv.
  1210. */
  1211. signals_restore_SIGCHLD();
  1212. // ------------------------------------------------------------------------
  1213. // initialize rrd, registry, health, rrdpush, etc.
  1214. netdata_anonymous_statistics_enabled=-1;
  1215. struct rrdhost_system_info *system_info = callocz(1, sizeof(struct rrdhost_system_info));
  1216. get_system_info(system_info);
  1217. system_info->hops = 0;
  1218. get_install_type(&system_info->install_type, &system_info->prebuilt_arch, &system_info->prebuilt_dist);
  1219. if(rrd_init(netdata_configured_hostname, system_info))
  1220. fatal("Cannot initialize localhost instance with name '%s'.", netdata_configured_hostname);
  1221. char agent_crash_file[FILENAME_MAX + 1];
  1222. char agent_incomplete_shutdown_file[FILENAME_MAX + 1];
  1223. snprintfz(agent_incomplete_shutdown_file, FILENAME_MAX, "%s/.agent_incomplete_shutdown", netdata_configured_varlib_dir);
  1224. int incomplete_shutdown_detected = (unlink(agent_incomplete_shutdown_file) == 0);
  1225. snprintfz(agent_crash_file, FILENAME_MAX, "%s/.agent_crash", netdata_configured_varlib_dir);
  1226. int crash_detected = (unlink(agent_crash_file) == 0);
  1227. int fd = open(agent_crash_file, O_WRONLY | O_CREAT | O_TRUNC, 444);
  1228. if (fd >= 0)
  1229. close(fd);
  1230. // ------------------------------------------------------------------------
  1231. // Claim netdata agent to a cloud endpoint
  1232. if (claiming_pending_arguments)
  1233. claim_agent(claiming_pending_arguments);
  1234. load_claiming_state();
  1235. // ------------------------------------------------------------------------
  1236. // enable log flood protection
  1237. error_log_limit_reset();
  1238. // Load host labels
  1239. reload_host_labels();
  1240. // ------------------------------------------------------------------------
  1241. // spawn the threads
  1242. web_server_config_options();
  1243. netdata_zero_metrics_enabled = config_get_boolean_ondemand(CONFIG_SECTION_DB, "enable zero metrics", CONFIG_BOOLEAN_NO);
  1244. set_late_global_environment();
  1245. for (i = 0; static_threads[i].name != NULL ; i++) {
  1246. struct netdata_static_thread *st = &static_threads[i];
  1247. if(st->enabled) {
  1248. st->thread = mallocz(sizeof(netdata_thread_t));
  1249. debug(D_SYSTEM, "Starting thread %s.", st->name);
  1250. netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_DEFAULT, st->start_routine, st);
  1251. }
  1252. else debug(D_SYSTEM, "Not starting thread %s.", st->name);
  1253. }
  1254. // ------------------------------------------------------------------------
  1255. // Initialize netdata agent command serving from cli and signals
  1256. commands_init();
  1257. info("netdata initialization completed. Enjoy real-time performance monitoring!");
  1258. netdata_ready = 1;
  1259. send_statistics("START", "-", "-");
  1260. if (crash_detected)
  1261. send_statistics("CRASH", "-", "-");
  1262. if (incomplete_shutdown_detected)
  1263. send_statistics("INCOMPLETE_SHUTDOWN", "-", "-");
  1264. //check if ANALYTICS needs to start
  1265. if (netdata_anonymous_statistics_enabled == 1) {
  1266. for (i = 0; static_threads[i].name != NULL; i++) {
  1267. if (!strncmp(static_threads[i].name, "ANALYTICS", 9)) {
  1268. struct netdata_static_thread *st = &static_threads[i];
  1269. st->thread = mallocz(sizeof(netdata_thread_t));
  1270. st->enabled = 1;
  1271. debug(D_SYSTEM, "Starting thread %s.", st->name);
  1272. netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_DEFAULT, st->start_routine, st);
  1273. }
  1274. }
  1275. }
  1276. // ------------------------------------------------------------------------
  1277. // Report ACLK build failure
  1278. #ifndef ENABLE_ACLK
  1279. error("This agent doesn't have ACLK.");
  1280. char filename[FILENAME_MAX + 1];
  1281. snprintfz(filename, FILENAME_MAX, "%s/.aclk_report_sent", netdata_configured_varlib_dir);
  1282. if (netdata_anonymous_statistics_enabled > 0 && access(filename, F_OK)) { // -1 -> not initialized
  1283. send_statistics("ACLK_DISABLED", "-", "-");
  1284. int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 444);
  1285. if (fd == -1)
  1286. error("Cannot create file '%s'. Please fix this.", filename);
  1287. else
  1288. close(fd);
  1289. }
  1290. #endif
  1291. // ------------------------------------------------------------------------
  1292. // unblock signals
  1293. signals_unblock();
  1294. // ------------------------------------------------------------------------
  1295. // Handle signals
  1296. signals_handle();
  1297. // should never reach this point
  1298. // but we need it for rpmlint #2752
  1299. return 1;
  1300. }