3 // ----------------------------------------------------------------------------
6 // 1. the default tracking cookie expires in 1 year, but the persons are not
7 // removed from the db - this means the database only grows - ideally the
8 // database should be cleaned in registry_save() for both on-disk and
12 // i. Find all the PERSONs that have expired cookie
13 // ii. For each of their PERSON_URLs:
14 // - decrement the linked MACHINE links
15 // - if the linked MACHINE has no other links, remove the linked MACHINE too
16 // - remove the PERSON_URL
18 // 2. add protection to prevent abusing the registry by flooding it with
19 // requests to fill the memory and crash it.
21 // Possible protections:
22 // - limit the number of URLs per person
23 // - limit the number of URLs per machine
24 // - limit the number of persons
25 // - limit the number of machines
26 // - [DONE] limit the size of URLs
27 // - [DONE] limit the size of PERSON_URL names
28 // - limit the number of requests that add data to the registry,
29 // per client IP per hour
33 #define REGISTRY_URL_FLAGS_DEFAULT 0x00
34 #define REGISTRY_URL_FLAGS_EXPIRED 0x01
36 #define DICTIONARY_FLAGS DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE | DICTIONARY_FLAG_NAME_LINK_DONT_CLONE
38 // ----------------------------------------------------------------------------
44 char machine_guid[36 + 1];
46 // entries counters / statistics
47 unsigned long long persons_count;
48 unsigned long long machines_count;
49 unsigned long long usages_count;
50 unsigned long long urls_count;
51 unsigned long long persons_urls_count;
52 unsigned long long machines_urls_count;
53 unsigned long long log_count;
55 // memory counters / statistics
56 unsigned long long persons_memory;
57 unsigned long long machines_memory;
58 unsigned long long urls_memory;
59 unsigned long long persons_urls_memory;
60 unsigned long long machines_urls_memory;
63 unsigned long long save_registry_every_entries;
64 char *registry_domain;
66 char *registry_to_announce;
67 time_t persons_expiration; // seconds to expire idle persons
68 int verify_cookies_redirects;
70 size_t max_url_length;
71 size_t max_name_length;
77 char *machine_guid_filename;
83 DICTIONARY *persons; // dictionary of PERSON *, with key the PERSON.guid
84 DICTIONARY *machines; // dictionary of MACHINE *, with key the MACHINE.guid
85 DICTIONARY *urls; // dictionary of URL *, with key the URL.url
87 // concurrency locking
88 // we keep different locks for different things
89 // so that many tasks can be completed in parallel
90 pthread_mutex_t persons_lock;
91 pthread_mutex_t machines_lock;
92 pthread_mutex_t urls_lock;
93 pthread_mutex_t person_urls_lock;
94 pthread_mutex_t machine_urls_lock;
95 pthread_mutex_t log_lock;
99 // ----------------------------------------------------------------------------
101 // Save memory by de-duplicating URLs
102 // so instead of storing URLs all over the place
103 // we store them here and we keep pointers elsewhere
106 uint32_t links; // the number of links to this URL - when none is left, we free it
107 uint16_t len; // the length of the URL in bytes
108 char url[1]; // the URL - dynamically allocated to more size
110 typedef struct url URL;
113 // ----------------------------------------------------------------------------
114 // MACHINE structures
116 // For each MACHINE-URL pair we keep this
118 URL *url; // de-duplicated URL
119 // DICTIONARY *persons; // dictionary of PERSON *
122 uint32_t first_t; // the first time we saw this
123 uint32_t last_t; // the last time we saw this
124 uint32_t usages; // how many times this has been accessed
126 typedef struct machine_url MACHINE_URL;
130 char guid[36 + 1]; // the GUID
132 uint32_t links; // the number of PERSON_URLs linked to this machine
134 DICTIONARY *urls; // MACHINE_URL *
136 uint32_t first_t; // the first time we saw this
137 uint32_t last_t; // the last time we saw this
138 uint32_t usages; // how many times this has been accessed
140 typedef struct machine MACHINE;
143 // ----------------------------------------------------------------------------
146 // for each PERSON-URL pair we keep this
148 URL *url; // de-duplicated URL
149 MACHINE *machine; // link the MACHINE of this URL
152 uint32_t first_t; // the first time we saw this
153 uint32_t last_t; // the last time we saw this
154 uint32_t usages; // how many times this has been accessed
156 char name[1]; // the name of the URL, as known by the user
157 // dynamically allocated to fit properly
159 typedef struct person_url PERSON_URL;
163 char guid[36 + 1]; // the person GUID
165 DICTIONARY *urls; // dictionary of PERSON_URL *
167 uint32_t first_t; // the first time we saw this
168 uint32_t last_t; // the last time we saw this
169 uint32_t usages; // how many times this has been accessed
171 typedef struct person PERSON;
174 // ----------------------------------------------------------------------------
175 // REGISTRY concurrency locking
177 static inline void registry_persons_lock(void) {
178 pthread_mutex_lock(®istry.persons_lock);
181 static inline void registry_persons_unlock(void) {
182 pthread_mutex_unlock(®istry.persons_lock);
185 static inline void registry_machines_lock(void) {
186 pthread_mutex_lock(®istry.machines_lock);
189 static inline void registry_machines_unlock(void) {
190 pthread_mutex_unlock(®istry.machines_lock);
193 static inline void registry_urls_lock(void) {
194 pthread_mutex_lock(®istry.urls_lock);
197 static inline void registry_urls_unlock(void) {
198 pthread_mutex_unlock(®istry.urls_lock);
201 // ideally, we should not lock the whole registry for
202 // updating a person's urls.
203 // however, to save the memory required for keeping a
204 // mutex (40 bytes) per person, we do...
205 static inline void registry_person_urls_lock(PERSON *p) {
207 pthread_mutex_lock(®istry.person_urls_lock);
210 static inline void registry_person_urls_unlock(PERSON *p) {
212 pthread_mutex_unlock(®istry.person_urls_lock);
215 // ideally, we should not lock the whole registry for
216 // updating a machine's urls.
217 // however, to save the memory required for keeping a
218 // mutex (40 bytes) per machine, we do...
219 static inline void registry_machine_urls_lock(MACHINE *m) {
221 pthread_mutex_lock(®istry.machine_urls_lock);
224 static inline void registry_machine_urls_unlock(MACHINE *m) {
226 pthread_mutex_unlock(®istry.machine_urls_lock);
229 static inline void registry_log_lock(void) {
230 pthread_mutex_lock(®istry.log_lock);
233 static inline void registry_log_unlock(void) {
234 pthread_mutex_unlock(®istry.log_lock);
238 // ----------------------------------------------------------------------------
241 // parse a GUID and re-generated to be always lower case
242 // this is used as a protection against the variations of GUIDs
243 static inline int registry_regenerate_guid(const char *guid, char *result) {
245 if(unlikely(uuid_parse(guid, uuid) == -1)) {
246 info("Registry: GUID '%s' is not a valid GUID.", guid);
250 uuid_unparse_lower(uuid, result);
252 #ifdef NETDATA_INTERNAL_CHECKS
253 if(strcmp(guid, result))
254 info("Registry: source GUID '%s' and re-generated GUID '%s' differ!", guid, result);
255 #endif /* NETDATA_INTERNAL_CHECKS */
261 // make sure the names of the machines / URLs do not contain any tabs
262 // (which are used as our separator in the database files)
263 // and are properly trimmed (before and after)
264 static inline char *registry_fix_machine_name(char *name, size_t *len) {
265 char *s = name?name:"";
267 // skip leading spaces
268 while(*s && isspace(*s)) s++;
270 // make sure all spaces are a SPACE
273 if(unlikely(isspace(*t)))
279 // remove trailing spaces
294 static inline char *registry_fix_url(char *url, size_t *len) {
295 return registry_fix_machine_name(url, len);
299 // ----------------------------------------------------------------------------
300 // forward definition of functions
302 extern PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
303 extern PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
306 // ----------------------------------------------------------------------------
309 static inline URL *registry_url_allocate_nolock(const char *url, size_t urllen) {
310 // protection from too big URLs
311 if(urllen > registry.max_url_length)
312 urllen = registry.max_url_length;
314 debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): allocating %zu bytes", url, sizeof(URL) + urllen);
315 URL *u = mallocz(sizeof(URL) + urllen);
317 // a simple strcpy() should do the job
318 // but I prefer to be safe, since the caller specified urllen
319 u->len = (uint16_t)urllen;
320 strncpyz(u->url, url, u->len);
323 registry.urls_memory += sizeof(URL) + urllen;
325 debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): indexing it", url);
326 dictionary_set(registry.urls, u->url, u, sizeof(URL));
331 static inline URL *registry_url_get(const char *url, size_t urllen) {
332 debug(D_REGISTRY, "Registry: registry_url_get('%s')", url);
334 registry_urls_lock();
336 URL *u = dictionary_get(registry.urls, url);
338 u = registry_url_allocate_nolock(url, urllen);
339 registry.urls_count++;
342 registry_urls_unlock();
347 static inline void registry_url_link_nolock(URL *u) {
349 debug(D_REGISTRY, "Registry: registry_url_link_nolock('%s'): URL has now %u links", u->url, u->links);
352 static inline void registry_url_unlink_nolock(URL *u) {
355 debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): No more links for this URL", u->url);
356 dictionary_del(registry.urls, u->url);
360 debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): URL has %u links left", u->url, u->links);
364 // ----------------------------------------------------------------------------
367 static inline MACHINE *registry_machine_find(const char *machine_guid) {
368 debug(D_REGISTRY, "Registry: registry_machine_find('%s')", machine_guid);
369 return dictionary_get(registry.machines, machine_guid);
372 static inline MACHINE_URL *registry_machine_url_allocate(MACHINE *m, URL *u, time_t when) {
373 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(MACHINE_URL));
375 MACHINE_URL *mu = mallocz(sizeof(MACHINE_URL));
377 // mu->persons = dictionary_create(DICTIONARY_FLAGS);
378 // dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
380 mu->first_t = mu->last_t = (uint32_t)when;
383 mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
385 registry.machines_urls_memory += sizeof(MACHINE_URL);
387 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): indexing URL in machine", m->guid, u->url);
388 dictionary_set(m->urls, u->url, mu, sizeof(MACHINE_URL));
389 registry_url_link_nolock(u);
394 static inline MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
395 debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(MACHINE));
397 MACHINE *m = mallocz(sizeof(MACHINE));
399 strncpyz(m->guid, machine_guid, 36);
401 debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
402 m->urls = dictionary_create(DICTIONARY_FLAGS);
404 m->first_t = m->last_t = (uint32_t)when;
407 registry.machines_memory += sizeof(MACHINE);
409 registry.machines_count++;
410 dictionary_set(registry.machines, m->guid, m, sizeof(MACHINE));
415 // 1. validate machine GUID
416 // 2. if it is valid, find it or create it and return it
417 // 3. if it is not valid, return NULL
418 static inline MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
421 registry_machines_lock();
423 if(likely(machine_guid && *machine_guid)) {
424 // validate it is a GUID
426 if(unlikely(registry_regenerate_guid(machine_guid, buf) == -1))
427 info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
430 m = registry_machine_find(machine_guid);
431 if(!m) m = registry_machine_allocate(machine_guid, when);
435 registry_machines_unlock();
441 // ----------------------------------------------------------------------------
444 static inline PERSON *registry_person_find(const char *person_guid) {
445 debug(D_REGISTRY, "Registry: registry_person_find('%s')", person_guid);
446 return dictionary_get(registry.persons, person_guid);
449 static inline PERSON_URL *registry_person_url_allocate(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when) {
450 // protection from too big names
451 if(namelen > registry.max_name_length)
452 namelen = registry.max_name_length;
454 debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
455 sizeof(PERSON_URL) + namelen);
457 PERSON_URL *pu = mallocz(sizeof(PERSON_URL) + namelen);
459 // a simple strcpy() should do the job
460 // but I prefer to be safe, since the caller specified urllen
461 strncpyz(pu->name, name, namelen);
464 pu->first_t = pu->last_t = when;
467 pu->flags = REGISTRY_URL_FLAGS_DEFAULT;
470 registry.persons_urls_memory += sizeof(PERSON_URL) + namelen;
472 debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): indexing URL in person", p->guid, m->guid, u->url);
473 dictionary_set(p->urls, u->url, pu, sizeof(PERSON_URL));
474 registry_url_link_nolock(u);
479 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) {
480 // this function is needed to change the name of a PERSON_URL
482 debug(D_REGISTRY, "registry_person_url_reallocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
483 sizeof(PERSON_URL) + namelen);
485 PERSON_URL *tpu = registry_person_url_allocate(p, m, u, name, namelen, when);
486 tpu->first_t = pu->first_t;
487 tpu->last_t = pu->last_t;
488 tpu->usages = pu->usages;
490 // ok, these are a hack - since the registry_person_url_allocate() is
491 // adding these, we have to subtract them
492 tpu->machine->links--;
493 registry.persons_urls_memory -= sizeof(PERSON_URL) + strlen(pu->name);
494 registry_url_unlink_nolock(u);
501 static inline PERSON *registry_person_allocate(const char *person_guid, time_t when) {
504 debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(PERSON));
506 p = mallocz(sizeof(PERSON));
512 uuid_unparse_lower(uuid, p->guid);
514 debug(D_REGISTRY, "Registry: Checking if the generated person guid '%s' is unique", p->guid);
515 if (!dictionary_get(registry.persons, p->guid)) {
516 debug(D_REGISTRY, "Registry: generated person guid '%s' is unique", p->guid);
520 info("Registry: generated person guid '%s' found in the registry. Retrying...", p->guid);
524 strncpyz(p->guid, person_guid, 36);
526 debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): creating dictionary of urls", p->guid);
527 p->urls = dictionary_create(DICTIONARY_FLAGS);
529 p->first_t = p->last_t = when;
532 registry.persons_memory += sizeof(PERSON);
534 registry.persons_count++;
535 dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
541 // 1. validate person GUID
542 // 2. if it is valid, find it
543 // 3. if it is not valid, create a new one
545 static inline PERSON *registry_person_get(const char *person_guid, time_t when) {
548 registry_persons_lock();
550 if(person_guid && *person_guid) {
552 // validate it is a GUID
553 if(unlikely(registry_regenerate_guid(person_guid, buf) == -1))
554 info("Registry: person guid '%s' is not a valid guid. Ignoring it.", person_guid);
557 p = registry_person_find(person_guid);
558 if(!p) person_guid = NULL;
562 if(!p) p = registry_person_allocate(NULL, when);
564 registry_persons_unlock();
569 // ----------------------------------------------------------------------------
570 // LINKING OF OBJECTS
572 static inline PERSON_URL *registry_person_link_to_url(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when) {
573 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): searching for URL in person", p->guid, m->guid, u->url);
575 registry_person_urls_lock(p);
577 PERSON_URL *pu = dictionary_get(p->urls, u->url);
579 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
580 pu = registry_person_url_allocate(p, m, u, name, namelen, when);
581 registry.persons_urls_count++;
584 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
586 if(likely(pu->last_t < (uint32_t)when)) pu->last_t = when;
588 if(pu->machine != m) {
589 MACHINE_URL *mu = dictionary_get(pu->machine->urls, u->url);
591 info("registry_person_link_to_url('%s', '%s', '%s'): URL switched machines (old was '%s') - expiring it from previous machine.",
592 p->guid, m->guid, u->url, pu->machine->guid);
593 mu->flags |= REGISTRY_URL_FLAGS_EXPIRED;
596 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.",
597 p->guid, m->guid, u->url, pu->machine->guid);
600 pu->machine->links--;
604 if(strcmp(pu->name, name)) {
605 // the name of the PERSON_URL has changed !
606 pu = registry_person_url_reallocate(p, m, u, name, namelen, when, pu);
611 if(likely(p->last_t < (uint32_t)when)) p->last_t = when;
613 if(pu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
614 info("registry_person_link_to_url('%s', '%s', '%s'): accessing an expired URL. Re-enabling URL.", p->guid, m->guid, u->url);
615 pu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
618 registry_person_urls_unlock(p);
623 static inline MACHINE_URL *registry_machine_link_to_url(PERSON *p, MACHINE *m, URL *u, time_t when) {
624 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): searching for URL in machine", p->guid, m->guid, u->url);
626 registry_machine_urls_lock(m);
628 MACHINE_URL *mu = dictionary_get(m->urls, u->url);
630 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
631 mu = registry_machine_url_allocate(m, u, when);
632 registry.machines_urls_count++;
635 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
637 if(likely(mu->last_t < (uint32_t)when)) mu->last_t = when;
640 //debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): indexing person in machine", p->guid, m->guid, u->url);
641 //dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
644 if(likely(m->last_t < (uint32_t)when)) m->last_t = when;
646 if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
647 info("registry_machine_link_to_url('%s', '%s', '%s'): accessing an expired URL.", p->guid, m->guid, u->url);
648 mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
651 registry_machine_urls_unlock(m);
656 // ----------------------------------------------------------------------------
657 // REGISTRY LOG LOAD/SAVE
659 static inline int registry_should_save_db(void) {
660 debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
661 return registry.log_count > registry.save_registry_every_entries;
664 static inline void registry_log(const char action, PERSON *p, MACHINE *m, URL *u, char *name) {
665 if(likely(registry.log_fp)) {
666 // we lock only if the file is open
667 // to allow replaying the log at registry_log_load()
670 if(unlikely(fprintf(registry.log_fp, "%c\t%08x\t%s\t%s\t%s\t%s\n",
677 error("Registry: failed to save log. Registry data may be lost in case of abnormal restart.");
679 // we increase the counter even on failures
680 // so that the registry will be saved periodically
681 registry.log_count++;
683 registry_log_unlock();
685 // this must be outside the log_lock(), or a deadlock will happen.
686 // registry_save() checks the same inside the log_lock, so only
687 // one thread will save the db
688 if(unlikely(registry_should_save_db()))
693 static inline int registry_log_open_nolock(void) {
695 fclose(registry.log_fp);
697 registry.log_fp = fopen(registry.log_filename, "a");
699 if(registry.log_fp) {
700 if (setvbuf(registry.log_fp, NULL, _IOLBF, 0) != 0)
701 error("Cannot set line buffering on registry log file.");
705 error("Cannot open registry log file '%s'. Registry data will be lost in case of netdata or server crash.", registry.log_filename);
709 static inline void registry_log_close_nolock(void) {
710 if(registry.log_fp) {
711 fclose(registry.log_fp);
712 registry.log_fp = NULL;
716 static inline void registry_log_recreate_nolock(void) {
717 if(registry.log_fp != NULL) {
718 registry_log_close_nolock();
720 // open it with truncate
721 registry.log_fp = fopen(registry.log_filename, "w");
722 if(registry.log_fp) fclose(registry.log_fp);
723 else error("Cannot truncate registry log '%s'", registry.log_filename);
725 registry.log_fp = NULL;
727 registry_log_open_nolock();
731 int registry_log_load(void) {
732 char *s, buf[4096 + 1];
735 // closing the log is required here
736 // otherwise we will append to it the values we read
737 registry_log_close_nolock();
739 debug(D_REGISTRY, "Registry: loading active db from: %s", registry.log_filename);
740 FILE *fp = fopen(registry.log_filename, "r");
742 error("Registry: cannot open registry file: %s", registry.log_filename);
746 while ((s = fgets_trim_len(buf, 4096, fp, &len))) {
750 case 'A': // accesses
753 // verify it is valid
754 if (unlikely(len < 85 || s[1] != '\t' || s[10] != '\t' || s[47] != '\t' || s[84] != '\t')) {
755 error("Registry: log line %zu is wrong (len = %zu).", line, len);
758 s[1] = s[10] = s[47] = s[84] = '\0';
761 time_t when = strtoul(&s[2], NULL, 16);
762 char *person_guid = &s[11];
763 char *machine_guid = &s[48];
766 // skip the name to find the url
768 while(*url && *url != '\t') url++;
770 error("Registry: log line %zu does not have a url.", line);
775 // make sure the person exists
776 // without this, a new person guid will be created
777 PERSON *p = registry_person_find(person_guid);
778 if(!p) p = registry_person_allocate(person_guid, when);
781 registry_request_access(p->guid, machine_guid, url, name, when);
783 registry_request_delete(p->guid, machine_guid, url, name, when);
788 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.log_filename, s);
796 // open the log again
797 registry_log_open_nolock();
803 // ----------------------------------------------------------------------------
806 PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
807 debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
809 MACHINE *m = registry_machine_get(machine_guid, when);
812 // make sure the name is valid
814 name = registry_fix_machine_name(name, &namelen);
817 url = registry_fix_url(url, &urllen);
819 URL *u = registry_url_get(url, urllen);
820 PERSON *p = registry_person_get(person_guid, when);
822 registry_person_link_to_url(p, m, u, name, namelen, when);
823 registry_machine_link_to_url(p, m, u, when);
825 registry_log('A', p, m, u, name);
827 registry.usages_count++;
831 // verify the person, the machine and the URL exist in our DB
832 PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, PERSON **pp, MACHINE **mm) {
833 char pbuf[36 + 1], mbuf[36 + 1];
835 if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
836 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");
841 url = registry_fix_url(url, NULL);
843 // make sure the person GUID is valid
844 if(registry_regenerate_guid(person_guid, pbuf) == -1) {
845 info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
850 // make sure the machine GUID is valid
851 if(registry_regenerate_guid(machine_guid, mbuf) == -1) {
852 info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
857 // make sure the machine exists
858 MACHINE *m = registry_machine_find(machine_guid);
860 info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
865 // make sure the person exist
866 PERSON *p = registry_person_find(person_guid);
868 info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
873 PERSON_URL *pu = dictionary_get(p->urls, url);
875 info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
881 PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
886 PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
887 if(!pu || !p || !m) return NULL;
890 delete_url = registry_fix_url(delete_url, NULL);
892 // make sure the user is not deleting the url it uses
893 if(!strcmp(delete_url, pu->url->url)) {
894 info("Registry Delete Request: delete URL is the one currently accessed, person: '%s', machine '%s', url '%s', delete url '%s'", p->guid, m->guid, pu->url->url, delete_url);
898 registry_person_urls_lock(p);
900 PERSON_URL *dpu = dictionary_get(p->urls, delete_url);
902 info("Registry Delete Request: URL not found for person: '%s', machine '%s', url '%s', delete url '%s'", p->guid, m->guid, pu->url->url, delete_url);
903 registry_person_urls_unlock(p);
907 registry_log('D', p, m, pu->url, dpu->url->url);
909 dictionary_del(p->urls, dpu->url->url);
910 registry_url_unlink_nolock(dpu->url);
913 registry_person_urls_unlock(p);
918 // a structure to pass to the dictionary_get_all() callback handler
919 struct machine_request_callback_data {
920 MACHINE *find_this_machine;
924 // the callback function
925 // this will be run for every PERSON_URL of this PERSON
926 int machine_request_callback(void *entry, void *data) {
927 PERSON_URL *mypu = (PERSON_URL *)entry;
928 struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
930 if(mypu->machine == myrdata->find_this_machine) {
931 myrdata->result = mypu;
932 return -1; // this will also stop the walk through
935 return 0; // continue
938 MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
945 PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
946 if(!pu || !p || !m) return NULL;
948 // make sure the machine GUID is valid
949 if(registry_regenerate_guid(request_machine, mbuf) == -1) {
950 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);
953 request_machine = mbuf;
955 // make sure the machine exists
956 m = registry_machine_find(request_machine);
958 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);
962 // Verify the user has in the past accessed this machine
963 // We will walk through the PERSON_URLs to find the machine
964 // linking to our machine
966 // a structure to pass to the dictionary_get_all() callback handler
967 struct machine_request_callback_data rdata = { m, NULL };
969 // request a walk through on the dictionary
970 // no need for locking here, the underlying dictionary has its own
971 dictionary_get_all(p->urls, machine_request_callback, &rdata);
980 // ----------------------------------------------------------------------------
981 // REGISTRY JSON generation
983 #define REGISTRY_STATUS_OK "ok"
984 #define REGISTRY_STATUS_FAILED "failed"
985 #define REGISTRY_STATUS_DISABLED "disabled"
987 int registry_verify_cookies_redirects(void) {
988 return registry.verify_cookies_redirects;
991 const char *registry_to_announce(void) {
992 return registry.registry_to_announce;
995 void registry_set_cookie(struct web_client *w, const char *guid) {
997 time_t et = time(NULL) + registry.persons_expiration;
998 struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
999 strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);
1001 snprintfz(w->cookie1, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", guid, edate);
1003 if(registry.registry_domain && registry.registry_domain[0])
1004 snprintfz(w->cookie2, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", guid, registry.registry_domain, edate);
1007 static inline void registry_set_person_cookie(struct web_client *w, PERSON *p) {
1008 registry_set_cookie(w, p->guid);
1011 static inline void registry_json_header(struct web_client *w, const char *action, const char *status) {
1012 buffer_flush(w->response.data);
1013 w->response.data->contenttype = CT_APPLICATION_JSON;
1014 buffer_sprintf(w->response.data, "{\n\t\"action\": \"%s\",\n\t\"status\": \"%s\",\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
1015 action, status, registry.hostname, registry.machine_guid);
1018 static inline void registry_json_footer(struct web_client *w) {
1019 buffer_strcat(w->response.data, "\n}\n");
1022 int registry_request_hello_json(struct web_client *w) {
1023 registry_json_header(w, "hello", REGISTRY_STATUS_OK);
1025 buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1026 registry.registry_to_announce);
1028 registry_json_footer(w);
1032 static inline int registry_json_disabled(struct web_client *w, const char *action) {
1033 registry_json_header(w, action, REGISTRY_STATUS_DISABLED);
1035 buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1036 registry.registry_to_announce);
1038 registry_json_footer(w);
1042 // structure used be the callbacks below
1043 struct registry_json_walk_person_urls_callback {
1046 struct web_client *w;
1050 // callback for rendering PERSON_URLs
1051 static inline int registry_json_person_url_callback(void *entry, void *data) {
1052 PERSON_URL *pu = (PERSON_URL *)entry;
1053 struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1054 struct web_client *w = c->w;
1056 if(unlikely(c->count++))
1057 buffer_strcat(w->response.data, ",");
1059 buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u, \"%s\" ]",
1060 pu->machine->guid, pu->url->url, pu->last_t, pu->usages, pu->name);
1065 // callback for rendering MACHINE_URLs
1066 static inline int registry_json_machine_url_callback(void *entry, void *data) {
1067 MACHINE_URL *mu = (MACHINE_URL *)entry;
1068 struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1069 struct web_client *w = c->w;
1072 if(unlikely(c->count++))
1073 buffer_strcat(w->response.data, ",");
1075 buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u ]",
1076 m->guid, mu->url->url, mu->last_t, mu->usages);
1082 // the main method for registering an access
1083 int registry_request_access_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
1084 if(!registry.enabled)
1085 return registry_json_disabled(w, "access");
1087 PERSON *p = registry_request_access(person_guid, machine_guid, url, name, when);
1089 registry_json_header(w, "access", REGISTRY_STATUS_FAILED);
1090 registry_json_footer(w);
1095 registry_set_person_cookie(w, p);
1097 // generate the response
1098 registry_json_header(w, "access", REGISTRY_STATUS_OK);
1100 buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\",\n\t\"urls\": [", p->guid);
1101 struct registry_json_walk_person_urls_callback c = { p, NULL, w, 0 };
1102 dictionary_get_all(p->urls, registry_json_person_url_callback, &c);
1103 buffer_strcat(w->response.data, "\n\t]\n");
1105 registry_json_footer(w);
1109 // the main method for deleting a URL from a person
1110 int registry_request_delete_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
1111 if(!registry.enabled)
1112 return registry_json_disabled(w, "delete");
1114 PERSON *p = registry_request_delete(person_guid, machine_guid, url, delete_url, when);
1116 registry_json_header(w, "delete", REGISTRY_STATUS_FAILED);
1117 registry_json_footer(w);
1121 // generate the response
1122 registry_json_header(w, "delete", REGISTRY_STATUS_OK);
1123 registry_json_footer(w);
1127 // the main method for searching the URLs of a netdata
1128 int registry_request_search_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
1129 if(!registry.enabled)
1130 return registry_json_disabled(w, "search");
1132 MACHINE *m = registry_request_machine(person_guid, machine_guid, url, request_machine, when);
1134 registry_json_header(w, "search", REGISTRY_STATUS_FAILED);
1135 registry_json_footer(w);
1139 registry_json_header(w, "search", REGISTRY_STATUS_OK);
1141 buffer_strcat(w->response.data, ",\n\t\"urls\": [");
1142 struct registry_json_walk_person_urls_callback c = { NULL, m, w, 0 };
1143 dictionary_get_all(m->urls, registry_json_machine_url_callback, &c);
1144 buffer_strcat(w->response.data, "\n\t]\n");
1146 registry_json_footer(w);
1150 // structure used be the callbacks below
1151 struct registry_person_url_callback_verify_machine_exists_data {
1156 int registry_person_url_callback_verify_machine_exists(void *entry, void *data) {
1157 struct registry_person_url_callback_verify_machine_exists_data *d = (struct registry_person_url_callback_verify_machine_exists_data *)data;
1158 PERSON_URL *pu = (PERSON_URL *)entry;
1161 if(pu->machine == m)
1167 // the main method for switching user identity
1168 int registry_request_switch_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *new_person_guid, time_t when) {
1172 if(!registry.enabled)
1173 return registry_json_disabled(w, "switch");
1175 PERSON *op = registry_person_find(person_guid);
1177 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1178 registry_json_footer(w);
1182 PERSON *np = registry_person_find(new_person_guid);
1184 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1185 registry_json_footer(w);
1189 MACHINE *m = registry_machine_find(machine_guid);
1191 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1192 registry_json_footer(w);
1196 struct registry_person_url_callback_verify_machine_exists_data data = { m, 0 };
1198 // verify the old person has access to this machine
1199 dictionary_get_all(op->urls, registry_person_url_callback_verify_machine_exists, &data);
1201 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1202 registry_json_footer(w);
1206 // verify the new person has access to this machine
1208 dictionary_get_all(np->urls, registry_person_url_callback_verify_machine_exists, &data);
1210 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1211 registry_json_footer(w);
1215 // set the cookie of the new person
1216 // the user just switched identity
1217 registry_set_person_cookie(w, np);
1219 // generate the response
1220 registry_json_header(w, "switch", REGISTRY_STATUS_OK);
1221 buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\"", np->guid);
1222 registry_json_footer(w);
1227 // ----------------------------------------------------------------------------
1228 // REGISTRY THIS MACHINE UNIQUE ID
1230 static inline int is_machine_guid_blacklisted(const char *guid) {
1231 // these are machine GUIDs that have been included in distribution packages.
1232 // we blacklist them here, so that the next version of netdata will generate
1235 if(!strcmp(guid, "8a795b0c-2311-11e6-8563-000c295076a6")
1236 || !strcmp(guid, "4aed1458-1c3e-11e6-a53f-000c290fc8f5")
1238 error("Blacklisted machine GUID '%s' found.", guid);
1245 char *registry_get_this_machine_guid(void) {
1246 if(likely(registry.machine_guid[0]))
1247 return registry.machine_guid;
1249 // read it from disk
1250 int fd = open(registry.machine_guid_filename, O_RDONLY);
1253 if(read(fd, buf, 36) != 36)
1254 error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
1257 if(registry_regenerate_guid(buf, registry.machine_guid) == -1) {
1258 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
1259 buf, registry.machine_guid_filename);
1261 registry.machine_guid[0] = '\0';
1263 else if(is_machine_guid_blacklisted(registry.machine_guid))
1264 registry.machine_guid[0] = '\0';
1269 // generate a new one?
1270 if(!registry.machine_guid[0]) {
1273 uuid_generate_time(uuid);
1274 uuid_unparse_lower(uuid, registry.machine_guid);
1275 registry.machine_guid[36] = '\0';
1278 fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
1280 fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1282 if(write(fd, registry.machine_guid, 36) != 36)
1283 fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1288 setenv("NETDATA_REGISTRY_UNIQUE_ID", registry.machine_guid, 1);
1290 return registry.machine_guid;
1294 // ----------------------------------------------------------------------------
1295 // REGISTRY LOAD/SAVE
1297 int registry_machine_save_url(void *entry, void *file) {
1298 MACHINE_URL *mu = entry;
1301 debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
1303 int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
1311 // error handling is done at registry_save()
1316 int registry_machine_save(void *entry, void *file) {
1320 debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
1322 int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
1330 int ret2 = dictionary_get_all(m->urls, registry_machine_save_url, fp);
1331 if(ret2 < 0) return ret2;
1335 // error handling is done at registry_save()
1340 static inline int registry_person_save_url(void *entry, void *file) {
1341 PERSON_URL *pu = entry;
1344 debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
1346 int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
1356 // error handling is done at registry_save()
1361 static inline int registry_person_save(void *entry, void *file) {
1365 debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
1367 int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
1375 int ret2 = dictionary_get_all(p->urls, registry_person_save_url, fp);
1376 if (ret2 < 0) return ret2;
1380 // error handling is done at registry_save()
1385 int registry_save(void) {
1386 if(!registry.enabled) return -1;
1388 // make sure the log is not updated
1389 registry_log_lock();
1391 if(unlikely(!registry_should_save_db())) {
1392 registry_log_unlock();
1396 char tmp_filename[FILENAME_MAX + 1];
1397 char old_filename[FILENAME_MAX + 1];
1399 snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
1400 snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
1402 debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
1403 FILE *fp = fopen(tmp_filename, "w");
1405 error("Registry: Cannot create file: %s", tmp_filename);
1406 registry_log_unlock();
1410 // dictionary_get_all() has its own locking, so this is safe to do
1412 debug(D_REGISTRY, "Saving all machines");
1413 int bytes1 = dictionary_get_all(registry.machines, registry_machine_save, fp);
1415 error("Registry: Cannot save registry machines - return value %d", bytes1);
1417 registry_log_unlock();
1420 debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
1422 debug(D_REGISTRY, "Saving all persons");
1423 int bytes2 = dictionary_get_all(registry.persons, registry_person_save, fp);
1425 error("Registry: Cannot save registry persons - return value %d", bytes2);
1427 registry_log_unlock();
1430 debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
1433 fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
1434 registry.persons_count,
1435 registry.machines_count,
1436 registry.usages_count + 1, // this is required - it is lost on db rotation
1437 registry.urls_count,
1438 registry.persons_urls_count,
1439 registry.machines_urls_count
1446 // remove the .old db
1447 debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
1448 if(unlink(old_filename) == -1 && errno != ENOENT)
1449 error("Registry: cannot remove old registry file '%s'", old_filename);
1451 // rename the db to .old
1452 debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
1453 if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
1454 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename, registry.db_filename);
1457 // remove the database (it is saved in .old)
1458 debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
1459 if (unlink(registry.db_filename) == -1 && errno != ENOENT)
1460 error("Registry: cannot remove old registry file '%s'", registry.db_filename);
1462 // move the .tmp to make it active
1463 debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
1464 if (link(tmp_filename, registry.db_filename) == -1) {
1465 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
1466 registry.db_filename);
1468 // move the .old back
1469 debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
1470 if(link(old_filename, registry.db_filename) == -1)
1471 error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
1474 debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
1475 if(unlink(tmp_filename) == -1)
1476 error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
1478 // it has been moved successfully
1479 // discard the current registry log
1480 registry_log_recreate_nolock();
1482 registry.log_count = 0;
1486 // continue operations
1487 registry_log_unlock();
1492 static inline size_t registry_load(void) {
1493 char *s, buf[4096 + 1];
1499 debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
1500 FILE *fp = fopen(registry.db_filename, "r");
1502 error("Registry: cannot open registry file: '%s'", registry.db_filename);
1508 while((s = fgets_trim_len(buf, 4096, fp, &len))) {
1511 debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
1514 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')) {
1515 error("Registry totals line %zu is wrong (len = %zu).", line, len);
1518 registry.persons_count = strtoull(&s[2], NULL, 16);
1519 registry.machines_count = strtoull(&s[19], NULL, 16);
1520 registry.usages_count = strtoull(&s[36], NULL, 16);
1521 registry.urls_count = strtoull(&s[53], NULL, 16);
1522 registry.persons_urls_count = strtoull(&s[70], NULL, 16);
1523 registry.machines_urls_count = strtoull(&s[87], NULL, 16);
1528 // verify it is valid
1529 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1530 error("Registry person line %zu is wrong (len = %zu).", line, len);
1534 s[1] = s[10] = s[19] = s[28] = '\0';
1535 p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
1536 p->last_t = strtoul(&s[11], NULL, 16);
1537 p->usages = strtoul(&s[20], NULL, 16);
1538 debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
1541 case 'M': // machine
1543 // verify it is valid
1544 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1545 error("Registry person line %zu is wrong (len = %zu).", line, len);
1549 s[1] = s[10] = s[19] = s[28] = '\0';
1550 m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
1551 m->last_t = strtoul(&s[11], NULL, 16);
1552 m->usages = strtoul(&s[20], NULL, 16);
1553 debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
1556 case 'U': // person URL
1558 error("Registry: ignoring line %zu, no person loaded: %s", line, s);
1562 // verify it is valid
1563 if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
1564 error("Registry person URL line %zu is wrong (len = %zu).", line, len);
1568 s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
1570 // skip the name to find the url
1572 while(*url && *url != '\t') url++;
1574 error("Registry person URL line %zu does not have a url.", line);
1579 u = registry_url_allocate_nolock(url, strlen(url));
1581 time_t first_t = strtoul(&s[2], NULL, 16);
1583 m = registry_machine_find(&s[32]);
1584 if(!m) m = registry_machine_allocate(&s[32], first_t);
1586 PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
1587 pu->last_t = strtoul(&s[11], NULL, 16);
1588 pu->usages = strtoul(&s[20], NULL, 16);
1589 pu->flags = strtoul(&s[29], NULL, 16);
1590 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);
1593 case 'V': // machine URL
1595 error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
1599 // verify it is valid
1600 if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
1601 error("Registry person URL line %zu is wrong (len = %zu).", line, len);
1605 s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
1606 u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
1608 MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
1609 mu->last_t = strtoul(&s[11], NULL, 16);
1610 mu->usages = strtoul(&s[20], NULL, 16);
1611 mu->flags = strtoul(&s[29], NULL, 16);
1612 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);
1616 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
1625 // ----------------------------------------------------------------------------
1628 int registry_init(void) {
1629 char filename[FILENAME_MAX + 1];
1631 // registry enabled?
1632 registry.enabled = config_get_boolean("registry", "enabled", 0);
1635 registry.pathname = config_get("registry", "registry db directory", VARLIB_DIR "/registry");
1636 if(mkdir(registry.pathname, 0770) == -1 && errno != EEXIST)
1637 fatal("Cannot create directory '%s'.", registry.pathname);
1640 snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
1641 registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
1642 registry_get_this_machine_guid();
1644 snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
1645 registry.db_filename = config_get("registry", "registry db file", filename);
1647 snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
1648 registry.log_filename = config_get("registry", "registry log file", filename);
1650 // configuration options
1651 registry.save_registry_every_entries = config_get_number("registry", "registry save db every new entries", 1000000);
1652 registry.persons_expiration = config_get_number("registry", "registry expire idle persons days", 365) * 86400;
1653 registry.registry_domain = config_get("registry", "registry domain", "");
1654 registry.registry_to_announce = config_get("registry", "registry to announce", "https://registry.my-netdata.io");
1655 registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", hostname));
1656 registry.verify_cookies_redirects = config_get_boolean("registry", "verify browser cookies support", 1);
1658 setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
1659 setenv("NETDATA_REGISTRY_URL", registry.registry_to_announce, 1);
1661 registry.max_url_length = config_get_number("registry", "max URL length", 1024);
1662 if(registry.max_url_length < 10) {
1663 registry.max_url_length = 10;
1664 config_set_number("registry", "max URL length", registry.max_url_length);
1667 registry.max_name_length = config_get_number("registry", "max URL name length", 50);
1668 if(registry.max_name_length < 10) {
1669 registry.max_name_length = 10;
1670 config_set_number("registry", "max URL name length", registry.max_name_length);
1673 // initialize entries counters
1674 registry.persons_count = 0;
1675 registry.machines_count = 0;
1676 registry.usages_count = 0;
1677 registry.urls_count = 0;
1678 registry.persons_urls_count = 0;
1679 registry.machines_urls_count = 0;
1681 // initialize memory counters
1682 registry.persons_memory = 0;
1683 registry.machines_memory = 0;
1684 registry.urls_memory = 0;
1685 registry.persons_urls_memory = 0;
1686 registry.machines_urls_memory = 0;
1689 pthread_mutex_init(®istry.persons_lock, NULL);
1690 pthread_mutex_init(®istry.machines_lock, NULL);
1691 pthread_mutex_init(®istry.urls_lock, NULL);
1692 pthread_mutex_init(®istry.person_urls_lock, NULL);
1693 pthread_mutex_init(®istry.machine_urls_lock, NULL);
1695 // create dictionaries
1696 registry.persons = dictionary_create(DICTIONARY_FLAGS);
1697 registry.machines = dictionary_create(DICTIONARY_FLAGS);
1698 registry.urls = dictionary_create(DICTIONARY_FLAGS);
1700 // load the registry database
1701 if(registry.enabled) {
1702 registry_log_open_nolock();
1704 registry_log_load();
1710 void registry_free(void) {
1711 if(!registry.enabled) return;
1713 // we need to destroy the dictionaries ourselves
1714 // since the dictionaries use memory we allocated
1716 while(registry.persons->values_index.root) {
1717 PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
1719 // fprintf(stderr, "\nPERSON: '%s', first: %u, last: %u, usages: %u\n", p->guid, p->first_t, p->last_t, p->usages);
1721 while(p->urls->values_index.root) {
1722 PERSON_URL *pu = ((NAME_VALUE *)p->urls->values_index.root)->value;
1724 // 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);
1726 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", pu->url->url, p->guid);
1727 dictionary_del(p->urls, pu->url->url);
1729 debug(D_REGISTRY, "Registry: unlinking url '%s' from person", pu->url->url);
1730 registry_url_unlink_nolock(pu->url);
1732 debug(D_REGISTRY, "Registry: freeing person url");
1736 debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
1737 dictionary_del(registry.persons, p->guid);
1739 debug(D_REGISTRY, "Registry: destroying URL dictionary of person '%s'", p->guid);
1740 dictionary_destroy(p->urls);
1742 debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
1746 while(registry.machines->values_index.root) {
1747 MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
1749 // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
1751 while(m->urls->values_index.root) {
1752 MACHINE_URL *mu = ((NAME_VALUE *)m->urls->values_index.root)->value;
1754 // 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);
1756 //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
1757 //dictionary_destroy(mu->persons);
1759 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
1760 dictionary_del(m->urls, mu->url->url);
1762 debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
1763 registry_url_unlink_nolock(mu->url);
1765 debug(D_REGISTRY, "Registry: freeing machine url");
1769 debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
1770 dictionary_del(registry.machines, m->guid);
1772 debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
1773 dictionary_destroy(m->urls);
1775 debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
1779 // and free the memory of remaining dictionary structures
1781 debug(D_REGISTRY, "Registry: destroying persons dictionary");
1782 dictionary_destroy(registry.persons);
1784 debug(D_REGISTRY, "Registry: destroying machines dictionary");
1785 dictionary_destroy(registry.machines);
1787 debug(D_REGISTRY, "Registry: destroying urls dictionary");
1788 dictionary_destroy(registry.urls);
1791 // ----------------------------------------------------------------------------
1794 void registry_statistics(void) {
1795 if(!registry.enabled) return;
1797 static RRDSET *sts = NULL, *stc = NULL, *stm = NULL;
1799 if(!sts) sts = rrdset_find("netdata.registry_sessions");
1801 sts = rrdset_create("netdata", "registry_sessions", NULL, "registry", NULL, "NetData Registry Sessions", "session", 131000, rrd_update_every, RRDSET_TYPE_LINE);
1803 rrddim_add(sts, "sessions", NULL, 1, 1, RRDDIM_ABSOLUTE);
1805 else rrdset_next(sts);
1807 rrddim_set(sts, "sessions", registry.usages_count);
1810 // ------------------------------------------------------------------------
1812 if(!stc) stc = rrdset_find("netdata.registry_entries");
1814 stc = rrdset_create("netdata", "registry_entries", NULL, "registry", NULL, "NetData Registry Entries", "entries", 131100, rrd_update_every, RRDSET_TYPE_LINE);
1816 rrddim_add(stc, "persons", NULL, 1, 1, RRDDIM_ABSOLUTE);
1817 rrddim_add(stc, "machines", NULL, 1, 1, RRDDIM_ABSOLUTE);
1818 rrddim_add(stc, "urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1819 rrddim_add(stc, "persons_urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1820 rrddim_add(stc, "machines_urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1822 else rrdset_next(stc);
1824 rrddim_set(stc, "persons", registry.persons_count);
1825 rrddim_set(stc, "machines", registry.machines_count);
1826 rrddim_set(stc, "urls", registry.urls_count);
1827 rrddim_set(stc, "persons_urls", registry.persons_urls_count);
1828 rrddim_set(stc, "machines_urls", registry.machines_urls_count);
1831 // ------------------------------------------------------------------------
1833 if(!stm) stm = rrdset_find("netdata.registry_mem");
1835 stm = rrdset_create("netdata", "registry_mem", NULL, "registry", NULL, "NetData Registry Memory", "KB", 131300, rrd_update_every, RRDSET_TYPE_STACKED);
1837 rrddim_add(stm, "persons", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1838 rrddim_add(stm, "machines", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1839 rrddim_add(stm, "urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1840 rrddim_add(stm, "persons_urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1841 rrddim_add(stm, "machines_urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1843 else rrdset_next(stm);
1845 rrddim_set(stm, "persons", registry.persons_memory + registry.persons_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1846 rrddim_set(stm, "machines", registry.machines_memory + registry.machines_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1847 rrddim_set(stm, "urls", registry.urls_memory + registry.urls_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1848 rrddim_set(stm, "persons_urls", registry.persons_urls_memory + registry.persons_count * sizeof(DICTIONARY) + registry.persons_urls_count * sizeof(NAME_VALUE));
1849 rrddim_set(stm, "machines_urls", registry.machines_urls_memory + registry.machines_count * sizeof(DICTIONARY) + registry.machines_urls_count * sizeof(NAME_VALUE));