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