]> arthur.barton.de Git - netdata.git/blob - src/registry.c
object renames in registry
[netdata.git] / src / registry.c
1 #include "common.h"
2
3 // ----------------------------------------------------------------------------
4 // TODO
5 //
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
9 //    on-memory entries.
10 //
11 //    Cleanup:
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
17 //
18 // 2. add protection to prevent abusing the registry by flooding it with
19 //    requests to fill the memory and crash it.
20 //
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
30 //
31 // 3. lower memory requirements
32 //
33 //    - embed avl structures directly into registry objects, instead of DICTIONARY
34 //    - store GUIDs in memory as UUID instead of char *
35 //    - do not track persons using the demo machines only
36 //      (i.e. start tracking them only when they access a non-demo machine)
37 //    - [DONE] do not track custom dashboards by default
38
39 #define REGISTRY_URL_FLAGS_DEFAULT 0x00
40 #define REGISTRY_URL_FLAGS_EXPIRED 0x01
41
42 #define DICTIONARY_FLAGS DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE | DICTIONARY_FLAG_NAME_LINK_DONT_CLONE
43
44 #define GUID_LEN 36
45
46 // ----------------------------------------------------------------------------
47 // COMMON structures
48
49 struct registry {
50     int enabled;
51
52     char machine_guid[GUID_LEN + 1];
53
54     // entries counters / statistics
55     unsigned long long persons_count;
56     unsigned long long machines_count;
57     unsigned long long usages_count;
58     unsigned long long urls_count;
59     unsigned long long persons_urls_count;
60     unsigned long long machines_urls_count;
61     unsigned long long log_count;
62
63     // memory counters / statistics
64     unsigned long long persons_memory;
65     unsigned long long machines_memory;
66     unsigned long long urls_memory;
67     unsigned long long persons_urls_memory;
68     unsigned long long machines_urls_memory;
69
70     // configuration
71     unsigned long long save_registry_every_entries;
72     char *registry_domain;
73     char *hostname;
74     char *registry_to_announce;
75     time_t persons_expiration; // seconds to expire idle persons
76     int verify_cookies_redirects;
77
78     size_t max_url_length;
79     size_t max_name_length;
80
81     // file/path names
82     char *pathname;
83     char *db_filename;
84     char *log_filename;
85     char *machine_guid_filename;
86
87     // open files
88     FILE *log_fp;
89
90     // the database
91     DICTIONARY *persons;    // dictionary of PERSON *, with key the PERSON.guid
92     DICTIONARY *machines;   // dictionary of MACHINE *, with key the MACHINE.guid
93     DICTIONARY *urls;       // dictionary of URL *, with key the URL.url
94
95     // concurrency locking
96     // we keep different locks for different things
97     // so that many tasks can be completed in parallel
98     pthread_mutex_t persons_lock;
99     pthread_mutex_t machines_lock;
100     pthread_mutex_t urls_lock;
101     pthread_mutex_t person_urls_lock;
102     pthread_mutex_t machine_urls_lock;
103     pthread_mutex_t log_lock;
104 } registry;
105
106
107 // ----------------------------------------------------------------------------
108 // URL structures
109 // Save memory by de-duplicating URLs
110 // so instead of storing URLs all over the place
111 // we store them here and we keep pointers elsewhere
112
113 struct registry_url {
114     uint32_t links; // the number of links to this URL - when none is left, we free it
115     uint16_t len;   // the length of the URL in bytes
116     char url[1];    // the URL - dynamically allocated to more size
117 };
118 typedef struct registry_url REGISTRY_URL;
119
120
121 // ----------------------------------------------------------------------------
122 // MACHINE structures
123
124 // For each MACHINE-URL pair we keep this
125 struct registry_machine_url {
126     REGISTRY_URL *url;          // de-duplicated URL
127 //  DICTIONARY *persons;        // dictionary of PERSON *
128
129     uint8_t flags;
130     uint32_t first_t;           // the first time we saw this
131     uint32_t last_t;            // the last time we saw this
132     uint32_t usages;            // how many times this has been accessed
133 };
134 typedef struct registry_machine_url REGISTRY_MACHINE_URL;
135
136 // A machine
137 struct registry_machine {
138     char guid[GUID_LEN + 1];    // the GUID
139
140     uint32_t links;             // the number of PERSON_URLs linked to this machine
141
142     DICTIONARY *urls;           // MACHINE_URL *
143
144     uint32_t first_t;           // the first time we saw this
145     uint32_t last_t;            // the last time we saw this
146     uint32_t usages;            // how many times this has been accessed
147 };
148 typedef struct registry_machine REGISTRY_MACHINE;
149
150
151 // ----------------------------------------------------------------------------
152 // PERSON structures
153
154 // for each PERSON-URL pair we keep this
155 struct person_url {
156     REGISTRY_URL *url;          // de-duplicated URL
157     REGISTRY_MACHINE *machine;  // link the MACHINE of this URL
158
159     uint8_t flags;
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
163
164     char name[1];               // the name of the URL, as known by the user
165                                 // dynamically allocated to fit properly
166 };
167 typedef struct person_url REGISTRY_PERSON_URL;
168
169 // A person
170 struct registry_person {
171     char guid[GUID_LEN + 1];    // the person GUID
172
173     DICTIONARY *urls;           // dictionary of PERSON_URL *
174
175     uint32_t first_t;           // the first time we saw this
176     uint32_t last_t;            // the last time we saw this
177     uint32_t usages;            // how many times this has been accessed
178 };
179 typedef struct registry_person REGISTRY_PERSON;
180
181
182 // ----------------------------------------------------------------------------
183 // REGISTRY concurrency locking
184
185 static inline void registry_persons_lock(void) {
186     pthread_mutex_lock(&registry.persons_lock);
187 }
188
189 static inline void registry_persons_unlock(void) {
190     pthread_mutex_unlock(&registry.persons_lock);
191 }
192
193 static inline void registry_machines_lock(void) {
194     pthread_mutex_lock(&registry.machines_lock);
195 }
196
197 static inline void registry_machines_unlock(void) {
198     pthread_mutex_unlock(&registry.machines_lock);
199 }
200
201 static inline void registry_urls_lock(void) {
202     pthread_mutex_lock(&registry.urls_lock);
203 }
204
205 static inline void registry_urls_unlock(void) {
206     pthread_mutex_unlock(&registry.urls_lock);
207 }
208
209 // ideally, we should not lock the whole registry for
210 // updating a person's urls.
211 // however, to save the memory required for keeping a
212 // mutex (40 bytes) per person, we do...
213 static inline void registry_person_urls_lock(REGISTRY_PERSON *p) {
214     (void)p;
215     pthread_mutex_lock(&registry.person_urls_lock);
216 }
217
218 static inline void registry_person_urls_unlock(REGISTRY_PERSON *p) {
219     (void)p;
220     pthread_mutex_unlock(&registry.person_urls_lock);
221 }
222
223 // ideally, we should not lock the whole registry for
224 // updating a machine's urls.
225 // however, to save the memory required for keeping a
226 // mutex (40 bytes) per machine, we do...
227 static inline void registry_machine_urls_lock(REGISTRY_MACHINE *m) {
228     (void)m;
229     pthread_mutex_lock(&registry.machine_urls_lock);
230 }
231
232 static inline void registry_machine_urls_unlock(REGISTRY_MACHINE *m) {
233     (void)m;
234     pthread_mutex_unlock(&registry.machine_urls_lock);
235 }
236
237 static inline void registry_log_lock(void) {
238     pthread_mutex_lock(&registry.log_lock);
239 }
240
241 static inline void registry_log_unlock(void) {
242     pthread_mutex_unlock(&registry.log_lock);
243 }
244
245
246 // ----------------------------------------------------------------------------
247 // common functions
248
249 // parse a GUID and re-generated to be always lower case
250 // this is used as a protection against the variations of GUIDs
251 static inline int registry_regenerate_guid(const char *guid, char *result) {
252     uuid_t uuid;
253     if(unlikely(uuid_parse(guid, uuid) == -1)) {
254         info("Registry: GUID '%s' is not a valid GUID.", guid);
255         return -1;
256     }
257     else {
258         uuid_unparse_lower(uuid, result);
259
260 #ifdef NETDATA_INTERNAL_CHECKS
261         if(strcmp(guid, result))
262             info("Registry: source GUID '%s' and re-generated GUID '%s' differ!", guid, result);
263 #endif /* NETDATA_INTERNAL_CHECKS */
264     }
265
266     return 0;
267 }
268
269 // make sure the names of the machines / URLs do not contain any tabs
270 // (which are used as our separator in the database files)
271 // and are properly trimmed (before and after)
272 static inline char *registry_fix_machine_name(char *name, size_t *len) {
273     char *s = name?name:"";
274
275     // skip leading spaces
276     while(*s && isspace(*s)) s++;
277
278     // make sure all spaces are a SPACE
279     char *t = s;
280     while(*t) {
281         if(unlikely(isspace(*t)))
282             *t = ' ';
283
284         t++;
285     }
286
287     // remove trailing spaces
288     while(--t >= s) {
289         if(*t == ' ')
290             *t = '\0';
291         else
292             break;
293     }
294     t++;
295
296     if(likely(len))
297         *len = (t - s);
298
299     return s;
300 }
301
302 static inline char *registry_fix_url(char *url, size_t *len) {
303     return registry_fix_machine_name(url, len);
304 }
305
306
307 // ----------------------------------------------------------------------------
308 // forward definition of functions
309
310 extern REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when);
311 extern REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
312
313
314 // ----------------------------------------------------------------------------
315 // URL
316
317 static inline REGISTRY_URL *registry_url_allocate_nolock(const char *url, size_t urllen) {
318     // protection from too big URLs
319     if(urllen > registry.max_url_length)
320         urllen = registry.max_url_length;
321
322     debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): allocating %zu bytes", url, sizeof(REGISTRY_URL) + urllen);
323     REGISTRY_URL *u = mallocz(sizeof(REGISTRY_URL) + urllen);
324
325     // a simple strcpy() should do the job
326     // but I prefer to be safe, since the caller specified urllen
327     u->len = (uint16_t)urllen;
328     strncpyz(u->url, url, u->len);
329     u->links = 0;
330
331     registry.urls_memory += sizeof(REGISTRY_URL) + urllen;
332
333     debug(D_REGISTRY, "Registry: registry_url_allocate_nolock('%s'): indexing it", url);
334     dictionary_set(registry.urls, u->url, u, sizeof(REGISTRY_URL));
335
336     return u;
337 }
338
339 static inline REGISTRY_URL *registry_url_get_nolock(const char *url, size_t urllen) {
340     debug(D_REGISTRY, "Registry: registry_url_get_nolock('%s')", url);
341
342     REGISTRY_URL *u = dictionary_get(registry.urls, url);
343     if(!u) {
344         u = registry_url_allocate_nolock(url, urllen);
345         registry.urls_count++;
346     }
347
348     return u;
349 }
350
351 static inline REGISTRY_URL *registry_url_get(const char *url, size_t urllen) {
352     debug(D_REGISTRY, "Registry: registry_url_get('%s')", url);
353
354     registry_urls_lock();
355
356     REGISTRY_URL *u = registry_url_get_nolock(url, urllen);
357
358     registry_urls_unlock();
359
360     return u;
361 }
362
363 static inline void registry_url_link_nolock(REGISTRY_URL *u) {
364     u->links++;
365     debug(D_REGISTRY, "Registry: registry_url_link_nolock('%s'): URL has now %u links", u->url, u->links);
366 }
367
368 static inline void registry_url_unlink_nolock(REGISTRY_URL *u) {
369     u->links--;
370     if(!u->links) {
371         debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): No more links for this URL", u->url);
372         dictionary_del(registry.urls, u->url);
373         freez(u);
374     }
375     else
376         debug(D_REGISTRY, "Registry: registry_url_unlink_nolock('%s'): URL has %u links left", u->url, u->links);
377 }
378
379
380 // ----------------------------------------------------------------------------
381 // MACHINE
382
383 static inline REGISTRY_MACHINE *registry_machine_find(const char *machine_guid) {
384     debug(D_REGISTRY, "Registry: registry_machine_find('%s')", machine_guid);
385     return dictionary_get(registry.machines, machine_guid);
386 }
387
388 static inline REGISTRY_MACHINE_URL *registry_machine_url_allocate(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
389     debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(REGISTRY_MACHINE_URL));
390
391     REGISTRY_MACHINE_URL *mu = mallocz(sizeof(REGISTRY_MACHINE_URL));
392
393     // mu->persons = dictionary_create(DICTIONARY_FLAGS);
394     // dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
395
396     mu->first_t = mu->last_t = (uint32_t)when;
397     mu->usages = 1;
398     mu->url = u;
399     mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
400
401     registry.machines_urls_memory += sizeof(REGISTRY_MACHINE_URL);
402
403     debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): indexing URL in machine", m->guid, u->url);
404     dictionary_set(m->urls, u->url, mu, sizeof(REGISTRY_MACHINE_URL));
405     registry_url_link_nolock(u);
406
407     return mu;
408 }
409
410 static inline REGISTRY_MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
411     debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(REGISTRY_MACHINE));
412
413     REGISTRY_MACHINE *m = mallocz(sizeof(REGISTRY_MACHINE));
414
415     strncpyz(m->guid, machine_guid, GUID_LEN);
416
417     debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
418     m->urls = dictionary_create(DICTIONARY_FLAGS);
419
420     m->first_t = m->last_t = (uint32_t)when;
421     m->usages = 0;
422
423     registry.machines_memory += sizeof(REGISTRY_MACHINE);
424
425     registry.machines_count++;
426     dictionary_set(registry.machines, m->guid, m, sizeof(REGISTRY_MACHINE));
427
428     return m;
429 }
430
431 // 1. validate machine GUID
432 // 2. if it is valid, find it or create it and return it
433 // 3. if it is not valid, return NULL
434 static inline REGISTRY_MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
435     REGISTRY_MACHINE *m = NULL;
436
437     registry_machines_lock();
438
439     if(likely(machine_guid && *machine_guid)) {
440         // validate it is a GUID
441         char buf[GUID_LEN + 1];
442         if(unlikely(registry_regenerate_guid(machine_guid, buf) == -1))
443             info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
444         else {
445             machine_guid = buf;
446             m = registry_machine_find(machine_guid);
447             if(!m) m = registry_machine_allocate(machine_guid, when);
448         }
449     }
450
451     registry_machines_unlock();
452
453     return m;
454 }
455
456
457 // ----------------------------------------------------------------------------
458 // PERSON
459
460 static inline REGISTRY_PERSON *registry_person_find(const char *person_guid) {
461     debug(D_REGISTRY, "Registry: registry_person_find('%s')", person_guid);
462     return dictionary_get(registry.persons, person_guid);
463 }
464
465 static inline REGISTRY_PERSON_URL *registry_person_url_allocate(REGISTRY_PERSON *p, REGISTRY_MACHINE *m, REGISTRY_URL *u, char *name, size_t namelen, time_t when) {
466     // protection from too big names
467     if(namelen > registry.max_name_length)
468         namelen = registry.max_name_length;
469
470     debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
471           sizeof(REGISTRY_PERSON_URL) + namelen);
472
473     REGISTRY_PERSON_URL *pu = mallocz(sizeof(REGISTRY_PERSON_URL) + namelen);
474
475     // a simple strcpy() should do the job
476     // but I prefer to be safe, since the caller specified urllen
477     strncpyz(pu->name, name, namelen);
478
479     pu->machine = m;
480     pu->first_t = pu->last_t = when;
481     pu->usages = 1;
482     pu->url = u;
483     pu->flags = REGISTRY_URL_FLAGS_DEFAULT;
484     m->links++;
485
486     registry.persons_urls_memory += sizeof(REGISTRY_PERSON_URL) + namelen;
487
488     debug(D_REGISTRY, "registry_person_url_allocate('%s', '%s', '%s'): indexing URL in person", p->guid, m->guid, u->url);
489     dictionary_set(p->urls, u->url, pu, sizeof(REGISTRY_PERSON_URL));
490     registry_url_link_nolock(u);
491
492     return pu;
493 }
494
495 static inline REGISTRY_PERSON_URL *registry_person_url_reallocate(REGISTRY_PERSON *p, REGISTRY_MACHINE *m, REGISTRY_URL *u, char *name, size_t namelen, time_t when, REGISTRY_PERSON_URL *pu) {
496     // this function is needed to change the name of a PERSON_URL
497
498     debug(D_REGISTRY, "registry_person_url_reallocate('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url,
499           sizeof(REGISTRY_PERSON_URL) + namelen);
500
501     REGISTRY_PERSON_URL *tpu = registry_person_url_allocate(p, m, u, name, namelen, when);
502     tpu->first_t = pu->first_t;
503     tpu->last_t = pu->last_t;
504     tpu->usages = pu->usages;
505
506     // ok, these are a hack - since the registry_person_url_allocate() is
507     // adding these, we have to subtract them
508     tpu->machine->links--;
509     registry.persons_urls_memory -= sizeof(REGISTRY_PERSON_URL) + strlen(pu->name);
510     registry_url_unlink_nolock(u);
511
512     freez(pu);
513
514     return tpu;
515 }
516
517 static inline REGISTRY_PERSON *registry_person_allocate(const char *person_guid, time_t when) {
518     REGISTRY_PERSON *p = NULL;
519
520     debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(REGISTRY_PERSON));
521
522     p = mallocz(sizeof(REGISTRY_PERSON));
523
524     if(!person_guid) {
525         for (; ;) {
526             uuid_t uuid;
527             uuid_generate(uuid);
528             uuid_unparse_lower(uuid, p->guid);
529
530             debug(D_REGISTRY, "Registry: Checking if the generated person guid '%s' is unique", p->guid);
531             if (!dictionary_get(registry.persons, p->guid)) {
532                 debug(D_REGISTRY, "Registry: generated person guid '%s' is unique", p->guid);
533                 break;
534             }
535             else
536                 info("Registry: generated person guid '%s' found in the registry. Retrying...", p->guid);
537         }
538     }
539     else
540         strncpyz(p->guid, person_guid, GUID_LEN);
541
542     debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): creating dictionary of urls", p->guid);
543     p->urls = dictionary_create(DICTIONARY_FLAGS);
544
545     p->first_t = p->last_t = when;
546     p->usages = 0;
547
548     registry.persons_memory += sizeof(REGISTRY_PERSON);
549
550     registry.persons_count++;
551     dictionary_set(registry.persons, p->guid, p, sizeof(REGISTRY_PERSON));
552
553     return p;
554 }
555
556
557 // 1. validate person GUID
558 // 2. if it is valid, find it
559 // 3. if it is not valid, create a new one
560 // 4. return it
561 static inline REGISTRY_PERSON *registry_person_get(const char *person_guid, time_t when) {
562     REGISTRY_PERSON *p = NULL;
563
564     registry_persons_lock();
565
566     if(person_guid && *person_guid) {
567         char buf[GUID_LEN + 1];
568         // validate it is a GUID
569         if(unlikely(registry_regenerate_guid(person_guid, buf) == -1))
570             info("Registry: person guid '%s' is not a valid guid. Ignoring it.", person_guid);
571         else {
572             person_guid = buf;
573             p = registry_person_find(person_guid);
574         }
575     }
576
577     if(!p) p = registry_person_allocate(NULL, when);
578
579     registry_persons_unlock();
580
581     return p;
582 }
583
584 // ----------------------------------------------------------------------------
585 // LINKING OF OBJECTS
586
587 static inline REGISTRY_PERSON_URL *registry_person_link_to_url(REGISTRY_PERSON *p, REGISTRY_MACHINE *m, REGISTRY_URL *u, char *name, size_t namelen, time_t when) {
588     debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): searching for URL in person", p->guid, m->guid, u->url);
589
590     registry_person_urls_lock(p);
591
592     REGISTRY_PERSON_URL *pu = dictionary_get(p->urls, u->url);
593     if(!pu) {
594         debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
595         pu = registry_person_url_allocate(p, m, u, name, namelen, when);
596         registry.persons_urls_count++;
597     }
598     else {
599         debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
600         pu->usages++;
601         if(likely(pu->last_t < (uint32_t)when)) pu->last_t = when;
602
603         if(pu->machine != m) {
604             REGISTRY_MACHINE_URL *mu = dictionary_get(pu->machine->urls, u->url);
605             if(mu) {
606                 info("registry_person_link_to_url('%s', '%s', '%s'): URL switched machines (old was '%s') - expiring it from previous machine.",
607                      p->guid, m->guid, u->url, pu->machine->guid);
608                 mu->flags |= REGISTRY_URL_FLAGS_EXPIRED;
609             }
610             else {
611                 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.",
612                      p->guid, m->guid, u->url, pu->machine->guid);
613             }
614
615             pu->machine->links--;
616             pu->machine = m;
617         }
618
619         if(strcmp(pu->name, name)) {
620             // the name of the PERSON_URL has changed !
621             pu = registry_person_url_reallocate(p, m, u, name, namelen, when, pu);
622         }
623     }
624
625     p->usages++;
626     if(likely(p->last_t < (uint32_t)when)) p->last_t = when;
627
628     if(pu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
629         info("registry_person_link_to_url('%s', '%s', '%s'): accessing an expired URL. Re-enabling URL.", p->guid, m->guid, u->url);
630         pu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
631     }
632
633     registry_person_urls_unlock(p);
634
635     return pu;
636 }
637
638 static inline REGISTRY_MACHINE_URL *registry_machine_link_to_url(REGISTRY_PERSON *p, REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
639     debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): searching for URL in machine", p->guid, m->guid, u->url);
640
641     registry_machine_urls_lock(m);
642
643     REGISTRY_MACHINE_URL *mu = dictionary_get(m->urls, u->url);
644     if(!mu) {
645         debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
646         mu = registry_machine_url_allocate(m, u, when);
647         registry.machines_urls_count++;
648     }
649     else {
650         debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
651         mu->usages++;
652         if(likely(mu->last_t < (uint32_t)when)) mu->last_t = when;
653     }
654
655     //debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): indexing person in machine", p->guid, m->guid, u->url);
656     //dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
657
658     m->usages++;
659     if(likely(m->last_t < (uint32_t)when)) m->last_t = when;
660
661     if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
662         info("registry_machine_link_to_url('%s', '%s', '%s'): accessing an expired URL.", p->guid, m->guid, u->url);
663         mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
664     }
665
666     registry_machine_urls_unlock(m);
667
668     return mu;
669 }
670
671 // ----------------------------------------------------------------------------
672 // REGISTRY LOG LOAD/SAVE
673
674 static inline int registry_should_save_db(void) {
675     debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
676     return registry.log_count > registry.save_registry_every_entries;
677 }
678
679 static inline void registry_log(const char action, REGISTRY_PERSON *p, REGISTRY_MACHINE *m, REGISTRY_URL *u, char *name) {
680     if(likely(registry.log_fp)) {
681         // we lock only if the file is open
682         // to allow replaying the log at registry_log_load()
683         registry_log_lock();
684
685         if(unlikely(fprintf(registry.log_fp, "%c\t%08x\t%s\t%s\t%s\t%s\n",
686                 action,
687                 p->last_t,
688                 p->guid,
689                 m->guid,
690                 name,
691                 u->url) < 0))
692             error("Registry: failed to save log. Registry data may be lost in case of abnormal restart.");
693
694         // we increase the counter even on failures
695         // so that the registry will be saved periodically
696         registry.log_count++;
697
698         registry_log_unlock();
699
700         // this must be outside the log_lock(), or a deadlock will happen.
701         // registry_save() checks the same inside the log_lock, so only
702         // one thread will save the db
703         if(unlikely(registry_should_save_db()))
704             registry_save();
705     }
706 }
707
708 static inline int registry_log_open_nolock(void) {
709     if(registry.log_fp)
710         fclose(registry.log_fp);
711
712     registry.log_fp = fopen(registry.log_filename, "a");
713
714     if(registry.log_fp) {
715         if (setvbuf(registry.log_fp, NULL, _IOLBF, 0) != 0)
716             error("Cannot set line buffering on registry log file.");
717         return 0;
718     }
719
720     error("Cannot open registry log file '%s'. Registry data will be lost in case of netdata or server crash.", registry.log_filename);
721     return -1;
722 }
723
724 static inline void registry_log_close_nolock(void) {
725     if(registry.log_fp) {
726         fclose(registry.log_fp);
727         registry.log_fp = NULL;
728     }
729 }
730
731 static inline void registry_log_recreate_nolock(void) {
732     if(registry.log_fp != NULL) {
733         registry_log_close_nolock();
734
735         // open it with truncate
736         registry.log_fp = fopen(registry.log_filename, "w");
737         if(registry.log_fp) fclose(registry.log_fp);
738         else error("Cannot truncate registry log '%s'", registry.log_filename);
739
740         registry.log_fp = NULL;
741
742         registry_log_open_nolock();
743     }
744 }
745
746 int registry_log_load(void) {
747     ssize_t line = -1;
748
749     // closing the log is required here
750     // otherwise we will append to it the values we read
751     registry_log_close_nolock();
752
753     debug(D_REGISTRY, "Registry: loading active db from: %s", registry.log_filename);
754     FILE *fp = fopen(registry.log_filename, "r");
755     if(!fp)
756         error("Registry: cannot open registry file: %s", registry.log_filename);
757     else {
758         char *s, buf[4096 + 1];
759         line = 0;
760         size_t len = 0;
761
762         while ((s = fgets_trim_len(buf, 4096, fp, &len))) {
763             line++;
764
765             switch (s[0]) {
766                 case 'A': // accesses
767                 case 'D': // deletes
768
769                     // verify it is valid
770                     if (unlikely(len < 85 || s[1] != '\t' || s[10] != '\t' || s[47] != '\t' || s[84] != '\t')) {
771                         error("Registry: log line %zd is wrong (len = %zu).", line, len);
772                         continue;
773                     }
774                     s[1] = s[10] = s[47] = s[84] = '\0';
775
776                     // get the variables
777                     time_t when = strtoul(&s[2], NULL, 16);
778                     char *person_guid = &s[11];
779                     char *machine_guid = &s[48];
780                     char *name = &s[85];
781
782                     // skip the name to find the url
783                     char *url = name;
784                     while(*url && *url != '\t') url++;
785                     if(!*url) {
786                         error("Registry: log line %zd does not have a url.", line);
787                         continue;
788                     }
789                     *url++ = '\0';
790
791                     // make sure the person exists
792                     // without this, a new person guid will be created
793                     REGISTRY_PERSON *p = registry_person_find(person_guid);
794                     if(!p) p = registry_person_allocate(person_guid, when);
795
796                     if(s[0] == 'A')
797                         registry_request_access(p->guid, machine_guid, url, name, when);
798                     else
799                         registry_request_delete(p->guid, machine_guid, url, name, when);
800
801                     registry.log_count++;
802                     break;
803
804                 default:
805                     error("Registry: ignoring line %zd of filename '%s': %s.", line, registry.log_filename, s);
806                     break;
807             }
808         }
809
810         fclose(fp);
811     }
812
813     // open the log again
814     registry_log_open_nolock();
815
816     return line;
817 }
818
819
820 // ----------------------------------------------------------------------------
821 // REGISTRY REQUESTS
822
823 REGISTRY_PERSON *registry_request_access(char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
824     debug(D_REGISTRY, "registry_request_access('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
825
826     REGISTRY_MACHINE *m = registry_machine_get(machine_guid, when);
827     if(!m) return NULL;
828
829     // make sure the name is valid
830     size_t namelen;
831     name = registry_fix_machine_name(name, &namelen);
832
833     size_t urllen;
834     url = registry_fix_url(url, &urllen);
835
836     REGISTRY_URL *u = registry_url_get(url, urllen);
837     REGISTRY_PERSON *p = registry_person_get(person_guid, when);
838
839     registry_person_link_to_url(p, m, u, name, namelen, when);
840     registry_machine_link_to_url(p, m, u, when);
841
842     registry_log('A', p, m, u, name);
843
844     registry.usages_count++;
845     return p;
846 }
847
848 // verify the person, the machine and the URL exist in our DB
849 REGISTRY_PERSON_URL *registry_verify_request(char *person_guid, char *machine_guid, char *url, REGISTRY_PERSON **pp, REGISTRY_MACHINE **mm) {
850     char pbuf[GUID_LEN + 1], mbuf[GUID_LEN + 1];
851
852     if(!person_guid || !*person_guid || !machine_guid || !*machine_guid || !url || !*url) {
853         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");
854         return NULL;
855     }
856
857     // normalize the url
858     url = registry_fix_url(url, NULL);
859
860     // make sure the person GUID is valid
861     if(registry_regenerate_guid(person_guid, pbuf) == -1) {
862         info("Registry Request Verification: invalid person GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
863         return NULL;
864     }
865     person_guid = pbuf;
866
867     // make sure the machine GUID is valid
868     if(registry_regenerate_guid(machine_guid, mbuf) == -1) {
869         info("Registry Request Verification: invalid machine GUID, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
870         return NULL;
871     }
872     machine_guid = mbuf;
873
874     // make sure the machine exists
875     REGISTRY_MACHINE *m = registry_machine_find(machine_guid);
876     if(!m) {
877         info("Registry Request Verification: machine not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
878         return NULL;
879     }
880     if(mm) *mm = m;
881
882     // make sure the person exist
883     REGISTRY_PERSON *p = registry_person_find(person_guid);
884     if(!p) {
885         info("Registry Request Verification: person not found, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
886         return NULL;
887     }
888     if(pp) *pp = p;
889
890     REGISTRY_PERSON_URL *pu = dictionary_get(p->urls, url);
891     if(!pu) {
892         info("Registry Request Verification: URL not found for person, person: '%s', machine '%s', url '%s'", person_guid, machine_guid, url);
893         return NULL;
894     }
895     return pu;
896 }
897
898 REGISTRY_PERSON *registry_request_delete(char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
899     (void)when;
900
901     REGISTRY_PERSON *p = NULL;
902     REGISTRY_MACHINE *m = NULL;
903     REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
904     if(!pu || !p || !m) return NULL;
905
906     // normalize the url
907     delete_url = registry_fix_url(delete_url, NULL);
908
909     // make sure the user is not deleting the url it uses
910     if(!strcmp(delete_url, pu->url->url)) {
911         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);
912         return NULL;
913     }
914
915     registry_person_urls_lock(p);
916
917     REGISTRY_PERSON_URL *dpu = dictionary_get(p->urls, delete_url);
918     if(!dpu) {
919         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);
920         registry_person_urls_unlock(p);
921         return NULL;
922     }
923
924     registry_log('D', p, m, pu->url, dpu->url->url);
925
926     dictionary_del(p->urls, dpu->url->url);
927     registry_url_unlink_nolock(dpu->url);
928     freez(dpu);
929
930     registry_person_urls_unlock(p);
931     return p;
932 }
933
934
935 // a structure to pass to the dictionary_get_all() callback handler
936 struct machine_request_callback_data {
937     REGISTRY_MACHINE *find_this_machine;
938     REGISTRY_PERSON_URL *result;
939 };
940
941 // the callback function
942 // this will be run for every PERSON_URL of this PERSON
943 int machine_request_callback(void *entry, void *data) {
944     REGISTRY_PERSON_URL *mypu = (REGISTRY_PERSON_URL *)entry;
945     struct machine_request_callback_data *myrdata = (struct machine_request_callback_data *)data;
946
947     if(mypu->machine == myrdata->find_this_machine) {
948         myrdata->result = mypu;
949         return -1; // this will also stop the walk through
950     }
951
952     return 0; // continue
953 }
954
955 REGISTRY_MACHINE *registry_request_machine(char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
956     (void)when;
957
958     char mbuf[GUID_LEN + 1];
959
960     REGISTRY_PERSON *p = NULL;
961     REGISTRY_MACHINE *m = NULL;
962     REGISTRY_PERSON_URL *pu = registry_verify_request(person_guid, machine_guid, url, &p, &m);
963     if(!pu || !p || !m) return NULL;
964
965     // make sure the machine GUID is valid
966     if(registry_regenerate_guid(request_machine, mbuf) == -1) {
967         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);
968         return NULL;
969     }
970     request_machine = mbuf;
971
972     // make sure the machine exists
973     m = registry_machine_find(request_machine);
974     if(!m) {
975         info("Registry Machine URLs request: machine not found, person: '%s', machine '%s', url '%s', request machine '%s'", p->guid, machine_guid, pu->url->url, request_machine);
976         return NULL;
977     }
978
979     // Verify the user has in the past accessed this machine
980     // We will walk through the PERSON_URLs to find the machine
981     // linking to our machine
982
983     // a structure to pass to the dictionary_get_all() callback handler
984     struct machine_request_callback_data rdata = { m, NULL };
985
986     // request a walk through on the dictionary
987     // no need for locking here, the underlying dictionary has its own
988     dictionary_get_all(p->urls, machine_request_callback, &rdata);
989
990     if(rdata.result)
991         return m;
992
993     return NULL;
994 }
995
996
997 // ----------------------------------------------------------------------------
998 // REGISTRY JSON generation
999
1000 #define REGISTRY_STATUS_OK "ok"
1001 #define REGISTRY_STATUS_FAILED "failed"
1002 #define REGISTRY_STATUS_DISABLED "disabled"
1003
1004 int registry_verify_cookies_redirects(void) {
1005     return registry.verify_cookies_redirects;
1006 }
1007
1008 const char *registry_to_announce(void) {
1009     return registry.registry_to_announce;
1010 }
1011
1012 void registry_set_cookie(struct web_client *w, const char *guid) {
1013     char edate[100];
1014     time_t et = now_realtime_sec() + registry.persons_expiration;
1015     struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
1016     strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);
1017
1018     snprintfz(w->cookie1, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", guid, edate);
1019
1020     if(registry.registry_domain && registry.registry_domain[0])
1021         snprintfz(w->cookie2, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", guid, registry.registry_domain, edate);
1022 }
1023
1024 static inline void registry_set_person_cookie(struct web_client *w, REGISTRY_PERSON *p) {
1025     registry_set_cookie(w, p->guid);
1026 }
1027
1028 static inline void registry_json_header(struct web_client *w, const char *action, const char *status) {
1029     buffer_flush(w->response.data);
1030     w->response.data->contenttype = CT_APPLICATION_JSON;
1031     buffer_sprintf(w->response.data, "{\n\t\"action\": \"%s\",\n\t\"status\": \"%s\",\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
1032                    action, status, registry.hostname, registry.machine_guid);
1033 }
1034
1035 static inline void registry_json_footer(struct web_client *w) {
1036     buffer_strcat(w->response.data, "\n}\n");
1037 }
1038
1039 int registry_request_hello_json(struct web_client *w) {
1040     registry_json_header(w, "hello", REGISTRY_STATUS_OK);
1041
1042     buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1043                    registry.registry_to_announce);
1044
1045     registry_json_footer(w);
1046     return 200;
1047 }
1048
1049 static inline int registry_json_disabled(struct web_client *w, const char *action) {
1050     registry_json_header(w, action, REGISTRY_STATUS_DISABLED);
1051
1052     buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
1053                    registry.registry_to_announce);
1054
1055     registry_json_footer(w);
1056     return 200;
1057 }
1058
1059 // structure used be the callbacks below
1060 struct registry_json_walk_person_urls_callback {
1061     REGISTRY_PERSON *p;
1062     REGISTRY_MACHINE *m;
1063     struct web_client *w;
1064     int count;
1065 };
1066
1067 // callback for rendering PERSON_URLs
1068 static inline int registry_json_person_url_callback(void *entry, void *data) {
1069     REGISTRY_PERSON_URL *pu = (REGISTRY_PERSON_URL *)entry;
1070     struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1071     struct web_client *w = c->w;
1072
1073     if(unlikely(c->count++))
1074         buffer_strcat(w->response.data, ",");
1075
1076     buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u, \"%s\" ]",
1077                    pu->machine->guid, pu->url->url, pu->last_t, pu->usages, pu->name);
1078
1079     return 1;
1080 }
1081
1082 // callback for rendering MACHINE_URLs
1083 static inline int registry_json_machine_url_callback(void *entry, void *data) {
1084     REGISTRY_MACHINE_URL *mu = (REGISTRY_MACHINE_URL *)entry;
1085     struct registry_json_walk_person_urls_callback *c = (struct registry_json_walk_person_urls_callback *)data;
1086     struct web_client *w = c->w;
1087     REGISTRY_MACHINE *m = c->m;
1088
1089     if(unlikely(c->count++))
1090         buffer_strcat(w->response.data, ",");
1091
1092     buffer_sprintf(w->response.data, "\n\t\t[ \"%s\", \"%s\", %u000, %u ]",
1093                    m->guid, mu->url->url, mu->last_t, mu->usages);
1094
1095     return 1;
1096 }
1097
1098
1099 // the main method for registering an access
1100 int registry_request_access_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when) {
1101     if(!registry.enabled)
1102         return registry_json_disabled(w, "access");
1103
1104     REGISTRY_PERSON *p = registry_request_access(person_guid, machine_guid, url, name, when);
1105     if(!p) {
1106         registry_json_header(w, "access", REGISTRY_STATUS_FAILED);
1107         registry_json_footer(w);
1108         return 412;
1109     }
1110
1111     // set the cookie
1112     registry_set_person_cookie(w, p);
1113
1114     // generate the response
1115     registry_json_header(w, "access", REGISTRY_STATUS_OK);
1116
1117     buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\",\n\t\"urls\": [", p->guid);
1118     struct registry_json_walk_person_urls_callback c = { p, NULL, w, 0 };
1119     dictionary_get_all(p->urls, registry_json_person_url_callback, &c);
1120     buffer_strcat(w->response.data, "\n\t]\n");
1121
1122     registry_json_footer(w);
1123     return 200;
1124 }
1125
1126 // the main method for deleting a URL from a person
1127 int registry_request_delete_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when) {
1128     if(!registry.enabled)
1129         return registry_json_disabled(w, "delete");
1130
1131     REGISTRY_PERSON *p = registry_request_delete(person_guid, machine_guid, url, delete_url, when);
1132     if(!p) {
1133         registry_json_header(w, "delete", REGISTRY_STATUS_FAILED);
1134         registry_json_footer(w);
1135         return 412;
1136     }
1137
1138     // generate the response
1139     registry_json_header(w, "delete", REGISTRY_STATUS_OK);
1140     registry_json_footer(w);
1141     return 200;
1142 }
1143
1144 // the main method for searching the URLs of a netdata
1145 int registry_request_search_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when) {
1146     if(!registry.enabled)
1147         return registry_json_disabled(w, "search");
1148
1149     REGISTRY_MACHINE *m = registry_request_machine(person_guid, machine_guid, url, request_machine, when);
1150     if(!m) {
1151         registry_json_header(w, "search", REGISTRY_STATUS_FAILED);
1152         registry_json_footer(w);
1153         return 404;
1154     }
1155
1156     registry_json_header(w, "search", REGISTRY_STATUS_OK);
1157
1158     buffer_strcat(w->response.data, ",\n\t\"urls\": [");
1159     struct registry_json_walk_person_urls_callback c = { NULL, m, w, 0 };
1160     dictionary_get_all(m->urls, registry_json_machine_url_callback, &c);
1161     buffer_strcat(w->response.data, "\n\t]\n");
1162
1163     registry_json_footer(w);
1164     return 200;
1165 }
1166
1167 // structure used be the callbacks below
1168 struct registry_person_url_callback_verify_machine_exists_data {
1169     REGISTRY_MACHINE *m;
1170     int count;
1171 };
1172
1173 int registry_person_url_callback_verify_machine_exists(void *entry, void *data) {
1174     struct registry_person_url_callback_verify_machine_exists_data *d = (struct registry_person_url_callback_verify_machine_exists_data *)data;
1175     REGISTRY_PERSON_URL *pu = (REGISTRY_PERSON_URL *)entry;
1176     REGISTRY_MACHINE *m = d->m;
1177
1178     if(pu->machine == m)
1179         d->count++;
1180
1181     return 0;
1182 }
1183
1184 // the main method for switching user identity
1185 int registry_request_switch_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *new_person_guid, time_t when) {
1186     (void)url;
1187     (void)when;
1188
1189     if(!registry.enabled)
1190         return registry_json_disabled(w, "switch");
1191
1192     REGISTRY_PERSON *op = registry_person_find(person_guid);
1193     if(!op) {
1194         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1195         registry_json_footer(w);
1196         return 430;
1197     }
1198
1199     REGISTRY_PERSON *np = registry_person_find(new_person_guid);
1200     if(!np) {
1201         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1202         registry_json_footer(w);
1203         return 431;
1204     }
1205
1206     REGISTRY_MACHINE *m = registry_machine_find(machine_guid);
1207     if(!m) {
1208         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1209         registry_json_footer(w);
1210         return 432;
1211     }
1212
1213     struct registry_person_url_callback_verify_machine_exists_data data = { m, 0 };
1214
1215     // verify the old person has access to this machine
1216     dictionary_get_all(op->urls, registry_person_url_callback_verify_machine_exists, &data);
1217     if(!data.count) {
1218         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1219         registry_json_footer(w);
1220         return 433;
1221     }
1222
1223     // verify the new person has access to this machine
1224     data.count = 0;
1225     dictionary_get_all(np->urls, registry_person_url_callback_verify_machine_exists, &data);
1226     if(!data.count) {
1227         registry_json_header(w, "switch", REGISTRY_STATUS_FAILED);
1228         registry_json_footer(w);
1229         return 434;
1230     }
1231
1232     // set the cookie of the new person
1233     // the user just switched identity
1234     registry_set_person_cookie(w, np);
1235
1236     // generate the response
1237     registry_json_header(w, "switch", REGISTRY_STATUS_OK);
1238     buffer_sprintf(w->response.data, ",\n\t\"person_guid\": \"%s\"", np->guid);
1239     registry_json_footer(w);
1240     return 200;
1241 }
1242
1243
1244 // ----------------------------------------------------------------------------
1245 // REGISTRY THIS MACHINE UNIQUE ID
1246
1247 static inline int is_machine_guid_blacklisted(const char *guid) {
1248     // these are machine GUIDs that have been included in distribution packages.
1249     // we blacklist them here, so that the next version of netdata will generate
1250     // new ones.
1251
1252     if(!strcmp(guid, "8a795b0c-2311-11e6-8563-000c295076a6")
1253     || !strcmp(guid, "4aed1458-1c3e-11e6-a53f-000c290fc8f5")
1254     ) {
1255         error("Blacklisted machine GUID '%s' found.", guid);
1256         return 1;
1257     }
1258
1259     return 0;
1260 }
1261
1262 char *registry_get_this_machine_guid(void) {
1263     if(likely(registry.machine_guid[0]))
1264         return registry.machine_guid;
1265
1266     // read it from disk
1267     int fd = open(registry.machine_guid_filename, O_RDONLY);
1268     if(fd != -1) {
1269         char buf[GUID_LEN + 1];
1270         if(read(fd, buf, GUID_LEN) != GUID_LEN)
1271             error("Failed to read machine GUID from '%s'", registry.machine_guid_filename);
1272         else {
1273             buf[GUID_LEN] = '\0';
1274             if(registry_regenerate_guid(buf, registry.machine_guid) == -1) {
1275                 error("Failed to validate machine GUID '%s' from '%s'. Ignoring it - this might mean this netdata will appear as duplicate in the registry.",
1276                       buf, registry.machine_guid_filename);
1277
1278                 registry.machine_guid[0] = '\0';
1279             }
1280             else if(is_machine_guid_blacklisted(registry.machine_guid))
1281                 registry.machine_guid[0] = '\0';
1282         }
1283         close(fd);
1284     }
1285
1286     // generate a new one?
1287     if(!registry.machine_guid[0]) {
1288         uuid_t uuid;
1289
1290         uuid_generate_time(uuid);
1291         uuid_unparse_lower(uuid, registry.machine_guid);
1292         registry.machine_guid[GUID_LEN] = '\0';
1293
1294         // save it
1295         fd = open(registry.machine_guid_filename, O_WRONLY|O_CREAT|O_TRUNC, 444);
1296         if(fd == -1)
1297             fatal("Cannot create unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1298
1299         if(write(fd, registry.machine_guid, GUID_LEN) != GUID_LEN)
1300             fatal("Cannot write the unique machine id file '%s'. Please fix this.", registry.machine_guid_filename);
1301
1302         close(fd);
1303     }
1304
1305     setenv("NETDATA_REGISTRY_UNIQUE_ID", registry.machine_guid, 1);
1306
1307     return registry.machine_guid;
1308 }
1309
1310
1311 // ----------------------------------------------------------------------------
1312 // REGISTRY LOAD/SAVE
1313
1314 int registry_machine_save_url(void *entry, void *file) {
1315     REGISTRY_MACHINE_URL *mu = entry;
1316     FILE *fp = file;
1317
1318     debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
1319
1320     int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
1321             mu->first_t,
1322             mu->last_t,
1323             mu->usages,
1324             mu->flags,
1325             mu->url->url
1326     );
1327
1328     // error handling is done at registry_save()
1329
1330     return ret;
1331 }
1332
1333 int registry_machine_save(void *entry, void *file) {
1334     REGISTRY_MACHINE *m = entry;
1335     FILE *fp = file;
1336
1337     debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
1338
1339     int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
1340             m->first_t,
1341             m->last_t,
1342             m->usages,
1343             m->guid
1344     );
1345
1346     if(ret >= 0) {
1347         int ret2 = dictionary_get_all(m->urls, registry_machine_save_url, fp);
1348         if(ret2 < 0) return ret2;
1349         ret += ret2;
1350     }
1351
1352     // error handling is done at registry_save()
1353
1354     return ret;
1355 }
1356
1357 static inline int registry_person_save_url(void *entry, void *file) {
1358     REGISTRY_PERSON_URL *pu = entry;
1359     FILE *fp = file;
1360
1361     debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
1362
1363     int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
1364             pu->first_t,
1365             pu->last_t,
1366             pu->usages,
1367             pu->flags,
1368             pu->machine->guid,
1369             pu->name,
1370             pu->url->url
1371     );
1372
1373     // error handling is done at registry_save()
1374
1375     return ret;
1376 }
1377
1378 static inline int registry_person_save(void *entry, void *file) {
1379     REGISTRY_PERSON *p = entry;
1380     FILE *fp = file;
1381
1382     debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
1383
1384     int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
1385             p->first_t,
1386             p->last_t,
1387             p->usages,
1388             p->guid
1389     );
1390
1391     if(ret >= 0) {
1392         int ret2 = dictionary_get_all(p->urls, registry_person_save_url, fp);
1393         if (ret2 < 0) return ret2;
1394         ret += ret2;
1395     }
1396
1397     // error handling is done at registry_save()
1398
1399     return ret;
1400 }
1401
1402 int registry_save(void) {
1403     if(!registry.enabled) return -1;
1404
1405     // make sure the log is not updated
1406     registry_log_lock();
1407
1408     if(unlikely(!registry_should_save_db())) {
1409         registry_log_unlock();
1410         return -2;
1411     }
1412
1413     error_log_limit_unlimited();
1414
1415     char tmp_filename[FILENAME_MAX + 1];
1416     char old_filename[FILENAME_MAX + 1];
1417
1418     snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
1419     snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
1420
1421     debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
1422     FILE *fp = fopen(tmp_filename, "w");
1423     if(!fp) {
1424         error("Registry: Cannot create file: %s", tmp_filename);
1425         registry_log_unlock();
1426         error_log_limit_reset();
1427         return -1;
1428     }
1429
1430     // dictionary_get_all() has its own locking, so this is safe to do
1431
1432     debug(D_REGISTRY, "Saving all machines");
1433     int bytes1 = dictionary_get_all(registry.machines, registry_machine_save, fp);
1434     if(bytes1 < 0) {
1435         error("Registry: Cannot save registry machines - return value %d", bytes1);
1436         fclose(fp);
1437         registry_log_unlock();
1438         error_log_limit_reset();
1439         return bytes1;
1440     }
1441     debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
1442
1443     debug(D_REGISTRY, "Saving all persons");
1444     int bytes2 = dictionary_get_all(registry.persons, registry_person_save, fp);
1445     if(bytes2 < 0) {
1446         error("Registry: Cannot save registry persons - return value %d", bytes2);
1447         fclose(fp);
1448         registry_log_unlock();
1449         error_log_limit_reset();
1450         return bytes2;
1451     }
1452     debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
1453
1454     // save the totals
1455     fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
1456             registry.persons_count,
1457             registry.machines_count,
1458             registry.usages_count + 1, // this is required - it is lost on db rotation
1459             registry.urls_count,
1460             registry.persons_urls_count,
1461             registry.machines_urls_count
1462     );
1463
1464     fclose(fp);
1465
1466     errno = 0;
1467
1468     // remove the .old db
1469     debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
1470     if(unlink(old_filename) == -1 && errno != ENOENT)
1471         error("Registry: cannot remove old registry file '%s'", old_filename);
1472
1473     // rename the db to .old
1474     debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
1475     if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
1476         error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", registry.db_filename, old_filename);
1477
1478     else {
1479         // remove the database (it is saved in .old)
1480         debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
1481         if (unlink(registry.db_filename) == -1 && errno != ENOENT)
1482             error("Registry: cannot remove old registry file '%s'", registry.db_filename);
1483
1484         // move the .tmp to make it active
1485         debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
1486         if (link(tmp_filename, registry.db_filename) == -1) {
1487             error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
1488                   registry.db_filename);
1489
1490             // move the .old back
1491             debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
1492             if(link(old_filename, registry.db_filename) == -1)
1493                 error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
1494         }
1495         else {
1496             debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
1497             if(unlink(tmp_filename) == -1)
1498                 error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
1499
1500             // it has been moved successfully
1501             // discard the current registry log
1502             registry_log_recreate_nolock();
1503             registry.log_count = 0;
1504         }
1505     }
1506
1507     // continue operations
1508     registry_log_unlock();
1509     error_log_limit_reset();
1510
1511     return -1;
1512 }
1513
1514 static inline size_t registry_load(void) {
1515     char *s, buf[4096 + 1];
1516     REGISTRY_PERSON *p = NULL;
1517     REGISTRY_MACHINE *m = NULL;
1518     REGISTRY_URL *u = NULL;
1519     size_t line = 0;
1520
1521     debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
1522     FILE *fp = fopen(registry.db_filename, "r");
1523     if(!fp) {
1524         error("Registry: cannot open registry file: '%s'", registry.db_filename);
1525         return 0;
1526     }
1527
1528     size_t len = 0;
1529     buf[4096] = '\0';
1530     while((s = fgets_trim_len(buf, 4096, fp, &len))) {
1531         line++;
1532
1533         debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
1534         switch(*s) {
1535             case 'T': // totals
1536                 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')) {
1537                     error("Registry totals line %zu is wrong (len = %zu).", line, len);
1538                     continue;
1539                 }
1540                 registry.persons_count = strtoull(&s[2], NULL, 16);
1541                 registry.machines_count = strtoull(&s[19], NULL, 16);
1542                 registry.usages_count = strtoull(&s[36], NULL, 16);
1543                 registry.urls_count = strtoull(&s[53], NULL, 16);
1544                 registry.persons_urls_count = strtoull(&s[70], NULL, 16);
1545                 registry.machines_urls_count = strtoull(&s[87], NULL, 16);
1546                 break;
1547
1548             case 'P': // person
1549                 m = NULL;
1550                 // verify it is valid
1551                 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1552                     error("Registry person line %zu is wrong (len = %zu).", line, len);
1553                     continue;
1554                 }
1555
1556                 s[1] = s[10] = s[19] = s[28] = '\0';
1557                 p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
1558                 p->last_t = strtoul(&s[11], NULL, 16);
1559                 p->usages = strtoul(&s[20], NULL, 16);
1560                 debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
1561                 break;
1562
1563             case 'M': // machine
1564                 p = NULL;
1565                 // verify it is valid
1566                 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
1567                     error("Registry person line %zu is wrong (len = %zu).", line, len);
1568                     continue;
1569                 }
1570
1571                 s[1] = s[10] = s[19] = s[28] = '\0';
1572                 m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
1573                 m->last_t = strtoul(&s[11], NULL, 16);
1574                 m->usages = strtoul(&s[20], NULL, 16);
1575                 debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
1576                 break;
1577
1578             case 'U': // person URL
1579                 if(unlikely(!p)) {
1580                     error("Registry: ignoring line %zu, no person loaded: %s", line, s);
1581                     continue;
1582                 }
1583
1584                 // verify it is valid
1585                 if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
1586                     error("Registry person URL line %zu is wrong (len = %zu).", line, len);
1587                     continue;
1588                 }
1589
1590                 s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
1591
1592                 // skip the name to find the url
1593                 char *url = &s[69];
1594                 while(*url && *url != '\t') url++;
1595                 if(!*url) {
1596                     error("Registry person URL line %zu does not have a url.", line);
1597                     continue;
1598                 }
1599                 *url++ = '\0';
1600
1601                 // u = registry_url_allocate_nolock(url, strlen(url));
1602                 u = registry_url_get_nolock(url, strlen(url));
1603
1604                 time_t first_t = strtoul(&s[2], NULL, 16);
1605
1606                 m = registry_machine_find(&s[32]);
1607                 if(!m) m = registry_machine_allocate(&s[32], first_t);
1608
1609                 REGISTRY_PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
1610                 pu->last_t = strtoul(&s[11], NULL, 16);
1611                 pu->usages = strtoul(&s[20], NULL, 16);
1612                 pu->flags = strtoul(&s[29], NULL, 16);
1613                 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);
1614                 break;
1615
1616             case 'V': // machine URL
1617                 if(unlikely(!m)) {
1618                     error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
1619                     continue;
1620                 }
1621
1622                 // verify it is valid
1623                 if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
1624                     error("Registry person URL line %zu is wrong (len = %zu).", line, len);
1625                     continue;
1626                 }
1627
1628                 s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
1629                 // u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
1630                 u = registry_url_get_nolock(&s[32], strlen(&s[32]));
1631
1632                 REGISTRY_MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
1633                 mu->last_t = strtoul(&s[11], NULL, 16);
1634                 mu->usages = strtoul(&s[20], NULL, 16);
1635                 mu->flags = strtoul(&s[29], NULL, 16);
1636                 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);
1637                 break;
1638
1639             default:
1640                 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
1641                 break;
1642         }
1643     }
1644     fclose(fp);
1645
1646     return line;
1647 }
1648
1649 // ----------------------------------------------------------------------------
1650 // REGISTRY
1651
1652 int registry_init(void) {
1653     char filename[FILENAME_MAX + 1];
1654
1655     // registry enabled?
1656     registry.enabled = config_get_boolean("registry", "enabled", 0);
1657
1658     // pathnames
1659     registry.pathname = config_get("registry", "registry db directory", VARLIB_DIR "/registry");
1660     if(mkdir(registry.pathname, 0770) == -1 && errno != EEXIST)
1661         fatal("Cannot create directory '%s'.", registry.pathname);
1662
1663     // filenames
1664     snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
1665     registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
1666     registry_get_this_machine_guid();
1667
1668     snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
1669     registry.db_filename = config_get("registry", "registry db file", filename);
1670
1671     snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
1672     registry.log_filename = config_get("registry", "registry log file", filename);
1673
1674     // configuration options
1675     registry.save_registry_every_entries = config_get_number("registry", "registry save db every new entries", 1000000);
1676     registry.persons_expiration = config_get_number("registry", "registry expire idle persons days", 365) * 86400;
1677     registry.registry_domain = config_get("registry", "registry domain", "");
1678     registry.registry_to_announce = config_get("registry", "registry to announce", "https://registry.my-netdata.io");
1679     registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", localhost.hostname));
1680     registry.verify_cookies_redirects = config_get_boolean("registry", "verify browser cookies support", 1);
1681
1682     setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
1683     setenv("NETDATA_REGISTRY_URL", registry.registry_to_announce, 1);
1684
1685     registry.max_url_length = config_get_number("registry", "max URL length", 1024);
1686     if(registry.max_url_length < 10) {
1687         registry.max_url_length = 10;
1688         config_set_number("registry", "max URL length", registry.max_url_length);
1689     }
1690
1691     registry.max_name_length = config_get_number("registry", "max URL name length", 50);
1692     if(registry.max_name_length < 10) {
1693         registry.max_name_length = 10;
1694         config_set_number("registry", "max URL name length", registry.max_name_length);
1695     }
1696
1697     // initialize entries counters
1698     registry.persons_count = 0;
1699     registry.machines_count = 0;
1700     registry.usages_count = 0;
1701     registry.urls_count = 0;
1702     registry.persons_urls_count = 0;
1703     registry.machines_urls_count = 0;
1704
1705     // initialize memory counters
1706     registry.persons_memory = 0;
1707     registry.machines_memory = 0;
1708     registry.urls_memory = 0;
1709     registry.persons_urls_memory = 0;
1710     registry.machines_urls_memory = 0;
1711
1712     // initialize locks
1713     pthread_mutex_init(&registry.persons_lock, NULL);
1714     pthread_mutex_init(&registry.machines_lock, NULL);
1715     pthread_mutex_init(&registry.urls_lock, NULL);
1716     pthread_mutex_init(&registry.person_urls_lock, NULL);
1717     pthread_mutex_init(&registry.machine_urls_lock, NULL);
1718
1719     // create dictionaries
1720     registry.persons = dictionary_create(DICTIONARY_FLAGS);
1721     registry.machines = dictionary_create(DICTIONARY_FLAGS);
1722     registry.urls = dictionary_create(DICTIONARY_FLAGS);
1723
1724     // load the registry database
1725     if(registry.enabled) {
1726         registry_log_open_nolock();
1727         registry_load();
1728         registry_log_load();
1729
1730         if(unlikely(registry_should_save_db()))
1731             registry_save();
1732     }
1733
1734     return 0;
1735 }
1736
1737 void registry_free(void) {
1738     if(!registry.enabled) return;
1739
1740     // we need to destroy the dictionaries ourselves
1741     // since the dictionaries use memory we allocated
1742
1743     while(registry.persons->values_index.root) {
1744         REGISTRY_PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
1745
1746         // fprintf(stderr, "\nPERSON: '%s', first: %u, last: %u, usages: %u\n", p->guid, p->first_t, p->last_t, p->usages);
1747
1748         while(p->urls->values_index.root) {
1749             REGISTRY_PERSON_URL *pu = ((NAME_VALUE *)p->urls->values_index.root)->value;
1750
1751             // 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);
1752
1753             debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", pu->url->url, p->guid);
1754             dictionary_del(p->urls, pu->url->url);
1755
1756             debug(D_REGISTRY, "Registry: unlinking url '%s' from person", pu->url->url);
1757             registry_url_unlink_nolock(pu->url);
1758
1759             debug(D_REGISTRY, "Registry: freeing person url");
1760             freez(pu);
1761         }
1762
1763         debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
1764         dictionary_del(registry.persons, p->guid);
1765
1766         debug(D_REGISTRY, "Registry: destroying URL dictionary of person '%s'", p->guid);
1767         dictionary_destroy(p->urls);
1768
1769         debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
1770         freez(p);
1771     }
1772
1773     while(registry.machines->values_index.root) {
1774         REGISTRY_MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
1775
1776         // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
1777
1778         while(m->urls->values_index.root) {
1779             REGISTRY_MACHINE_URL *mu = ((NAME_VALUE *)m->urls->values_index.root)->value;
1780
1781             // 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);
1782
1783             //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
1784             //dictionary_destroy(mu->persons);
1785
1786             debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
1787             dictionary_del(m->urls, mu->url->url);
1788
1789             debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
1790             registry_url_unlink_nolock(mu->url);
1791
1792             debug(D_REGISTRY, "Registry: freeing machine url");
1793             freez(mu);
1794         }
1795
1796         debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
1797         dictionary_del(registry.machines, m->guid);
1798
1799         debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
1800         dictionary_destroy(m->urls);
1801
1802         debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
1803         freez(m);
1804     }
1805
1806     // and free the memory of remaining dictionary structures
1807
1808     debug(D_REGISTRY, "Registry: destroying persons dictionary");
1809     dictionary_destroy(registry.persons);
1810
1811     debug(D_REGISTRY, "Registry: destroying machines dictionary");
1812     dictionary_destroy(registry.machines);
1813
1814     debug(D_REGISTRY, "Registry: destroying urls dictionary");
1815     dictionary_destroy(registry.urls);
1816 }
1817
1818 // ----------------------------------------------------------------------------
1819 // STATISTICS
1820
1821 void registry_statistics(void) {
1822     if(!registry.enabled) return;
1823
1824     static RRDSET *sts = NULL, *stc = NULL, *stm = NULL;
1825
1826     if(!sts) sts = rrdset_find("netdata.registry_sessions");
1827     if(!sts) {
1828         sts = rrdset_create("netdata", "registry_sessions", NULL, "registry", NULL, "NetData Registry Sessions", "session", 131000, rrd_update_every, RRDSET_TYPE_LINE);
1829
1830         rrddim_add(sts, "sessions",  NULL,  1, 1, RRDDIM_ABSOLUTE);
1831     }
1832     else rrdset_next(sts);
1833
1834     rrddim_set(sts, "sessions", registry.usages_count);
1835     rrdset_done(sts);
1836
1837     // ------------------------------------------------------------------------
1838
1839     if(!stc) stc = rrdset_find("netdata.registry_entries");
1840     if(!stc) {
1841         stc = rrdset_create("netdata", "registry_entries", NULL, "registry", NULL, "NetData Registry Entries", "entries", 131100, rrd_update_every, RRDSET_TYPE_LINE);
1842
1843         rrddim_add(stc, "persons",        NULL,  1, 1, RRDDIM_ABSOLUTE);
1844         rrddim_add(stc, "machines",       NULL,  1, 1, RRDDIM_ABSOLUTE);
1845         rrddim_add(stc, "urls",           NULL,  1, 1, RRDDIM_ABSOLUTE);
1846         rrddim_add(stc, "persons_urls",   NULL,  1, 1, RRDDIM_ABSOLUTE);
1847         rrddim_add(stc, "machines_urls",  NULL,  1, 1, RRDDIM_ABSOLUTE);
1848     }
1849     else rrdset_next(stc);
1850
1851     rrddim_set(stc, "persons",       registry.persons_count);
1852     rrddim_set(stc, "machines",      registry.machines_count);
1853     rrddim_set(stc, "urls",          registry.urls_count);
1854     rrddim_set(stc, "persons_urls",  registry.persons_urls_count);
1855     rrddim_set(stc, "machines_urls", registry.machines_urls_count);
1856     rrdset_done(stc);
1857
1858     // ------------------------------------------------------------------------
1859
1860     if(!stm) stm = rrdset_find("netdata.registry_mem");
1861     if(!stm) {
1862         stm = rrdset_create("netdata", "registry_mem", NULL, "registry", NULL, "NetData Registry Memory", "KB", 131300, rrd_update_every, RRDSET_TYPE_STACKED);
1863
1864         rrddim_add(stm, "persons",        NULL,  1, 1024, RRDDIM_ABSOLUTE);
1865         rrddim_add(stm, "machines",       NULL,  1, 1024, RRDDIM_ABSOLUTE);
1866         rrddim_add(stm, "urls",           NULL,  1, 1024, RRDDIM_ABSOLUTE);
1867         rrddim_add(stm, "persons_urls",   NULL,  1, 1024, RRDDIM_ABSOLUTE);
1868         rrddim_add(stm, "machines_urls",  NULL,  1, 1024, RRDDIM_ABSOLUTE);
1869     }
1870     else rrdset_next(stm);
1871
1872     rrddim_set(stm, "persons",       registry.persons_memory + registry.persons_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1873     rrddim_set(stm, "machines",      registry.machines_memory + registry.machines_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1874     rrddim_set(stm, "urls",          registry.urls_memory + registry.urls_count * sizeof(NAME_VALUE) + sizeof(DICTIONARY));
1875     rrddim_set(stm, "persons_urls",  registry.persons_urls_memory + registry.persons_count * sizeof(DICTIONARY) + registry.persons_urls_count * sizeof(NAME_VALUE));
1876     rrddim_set(stm, "machines_urls", registry.machines_urls_memory + registry.machines_count * sizeof(DICTIONARY) + registry.machines_urls_count * sizeof(NAME_VALUE));
1877     rrdset_done(stm);
1878 }