1 #include "registry_internals.h"
3 int registry_init(void) {
4 char filename[FILENAME_MAX + 1];
7 if(web_server_mode != WEB_SERVER_MODE_NONE) {
8 registry.enabled = config_get_boolean(CONFIG_SECTION_REGISTRY, "enabled", 0);
11 info("Registry is disabled - use the central netdata");
12 config_set_boolean(CONFIG_SECTION_REGISTRY, "enabled", 0);
17 snprintfz(filename, FILENAME_MAX, "%s/registry", netdata_configured_varlib_dir);
18 registry.pathname = config_get(CONFIG_SECTION_REGISTRY, "registry db directory", filename);
19 if(mkdir(registry.pathname, 0770) == -1 && errno != EEXIST)
20 fatal("Cannot create directory '%s'.", registry.pathname);
23 snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
24 registry.machine_guid_filename = config_get(CONFIG_SECTION_REGISTRY, "netdata unique id file", filename);
26 snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
27 registry.db_filename = config_get(CONFIG_SECTION_REGISTRY, "registry db file", filename);
29 snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
30 registry.log_filename = config_get(CONFIG_SECTION_REGISTRY, "registry log file", filename);
32 // configuration options
33 registry.save_registry_every_entries = (unsigned long long)config_get_number(CONFIG_SECTION_REGISTRY, "registry save db every new entries", 1000000);
34 registry.persons_expiration = config_get_number(CONFIG_SECTION_REGISTRY, "registry expire idle persons days", 365) * 86400;
35 registry.registry_domain = config_get(CONFIG_SECTION_REGISTRY, "registry domain", "");
36 registry.registry_to_announce = config_get(CONFIG_SECTION_REGISTRY, "registry to announce", "https://registry.my-netdata.io");
37 registry.hostname = config_get(CONFIG_SECTION_REGISTRY, "registry hostname", config_get(CONFIG_SECTION_GLOBAL, "hostname", "localhost"));
38 registry.verify_cookies_redirects = config_get_boolean(CONFIG_SECTION_REGISTRY, "verify browser cookies support", 1);
40 setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
41 setenv("NETDATA_REGISTRY_URL", registry.registry_to_announce, 1);
43 registry.max_url_length = (size_t)config_get_number(CONFIG_SECTION_REGISTRY, "max URL length", 1024);
44 if(registry.max_url_length < 10) {
45 registry.max_url_length = 10;
46 config_set_number(CONFIG_SECTION_REGISTRY, "max URL length", (long long)registry.max_url_length);
49 registry.max_name_length = (size_t)config_get_number(CONFIG_SECTION_REGISTRY, "max URL name length", 50);
50 if(registry.max_name_length < 10) {
51 registry.max_name_length = 10;
52 config_set_number(CONFIG_SECTION_REGISTRY, "max URL name length", (long long)registry.max_name_length);
55 // initialize entries counters
56 registry.persons_count = 0;
57 registry.machines_count = 0;
58 registry.usages_count = 0;
59 registry.urls_count = 0;
60 registry.persons_urls_count = 0;
61 registry.machines_urls_count = 0;
63 // initialize memory counters
64 registry.persons_memory = 0;
65 registry.machines_memory = 0;
66 registry.urls_memory = 0;
67 registry.persons_urls_memory = 0;
68 registry.machines_urls_memory = 0;
71 pthread_mutex_init(®istry.lock, NULL);
73 // create dictionaries
74 registry.persons = dictionary_create(DICTIONARY_FLAGS);
75 registry.machines = dictionary_create(DICTIONARY_FLAGS);
76 avl_init(®istry.registry_urls_root_index, registry_url_compare);
78 // load the registry database
79 if(registry.enabled) {
84 if(unlikely(registry_db_should_be_saved()))
91 void registry_free(void) {
92 if(!registry.enabled) return;
94 // we need to destroy the dictionaries ourselves
95 // since the dictionaries use memory we allocated
97 while(registry.persons->values_index.root) {
98 REGISTRY_PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
99 registry_person_del(p);
102 while(registry.machines->values_index.root) {
103 REGISTRY_MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
105 // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
107 while(m->machine_urls->values_index.root) {
108 REGISTRY_MACHINE_URL *mu = ((NAME_VALUE *)m->machine_urls->values_index.root)->value;
110 // 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);
112 //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
113 //dictionary_destroy(mu->persons);
115 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
116 dictionary_del(m->machine_urls, mu->url->url);
118 debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
119 registry_url_unlink(mu->url);
121 debug(D_REGISTRY, "Registry: freeing machine url");
125 debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
126 dictionary_del(registry.machines, m->guid);
128 debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
129 dictionary_destroy(m->machine_urls);
131 debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
135 // and free the memory of remaining dictionary structures
137 debug(D_REGISTRY, "Registry: destroying persons dictionary");
138 dictionary_destroy(registry.persons);
140 debug(D_REGISTRY, "Registry: destroying machines dictionary");
141 dictionary_destroy(registry.machines);