registry_init.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "daemon/common.h"
  3. #include "registry_internals.h"
  4. int registry_init(void) {
  5. char filename[FILENAME_MAX + 1];
  6. // registry enabled?
  7. if(web_server_mode != WEB_SERVER_MODE_NONE) {
  8. registry.enabled = config_get_boolean(CONFIG_SECTION_REGISTRY, "enabled", 0);
  9. }
  10. else {
  11. info("Registry is disabled - use the central netdata");
  12. config_set_boolean(CONFIG_SECTION_REGISTRY, "enabled", 0);
  13. registry.enabled = 0;
  14. }
  15. // pathnames
  16. snprintfz(filename, FILENAME_MAX, "%s/registry", netdata_configured_varlib_dir);
  17. registry.pathname = config_get(CONFIG_SECTION_REGISTRY, "registry db directory", filename);
  18. if(mkdir(registry.pathname, 0770) == -1 && errno != EEXIST)
  19. fatal("Cannot create directory '%s'.", registry.pathname);
  20. // filenames
  21. snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
  22. registry.machine_guid_filename = config_get(CONFIG_SECTION_REGISTRY, "netdata unique id file", filename);
  23. snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
  24. registry.db_filename = config_get(CONFIG_SECTION_REGISTRY, "registry db file", filename);
  25. snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
  26. registry.log_filename = config_get(CONFIG_SECTION_REGISTRY, "registry log file", filename);
  27. // configuration options
  28. registry.save_registry_every_entries = (unsigned long long)config_get_number(CONFIG_SECTION_REGISTRY, "registry save db every new entries", 1000000);
  29. registry.persons_expiration = config_get_number(CONFIG_SECTION_REGISTRY, "registry expire idle persons days", 365) * 86400;
  30. registry.registry_domain = config_get(CONFIG_SECTION_REGISTRY, "registry domain", "");
  31. registry.registry_to_announce = config_get(CONFIG_SECTION_REGISTRY, "registry to announce", "https://registry.my-netdata.io");
  32. registry.hostname = config_get(CONFIG_SECTION_REGISTRY, "registry hostname", netdata_configured_hostname);
  33. registry.verify_cookies_redirects = config_get_boolean(CONFIG_SECTION_REGISTRY, "verify browser cookies support", 1);
  34. registry.enable_cookies_samesite_secure = config_get_boolean(CONFIG_SECTION_REGISTRY, "enable cookies SameSite and Secure", 1);
  35. registry_update_cloud_base_url();
  36. setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
  37. setenv("NETDATA_REGISTRY_URL", registry.registry_to_announce, 1);
  38. registry.max_url_length = (size_t)config_get_number(CONFIG_SECTION_REGISTRY, "max URL length", 1024);
  39. if(registry.max_url_length < 10) {
  40. registry.max_url_length = 10;
  41. config_set_number(CONFIG_SECTION_REGISTRY, "max URL length", (long long)registry.max_url_length);
  42. }
  43. registry.max_name_length = (size_t)config_get_number(CONFIG_SECTION_REGISTRY, "max URL name length", 50);
  44. if(registry.max_name_length < 10) {
  45. registry.max_name_length = 10;
  46. config_set_number(CONFIG_SECTION_REGISTRY, "max URL name length", (long long)registry.max_name_length);
  47. }
  48. // initialize entries counters
  49. registry.persons_count = 0;
  50. registry.machines_count = 0;
  51. registry.usages_count = 0;
  52. registry.urls_count = 0;
  53. registry.persons_urls_count = 0;
  54. registry.machines_urls_count = 0;
  55. // initialize memory counters
  56. registry.persons_memory = 0;
  57. registry.machines_memory = 0;
  58. registry.urls_memory = 0;
  59. registry.persons_urls_memory = 0;
  60. registry.machines_urls_memory = 0;
  61. // initialize locks
  62. netdata_mutex_init(&registry.lock);
  63. // create dictionaries
  64. registry.persons = dictionary_create(DICTIONARY_FLAGS);
  65. registry.machines = dictionary_create(DICTIONARY_FLAGS);
  66. avl_init(&registry.registry_urls_root_index, registry_url_compare);
  67. // load the registry database
  68. if(registry.enabled) {
  69. registry_log_open();
  70. registry_db_load();
  71. registry_log_load();
  72. if(unlikely(registry_db_should_be_saved()))
  73. registry_db_save();
  74. }
  75. return 0;
  76. }
  77. void registry_free(void) {
  78. if(!registry.enabled) return;
  79. // we need to destroy the dictionaries ourselves
  80. // since the dictionaries use memory we allocated
  81. while(registry.persons->values_index.root) {
  82. REGISTRY_PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
  83. registry_person_del(p);
  84. }
  85. while(registry.machines->values_index.root) {
  86. REGISTRY_MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
  87. // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
  88. while(m->machine_urls->values_index.root) {
  89. REGISTRY_MACHINE_URL *mu = ((NAME_VALUE *)m->machine_urls->values_index.root)->value;
  90. // fprintf(stderr, "\tURL: '%s', first: %u, last: %u, usages: %u, flags: 0x%02x\n", mu->url->url, mu->first_t, mu->last_t, mu->usages, mu->flags);
  91. //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
  92. //dictionary_destroy(mu->persons);
  93. debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
  94. dictionary_del(m->machine_urls, mu->url->url);
  95. debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
  96. registry_url_unlink(mu->url);
  97. debug(D_REGISTRY, "Registry: freeing machine url");
  98. freez(mu);
  99. }
  100. debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
  101. dictionary_del(registry.machines, m->guid);
  102. debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
  103. dictionary_destroy(m->machine_urls);
  104. debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
  105. freez(m);
  106. }
  107. // and free the memory of remaining dictionary structures
  108. debug(D_REGISTRY, "Registry: destroying persons dictionary");
  109. dictionary_destroy(registry.persons);
  110. debug(D_REGISTRY, "Registry: destroying machines dictionary");
  111. dictionary_destroy(registry.machines);
  112. }