1 #include "registry_internals.h"
3 void registry_log(const char action, REGISTRY_PERSON *p, REGISTRY_MACHINE *m, REGISTRY_URL *u, char *name) {
4 if(likely(registry.log_fp)) {
5 if(unlikely(fprintf(registry.log_fp, "%c\t%08x\t%s\t%s\t%s\t%s\n",
12 error("Registry: failed to save log. Registry data may be lost in case of abnormal restart.");
14 // we increase the counter even on failures
15 // so that the registry will be saved periodically
18 // this must be outside the log_lock(), or a deadlock will happen.
19 // registry_db_save() checks the same inside the log_lock, so only
20 // one thread will save the db
21 if(unlikely(registry_db_should_be_saved()))
26 int registry_log_open(void) {
28 fclose(registry.log_fp);
30 registry.log_fp = fopen(registry.log_filename, "a");
32 if (setvbuf(registry.log_fp, NULL, _IOLBF, 0) != 0)
33 error("Cannot set line buffering on registry log file.");
37 error("Cannot open registry log file '%s'. Registry data will be lost in case of netdata or server crash.", registry.log_filename);
41 void registry_log_close(void) {
43 fclose(registry.log_fp);
44 registry.log_fp = NULL;
48 void registry_log_recreate(void) {
49 if(registry.log_fp != NULL) {
52 // open it with truncate
53 registry.log_fp = fopen(registry.log_filename, "w");
54 if(registry.log_fp) fclose(registry.log_fp);
55 else error("Cannot truncate registry log '%s'", registry.log_filename);
57 registry.log_fp = NULL;
62 ssize_t registry_log_load(void) {
65 // closing the log is required here
66 // otherwise we will append to it the values we read
69 debug(D_REGISTRY, "Registry: loading active db from: %s", registry.log_filename);
70 FILE *fp = fopen(registry.log_filename, "r");
72 error("Registry: cannot open registry file: %s", registry.log_filename);
74 char *s, buf[4096 + 1];
78 while ((s = fgets_trim_len(buf, 4096, fp, &len))) {
86 if (unlikely(len < 85 || s[1] != '\t' || s[10] != '\t' || s[47] != '\t' || s[84] != '\t')) {
87 error("Registry: log line %zd is wrong (len = %zu).", line, len);
90 s[1] = s[10] = s[47] = s[84] = '\0';
93 time_t when = strtoul(&s[2], NULL, 16);
94 char *person_guid = &s[11];
95 char *machine_guid = &s[48];
98 // skip the name to find the url
100 while(*url && *url != '\t') url++;
102 error("Registry: log line %zd does not have a url.", line);
107 // make sure the person exists
108 // without this, a new person guid will be created
109 REGISTRY_PERSON *p = registry_person_find(person_guid);
110 if(!p) p = registry_person_allocate(person_guid, when);
113 registry_request_access(p->guid, machine_guid, url, name, when);
115 registry_request_delete(p->guid, machine_guid, url, name, when);
117 registry.log_count++;
121 error("Registry: ignoring line %zd of filename '%s': %s.", line, registry.log_filename, s);
129 // open the log again