12 #include <sys/types.h>
18 #include "dictionary.h"
19 #include "appconfig.h"
21 #include "web_client.h"
27 // ----------------------------------------------------------------------------
30 // 1. the default tracking cookie expires in 1 year, but the persons are not
31 // removed from the db - this means the database only grows - ideally the
32 // database should be cleaned in registry_save() for both on-disk and
36 // i. Find all the PERSONs that have expired cookie
37 // ii. For each of their PERSON_URLs:
38 // - decrement the linked MACHINE links
39 // - if the linked MACHINE has no other links, remove the linked MACHINE too
40 // - remove the PERSON_URL
42 // 2. add protection to prevent abusing the registry by flooding it with
43 // requests to fill the memory and crash it.
45 // Possible protections:
46 // - limit the number of URLs per person
47 // - limit the number of URLs per machine
48 // - limit the number of persons
49 // - limit the number of machines
50 // - [DONE] limit the size of URLs
51 // - [DONE] limit the size of PERSON_URL names
52 // - limit the number of requests that add data to the registry,
53 // per client IP per hour
57 #define REGISTRY_URL_FLAGS_DEFAULT 0x00
58 #define REGISTRY_URL_FLAGS_EXPIRED 0x01
60 #define DICTIONARY_FLAGS DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE | DICTIONARY_FLAG_NAME_LINK_DONT_CLONE
62 // ----------------------------------------------------------------------------
68 char machine_guid[36 + 1];
70 // entries counters / statistics
71 unsigned long long persons_count;
72 unsigned long long machines_count;
73 unsigned long long usages_count;
74 unsigned long long urls_count;
75 unsigned long long persons_urls_count;
76 unsigned long long machines_urls_count;
77 unsigned long long log_count;
79 // memory counters / statistics
80 unsigned long long persons_memory;
81 unsigned long long machines_memory;
82 unsigned long long urls_memory;
83 unsigned long long persons_urls_memory;
84 unsigned long long machines_urls_memory;
87 unsigned long long save_registry_every_entries;
88 char *registry_domain;
90 char *registry_to_announce;
91 time_t persons_expiration; // seconds to expire idle persons
93 size_t max_url_length;
94 size_t max_name_length;
100 char *machine_guid_filename;
106 DICTIONARY *persons; // dictionary of PERSON *, with key the PERSON.guid
107 DICTIONARY *machines; // dictionary of MACHINE *, with key the MACHINE.guid
108 DICTIONARY *urls; // dictionary of URL *, with key the URL.url
110 // concurrency locking
111 // we keep different locks for different things
112 // so that many tasks can be completed in parallel
113 pthread_mutex_t persons_lock;
114 pthread_mutex_t machines_lock;
115 pthread_mutex_t urls_lock;
116 pthread_mutex_t person_urls_lock;
117 pthread_mutex_t machine_urls_lock;
118 pthread_mutex_t log_lock;
122 // ----------------------------------------------------------------------------
124 // Save memory by de-duplicating URLs
125 // so instead of storing URLs all over the place
126 // we store them here and we keep pointers elsewhere
129 uint32_t links; // the number of links to this URL - when none is left, we free it
130 uint16_t len; // the length of the URL in bytes
131 char url[1]; // the URL - dynamically allocated to more size
133 typedef struct url URL;
136 // ----------------------------------------------------------------------------
137 // MACHINE structures
139 // For each MACHINE-URL pair we keep this
141 URL *url; // de-duplicated URL
142 // DICTIONARY *persons; // dictionary of PERSON *
145 uint32_t first_t; // the first time we saw this
146 uint32_t last_t; // the last time we saw this
147 uint32_t usages; // how many times this has been accessed
149 typedef struct machine_url MACHINE_URL;
153 char guid[36 + 1]; // the GUID
155 uint32_t links; // the number of PERSON_URLs linked to this machine
157 DICTIONARY *urls; // MACHINE_URL *
159 uint32_t first_t; // the first time we saw this
160 uint32_t last_t; // the last time we saw this
161 uint32_t usages; // how many times this has been accessed
163 typedef struct machine MACHINE;
166 // ----------------------------------------------------------------------------
169 // for each PERSON-URL pair we keep this
171 URL *url; // de-duplicated URL
172 MACHINE *machine; // link the MACHINE of this URL
175 uint32_t first_t; // the first time we saw this
176 uint32_t last_t; // the last time we saw this
177 uint32_t usages; // how many times this has been accessed
179 char name[1]; // the name of the URL, as known by the user
180 // dynamically allocated to fit properly
182 typedef struct person_url PERSON_URL;
186 char guid[36 + 1]; // the person GUID
188 DICTIONARY *urls; // dictionary of PERSON_URL *
190 uint32_t first_t; // the first time we saw this
191 uint32_t last_t; // the last time we saw this
192 uint32_t usages; // how many times this has been accessed
194 typedef struct person PERSON;
197 // ----------------------------------------------------------------------------
198 // REGISTRY concurrency locking
200 static inline void registry_persons_lock(void) {
201 pthread_mutex_lock(®istry.persons_lock);
204 static inline void registry_persons_unlock(void) {
205 pthread_mutex_unlock(®istry.persons_lock);
208 static inline void registry_machines_lock(void) {
209 pthread_mutex_lock(®istry.machines_lock);
212 static inline void registry_machines_unlock(void) {
213 pthread_mutex_unlock(®istry.machines_lock);
216 static inline void registry_urls_lock(void) {
217 pthread_mutex_lock(®istry.urls_lock);
220 static inline void registry_urls_unlock(void) {
221 pthread_mutex_unlock(®istry.urls_lock);
224 // ideally, we should not lock the whole registry for
225 // updating a person's urls.
226 // however, to save the memory required for keeping a
227 // mutex (40 bytes) per person, we do...
228 static inline void registry_person_urls_lock(PERSON *p) {
230 pthread_mutex_lock(®istry.person_urls_lock);
233 static inline void registry_person_urls_unlock(PERSON *p) {
235 pthread_mutex_unlock(®istry.person_urls_lock);
238 // ideally, we should not lock the whole registry for
239 // updating a machine's urls.
240 // however, to save the memory required for keeping a
241 // mutex (40 bytes) per machine, we do...
242 static inline void registry_machine_urls_lock(MACHINE *m) {
244 pthread_mutex_lock(®istry.machine_urls_lock);
247 static inline void registry_machine_urls_unlock(MACHINE *m) {
249 pthread_mutex_unlock(®istry.machine_urls_lock);
252 static inline void registry_log_lock(void) {
253 pthread_mutex_lock(®istry.log_lock);
256 static inline void registry_log_unlock(void) {
257 pthread_mutex_unlock(®istry.log_lock);
261 // ----------------------------------------------------------------------------
264 // parse a GUID and re-generated to be always lower case
265 // this is used as a protection against the variations of GUIDs
266 static inline int registry_regenerate_guid(const char *guid, char *result) {
268 if(unlikely(uuid_parse(guid, uuid) == -1)) {
269 info("Registry: GUID '%s' is not a valid GUID.", guid);
273 uuid_unparse_lower(uuid, result);
275 #ifdef NETDATA_INTERNAL_CHECKS
276 if(strcmp(guid, result))
277 info("Registry: source GUID '%s' and re-generated GUID '%s' differ!", guid, result);
278 #endif /* NETDATA_INTERNAL_CHECKS */
284 // make sure the names of the machines / URLs do not contain any tabs
285 // (which are used as our separator in the database files)
286 // and are properly trimmed (before and after)
287 static inline char *registry_fix_machine_name(char *name, size_t *len) {
288 char *s = name?name:"";
290 // skip leading spaces
291 while(*s && isspace(*s)) s++;
293 // make sure all spaces are a SPACE
296 if(unlikely(isspace(*t)))
302 // remove trailing spaces
317 static inline char *registry_fix_url(char *url, size_t *len) {
318 return registry_fix_machine_name(url, len);
322 // ----------------------------------------------------------------------------
323 // forward definition of functions
325 extern PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
326 extern PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
329 // ----------------------------------------------------------------------------
332 static inline URL *registry_url_allocate_nolock(const char *url, size_t urllen) {
333 // protection from too big URLs
334 if(urllen > registry.max_url_length)
335 urllen = registry.max_url_length;
337 debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): allocating %zu bytes", url, sizeof(URL) + urllen);
338 URL *u = malloc(sizeof(URL) + urllen);
339 if(!u) fatal("Cannot allocate %zu bytes for URL '%s'", sizeof(URL) + urllen);
341 // a simple strcpy() should do the job
342 // but I prefer to be safe, since the caller specified urllen
343 strncpy(u->url, url, urllen);
344 u->url[urllen] = '\0';
349 registry.urls_memory += sizeof(URL) + urllen;
351 debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): indexing it", url);
352 dictionary_set(registry.urls, u->url, u, sizeof(URL));
357 static inline URL *registry_url_get(const char *url, size_t urllen) {
358 debug(D_REGISTRY, "Registry: registry_url_get('%s')", url);
360 registry_urls_lock();
362 URL *u = dictionary_get(registry.urls, url);
364 u = registry_url_allocate_nolock(url, urllen);
365 registry.urls_count++;
368 registry_urls_unlock();
373 static inline void registry_url_link_nolock(URL *u) {
375 debug(D_REGISTRY, "Registry: registry_url_link_nolock('%s'): URL has now %u links", u->url, u->links);
378 static inline void registry_url_unlink_nolock(URL *u) {
381 debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): No more links for this URL", u->url);
382 dictionary_del(registry.urls, u->url);
386 debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): URL has %u links left", u->url, u->links);
390 // ----------------------------------------------------------------------------
393 static inline MACHINE *registry_machine_find(const char *machine_guid) {
394 debug(D_REGISTRY, "Registry: registry_machine_find('%s')", machine_guid);
395 return dictionary_get(registry.machines, machine_guid);
398 static inline MACHINE_URL *registry_machine_url_allocate(MACHINE *m, URL *u, time_t when) {
399 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(MACHINE_URL));
401 MACHINE_URL *mu = malloc(sizeof(MACHINE_URL));
402 if(!mu) fatal("registry_machine_link_to_url('%s', '%s'): cannot allocate %zu bytes.", m->guid, u->url, sizeof(MACHINE_URL));
404 // mu->persons = dictionary_create(DICTIONARY_FLAGS);
405 // dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
407 mu->first_t = mu->last_t = when;
410 mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
412 registry.machines_urls_memory += sizeof(MACHINE_URL);
414 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): indexing URL in machine", m->guid, u->url);
415 dictionary_set(m->urls, u->url, mu, sizeof(MACHINE_URL));
416 registry_url_link_nolock(u);
421 static inline MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
422 debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(MACHINE));
424 MACHINE *m = malloc(sizeof(MACHINE));
425 if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
427 strncpy(m->guid, machine_guid, 36);
429 debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
430 m->urls = dictionary_create(DICTIONARY_FLAGS);
432 m->first_t = m->last_t = when;
435 registry.machines_memory += sizeof(MACHINE);
437 registry.machines_count++;
438 dictionary_set(registry.machines, m->guid, m, sizeof(MACHINE));
443 // 1. validate machine GUID
444 // 2. if it is valid, find it or create it and return it
445 // 3. if it is not valid, return NULL
446 static inline MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
449 registry_machines_lock();
451 if(likely(machine_guid && *machine_guid)) {
452 // validate it is a GUID
454 if(unlikely(registry_regenerate_guid(machine_guid, buf) == -1))
455 info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
458 m = registry_machine_find(machine_guid);
459 if(!m) m = registry_machine_allocate(machine_guid, when);
463 registry_machines_unlock();
469 // ----------------------------------------------------------------------------
472 static inline PERSON *registry_person_find(const char *person_guid) {
473 debug(D_REGISTRY, "Registry: registry_person_find('%s')", person_guid);
474 return dictionary_get(registry.persons, person_guid);
477 static inline PERSON_URL *registry_person_url_allocate(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when) {
478 // protection from too big names
479 if(namelen > registry.max_name_length)
480 namelen = registry.max_name_length;
482 debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
483 sizeof(PERSON_URL) + namelen);
485 PERSON_URL *pu = malloc(sizeof(PERSON_URL) + namelen);
486 if(!pu) fatal("registry_person_url_allocate('%s', '%s', '%s'): cannot allocate %zu bytes.", p->guid, m->guid, u->url, sizeof(PERSON_URL) + namelen);
488 // a simple strcpy() should do the job
489 // but I prefer to be safe, since the caller specified urllen
490 strncpy(pu->name, name, namelen);
491 pu->name[namelen] = '\0';
494 pu->first_t = pu->last_t = when;
497 pu->flags = REGISTRY_URL_FLAGS_DEFAULT;
500 registry.persons_urls_memory += sizeof(PERSON_URL) + namelen;
502 debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): indexing URL in person", p->guid, m->guid, u->url);
503 dictionary_set(p->urls, u->url, pu, sizeof(PERSON_URL));
504 registry_url_link_nolock(u);
509 static inline PERSON_URL *registry_person_url_reallocate(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when, PERSON_URL *pu) {
510 // this function is needed to change the name of a PERSON_URL
512 debug(D_REGISTRY, "registry_person_url_reallocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
513 sizeof(PERSON_URL) + namelen);
515 PERSON_URL *tpu = registry_person_url_allocate(p, m, u, name, namelen, when);
516 tpu->first_t = pu->first_t;
517 tpu->last_t = pu->last_t;
518 tpu->usages = pu->usages;
520 // ok, these are a hack - since the registry_person_url_allocate() is
521 // adding these, we have to subtract them
522 tpu->machine->links--;
523 registry.persons_urls_memory -= sizeof(PERSON_URL) + strlen(pu->name);
524 registry_url_unlink_nolock(u);
531 static inline PERSON *registry_person_allocate(const char *person_guid, time_t when) {
534 debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(PERSON));
536 p = malloc(sizeof(PERSON));
537 if(!p) fatal("Registry: cannot allocate memory for new person.");
542 if (uuid_generate_time_safe(uuid) == -1)
543 info("Registry: uuid_generate_time_safe() reports UUID generation is not safe for uniqueness.");
545 uuid_unparse_lower(uuid, p->guid);
547 debug(D_REGISTRY, "Registry: Checking if the generated person guid '%s' is unique", p->guid);
548 if (!dictionary_get(registry.persons, p->guid)) {
549 debug(D_REGISTRY, "Registry: generated person guid '%s' is unique", p->guid);
553 info("Registry: generated person guid '%s' found in the registry. Retrying...", p->guid);
557 strncpy(p->guid, person_guid, 36);
561 debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): creating dictionary of urls", p->guid);
562 p->urls = dictionary_create(DICTIONARY_FLAGS);
564 p->first_t = p->last_t = when;
567 registry.persons_memory += sizeof(PERSON);
569 registry.persons_count++;
570 dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
576 // 1. validate person GUID
577 // 2. if it is valid, find it
578 // 3. if it is not valid, create a new one
580 static inline PERSON *registry_person_get(const char *person_guid, time_t when) {
583 registry_persons_lock();
585 if(person_guid && *person_guid) {
587 // validate it is a GUID
588 if(unlikely(registry_regenerate_guid(person_guid, buf) == -1))
589 info("Registry: person guid '%s' is not a valid guid. Ignoring it.", person_guid);
592 p = registry_person_find(person_guid);
593 if(!p) person_guid = NULL;
597 if(!p) p = registry_person_allocate(NULL, when);
599 registry_persons_unlock();
604 // ----------------------------------------------------------------------------
605 // LINKING OF OBJECTS
607 static inline PERSON_URL *registry_person_link_to_url(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when) {
608 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): searching for URL in person", p->guid, m->guid, u->url);
610 registry_person_urls_lock(p);
612 PERSON_URL *pu = dictionary_get(p->urls, u->url);
614 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
615 pu = registry_person_url_allocate(p, m, u, name, namelen, when);
616 registry.persons_urls_count++;
619 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
622 if(pu->machine != m) {
623 MACHINE_URL *mu = dictionary_get(pu->machine->urls, u->url);
625 info("registry_person_link_to_url('%s', '%s', '%s'): URL switched machines (old was '%s') - expiring it from previous machine.",
626 p->guid, m->guid, u->url, pu->machine->guid);
627 mu->flags |= REGISTRY_URL_FLAGS_EXPIRED;
630 info("registry_person_link_to_url('%s', '%s', '%s'): URL switched machines (old was '%s') - but the URL is not linked to the old machine.",
631 p->guid, m->guid, u->url, pu->machine->guid);
634 pu->machine->links--;
638 if(strcmp(pu->name, name)) {
639 // the name of the PERSON_URL has changed !
640 pu = registry_person_url_reallocate(p, m, u, name, namelen, when, pu);
647 if(pu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
648 info("registry_person_link_to_url('%s', '%s', '%s'): accessing an expired URL. Re-enabling URL.", p->guid, m->guid, u->url);
649 pu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
652 registry_person_urls_unlock(p);
657 static inline MACHINE_URL *registry_machine_link_to_url(PERSON *p, MACHINE *m, URL *u, time_t when) {
658 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): searching for URL in machine", p->guid, m->guid, u->url);
660 registry_machine_urls_lock(m);
662 MACHINE_URL *mu = dictionary_get(m->urls, u->url);
664 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
665 mu = registry_machine_url_allocate(m, u, when);
666 registry.machines_urls_count++;
669 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
673 //debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): indexing person in machine", p->guid, m->guid, u->url);
674 //dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
679 if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
680 info("registry_machine_link_to_url('%s', '%s', '%s'): accessing an expired URL.", p->guid, m->guid, u->url);
681 mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
684 registry_machine_urls_unlock(m);
689 // ----------------------------------------------------------------------------
690 // REGISTRY LOG LOAD/SAVE
692 static inline int registry_should_save_db(void) {
693 debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
694 return registry.log_count > registry.save_registry_every_entries;
697 static inline void registry_log(const char action, PERSON *p, MACHINE *m, URL *u, char *name) {
698 if(likely(registry.log_fp)) {
699 // we lock only if the file is open
700 // to allow replaying the log at registry_log_load()
703 if(unlikely(fprintf(registry.log_fp, "%c\t%08x\t%s\t%s\t%s\t%s\n",
710 error("Registry: failed to save log. Registry data may be lost in case of abnormal restart.");
712 // we increase the counter even on failures
713 // so that the registry will be saved periodically
714 registry.log_count++;
716 registry_log_unlock();
718 // this must be outside the log_lock(), or a deadlock will happen.
719 // registry_save() checks the same inside the log_lock, so only
720 // one thread will save the db
721 if(unlikely(registry_should_save_db()))
726 static inline int registry_log_open_nolock(void) {
728 fclose(registry.log_fp);
730 registry.log_fp = fopen(registry.log_filename, "a");
732 if(registry.log_fp) {
733 if (setvbuf(registry.log_fp, NULL, _IOLBF, 0) != 0)
734 error("Cannot set line buffering on registry log file.");
738 error("Cannot open registry log file '%s'. Registry data will be lost in case of netdata or server crash.", registry.log_filename);
742 static inline void registry_log_close_nolock(void) {
743 if(registry.log_fp) {
744 fclose(registry.log_fp);
745 registry.log_fp = NULL;
749 static inline void registry_log_recreate_nolock(void) {
750 if(registry.log_fp != NULL) {
751 registry_log_close_nolock();
753 // open it with truncate
754 registry.log_fp = fopen(registry.log_filename, "w");
755 if(registry.log_fp) fclose(registry.log_fp);
756 else error("Cannot truncate registry log '%s'", registry.log_filename);
758 registry.log_fp = NULL;
760 registry_log_open_nolock();
764 int registry_log_load(void) {
765 char *s, buf[4096 + 1];
768 // closing the log is required here
769 // otherwise we will append to it the values we read
770 registry_log_close_nolock();
772 debug(D_REGISTRY, "Registry: loading active db from: %s", registry.log_filename);
773 FILE *fp = fopen(registry.log_filename, "r");
775 error("Registry: cannot open registry file: %s", registry.log_filename);
779 while ((s = fgets_trim_len(buf, 4096, fp, &len))) {
783 case 'A': // accesses
786 // verify it is valid
787 if (unlikely(len < 85 || s[1] != '\t' || s[10] != '\t' || s[47] != '\t' || s[84] != '\t')) {
788 error("Registry: log line %u is wrong (len = %zu).", line, len);
791 s[1] = s[10] = s[47] = s[84] = '\0';
794 time_t when = strtoul(&s[2], NULL, 16);
795 char *person_guid = &s[11];
796 char *machine_guid = &s[48];
799 // skip the name to find the url
801 while(*url && *url != '\t') url++;
803 error("Registry: log line %u does not have a url.", line);
808 // make sure the person exists
809 // without this, a new person guid will be created
810 PERSON *p = registry_person_find(person_guid);
811 if(!p) p = registry_person_allocate(person_guid, when);
814 registry_request_access(p->guid, machine_guid, url, name, when);
816 registry_request_delete(p->guid, machine_guid, url, name, when);
821 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.log_filename, s);
827 // open the log again
828 registry_log_open_nolock();
834 // ----------------------------------------------------------------------------
837 PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
838 debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
840 MACHINE *m = registry_machine_get(machine_guid, when);
843 // make sure the name is valid
845 name = registry_fix_machine_name(name, &namelen);
848 url = registry_fix_url(url, &urllen);
850 URL *u = registry_url_get(url, urllen);
851 PERSON *p = registry_person_get(person_guid, when);
853 registry_person_link_to_url(p, m, u, name, namelen, when);
854 registry_machine_link_to_url(p, m, u, when);
856 registry_log('A', p, m, u, name);
858 registry.usages_count++;
862 // verify the person, the machine and the URL exist in our DB
863 PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, PERSON **pp, MACHINE **mm) {
864 char pbuf[36 + 1], mbuf[36 + 1];
866 if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
867 info("Registry Request Verification: invalid request! person: '%s', machine '%s', url '%s'", person_guid?person_guid:"UNSET", machine_guid?machine_guid:"UNSET", url?url:"UNSET");
872 url = registry_fix_url(url, NULL);
874 // make sure the person GUID is valid
875 if(registry_regenerate_guid(person_guid, pbuf) == -1) {
876 info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
881 // make sure the machine GUID is valid
882 if(registry_regenerate_guid(machine_guid, mbuf) == -1) {
883 info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
888 // make sure the machine exists
889 MACHINE *m = registry_machine_find(machine_guid);
891 info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
896 // make sure the person exist
897 PERSON *p = registry_person_find(person_guid);
899 info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
904 PERSON_URL *pu = dictionary_get(p->urls, url);
906 info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
912 PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
917 PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
918 if(!pu || !p || !m) return NULL;
921 delete_url = registry_fix_url(delete_url, NULL);
923 // make sure the user is not deleting the url it uses
924 if(!strcmp(delete_url, pu->url->url)) {
925 info("Registry Delete Request: delete URL is the one currently accessing, person: '%s', machine '%s', url '%s', delete url '%s'", p->guid, m->guid, pu->url->url, delete_url);
929 registry_person_urls_lock(p);
931 PERSON_URL *dpu = dictionary_get(p->urls, delete_url);
933 info("Registry Delete Request: URL not found for person, person: '%s', machine '%s', url '%s', delete url '%s'", p->guid, m->guid, pu->url->url, delete_url);
934 registry_person_urls_unlock(p);
938 registry_log('D', p, m, pu->url, dpu->url->url);
940 dictionary_del(p->urls, dpu->url->url);
941 registry_url_unlink_nolock(dpu->url);
944 registry_person_urls_unlock(p);
949 // a structure to pass to the dictionary_get_all() callback handler
950 struct machine_request_callback_data {
951 MACHINE *find_this_machine;
955 // the callback function
956 // this will be run for every PERSON_URL of this PERSON
957 int machine_request_callback(void *entry, void *data) {
958 PERSON_URL *mypu = (PERSON_URL *)entry;
959 struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
961 if(mypu->machine == myrdata->find_this_machine) {
962 myrdata->result = mypu;
963 return -1; // this will also stop the walk through
966 return 0; // continue
969 MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
976 PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
977 if(!pu || !p || !m) return NULL;
979 // make sure the machine GUID is valid
980 if(registry_regenerate_guid(request_machine, mbuf) == -1) {
981 info("Registry Machine URLs request: invalid machine GUID, person: '%s', machine '%s', url '%s', request machine '%s'", p->guid, m->guid, pu->url->url, request_machine);
984 request_machine = mbuf;
986 // make sure the machine exists
987 m = registry_machine_find(request_machine);
989 info("Registry Machine URLs request: machine not found, person: '%s', machine '%s', url '%s', request machine '%s'", p->guid, m->guid, pu->url->url, request_machine);
993 // Verify the user has in the past accessed this machine
994 // We will walk through the PERSON_URLs to find the machine
995 // linking to our machine
997 // a structure to pass to the dictionary_get_all() callback handler
998 struct machine_request_callback_data rdata = { m, NULL };
1000 // request a walk through on the dictionary
1001 // no need for locking here, the underlying dictionary has its own
1002 dictionary_get_all(p->urls, machine_request_callback, &rdata);
1011 // ----------------------------------------------------------------------------
1012 // REGISTRY JSON generation
1014 #ifndef REGISTRY_STANDALONE_TESTS
1016 static inline void registry_set_person_cookie(struct web_client *w, PERSON *p) {
1018 time_t et = time(NULL) + registry.persons_expiration;
1019 struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
1020 strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);
1022 if(registry.registry_domain && registry.registry_domain[0])
1023 snprintf(w->cookie, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", p->guid, registry.registry_domain, edate);
1025 snprintf(w->cookie, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", p->guid, edate);
1027 w->cookie[COOKIE_MAX] = '\0';
1031 static inline void registry_json_header(struct web_client *w, int status) {
1032 w->response.data->contenttype = CT_APPLICATION_JSON;
1033 buffer_flush(w->response.data);
1034 buffer_sprintf(w->response.data, "{\n\t\"success\": %s,\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
1035 status?"true":"false", registry.hostname, registry.machine_guid);
1038 static inline void registry_json_footer(struct web_client *w) {
1039 buffer_strcat(w->response.data, "\n}\n");
1042 int registry_json_redirect(struct web_client *w) {
1043 registry_json_header(w, 0);
1045 buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1046 registry.registry_to_announce);
1048 registry_json_footer(w);
1052 // structure used be the callbacks below
1053 struct registry_json_walk_person_urls_callback {
1056 struct web_client *w;
1060 // callback for rendering PERSON_URLs
1061 static inline int registry_json_person_url_callback(void *entry, void *data) {
1062 PERSON_URL *pu = (PERSON_URL *)entry;
1063 struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1064 struct web_client *w = c->w;
1066 if(unlikely(c->count++))
1067 buffer_strcat(w->response.data, ",");
1069 buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u, \"%s\" ]",
1070 pu->machine->guid, pu->url->url, pu->last_t, pu->usages, pu->name);
1075 // callback for rendering MACHINE_URLs
1076 static inline int registry_json_machine_url_callback(void *entry, void *data) {
1077 MACHINE_URL *mu = (MACHINE_URL *)entry;
1078 struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1079 struct web_client *w = c->w;
1082 if(unlikely(c->count++))
1083 buffer_strcat(w->response.data, ",");
1085 buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u ]",
1086 m->guid, mu->url->url, mu->last_t, mu->usages);
1092 // the main method for registering an access
1093 int registry_request_access_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
1094 if(!registry.enabled)
1095 return registry_json_redirect(w);
1097 PERSON *p = registry_request_access(person_guid, machine_guid, url, name, when);
1099 registry_json_header(w, 0);
1100 registry_json_footer(w);
1105 registry_set_person_cookie(w, p);
1107 // generate the response
1108 registry_json_header(w, 1);
1110 buffer_strcat(w->response.data, ",\n\t\"urls\": [");
1111 struct registry_json_walk_person_urls_callback c = { p, NULL, w, 0 };
1112 dictionary_get_all(p->urls, registry_json_person_url_callback, &c);
1113 buffer_strcat(w->response.data, "\n\t]\n");
1115 registry_json_footer(w);
1119 // the main method for deleting a URL from a person
1120 int registry_request_delete_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
1121 if(!registry.enabled)
1122 return registry_json_redirect(w);
1124 PERSON *p = registry_request_delete(person_guid, machine_guid, url, delete_url, when);
1126 registry_json_header(w, 0);
1127 registry_json_footer(w);
1131 // generate the response
1132 registry_json_header(w, 1);
1133 registry_json_footer(w);
1137 // the main method for searching the URLs of a netdata
1138 int registry_request_search_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
1139 if(!registry.enabled)
1140 return registry_json_redirect(w);
1142 MACHINE *m = registry_request_machine(person_guid, machine_guid, url, request_machine, when);
1144 registry_json_header(w, 0);
1145 registry_json_footer(w);
1149 registry_json_header(w, 1);
1151 buffer_strcat(w->response.data, ",\n\t\"urls\": [");
1152 struct registry_json_walk_person_urls_callback c = { NULL, m, w, 0 };
1153 dictionary_get_all(m->urls, registry_json_machine_url_callback, &c);
1154 buffer_strcat(w->response.data, "\n\t]\n");
1156 registry_json_footer(w);
1160 #endif /* ! REGISTRY_STANDALONE_TESTS */
1163 // ----------------------------------------------------------------------------
1164 // REGISTRY THIS MACHINE UNIQUE ID
1166 char *registry_get_this_machine_guid(void) {
1167 if(likely(registry.machine_guid[0]))
1168 return registry.machine_guid;
1170 // read it from disk
1171 int fd = open(registry.machine_guid_filename, O_RDONLY);
1174 if(read(fd, buf, 36) != 36)
1175 error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
1178 if(registry_regenerate_guid(buf, registry.machine_guid) == -1) {
1179 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
1180 buf, registry.machine_guid_filename);
1182 registry.machine_guid[0] = '\0';
1188 // generate a new one?
1189 if(!registry.machine_guid[0]) {
1190 int count = 10, ret = 0;
1193 // for some reason it reports unsafe generation the first time
1194 while(count-- && (ret = uuid_generate_time_safe(uuid)) == -1) ;
1195 uuid_unparse_lower(uuid, registry.machine_guid);
1196 registry.machine_guid[36] = '\0';
1199 info("Warning: generated machine GUID '%s' was not generated using a safe method.", registry.machine_guid);
1202 fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
1204 fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1206 if(write(fd, registry.machine_guid, 36) != 36)
1207 fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1212 return registry.machine_guid;
1216 // ----------------------------------------------------------------------------
1217 // REGISTRY LOAD/SAVE
1219 int registry_machine_save_url(void *entry, void *file) {
1220 MACHINE_URL *mu = entry;
1223 debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
1225 int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
1233 // error handling is done at registry_save()
1238 int registry_machine_save(void *entry, void *file) {
1242 debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
1244 int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
1252 int ret2 = dictionary_get_all(m->urls, registry_machine_save_url, fp);
1253 if(ret2 < 0) return ret2;
1257 // error handling is done at registry_save()
1262 static inline int registry_person_save_url(void *entry, void *file) {
1263 PERSON_URL *pu = entry;
1266 debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
1268 int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
1278 // error handling is done at registry_save()
1283 static inline int registry_person_save(void *entry, void *file) {
1287 debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
1289 int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
1297 int ret2 = dictionary_get_all(p->urls, registry_person_save_url, fp);
1298 if (ret2 < 0) return ret2;
1302 // error handling is done at registry_save()
1307 int registry_save(void) {
1308 if(!registry.enabled) return -1;
1310 // make sure the log is not updated
1311 registry_log_lock();
1313 if(unlikely(!registry_should_save_db())) {
1314 registry_log_unlock();
1318 char tmp_filename[FILENAME_MAX + 1];
1319 char old_filename[FILENAME_MAX + 1];
1321 snprintf(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
1322 snprintf(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
1324 debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
1325 FILE *fp = fopen(tmp_filename, "w");
1327 error("Registry: Cannot create file: %s", tmp_filename);
1328 registry_log_unlock();
1332 // dictionary_get_all() has its own locking, so this is safe to do
1334 debug(D_REGISTRY, "Saving all machines");
1335 int bytes1 = dictionary_get_all(registry.machines, registry_machine_save, fp);
1337 error("Registry: Cannot save registry machines - return value %d", bytes1);
1339 registry_log_unlock();
1342 debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
1344 debug(D_REGISTRY, "Saving all persons");
1345 int bytes2 = dictionary_get_all(registry.persons, registry_person_save, fp);
1347 error("Registry: Cannot save registry persons - return value %d", bytes2);
1349 registry_log_unlock();
1352 debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
1355 fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
1356 registry.persons_count,
1357 registry.machines_count,
1358 registry.usages_count + 1, // this is required - it is lost on db rotation
1359 registry.urls_count,
1360 registry.persons_urls_count,
1361 registry.machines_urls_count
1368 // remove the .old db
1369 debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
1370 if(unlink(old_filename) == -1 && errno != ENOENT)
1371 error("Registry: cannot remove old registry file '%s'", old_filename);
1373 // rename the db to .old
1374 debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
1375 if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
1376 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename, registry.db_filename);
1379 // remove the database (it is saved in .old)
1380 debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
1381 if (unlink(registry.db_filename) == -1 && errno != ENOENT)
1382 error("Registry: cannot remove old registry file '%s'", registry.db_filename);
1384 // move the .tmp to make it active
1385 debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
1386 if (link(tmp_filename, registry.db_filename) == -1) {
1387 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
1388 registry.db_filename);
1390 // move the .old back
1391 debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
1392 if(link(old_filename, registry.db_filename) == -1)
1393 error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
1396 debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
1397 if(unlink(tmp_filename) == -1)
1398 error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
1400 // it has been moved successfully
1401 // discard the current registry log
1402 registry_log_recreate_nolock();
1404 registry.log_count = 0;
1408 // continue operations
1409 registry_log_unlock();
1414 static inline size_t registry_load(void) {
1415 char *s, buf[4096 + 1];
1421 debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
1422 FILE *fp = fopen(registry.db_filename, "r");
1424 error("Registry: cannot open registry file: '%s'", registry.db_filename);
1429 while((s = fgets_trim_len(buf, 4096, fp, &len))) {
1432 debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
1435 if(unlikely(len != 103 || s[1] != '\t' || s[18] != '\t' || s[35] != '\t' || s[52] != '\t' || s[69] != '\t' || s[86] != '\t' || s[103] != '\0')) {
1436 error("Registry totals line %u is wrong (len = %zu).", line, len);
1439 registry.persons_count = strtoull(&s[2], NULL, 16);
1440 registry.machines_count = strtoull(&s[19], NULL, 16);
1441 registry.usages_count = strtoull(&s[36], NULL, 16);
1442 registry.urls_count = strtoull(&s[53], NULL, 16);
1443 registry.persons_urls_count = strtoull(&s[70], NULL, 16);
1444 registry.machines_urls_count = strtoull(&s[87], NULL, 16);
1449 // verify it is valid
1450 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1451 error("Registry person line %u is wrong (len = %zu).", line, len);
1455 s[1] = s[10] = s[19] = s[28] = '\0';
1456 p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
1457 p->last_t = strtoul(&s[11], NULL, 16);
1458 p->usages = strtoul(&s[20], NULL, 16);
1459 debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
1462 case 'M': // machine
1464 // verify it is valid
1465 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1466 error("Registry person line %u is wrong (len = %zu).", line, len);
1470 s[1] = s[10] = s[19] = s[28] = '\0';
1471 m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
1472 m->last_t = strtoul(&s[11], NULL, 16);
1473 m->usages = strtoul(&s[20], NULL, 16);
1474 debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
1477 case 'U': // person URL
1479 error("Registry: ignoring line %zu, no person loaded: %s", line, s);
1483 // verify it is valid
1484 if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
1485 error("Registry person URL line %u is wrong (len = %zu).", line, len);
1489 s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
1491 // skip the name to find the url
1493 while(*url && *url != '\t') url++;
1495 error("Registry person URL line %u does not have a url.", line);
1500 u = registry_url_allocate_nolock(url, strlen(url));
1502 time_t first_t = strtoul(&s[2], NULL, 16);
1504 m = registry_machine_find(&s[32]);
1505 if(!m) m = registry_machine_allocate(&s[32], first_t);
1507 PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
1508 pu->last_t = strtoul(&s[11], NULL, 16);
1509 pu->usages = strtoul(&s[20], NULL, 16);
1510 pu->flags = strtoul(&s[29], NULL, 16);
1511 debug(D_REGISTRY, "Registry loaded person URL '%s' with name '%s' of machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, pu->name, m->guid, pu->first_t, pu->last_t, pu->usages, pu->flags);
1514 case 'V': // machine URL
1516 error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
1520 // verify it is valid
1521 if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
1522 error("Registry person URL line %u is wrong (len = %zu).", line, len);
1526 s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
1527 u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
1529 MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
1530 mu->last_t = strtoul(&s[11], NULL, 16);
1531 mu->usages = strtoul(&s[20], NULL, 16);
1532 mu->flags = strtoul(&s[29], NULL, 16);
1533 debug(D_REGISTRY, "Registry loaded machine URL '%s', machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, m->guid, mu->first_t, mu->last_t, mu->usages, mu->flags);
1537 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
1546 // ----------------------------------------------------------------------------
1549 int registry_init(void) {
1550 char filename[FILENAME_MAX + 1];
1552 // registry enabled?
1553 registry.enabled = config_get_boolean("registry", "enabled", 0);
1556 registry.pathname = config_get("registry", "registry db directory", VARLIB_DIR "/registry");
1557 if(mkdir(registry.pathname, 0755) == -1 && errno != EEXIST) {
1558 error("Cannot create directory '%s'. Registry disabled.", registry.pathname);
1559 registry.enabled = 0;
1564 snprintf(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
1565 registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
1566 registry_get_this_machine_guid();
1568 snprintf(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
1569 registry.db_filename = config_get("registry", "registry db file", filename);
1571 snprintf(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
1572 registry.log_filename = config_get("registry", "registry log file", filename);
1574 // configuration options
1575 registry.save_registry_every_entries = config_get_number("registry", "registry save db every new entries", 1000000);
1576 registry.persons_expiration = config_get_number("registry", "registry expire idle persons days", 365) * 86400;
1577 registry.registry_domain = config_get("registry", "registry domain", "");
1578 registry.registry_to_announce = config_get("registry", "registry to announce", "https://registry.netdata.online");
1579 registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", hostname));
1581 registry.max_url_length = config_get_number("registry", "max URL length", 1024);
1582 registry.max_name_length = config_get_number("registry", "max URL name length", 50);
1585 // initialize entries counters
1586 registry.persons_count = 0;
1587 registry.machines_count = 0;
1588 registry.usages_count = 0;
1589 registry.urls_count = 0;
1590 registry.persons_urls_count = 0;
1591 registry.machines_urls_count = 0;
1593 // initialize memory counters
1594 registry.persons_memory = 0;
1595 registry.machines_memory = 0;
1596 registry.urls_memory = 0;
1597 registry.persons_urls_memory = 0;
1598 registry.machines_urls_memory = 0;
1601 pthread_mutex_init(®istry.persons_lock, NULL);
1602 pthread_mutex_init(®istry.machines_lock, NULL);
1603 pthread_mutex_init(®istry.urls_lock, NULL);
1604 pthread_mutex_init(®istry.person_urls_lock, NULL);
1605 pthread_mutex_init(®istry.machine_urls_lock, NULL);
1607 // create dictionaries
1608 registry.persons = dictionary_create(DICTIONARY_FLAGS);
1609 registry.machines = dictionary_create(DICTIONARY_FLAGS);
1610 registry.urls = dictionary_create(DICTIONARY_FLAGS);
1612 // load the registry database
1613 if(registry.enabled) {
1614 registry_log_open_nolock();
1616 registry_log_load();
1622 void registry_free(void) {
1623 if(!registry.enabled) return;
1625 // we need to destroy the dictionaries ourselves
1626 // since the dictionaries use memory we allocated
1628 while(registry.persons->values_index.root) {
1629 PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
1631 // fprintf(stderr, "\nPERSON: '%s', first: %u, last: %u, usages: %u\n", p->guid, p->first_t, p->last_t, p->usages);
1633 while(p->urls->values_index.root) {
1634 PERSON_URL *pu = ((NAME_VALUE *)p->urls->values_index.root)->value;
1636 // fprintf(stderr, "\tURL: '%s', first: %u, last: %u, usages: %u, flags: 0x%02x\n", pu->url->url, pu->first_t, pu->last_t, pu->usages, pu->flags);
1638 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", pu->url->url, p->guid);
1639 dictionary_del(p->urls, pu->url->url);
1641 debug(D_REGISTRY, "Registry: unlinking url '%s' from person", pu->url->url);
1642 registry_url_unlink_nolock(pu->url);
1644 debug(D_REGISTRY, "Registry: freeing person url");
1648 debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
1649 dictionary_del(registry.persons, p->guid);
1651 debug(D_REGISTRY, "Registry: destroying URL dictionary of person '%s'", p->guid);
1652 dictionary_destroy(p->urls);
1654 debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
1658 while(registry.machines->values_index.root) {
1659 MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
1661 // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
1663 while(m->urls->values_index.root) {
1664 MACHINE_URL *mu = ((NAME_VALUE *)m->urls->values_index.root)->value;
1666 // 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);
1668 //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
1669 //dictionary_destroy(mu->persons);
1671 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
1672 dictionary_del(m->urls, mu->url->url);
1674 debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
1675 registry_url_unlink_nolock(mu->url);
1677 debug(D_REGISTRY, "Registry: freeing machine url");
1681 debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
1682 dictionary_del(registry.machines, m->guid);
1684 debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
1685 dictionary_destroy(m->urls);
1687 debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
1691 // and free the memory of remaining dictionary structures
1693 debug(D_REGISTRY, "Registry: destroying persons dictionary");
1694 dictionary_destroy(registry.persons);
1696 debug(D_REGISTRY, "Registry: destroying machines dictionary");
1697 dictionary_destroy(registry.machines);
1699 debug(D_REGISTRY, "Registry: destroying urls dictionary");
1700 dictionary_destroy(registry.urls);
1703 // ----------------------------------------------------------------------------
1706 void registry_statistics(void) {
1707 if(!registry.enabled) return;
1709 static RRDSET *sts = NULL, *stc = NULL, *stm = NULL;
1711 if(!sts) sts = rrdset_find("netdata.registry_sessions");
1713 sts = rrdset_create("netdata", "registry_sessions", NULL, "registry", NULL, "NetData Registry Sessions", "session", 131000, rrd_update_every, RRDSET_TYPE_LINE);
1715 rrddim_add(sts, "sessions", NULL, 1, 1, RRDDIM_ABSOLUTE);
1717 else rrdset_next(sts);
1719 rrddim_set(sts, "sessions", registry.usages_count);
1722 // ------------------------------------------------------------------------
1724 if(!stc) stc = rrdset_find("netdata.registry_entries");
1726 stc = rrdset_create("netdata", "registry_entries", NULL, "registry", NULL, "NetData Registry Entries", "entries", 131100, rrd_update_every, RRDSET_TYPE_LINE);
1728 rrddim_add(stc, "persons", NULL, 1, 1, RRDDIM_ABSOLUTE);
1729 rrddim_add(stc, "machines", NULL, 1, 1, RRDDIM_ABSOLUTE);
1730 rrddim_add(stc, "urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1731 rrddim_add(stc, "persons_urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1732 rrddim_add(stc, "machines_urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1734 else rrdset_next(stc);
1736 rrddim_set(stc, "persons", registry.persons_count);
1737 rrddim_set(stc, "machines", registry.machines_count);
1738 rrddim_set(stc, "urls", registry.urls_count);
1739 rrddim_set(stc, "persons_urls", registry.persons_urls_count);
1740 rrddim_set(stc, "machines_urls", registry.machines_urls_count);
1743 // ------------------------------------------------------------------------
1745 if(!stm) stm = rrdset_find("netdata.registry_mem");
1747 stm = rrdset_create("netdata", "registry_mem", NULL, "registry", NULL, "NetData Registry Memory", "KB", 131300, rrd_update_every, RRDSET_TYPE_STACKED);
1749 rrddim_add(stm, "persons", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1750 rrddim_add(stm, "machines", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1751 rrddim_add(stm, "urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1752 rrddim_add(stm, "persons_urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1753 rrddim_add(stm, "machines_urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1755 else rrdset_next(stm);
1757 rrddim_set(stm, "persons", registry.persons_memory + registry.persons_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1758 rrddim_set(stm, "machines", registry.machines_memory + registry.machines_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1759 rrddim_set(stm, "urls", registry.urls_memory + registry.urls_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1760 rrddim_set(stm, "persons_urls", registry.persons_urls_memory + registry.persons_count * sizeof(DICTIONARY) + registry.persons_urls_count * sizeof(NAME_VALUE));
1761 rrddim_set(stm, "machines_urls", registry.machines_urls_memory + registry.machines_count * sizeof(DICTIONARY) + registry.machines_urls_count * sizeof(NAME_VALUE));
1766 #ifdef REGISTRY_STANDALONE_TESTS
1768 // ----------------------------------------------------------------------------
1771 int test1(int argc, char **argv) {
1773 void print_stats(uint32_t requests, unsigned long long start, unsigned long long end) {
1774 fprintf(stderr, " > SPEED: %u requests served in %0.2f seconds ( >>> %llu per second <<< )\n",
1775 requests, (end-start) / 1000000.0, (unsigned long long)requests * 1000000ULL / (end-start));
1777 fprintf(stderr, " > DB : persons %llu, machines %llu, unique URLs %llu, accesses %llu, URLs: for persons %llu, for machines %llu\n",
1778 registry.persons_count, registry.machines_count, registry.urls_count, registry.usages_count,
1779 registry.persons_urls_count, registry.machines_urls_count);
1785 uint32_t u, users = 1000000;
1786 uint32_t m, machines = 200000;
1787 uint32_t machines2 = machines * 2;
1789 char **users_guids = malloc(users * sizeof(char *));
1790 char **machines_guids = malloc(machines2 * sizeof(char *));
1791 char **machines_urls = malloc(machines2 * sizeof(char *));
1792 unsigned long long start;
1796 fprintf(stderr, "Generating %u machine guids\n", machines2);
1797 for(m = 0; m < machines2 ;m++) {
1799 machines_guids[m] = malloc(36+1);
1800 uuid_generate(uuid);
1801 uuid_unparse(uuid, machines_guids[m]);
1803 char buf[FILENAME_MAX + 1];
1804 snprintf(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
1805 machines_urls[m] = strdup(buf);
1807 // fprintf(stderr, "\tmachine %u: '%s', url: '%s'\n", m + 1, machines_guids[m], machines_urls[m]);
1811 fprintf(stderr, "\nGenerating %u users accessing %u machines\n", users, machines);
1813 time_t now = time(NULL);
1814 for(u = 0; u < users ; u++) {
1815 if(++m == machines) m = 0;
1817 PERSON *p = registry_request_access(NULL, machines_guids[m], machines_urls[m], "test", now);
1818 users_guids[u] = p->guid;
1820 print_stats(u, start, timems());
1823 fprintf(stderr, "\nAll %u users accessing again the same %u servers\n", users, machines);
1826 for(u = 0; u < users ; u++) {
1827 if(++m == machines) m = 0;
1829 PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
1831 if(p->guid != users_guids[u])
1832 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
1834 print_stats(u, start, timems());
1837 fprintf(stderr, "\nAll %u users accessing a new server, out of the %u servers\n", users, machines);
1840 for(u = 0; u < users ; u++) {
1841 if(++m == machines) m = 0;
1843 PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
1845 if(p->guid != users_guids[u])
1846 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
1848 print_stats(u, start, timems());
1851 fprintf(stderr, "\n%u random users accessing a random server, out of the %u servers\n", users, machines);
1853 for(u = 0; u < users ; u++) {
1854 uint32_t tu = random() * users / RAND_MAX;
1855 uint32_t tm = random() * machines / RAND_MAX;
1857 PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
1859 if(p->guid != users_guids[tu])
1860 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
1862 print_stats(u, start, timems());
1865 fprintf(stderr, "\n%u random users accessing a random server, out of %u servers\n", users, machines2);
1867 for(u = 0; u < users ; u++) {
1868 uint32_t tu = random() * users / RAND_MAX;
1869 uint32_t tm = random() * machines2 / RAND_MAX;
1871 PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
1873 if(p->guid != users_guids[tu])
1874 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
1876 print_stats(u, start, timems());
1878 for(m = 0; m < 10; m++) {
1881 "\n%u random user accesses to a random server, out of %u servers,\n > using 1/10000 with a random url, 1/1000 with a mismatched url\n",
1882 users * 2, machines2);
1884 for (u = 0; u < users * 2; u++) {
1885 uint32_t tu = random() * users / RAND_MAX;
1886 uint32_t tm = random() * machines2 / RAND_MAX;
1888 char *url = machines_urls[tm];
1889 char buf[FILENAME_MAX + 1];
1890 if (random() % 10000 == 1234) {
1891 snprintf(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
1894 else if (random() % 1000 == 123)
1895 url = machines_urls[random() * machines2 / RAND_MAX];
1897 PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], url, "test", now);
1899 if (p->guid != users_guids[tu])
1900 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
1902 print_stats(u, start, timems());
1905 fprintf(stderr, "\n\nSAVE\n");
1908 print_stats(registry.persons_count, start, timems());
1910 fprintf(stderr, "\n\nCLEANUP\n");
1913 print_stats(registry.persons_count, start, timems());
1917 // ----------------------------------------------------------------------------
1920 int main(int argc, char **argv) {
1921 // debug_flags = 0xFFFFFFFF;
1922 // test1(argc, argv);
1931 fprintf(stderr, "\n\nINITIALIZATION\n");
1937 fprintf(stderr, "\n\nADDING ENTRY\n");
1938 p1 = registry_request_access("2c95abd0-1542-11e6-8c66-00508db7e9c9", "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
1942 #ifdef REGISTRY_STDOUT_DUMP
1943 fprintf(stderr, "\n\nADDING ENTRY\n");
1944 #endif /* REGISTRY_STDOUT_DUMP */
1945 p1 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
1947 #ifdef REGISTRY_STDOUT_DUMP
1948 fprintf(stderr, "\n\nADDING ANOTHER URL\n");
1949 #endif /* REGISTRY_STDOUT_DUMP */
1950 p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://127.0.0.1:19999/", "test", time(NULL));
1952 #ifdef REGISTRY_STDOUT_DUMP
1953 fprintf(stderr, "\n\nADDING ANOTHER URL\n");
1954 #endif /* REGISTRY_STDOUT_DUMP */
1955 p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
1957 #ifdef REGISTRY_STDOUT_DUMP
1958 fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
1959 #endif /* REGISTRY_STDOUT_DUMP */
1960 p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
1962 #ifdef REGISTRY_STDOUT_DUMP
1963 fprintf(stderr, "\n\nADDING ANOTHER PERSON\n");
1964 #endif /* REGISTRY_STDOUT_DUMP */
1965 p2 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
1967 #ifdef REGISTRY_STDOUT_DUMP
1968 fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
1969 #endif /* REGISTRY_STDOUT_DUMP */
1970 p2 = registry_request_access(p2->guid, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
1973 fprintf(stderr, "\n\nSAVE\n");
1976 fprintf(stderr, "\n\nCLEANUP\n");
1981 #endif /* REGISTRY_STANDALONE_TESTS */