]> arthur.barton.de Git - netdata.git/blob - src/registry_init.c
Merge branch 'master' into ab-debian
[netdata.git] / src / registry_init.c
1 #include "registry_internals.h"
2
3 int registry_init(void) {
4     char filename[FILENAME_MAX + 1];
5
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
16     // pathnames
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);
21
22     // filenames
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);
25
26     snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
27     registry.db_filename = config_get(CONFIG_SECTION_REGISTRY, "registry db file", filename);
28
29     snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
30     registry.log_filename = config_get(CONFIG_SECTION_REGISTRY, "registry log file", filename);
31
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);
39
40     setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
41     setenv("NETDATA_REGISTRY_URL", registry.registry_to_announce, 1);
42
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);
47     }
48
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);
53     }
54
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;
62
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;
69
70     // initialize locks
71     netdata_mutex_init(&registry.lock);
72
73     // create dictionaries
74     registry.persons = dictionary_create(DICTIONARY_FLAGS);
75     registry.machines = dictionary_create(DICTIONARY_FLAGS);
76     avl_init(&registry.registry_urls_root_index, registry_url_compare);
77
78     // load the registry database
79     if(registry.enabled) {
80         registry_log_open();
81         registry_db_load();
82         registry_log_load();
83
84         if(unlikely(registry_db_should_be_saved()))
85             registry_db_save();
86     }
87
88     return 0;
89 }
90
91 void registry_free(void) {
92     if(!registry.enabled) return;
93
94     // we need to destroy the dictionaries ourselves
95     // since the dictionaries use memory we allocated
96
97     while(registry.persons->values_index.root) {
98         REGISTRY_PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
99         registry_person_del(p);
100     }
101
102     while(registry.machines->values_index.root) {
103         REGISTRY_MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
104
105         // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
106
107         while(m->machine_urls->values_index.root) {
108             REGISTRY_MACHINE_URL *mu = ((NAME_VALUE *)m->machine_urls->values_index.root)->value;
109
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);
111
112             //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
113             //dictionary_destroy(mu->persons);
114
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);
117
118             debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
119             registry_url_unlink(mu->url);
120
121             debug(D_REGISTRY, "Registry: freeing machine url");
122             freez(mu);
123         }
124
125         debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
126         dictionary_del(registry.machines, m->guid);
127
128         debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
129         dictionary_destroy(m->machine_urls);
130
131         debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
132         freez(m);
133     }
134
135     // and free the memory of remaining dictionary structures
136
137     debug(D_REGISTRY, "Registry: destroying persons dictionary");
138     dictionary_destroy(registry.persons);
139
140     debug(D_REGISTRY, "Registry: destroying machines dictionary");
141     dictionary_destroy(registry.machines);
142 }
143