]> arthur.barton.de Git - netdata.git/blob - src/registry_init.c
web_log_plugin: requests per http version chart added
[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     registry.enabled = config_get_boolean("registry", "enabled", 0);
8
9     // pathnames
10     snprintfz(filename, FILENAME_MAX, "%s/registry", netdata_configured_varlib_dir);
11     registry.pathname = config_get("registry", "registry db directory", filename);
12     if(mkdir(registry.pathname, 0770) == -1 && errno != EEXIST)
13         fatal("Cannot create directory '%s'.", registry.pathname);
14
15     // filenames
16     snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
17     registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
18     registry_get_this_machine_guid();
19
20     snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
21     registry.db_filename = config_get("registry", "registry db file", filename);
22
23     snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
24     registry.log_filename = config_get("registry", "registry log file", filename);
25
26     // configuration options
27     registry.save_registry_every_entries = (unsigned long long)config_get_number("registry", "registry save db every new entries", 1000000);
28     registry.persons_expiration = config_get_number("registry", "registry expire idle persons days", 365) * 86400;
29     registry.registry_domain = config_get("registry", "registry domain", "");
30     registry.registry_to_announce = config_get("registry", "registry to announce", "https://registry.my-netdata.io");
31     registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", localhost.hostname));
32     registry.verify_cookies_redirects = config_get_boolean("registry", "verify browser cookies support", 1);
33
34     setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
35     setenv("NETDATA_REGISTRY_URL", registry.registry_to_announce, 1);
36
37     registry.max_url_length = (size_t)config_get_number("registry", "max URL length", 1024);
38     if(registry.max_url_length < 10) {
39         registry.max_url_length = 10;
40         config_set_number("registry", "max URL length", (long long)registry.max_url_length);
41     }
42
43     registry.max_name_length = (size_t)config_get_number("registry", "max URL name length", 50);
44     if(registry.max_name_length < 10) {
45         registry.max_name_length = 10;
46         config_set_number("registry", "max URL name length", (long long)registry.max_name_length);
47     }
48
49     // initialize entries counters
50     registry.persons_count = 0;
51     registry.machines_count = 0;
52     registry.usages_count = 0;
53     registry.urls_count = 0;
54     registry.persons_urls_count = 0;
55     registry.machines_urls_count = 0;
56
57     // initialize memory counters
58     registry.persons_memory = 0;
59     registry.machines_memory = 0;
60     registry.urls_memory = 0;
61     registry.persons_urls_memory = 0;
62     registry.machines_urls_memory = 0;
63
64     // initialize locks
65     pthread_mutex_init(&registry.lock, NULL);
66
67     // create dictionaries
68     registry.persons = dictionary_create(DICTIONARY_FLAGS);
69     registry.machines = dictionary_create(DICTIONARY_FLAGS);
70     avl_init(&registry.registry_urls_root_index, registry_url_compare);
71
72     // load the registry database
73     if(registry.enabled) {
74         registry_log_open();
75         registry_db_load();
76         registry_log_load();
77
78         if(unlikely(registry_db_should_be_saved()))
79             registry_db_save();
80     }
81
82     return 0;
83 }
84
85 void registry_free(void) {
86     if(!registry.enabled) return;
87
88     // we need to destroy the dictionaries ourselves
89     // since the dictionaries use memory we allocated
90
91     while(registry.persons->values_index.root) {
92         REGISTRY_PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
93         registry_person_del(p);
94     }
95
96     while(registry.machines->values_index.root) {
97         REGISTRY_MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
98
99         // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
100
101         while(m->machine_urls->values_index.root) {
102             REGISTRY_MACHINE_URL *mu = ((NAME_VALUE *)m->machine_urls->values_index.root)->value;
103
104             // 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);
105
106             //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
107             //dictionary_destroy(mu->persons);
108
109             debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
110             dictionary_del(m->machine_urls, mu->url->url);
111
112             debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
113             registry_url_unlink(mu->url);
114
115             debug(D_REGISTRY, "Registry: freeing machine url");
116             freez(mu);
117         }
118
119         debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
120         dictionary_del(registry.machines, m->guid);
121
122         debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
123         dictionary_destroy(m->machine_urls);
124
125         debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
126         freez(m);
127     }
128
129     // and free the memory of remaining dictionary structures
130
131     debug(D_REGISTRY, "Registry: destroying persons dictionary");
132     dictionary_destroy(registry.persons);
133
134     debug(D_REGISTRY, "Registry: destroying machines dictionary");
135     dictionary_destroy(registry.machines);
136 }
137