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