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
92 int verify_cookies_redirects;
94 size_t max_url_length;
95 size_t max_name_length;
101 char *machine_guid_filename;
107 DICTIONARY *persons; // dictionary of PERSON *, with key the PERSON.guid
108 DICTIONARY *machines; // dictionary of MACHINE *, with key the MACHINE.guid
109 DICTIONARY *urls; // dictionary of URL *, with key the URL.url
111 // concurrency locking
112 // we keep different locks for different things
113 // so that many tasks can be completed in parallel
114 pthread_mutex_t persons_lock;
115 pthread_mutex_t machines_lock;
116 pthread_mutex_t urls_lock;
117 pthread_mutex_t person_urls_lock;
118 pthread_mutex_t machine_urls_lock;
119 pthread_mutex_t log_lock;
123 // ----------------------------------------------------------------------------
125 // Save memory by de-duplicating URLs
126 // so instead of storing URLs all over the place
127 // we store them here and we keep pointers elsewhere
130 uint32_t links; // the number of links to this URL - when none is left, we free it
131 uint16_t len; // the length of the URL in bytes
132 char url[1]; // the URL - dynamically allocated to more size
134 typedef struct url URL;
137 // ----------------------------------------------------------------------------
138 // MACHINE structures
140 // For each MACHINE-URL pair we keep this
142 URL *url; // de-duplicated URL
143 // DICTIONARY *persons; // dictionary of PERSON *
146 uint32_t first_t; // the first time we saw this
147 uint32_t last_t; // the last time we saw this
148 uint32_t usages; // how many times this has been accessed
150 typedef struct machine_url MACHINE_URL;
154 char guid[36 + 1]; // the GUID
156 uint32_t links; // the number of PERSON_URLs linked to this machine
158 DICTIONARY *urls; // MACHINE_URL *
160 uint32_t first_t; // the first time we saw this
161 uint32_t last_t; // the last time we saw this
162 uint32_t usages; // how many times this has been accessed
164 typedef struct machine MACHINE;
167 // ----------------------------------------------------------------------------
170 // for each PERSON-URL pair we keep this
172 URL *url; // de-duplicated URL
173 MACHINE *machine; // link the MACHINE of this URL
176 uint32_t first_t; // the first time we saw this
177 uint32_t last_t; // the last time we saw this
178 uint32_t usages; // how many times this has been accessed
180 char name[1]; // the name of the URL, as known by the user
181 // dynamically allocated to fit properly
183 typedef struct person_url PERSON_URL;
187 char guid[36 + 1]; // the person GUID
189 DICTIONARY *urls; // dictionary of PERSON_URL *
191 uint32_t first_t; // the first time we saw this
192 uint32_t last_t; // the last time we saw this
193 uint32_t usages; // how many times this has been accessed
195 typedef struct person PERSON;
198 // ----------------------------------------------------------------------------
199 // REGISTRY concurrency locking
201 static inline void registry_persons_lock(void) {
202 pthread_mutex_lock(®istry.persons_lock);
205 static inline void registry_persons_unlock(void) {
206 pthread_mutex_unlock(®istry.persons_lock);
209 static inline void registry_machines_lock(void) {
210 pthread_mutex_lock(®istry.machines_lock);
213 static inline void registry_machines_unlock(void) {
214 pthread_mutex_unlock(®istry.machines_lock);
217 static inline void registry_urls_lock(void) {
218 pthread_mutex_lock(®istry.urls_lock);
221 static inline void registry_urls_unlock(void) {
222 pthread_mutex_unlock(®istry.urls_lock);
225 // ideally, we should not lock the whole registry for
226 // updating a person's urls.
227 // however, to save the memory required for keeping a
228 // mutex (40 bytes) per person, we do...
229 static inline void registry_person_urls_lock(PERSON *p) {
231 pthread_mutex_lock(®istry.person_urls_lock);
234 static inline void registry_person_urls_unlock(PERSON *p) {
236 pthread_mutex_unlock(®istry.person_urls_lock);
239 // ideally, we should not lock the whole registry for
240 // updating a machine's urls.
241 // however, to save the memory required for keeping a
242 // mutex (40 bytes) per machine, we do...
243 static inline void registry_machine_urls_lock(MACHINE *m) {
245 pthread_mutex_lock(®istry.machine_urls_lock);
248 static inline void registry_machine_urls_unlock(MACHINE *m) {
250 pthread_mutex_unlock(®istry.machine_urls_lock);
253 static inline void registry_log_lock(void) {
254 pthread_mutex_lock(®istry.log_lock);
257 static inline void registry_log_unlock(void) {
258 pthread_mutex_unlock(®istry.log_lock);
262 // ----------------------------------------------------------------------------
265 // parse a GUID and re-generated to be always lower case
266 // this is used as a protection against the variations of GUIDs
267 static inline int registry_regenerate_guid(const char *guid, char *result) {
269 if(unlikely(uuid_parse(guid, uuid) == -1)) {
270 info("Registry: GUID '%s' is not a valid GUID.", guid);
274 uuid_unparse_lower(uuid, result);
276 #ifdef NETDATA_INTERNAL_CHECKS
277 if(strcmp(guid, result))
278 info("Registry: source GUID '%s' and re-generated GUID '%s' differ!", guid, result);
279 #endif /* NETDATA_INTERNAL_CHECKS */
285 // make sure the names of the machines / URLs do not contain any tabs
286 // (which are used as our separator in the database files)
287 // and are properly trimmed (before and after)
288 static inline char *registry_fix_machine_name(char *name, size_t *len) {
289 char *s = name?name:"";
291 // skip leading spaces
292 while(*s && isspace(*s)) s++;
294 // make sure all spaces are a SPACE
297 if(unlikely(isspace(*t)))
303 // remove trailing spaces
318 static inline char *registry_fix_url(char *url, size_t *len) {
319 return registry_fix_machine_name(url, len);
323 // ----------------------------------------------------------------------------
324 // forward definition of functions
326 extern PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
327 extern PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
330 // ----------------------------------------------------------------------------
333 static inline URL *registry_url_allocate_nolock(const char *url, size_t urllen) {
334 // protection from too big URLs
335 if(urllen > registry.max_url_length)
336 urllen = registry.max_url_length;
338 debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): allocating %zu bytes", url, sizeof(URL) + urllen);
339 URL *u = malloc(sizeof(URL) + urllen);
340 if(!u) fatal("Cannot allocate %zu bytes for URL '%s'", sizeof(URL) + urllen, url);
342 // a simple strcpy() should do the job
343 // but I prefer to be safe, since the caller specified urllen
344 strncpyz(u->url, url, urllen);
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 strncpyz(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 strncpyz(pu->name, name, namelen);
493 pu->first_t = pu->last_t = when;
496 pu->flags = REGISTRY_URL_FLAGS_DEFAULT;
499 registry.persons_urls_memory += sizeof(PERSON_URL) + namelen;
501 debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): indexing URL in person", p->guid, m->guid, u->url);
502 dictionary_set(p->urls, u->url, pu, sizeof(PERSON_URL));
503 registry_url_link_nolock(u);
508 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) {
509 // this function is needed to change the name of a PERSON_URL
511 debug(D_REGISTRY, "registry_person_url_reallocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
512 sizeof(PERSON_URL) + namelen);
514 PERSON_URL *tpu = registry_person_url_allocate(p, m, u, name, namelen, when);
515 tpu->first_t = pu->first_t;
516 tpu->last_t = pu->last_t;
517 tpu->usages = pu->usages;
519 // ok, these are a hack - since the registry_person_url_allocate() is
520 // adding these, we have to subtract them
521 tpu->machine->links--;
522 registry.persons_urls_memory -= sizeof(PERSON_URL) + strlen(pu->name);
523 registry_url_unlink_nolock(u);
530 static inline PERSON *registry_person_allocate(const char *person_guid, time_t when) {
533 debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(PERSON));
535 p = malloc(sizeof(PERSON));
536 if(!p) fatal("Registry: cannot allocate memory for new person.");
542 uuid_unparse_lower(uuid, p->guid);
544 debug(D_REGISTRY, "Registry: Checking if the generated person guid '%s' is unique", p->guid);
545 if (!dictionary_get(registry.persons, p->guid)) {
546 debug(D_REGISTRY, "Registry: generated person guid '%s' is unique", p->guid);
550 info("Registry: generated person guid '%s' found in the registry. Retrying...", p->guid);
554 strncpyz(p->guid, person_guid, 36);
556 debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): creating dictionary of urls", p->guid);
557 p->urls = dictionary_create(DICTIONARY_FLAGS);
559 p->first_t = p->last_t = when;
562 registry.persons_memory += sizeof(PERSON);
564 registry.persons_count++;
565 dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
571 // 1. validate person GUID
572 // 2. if it is valid, find it
573 // 3. if it is not valid, create a new one
575 static inline PERSON *registry_person_get(const char *person_guid, time_t when) {
578 registry_persons_lock();
580 if(person_guid && *person_guid) {
582 // validate it is a GUID
583 if(unlikely(registry_regenerate_guid(person_guid, buf) == -1))
584 info("Registry: person guid '%s' is not a valid guid. Ignoring it.", person_guid);
587 p = registry_person_find(person_guid);
588 if(!p) person_guid = NULL;
592 if(!p) p = registry_person_allocate(NULL, when);
594 registry_persons_unlock();
599 // ----------------------------------------------------------------------------
600 // LINKING OF OBJECTS
602 static inline PERSON_URL *registry_person_link_to_url(PERSON *p, MACHINE *m, URL *u, char *name, size_t namelen, time_t when) {
603 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): searching for URL in person", p->guid, m->guid, u->url);
605 registry_person_urls_lock(p);
607 PERSON_URL *pu = dictionary_get(p->urls, u->url);
609 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
610 pu = registry_person_url_allocate(p, m, u, name, namelen, when);
611 registry.persons_urls_count++;
614 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
616 if(likely(pu->last_t < (uint32_t)when)) pu->last_t = when;
618 if(pu->machine != m) {
619 MACHINE_URL *mu = dictionary_get(pu->machine->urls, u->url);
621 info("registry_person_link_to_url('%s', '%s', '%s'): URL switched machines (old was '%s') - expiring it from previous machine.",
622 p->guid, m->guid, u->url, pu->machine->guid);
623 mu->flags |= REGISTRY_URL_FLAGS_EXPIRED;
626 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.",
627 p->guid, m->guid, u->url, pu->machine->guid);
630 pu->machine->links--;
634 if(strcmp(pu->name, name)) {
635 // the name of the PERSON_URL has changed !
636 pu = registry_person_url_reallocate(p, m, u, name, namelen, when, pu);
641 if(likely(p->last_t < (uint32_t)when)) p->last_t = when;
643 if(pu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
644 info("registry_person_link_to_url('%s', '%s', '%s'): accessing an expired URL. Re-enabling URL.", p->guid, m->guid, u->url);
645 pu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
648 registry_person_urls_unlock(p);
653 static inline MACHINE_URL *registry_machine_link_to_url(PERSON *p, MACHINE *m, URL *u, time_t when) {
654 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): searching for URL in machine", p->guid, m->guid, u->url);
656 registry_machine_urls_lock(m);
658 MACHINE_URL *mu = dictionary_get(m->urls, u->url);
660 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
661 mu = registry_machine_url_allocate(m, u, when);
662 registry.machines_urls_count++;
665 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
667 if(likely(mu->last_t < (uint32_t)when)) mu->last_t = when;
670 //debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): indexing person in machine", p->guid, m->guid, u->url);
671 //dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
674 if(likely(m->last_t < (uint32_t)when)) m->last_t = when;
676 if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
677 info("registry_machine_link_to_url('%s', '%s', '%s'): accessing an expired URL.", p->guid, m->guid, u->url);
678 mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
681 registry_machine_urls_unlock(m);
686 // ----------------------------------------------------------------------------
687 // REGISTRY LOG LOAD/SAVE
689 static inline int registry_should_save_db(void) {
690 debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
691 return registry.log_count > registry.save_registry_every_entries;
694 static inline void registry_log(const char action, PERSON *p, MACHINE *m, URL *u, char *name) {
695 if(likely(registry.log_fp)) {
696 // we lock only if the file is open
697 // to allow replaying the log at registry_log_load()
700 if(unlikely(fprintf(registry.log_fp, "%c\t%08x\t%s\t%s\t%s\t%s\n",
707 error("Registry: failed to save log. Registry data may be lost in case of abnormal restart.");
709 // we increase the counter even on failures
710 // so that the registry will be saved periodically
711 registry.log_count++;
713 registry_log_unlock();
715 // this must be outside the log_lock(), or a deadlock will happen.
716 // registry_save() checks the same inside the log_lock, so only
717 // one thread will save the db
718 if(unlikely(registry_should_save_db()))
723 static inline int registry_log_open_nolock(void) {
725 fclose(registry.log_fp);
727 registry.log_fp = fopen(registry.log_filename, "a");
729 if(registry.log_fp) {
730 if (setvbuf(registry.log_fp, NULL, _IOLBF, 0) != 0)
731 error("Cannot set line buffering on registry log file.");
735 error("Cannot open registry log file '%s'. Registry data will be lost in case of netdata or server crash.", registry.log_filename);
739 static inline void registry_log_close_nolock(void) {
740 if(registry.log_fp) {
741 fclose(registry.log_fp);
742 registry.log_fp = NULL;
746 static inline void registry_log_recreate_nolock(void) {
747 if(registry.log_fp != NULL) {
748 registry_log_close_nolock();
750 // open it with truncate
751 registry.log_fp = fopen(registry.log_filename, "w");
752 if(registry.log_fp) fclose(registry.log_fp);
753 else error("Cannot truncate registry log '%s'", registry.log_filename);
755 registry.log_fp = NULL;
757 registry_log_open_nolock();
761 int registry_log_load(void) {
762 char *s, buf[4096 + 1];
765 // closing the log is required here
766 // otherwise we will append to it the values we read
767 registry_log_close_nolock();
769 debug(D_REGISTRY, "Registry: loading active db from: %s", registry.log_filename);
770 FILE *fp = fopen(registry.log_filename, "r");
772 error("Registry: cannot open registry file: %s", registry.log_filename);
776 while ((s = fgets_trim_len(buf, 4096, fp, &len))) {
780 case 'A': // accesses
783 // verify it is valid
784 if (unlikely(len < 85 || s[1] != '\t' || s[10] != '\t' || s[47] != '\t' || s[84] != '\t')) {
785 error("Registry: log line %zu is wrong (len = %zu).", line, len);
788 s[1] = s[10] = s[47] = s[84] = '\0';
791 time_t when = strtoul(&s[2], NULL, 16);
792 char *person_guid = &s[11];
793 char *machine_guid = &s[48];
796 // skip the name to find the url
798 while(*url && *url != '\t') url++;
800 error("Registry: log line %zu does not have a url.", line);
805 // make sure the person exists
806 // without this, a new person guid will be created
807 PERSON *p = registry_person_find(person_guid);
808 if(!p) p = registry_person_allocate(person_guid, when);
811 registry_request_access(p->guid, machine_guid, url, name, when);
813 registry_request_delete(p->guid, machine_guid, url, name, when);
818 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.log_filename, s);
826 // open the log again
827 registry_log_open_nolock();
833 // ----------------------------------------------------------------------------
836 PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
837 debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
839 MACHINE *m = registry_machine_get(machine_guid, when);
842 // make sure the name is valid
844 name = registry_fix_machine_name(name, &namelen);
847 url = registry_fix_url(url, &urllen);
849 URL *u = registry_url_get(url, urllen);
850 PERSON *p = registry_person_get(person_guid, when);
852 registry_person_link_to_url(p, m, u, name, namelen, when);
853 registry_machine_link_to_url(p, m, u, when);
855 registry_log('A', p, m, u, name);
857 registry.usages_count++;
861 // verify the person, the machine and the URL exist in our DB
862 PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, PERSON **pp, MACHINE **mm) {
863 char pbuf[36 + 1], mbuf[36 + 1];
865 if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
866 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");
871 url = registry_fix_url(url, NULL);
873 // make sure the person GUID is valid
874 if(registry_regenerate_guid(person_guid, pbuf) == -1) {
875 info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
880 // make sure the machine GUID is valid
881 if(registry_regenerate_guid(machine_guid, mbuf) == -1) {
882 info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
887 // make sure the machine exists
888 MACHINE *m = registry_machine_find(machine_guid);
890 info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
895 // make sure the person exist
896 PERSON *p = registry_person_find(person_guid);
898 info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
903 PERSON_URL *pu = dictionary_get(p->urls, url);
905 info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
911 PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
916 PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
917 if(!pu || !p || !m) return NULL;
920 delete_url = registry_fix_url(delete_url, NULL);
922 // make sure the user is not deleting the url it uses
923 if(!strcmp(delete_url, pu->url->url)) {
924 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);
928 registry_person_urls_lock(p);
930 PERSON_URL *dpu = dictionary_get(p->urls, delete_url);
932 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);
933 registry_person_urls_unlock(p);
937 registry_log('D', p, m, pu->url, dpu->url->url);
939 dictionary_del(p->urls, dpu->url->url);
940 registry_url_unlink_nolock(dpu->url);
943 registry_person_urls_unlock(p);
948 // a structure to pass to the dictionary_get_all() callback handler
949 struct machine_request_callback_data {
950 MACHINE *find_this_machine;
954 // the callback function
955 // this will be run for every PERSON_URL of this PERSON
956 int machine_request_callback(void *entry, void *data) {
957 PERSON_URL *mypu = (PERSON_URL *)entry;
958 struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
960 if(mypu->machine == myrdata->find_this_machine) {
961 myrdata->result = mypu;
962 return -1; // this will also stop the walk through
965 return 0; // continue
968 MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
975 PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
976 if(!pu || !p || !m) return NULL;
978 // make sure the machine GUID is valid
979 if(registry_regenerate_guid(request_machine, mbuf) == -1) {
980 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);
983 request_machine = mbuf;
985 // make sure the machine exists
986 m = registry_machine_find(request_machine);
988 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);
992 // Verify the user has in the past accessed this machine
993 // We will walk through the PERSON_URLs to find the machine
994 // linking to our machine
996 // a structure to pass to the dictionary_get_all() callback handler
997 struct machine_request_callback_data rdata = { m, NULL };
999 // request a walk through on the dictionary
1000 // no need for locking here, the underlying dictionary has its own
1001 dictionary_get_all(p->urls, machine_request_callback, &rdata);
1010 // ----------------------------------------------------------------------------
1011 // REGISTRY JSON generation
1013 #define REGISTRY_STATUS_OK "ok"
1014 #define REGISTRY_STATUS_FAILED "failed"
1015 #define REGISTRY_STATUS_DISABLED "disabled"
1017 int registry_verify_cookies_redirects(void) {
1018 return registry.verify_cookies_redirects;
1021 const char *registry_to_announce(void) {
1022 return registry.registry_to_announce;
1025 void registry_set_cookie(struct web_client *w, const char *guid) {
1027 time_t et = time(NULL) + registry.persons_expiration;
1028 struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
1029 strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);
1031 snprintfz(w->cookie1, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", guid, edate);
1033 if(registry.registry_domain && registry.registry_domain[0])
1034 snprintfz(w->cookie2, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", guid, registry.registry_domain, edate);
1037 static inline void registry_set_person_cookie(struct web_client *w, PERSON *p) {
1038 registry_set_cookie(w, p->guid);
1041 static inline void registry_json_header(struct web_client *w, const char *action, const char *status) {
1042 buffer_flush(w->response.data);
1043 w->response.data->contenttype = CT_APPLICATION_JSON;
1044 buffer_sprintf(w->response.data, "{\n\t\"action\": \"%s\",\n\t\"status\": \"%s\",\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
1045 action, status, registry.hostname, registry.machine_guid);
1048 static inline void registry_json_footer(struct web_client *w) {
1049 buffer_strcat(w->response.data, "\n}\n");
1052 int registry_request_hello_json(struct web_client *w) {
1053 registry_json_header(w, "hello", REGISTRY_STATUS_OK);
1055 buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1056 registry.registry_to_announce);
1058 registry_json_footer(w);
1062 static inline int registry_json_disabled(struct web_client *w, const char *action) {
1063 registry_json_header(w, action, REGISTRY_STATUS_DISABLED);
1065 buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1066 registry.registry_to_announce);
1068 registry_json_footer(w);
1072 // structure used be the callbacks below
1073 struct registry_json_walk_person_urls_callback {
1076 struct web_client *w;
1080 // callback for rendering PERSON_URLs
1081 static inline int registry_json_person_url_callback(void *entry, void *data) {
1082 PERSON_URL *pu = (PERSON_URL *)entry;
1083 struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1084 struct web_client *w = c->w;
1086 if(unlikely(c->count++))
1087 buffer_strcat(w->response.data, ",");
1089 buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u, \"%s\" ]",
1090 pu->machine->guid, pu->url->url, pu->last_t, pu->usages, pu->name);
1095 // callback for rendering MACHINE_URLs
1096 static inline int registry_json_machine_url_callback(void *entry, void *data) {
1097 MACHINE_URL *mu = (MACHINE_URL *)entry;
1098 struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1099 struct web_client *w = c->w;
1102 if(unlikely(c->count++))
1103 buffer_strcat(w->response.data, ",");
1105 buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u ]",
1106 m->guid, mu->url->url, mu->last_t, mu->usages);
1112 // the main method for registering an access
1113 int registry_request_access_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
1114 if(!registry.enabled)
1115 return registry_json_disabled(w, "access");
1117 PERSON *p = registry_request_access(person_guid, machine_guid, url, name, when);
1119 registry_json_header(w, "access", REGISTRY_STATUS_FAILED);
1120 registry_json_footer(w);
1125 registry_set_person_cookie(w, p);
1127 // generate the response
1128 registry_json_header(w, "access", REGISTRY_STATUS_OK);
1130 buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\",\n\t\"urls\": [", p->guid);
1131 struct registry_json_walk_person_urls_callback c = { p, NULL, w, 0 };
1132 dictionary_get_all(p->urls, registry_json_person_url_callback, &c);
1133 buffer_strcat(w->response.data, "\n\t]\n");
1135 registry_json_footer(w);
1139 // the main method for deleting a URL from a person
1140 int registry_request_delete_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
1141 if(!registry.enabled)
1142 return registry_json_disabled(w, "delete");
1144 PERSON *p = registry_request_delete(person_guid, machine_guid, url, delete_url, when);
1146 registry_json_header(w, "delete", REGISTRY_STATUS_FAILED);
1147 registry_json_footer(w);
1151 // generate the response
1152 registry_json_header(w, "delete", REGISTRY_STATUS_OK);
1153 registry_json_footer(w);
1157 // the main method for searching the URLs of a netdata
1158 int registry_request_search_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
1159 if(!registry.enabled)
1160 return registry_json_disabled(w, "search");
1162 MACHINE *m = registry_request_machine(person_guid, machine_guid, url, request_machine, when);
1164 registry_json_header(w, "search", REGISTRY_STATUS_FAILED);
1165 registry_json_footer(w);
1169 registry_json_header(w, "search", REGISTRY_STATUS_OK);
1171 buffer_strcat(w->response.data, ",\n\t\"urls\": [");
1172 struct registry_json_walk_person_urls_callback c = { NULL, m, w, 0 };
1173 dictionary_get_all(m->urls, registry_json_machine_url_callback, &c);
1174 buffer_strcat(w->response.data, "\n\t]\n");
1176 registry_json_footer(w);
1180 // structure used be the callbacks below
1181 struct registry_person_url_callback_verify_machine_exists_data {
1186 int registry_person_url_callback_verify_machine_exists(void *entry, void *data) {
1187 struct registry_person_url_callback_verify_machine_exists_data *d = (struct registry_person_url_callback_verify_machine_exists_data *)data;
1188 PERSON_URL *pu = (PERSON_URL *)entry;
1191 if(pu->machine == m)
1197 // the main method for switching user identity
1198 int registry_request_switch_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *new_person_guid, time_t when) {
1202 if(!registry.enabled)
1203 return registry_json_disabled(w, "switch");
1205 PERSON *op = registry_person_find(person_guid);
1207 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1208 registry_json_footer(w);
1212 PERSON *np = registry_person_find(new_person_guid);
1214 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1215 registry_json_footer(w);
1219 MACHINE *m = registry_machine_find(machine_guid);
1221 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1222 registry_json_footer(w);
1226 struct registry_person_url_callback_verify_machine_exists_data data = { m, 0 };
1228 // verify the old person has access to this machine
1229 dictionary_get_all(op->urls, registry_person_url_callback_verify_machine_exists, &data);
1231 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1232 registry_json_footer(w);
1236 // verify the new person has access to this machine
1238 dictionary_get_all(np->urls, registry_person_url_callback_verify_machine_exists, &data);
1240 registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1241 registry_json_footer(w);
1245 // set the cookie of the new person
1246 // the user just switched identity
1247 registry_set_person_cookie(w, np);
1249 // generate the response
1250 registry_json_header(w, "switch", REGISTRY_STATUS_OK);
1251 buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\"", np->guid);
1252 registry_json_footer(w);
1257 // ----------------------------------------------------------------------------
1258 // REGISTRY THIS MACHINE UNIQUE ID
1260 char *registry_get_this_machine_guid(void) {
1261 if(likely(registry.machine_guid[0]))
1262 return registry.machine_guid;
1264 // read it from disk
1265 int fd = open(registry.machine_guid_filename, O_RDONLY);
1268 if(read(fd, buf, 36) != 36)
1269 error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
1272 if(registry_regenerate_guid(buf, registry.machine_guid) == -1) {
1273 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
1274 buf, registry.machine_guid_filename);
1276 registry.machine_guid[0] = '\0';
1282 // generate a new one?
1283 if(!registry.machine_guid[0]) {
1286 uuid_generate_time(uuid);
1287 uuid_unparse_lower(uuid, registry.machine_guid);
1288 registry.machine_guid[36] = '\0';
1291 fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
1293 fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1295 if(write(fd, registry.machine_guid, 36) != 36)
1296 fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1301 return registry.machine_guid;
1305 // ----------------------------------------------------------------------------
1306 // REGISTRY LOAD/SAVE
1308 int registry_machine_save_url(void *entry, void *file) {
1309 MACHINE_URL *mu = entry;
1312 debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
1314 int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
1322 // error handling is done at registry_save()
1327 int registry_machine_save(void *entry, void *file) {
1331 debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
1333 int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
1341 int ret2 = dictionary_get_all(m->urls, registry_machine_save_url, fp);
1342 if(ret2 < 0) return ret2;
1346 // error handling is done at registry_save()
1351 static inline int registry_person_save_url(void *entry, void *file) {
1352 PERSON_URL *pu = entry;
1355 debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
1357 int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
1367 // error handling is done at registry_save()
1372 static inline int registry_person_save(void *entry, void *file) {
1376 debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
1378 int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
1386 int ret2 = dictionary_get_all(p->urls, registry_person_save_url, fp);
1387 if (ret2 < 0) return ret2;
1391 // error handling is done at registry_save()
1396 int registry_save(void) {
1397 if(!registry.enabled) return -1;
1399 // make sure the log is not updated
1400 registry_log_lock();
1402 if(unlikely(!registry_should_save_db())) {
1403 registry_log_unlock();
1407 char tmp_filename[FILENAME_MAX + 1];
1408 char old_filename[FILENAME_MAX + 1];
1410 snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
1411 snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
1413 debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
1414 FILE *fp = fopen(tmp_filename, "w");
1416 error("Registry: Cannot create file: %s", tmp_filename);
1417 registry_log_unlock();
1421 // dictionary_get_all() has its own locking, so this is safe to do
1423 debug(D_REGISTRY, "Saving all machines");
1424 int bytes1 = dictionary_get_all(registry.machines, registry_machine_save, fp);
1426 error("Registry: Cannot save registry machines - return value %d", bytes1);
1428 registry_log_unlock();
1431 debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
1433 debug(D_REGISTRY, "Saving all persons");
1434 int bytes2 = dictionary_get_all(registry.persons, registry_person_save, fp);
1436 error("Registry: Cannot save registry persons - return value %d", bytes2);
1438 registry_log_unlock();
1441 debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
1444 fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
1445 registry.persons_count,
1446 registry.machines_count,
1447 registry.usages_count + 1, // this is required - it is lost on db rotation
1448 registry.urls_count,
1449 registry.persons_urls_count,
1450 registry.machines_urls_count
1457 // remove the .old db
1458 debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
1459 if(unlink(old_filename) == -1 && errno != ENOENT)
1460 error("Registry: cannot remove old registry file '%s'", old_filename);
1462 // rename the db to .old
1463 debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
1464 if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
1465 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename, registry.db_filename);
1468 // remove the database (it is saved in .old)
1469 debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
1470 if (unlink(registry.db_filename) == -1 && errno != ENOENT)
1471 error("Registry: cannot remove old registry file '%s'", registry.db_filename);
1473 // move the .tmp to make it active
1474 debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
1475 if (link(tmp_filename, registry.db_filename) == -1) {
1476 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
1477 registry.db_filename);
1479 // move the .old back
1480 debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
1481 if(link(old_filename, registry.db_filename) == -1)
1482 error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
1485 debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
1486 if(unlink(tmp_filename) == -1)
1487 error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
1489 // it has been moved successfully
1490 // discard the current registry log
1491 registry_log_recreate_nolock();
1493 registry.log_count = 0;
1497 // continue operations
1498 registry_log_unlock();
1503 static inline size_t registry_load(void) {
1504 char *s, buf[4096 + 1];
1510 debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
1511 FILE *fp = fopen(registry.db_filename, "r");
1513 error("Registry: cannot open registry file: '%s'", registry.db_filename);
1519 while((s = fgets_trim_len(buf, 4096, fp, &len))) {
1522 debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
1525 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')) {
1526 error("Registry totals line %zu is wrong (len = %zu).", line, len);
1529 registry.persons_count = strtoull(&s[2], NULL, 16);
1530 registry.machines_count = strtoull(&s[19], NULL, 16);
1531 registry.usages_count = strtoull(&s[36], NULL, 16);
1532 registry.urls_count = strtoull(&s[53], NULL, 16);
1533 registry.persons_urls_count = strtoull(&s[70], NULL, 16);
1534 registry.machines_urls_count = strtoull(&s[87], NULL, 16);
1539 // verify it is valid
1540 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1541 error("Registry person line %zu is wrong (len = %zu).", line, len);
1545 s[1] = s[10] = s[19] = s[28] = '\0';
1546 p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
1547 p->last_t = strtoul(&s[11], NULL, 16);
1548 p->usages = strtoul(&s[20], NULL, 16);
1549 debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
1552 case 'M': // machine
1554 // verify it is valid
1555 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1556 error("Registry person line %zu is wrong (len = %zu).", line, len);
1560 s[1] = s[10] = s[19] = s[28] = '\0';
1561 m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
1562 m->last_t = strtoul(&s[11], NULL, 16);
1563 m->usages = strtoul(&s[20], NULL, 16);
1564 debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
1567 case 'U': // person URL
1569 error("Registry: ignoring line %zu, no person loaded: %s", line, s);
1573 // verify it is valid
1574 if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
1575 error("Registry person URL line %zu is wrong (len = %zu).", line, len);
1579 s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
1581 // skip the name to find the url
1583 while(*url && *url != '\t') url++;
1585 error("Registry person URL line %zu does not have a url.", line);
1590 u = registry_url_allocate_nolock(url, strlen(url));
1592 time_t first_t = strtoul(&s[2], NULL, 16);
1594 m = registry_machine_find(&s[32]);
1595 if(!m) m = registry_machine_allocate(&s[32], first_t);
1597 PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
1598 pu->last_t = strtoul(&s[11], NULL, 16);
1599 pu->usages = strtoul(&s[20], NULL, 16);
1600 pu->flags = strtoul(&s[29], NULL, 16);
1601 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);
1604 case 'V': // machine URL
1606 error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
1610 // verify it is valid
1611 if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
1612 error("Registry person URL line %zu is wrong (len = %zu).", line, len);
1616 s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
1617 u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
1619 MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
1620 mu->last_t = strtoul(&s[11], NULL, 16);
1621 mu->usages = strtoul(&s[20], NULL, 16);
1622 mu->flags = strtoul(&s[29], NULL, 16);
1623 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);
1627 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
1636 // ----------------------------------------------------------------------------
1639 int registry_init(void) {
1640 char filename[FILENAME_MAX + 1];
1642 // registry enabled?
1643 registry.enabled = config_get_boolean("registry", "enabled", 0);
1646 registry.pathname = config_get("registry", "registry db directory", VARLIB_DIR "/registry");
1647 if(mkdir(registry.pathname, 0755) == -1 && errno != EEXIST) {
1648 error("Cannot create directory '%s'. Registry disabled.", registry.pathname);
1649 registry.enabled = 0;
1654 snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
1655 registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
1656 registry_get_this_machine_guid();
1658 snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
1659 registry.db_filename = config_get("registry", "registry db file", filename);
1661 snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
1662 registry.log_filename = config_get("registry", "registry log file", filename);
1664 // configuration options
1665 registry.save_registry_every_entries = config_get_number("registry", "registry save db every new entries", 1000000);
1666 registry.persons_expiration = config_get_number("registry", "registry expire idle persons days", 365) * 86400;
1667 registry.registry_domain = config_get("registry", "registry domain", "");
1668 registry.registry_to_announce = config_get("registry", "registry to announce", "https://registry.my-netdata.io");
1669 registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", hostname));
1670 registry.verify_cookies_redirects = config_get_boolean("registry", "verify browser cookies support", 1);
1672 registry.max_url_length = config_get_number("registry", "max URL length", 1024);
1673 if(registry.max_url_length < 10) {
1674 registry.max_url_length = 10;
1675 config_set_number("registry", "max URL length", registry.max_url_length);
1678 registry.max_name_length = config_get_number("registry", "max URL name length", 50);
1679 if(registry.max_name_length < 10) {
1680 registry.max_name_length = 10;
1681 config_set_number("registry", "max URL name length", registry.max_name_length);
1684 // initialize entries counters
1685 registry.persons_count = 0;
1686 registry.machines_count = 0;
1687 registry.usages_count = 0;
1688 registry.urls_count = 0;
1689 registry.persons_urls_count = 0;
1690 registry.machines_urls_count = 0;
1692 // initialize memory counters
1693 registry.persons_memory = 0;
1694 registry.machines_memory = 0;
1695 registry.urls_memory = 0;
1696 registry.persons_urls_memory = 0;
1697 registry.machines_urls_memory = 0;
1700 pthread_mutex_init(®istry.persons_lock, NULL);
1701 pthread_mutex_init(®istry.machines_lock, NULL);
1702 pthread_mutex_init(®istry.urls_lock, NULL);
1703 pthread_mutex_init(®istry.person_urls_lock, NULL);
1704 pthread_mutex_init(®istry.machine_urls_lock, NULL);
1706 // create dictionaries
1707 registry.persons = dictionary_create(DICTIONARY_FLAGS);
1708 registry.machines = dictionary_create(DICTIONARY_FLAGS);
1709 registry.urls = dictionary_create(DICTIONARY_FLAGS);
1711 // load the registry database
1712 if(registry.enabled) {
1713 registry_log_open_nolock();
1715 registry_log_load();
1721 void registry_free(void) {
1722 if(!registry.enabled) return;
1724 // we need to destroy the dictionaries ourselves
1725 // since the dictionaries use memory we allocated
1727 while(registry.persons->values_index.root) {
1728 PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
1730 // fprintf(stderr, "\nPERSON: '%s', first: %u, last: %u, usages: %u\n", p->guid, p->first_t, p->last_t, p->usages);
1732 while(p->urls->values_index.root) {
1733 PERSON_URL *pu = ((NAME_VALUE *)p->urls->values_index.root)->value;
1735 // 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);
1737 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", pu->url->url, p->guid);
1738 dictionary_del(p->urls, pu->url->url);
1740 debug(D_REGISTRY, "Registry: unlinking url '%s' from person", pu->url->url);
1741 registry_url_unlink_nolock(pu->url);
1743 debug(D_REGISTRY, "Registry: freeing person url");
1747 debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
1748 dictionary_del(registry.persons, p->guid);
1750 debug(D_REGISTRY, "Registry: destroying URL dictionary of person '%s'", p->guid);
1751 dictionary_destroy(p->urls);
1753 debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
1757 while(registry.machines->values_index.root) {
1758 MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
1760 // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
1762 while(m->urls->values_index.root) {
1763 MACHINE_URL *mu = ((NAME_VALUE *)m->urls->values_index.root)->value;
1765 // 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);
1767 //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
1768 //dictionary_destroy(mu->persons);
1770 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
1771 dictionary_del(m->urls, mu->url->url);
1773 debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
1774 registry_url_unlink_nolock(mu->url);
1776 debug(D_REGISTRY, "Registry: freeing machine url");
1780 debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
1781 dictionary_del(registry.machines, m->guid);
1783 debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
1784 dictionary_destroy(m->urls);
1786 debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
1790 // and free the memory of remaining dictionary structures
1792 debug(D_REGISTRY, "Registry: destroying persons dictionary");
1793 dictionary_destroy(registry.persons);
1795 debug(D_REGISTRY, "Registry: destroying machines dictionary");
1796 dictionary_destroy(registry.machines);
1798 debug(D_REGISTRY, "Registry: destroying urls dictionary");
1799 dictionary_destroy(registry.urls);
1802 // ----------------------------------------------------------------------------
1805 void registry_statistics(void) {
1806 if(!registry.enabled) return;
1808 static RRDSET *sts = NULL, *stc = NULL, *stm = NULL;
1810 if(!sts) sts = rrdset_find("netdata.registry_sessions");
1812 sts = rrdset_create("netdata", "registry_sessions", NULL, "registry", NULL, "NetData Registry Sessions", "session", 131000, rrd_update_every, RRDSET_TYPE_LINE);
1814 rrddim_add(sts, "sessions", NULL, 1, 1, RRDDIM_ABSOLUTE);
1816 else rrdset_next(sts);
1818 rrddim_set(sts, "sessions", registry.usages_count);
1821 // ------------------------------------------------------------------------
1823 if(!stc) stc = rrdset_find("netdata.registry_entries");
1825 stc = rrdset_create("netdata", "registry_entries", NULL, "registry", NULL, "NetData Registry Entries", "entries", 131100, rrd_update_every, RRDSET_TYPE_LINE);
1827 rrddim_add(stc, "persons", NULL, 1, 1, RRDDIM_ABSOLUTE);
1828 rrddim_add(stc, "machines", NULL, 1, 1, RRDDIM_ABSOLUTE);
1829 rrddim_add(stc, "urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1830 rrddim_add(stc, "persons_urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1831 rrddim_add(stc, "machines_urls", NULL, 1, 1, RRDDIM_ABSOLUTE);
1833 else rrdset_next(stc);
1835 rrddim_set(stc, "persons", registry.persons_count);
1836 rrddim_set(stc, "machines", registry.machines_count);
1837 rrddim_set(stc, "urls", registry.urls_count);
1838 rrddim_set(stc, "persons_urls", registry.persons_urls_count);
1839 rrddim_set(stc, "machines_urls", registry.machines_urls_count);
1842 // ------------------------------------------------------------------------
1844 if(!stm) stm = rrdset_find("netdata.registry_mem");
1846 stm = rrdset_create("netdata", "registry_mem", NULL, "registry", NULL, "NetData Registry Memory", "KB", 131300, rrd_update_every, RRDSET_TYPE_STACKED);
1848 rrddim_add(stm, "persons", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1849 rrddim_add(stm, "machines", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1850 rrddim_add(stm, "urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1851 rrddim_add(stm, "persons_urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1852 rrddim_add(stm, "machines_urls", NULL, 1, 1024, RRDDIM_ABSOLUTE);
1854 else rrdset_next(stm);
1856 rrddim_set(stm, "persons", registry.persons_memory + registry.persons_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1857 rrddim_set(stm, "machines", registry.machines_memory + registry.machines_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1858 rrddim_set(stm, "urls", registry.urls_memory + registry.urls_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1859 rrddim_set(stm, "persons_urls", registry.persons_urls_memory + registry.persons_count * sizeof(DICTIONARY) + registry.persons_urls_count * sizeof(NAME_VALUE));
1860 rrddim_set(stm, "machines_urls", registry.machines_urls_memory + registry.machines_count * sizeof(DICTIONARY) + registry.machines_urls_count * sizeof(NAME_VALUE));