registry_init.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_DIRECTORIES, "registry", 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(REGISTRY_DICTIONARY_OPTIONS);
  65. registry.machines = dictionary_create(REGISTRY_DICTIONARY_OPTIONS);
  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. static int machine_urls_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *data) {
  78. REGISTRY_MACHINE *m = (REGISTRY_MACHINE *)data;
  79. (void)m;
  80. REGISTRY_MACHINE_URL *mu = (REGISTRY_MACHINE_URL *)entry;
  81. debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
  82. registry_url_unlink(mu->url);
  83. debug(D_REGISTRY, "Registry: freeing machine url");
  84. freez(mu);
  85. return 1;
  86. }
  87. static int machine_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *data __maybe_unused) {
  88. REGISTRY_MACHINE *m = (REGISTRY_MACHINE *)entry;
  89. int ret = dictionary_walkthrough_read(m->machine_urls, machine_urls_delete_callback, m);
  90. dictionary_destroy(m->machine_urls);
  91. freez(m);
  92. return ret + 1;
  93. }
  94. static int registry_person_del_callback(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *d __maybe_unused) {
  95. REGISTRY_PERSON *p = (REGISTRY_PERSON *)entry;
  96. debug(D_REGISTRY, "Registry: registry_person_del('%s'): deleting person", p->guid);
  97. while(p->person_urls.root)
  98. registry_person_unlink_from_url(p, (REGISTRY_PERSON_URL *)p->person_urls.root);
  99. //debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
  100. //dictionary_del(registry.persons, p->guid);
  101. debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
  102. freez(p);
  103. return 1;
  104. }
  105. void registry_free(void) {
  106. if(!registry.enabled) return;
  107. debug(D_REGISTRY, "Registry: destroying persons dictionary");
  108. dictionary_walkthrough_read(registry.persons, registry_person_del_callback, NULL);
  109. dictionary_destroy(registry.persons);
  110. debug(D_REGISTRY, "Registry: destroying machines dictionary");
  111. dictionary_walkthrough_read(registry.machines, machine_delete_callback, NULL);
  112. dictionary_destroy(registry.machines);
  113. }