11
// ----------------------------------------------------------------------------
12
// INTERNAL FUNCTIONS FOR SAVING REGISTRY OBJECTS
13
14
-static int registry_machine_save_url(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *file) {
15
- REGISTRY_MACHINE_URL *mu = entry;
16
- FILE *fp = file;
17
-
18
- debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
14
+static int registry_machine_save_url(REGISTRY_MACHINE_URL *mu, FILE *fp) {
15
+ debug(D_REGISTRY, "REGISTRY: registry_machine_save_url('%s')", string2str(mu->url));
16
17
int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
18
mu->first_t,
19
mu->last_t,
20
mu->usages,
21
mu->flags,
25
- mu->url->url
22
+ string2str(mu->url)
23
);
24
25
// error handling is done at registry_db_save()
32
REGISTRY_MACHINE *m = entry;
33
FILE *fp = file;
34
38
- debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
35
+ debug(D_REGISTRY, "REGISTRY: registry_machine_save('%s')", m->guid);
36
37
int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
38
m->first_t,
42
);
43
44
if(ret >= 0) {
48
- int ret2 = dictionary_walkthrough_read(m->machine_urls, registry_machine_save_url, fp);
49
- if(ret2 < 0) return ret2;
50
- ret += ret2;
45
+ for(REGISTRY_MACHINE_URL *mu = m->machine_urls; mu ; mu = mu->next) {
46
+ int rc = registry_machine_save_url(mu, fp);
47
+ if(rc < 0)
48
+ return rc;
49
+
50
+ ret += rc;
51
+ }
52
}
53
54
// error handling is done at registry_db_save()
56
return ret;
57
}
58
58
-static inline int registry_person_save_url(void *entry, void *file) {
59
- REGISTRY_PERSON_URL *pu = entry;
60
- FILE *fp = file;
61
-
62
- debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
59
+static inline int registry_person_save_url(REGISTRY_PERSON_URL *pu, FILE *fp) {
60
+ debug(D_REGISTRY, "REGISTRY: registry_person_save_url('%s')", string2str(pu->url));
61
62
int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
63
pu->first_t,
65
pu->usages,
66
pu->flags,
67
pu->machine->guid,
70
- pu->machine_name,
71
- pu->url->url
68
+ string2str(pu->machine_name),
69
+ string2str(pu->url)
70
);
71
72
// error handling is done at registry_db_save()
78
REGISTRY_PERSON *p = entry;
79
FILE *fp = file;
80
83
- debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
81
+ debug(D_REGISTRY, "REGISTRY: registry_person_save('%s')", p->guid);
82
83
int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
84
p->first_t,
88
);
89
90
if(ret >= 0) {
93
- //int ret2 = dictionary_walkthrough_read(p->person_urls, registry_person_save_url, fp);
94
- int ret2 = avl_traverse(&p->person_urls, registry_person_save_url, fp);
95
- if (ret2 < 0) return ret2;
96
- ret += ret2;
91
+ for(REGISTRY_PERSON_URL *pu = p->person_urls; pu ;pu = pu->next) {
92
+ int rc = registry_person_save_url(pu, fp);
93
+ if(rc < 0)
94
+ return rc;
95
+ else
96
+ ret += rc;
97
+ }
98
}
99
100
// error handling is done at registry_db_save()
120
snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
121
snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
122
122
- debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
123
+ debug(D_REGISTRY, "REGISTRY: Creating file '%s'", tmp_filename);
124
FILE *fp = fopen(tmp_filename, "w");
125
if(!fp) {
125
- netdata_log_error("Registry: Cannot create file: %s", tmp_filename);
126
+ netdata_log_error("REGISTRY: Cannot create file: %s", tmp_filename);
127
error_log_limit_reset();
128
return -1;
129
}
130
131
// dictionary_walkthrough_read() has its own locking, so this is safe to do
132
132
- debug(D_REGISTRY, "Saving all machines");
133
+ debug(D_REGISTRY, "REGISTRY: saving all machines");
134
int bytes1 = dictionary_walkthrough_read(registry.machines, registry_machine_save, fp);
135
if(bytes1 < 0) {
135
- netdata_log_error("Registry: Cannot save registry machines - return value %d", bytes1);
136
+ netdata_log_error("REGISTRY: Cannot save registry machines - return value %d", bytes1);
137
fclose(fp);
138
error_log_limit_reset();
139
return bytes1;
140
}
140
- debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
141
+ debug(D_REGISTRY, "REGISTRY: saving machines took %d bytes", bytes1);
142
143
debug(D_REGISTRY, "Saving all persons");
144
int bytes2 = dictionary_walkthrough_read(registry.persons, registry_person_save, fp);
145
if(bytes2 < 0) {
145
- netdata_log_error("Registry: Cannot save registry persons - return value %d", bytes2);
146
+ netdata_log_error("REGISTRY: Cannot save registry persons - return value %d", bytes2);
147
fclose(fp);
148
error_log_limit_reset();
149
return bytes2;
150
}
150
- debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
151
+ debug(D_REGISTRY, "REGISTRY: saving persons took %d bytes", bytes2);
152
153
// save the totals
154
fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
155
registry.persons_count,
156
registry.machines_count,
157
registry.usages_count + 1, // this is required - it is lost on db rotation
157
- registry.urls_count,
158
+ 0LLU, //registry.urls_count,
159
registry.persons_urls_count,
160
registry.machines_urls_count
161
);
165
errno = 0;
166
167
// remove the .old db
167
- debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
168
+ debug(D_REGISTRY, "REGISTRY: Removing old db '%s'", old_filename);
169
if(unlink(old_filename) == -1 && errno != ENOENT)
169
- netdata_log_error("Registry: cannot remove old registry file '%s'", old_filename);
170
+ netdata_log_error("REGISTRY: cannot remove old registry file '%s'", old_filename);
171
172
// rename the db to .old
172
- debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
173
+ debug(D_REGISTRY, "REGISTRY: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
174
if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
174
- netdata_log_error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", registry.db_filename, old_filename);
175
+ netdata_log_error("REGISTRY: cannot move file '%s' to '%s'. Saving registry DB failed!", registry.db_filename, old_filename);
176
177
else {
178
// remove the database (it is saved in .old)
178
- debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
179
+ debug(D_REGISTRY, "REGISTRY: removing db '%s'", registry.db_filename);
180
if (unlink(registry.db_filename) == -1 && errno != ENOENT)
180
- netdata_log_error("Registry: cannot remove old registry file '%s'", registry.db_filename);
181
+ netdata_log_error("REGISTRY: cannot remove old registry file '%s'", registry.db_filename);
182
183
// move the .tmp to make it active
183
- debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
184
+ debug(D_REGISTRY, "REGISTRY: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
185
if (link(tmp_filename, registry.db_filename) == -1) {
185
- netdata_log_error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
186
+ netdata_log_error("REGISTRY: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
187
registry.db_filename);
188
189
// move the .old back
189
- debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
190
+ debug(D_REGISTRY, "REGISTRY: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
191
if(link(old_filename, registry.db_filename) == -1)
191
- netdata_log_error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
192
+ netdata_log_error("REGISTRY: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
193
}
194
else {
194
- debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
195
+ debug(D_REGISTRY, "REGISTRY: removing tmp db '%s'", tmp_filename);
196
if(unlink(tmp_filename) == -1)
196
- netdata_log_error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
197
+ netdata_log_error("REGISTRY: cannot remove tmp registry file '%s'", tmp_filename);
198
199
// it has been moved successfully
200
// discard the current registry log
216
char *s, buf[4096 + 1];
217
REGISTRY_PERSON *p = NULL;
218
REGISTRY_MACHINE *m = NULL;
218
- REGISTRY_URL *u = NULL;
219
+ STRING *u = NULL;
220
size_t line = 0;
221
221
- debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
222
+ debug(D_REGISTRY, "REGISTRY: loading active db from: '%s'", registry.db_filename);
223
FILE *fp = fopen(registry.db_filename, "r");
224
if(!fp) {
224
- netdata_log_error("Registry: cannot open registry file: '%s'", registry.db_filename);
225
+ netdata_log_error("REGISTRY: cannot open registry file: '%s'", registry.db_filename);
226
return 0;
227
}
228
229
+ REGISTRY_MACHINE_URL *mu;
230
size_t len = 0;
231
buf[4096] = '\0';
232
while((s = fgets_trim_len(buf, 4096, fp, &len))) {
233
line++;
234
233
- debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
235
+ debug(D_REGISTRY, "REGISTRY: read line %zu to length %zu: %s", line, len, s);
236
switch(*s) {
235
- case 'T': // totals
236
- 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')) {
237
- netdata_log_error("Registry totals line %zu is wrong (len = %zu).", line, len);
238
- continue;
239
- }
240
- registry.persons_count = strtoull(&s[2], NULL, 16);
241
- registry.machines_count = strtoull(&s[19], NULL, 16);
242
- registry.usages_count = strtoull(&s[36], NULL, 16);
243
- registry.urls_count = strtoull(&s[53], NULL, 16);
244
- registry.persons_urls_count = strtoull(&s[70], NULL, 16);
245
- registry.machines_urls_count = strtoull(&s[87], NULL, 16);
246
- break;
247
-
248
- case 'P': // person
249
- m = NULL;
250
- // verify it is valid
251
- if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
252
- netdata_log_error("Registry person line %zu is wrong (len = %zu).", line, len);
253
- continue;
254
- }
255
-
256
- s[1] = s[10] = s[19] = s[28] = '\0';
257
- p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
258
- p->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
259
- p->usages = (uint32_t)strtoul(&s[20], NULL, 16);
260
- debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
261
- break;
262
-
263
- case 'M': // machine
264
- p = NULL;
265
- // verify it is valid
266
- if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
267
- netdata_log_error("Registry person line %zu is wrong (len = %zu).", line, len);
268
- continue;
269
- }
270
-
271
- s[1] = s[10] = s[19] = s[28] = '\0';
272
- m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
273
- m->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
274
- m->usages = (uint32_t)strtoul(&s[20], NULL, 16);
275
- debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
276
- break;
277
-
237
case 'U': // person URL
238
if(unlikely(!p)) {
280
- netdata_log_error("Registry: ignoring line %zu, no person loaded: %s", line, s);
239
+ netdata_log_error("REGISTRY: ignoring line %zu, no person loaded: %s", line, s);
240
continue;
241
}
242
243
// verify it is valid
244
if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
286
- netdata_log_error("Registry person URL line %zu is wrong (len = %zu).", line, len);
245
+ netdata_log_error("REGISTRY: person URL line %zu is wrong (len = %zu).", line, len);
246
continue;
247
}
248
252
char *url = &s[69];
253
while(*url && *url != '\t') url++;
254
if(!*url) {
296
- netdata_log_error("Registry person URL line %zu does not have a url.", line);
255
+ netdata_log_error("REGISTRY: person URL line %zu does not have a url.", line);
256
continue;
257
}
258
*url++ = '\0';
259
301
- // u = registry_url_allocate_nolock(url, strlen(url));
302
- u = registry_url_get(url, strlen(url));
260
+ if(*url != 'h' && *url != '*') {
261
+ netdata_log_error("REGISTRY: person URL line %zu does not have a valid url: %s", line, url);
262
+ continue;
263
+ }
264
+
265
+ u = string_strdupz(url);
266
304
- time_t first_t = strtoul(&s[2], NULL, 16);
267
+ time_t first_t = (time_t)strtoul(&s[2], NULL, 16);
268
269
m = registry_machine_find(&s[32]);
270
if(!m) m = registry_machine_allocate(&s[32], first_t);
271
309
- REGISTRY_PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
272
+ mu = registry_machine_url_find(m, u);
273
+ if(!mu) {
274
+ netdata_log_error("REGISTRY: person URL line %zu was not linked to the machine it refers to", line);
275
+ mu = registry_machine_url_allocate(m, u, first_t);
276
+ }
277
+
278
+ REGISTRY_PERSON_URL *pu = registry_person_url_index_find(p, u);
279
+ if(!pu)
280
+ pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
281
+ else
282
+ netdata_log_error("REGISTRY: person URL line %zu is duplicate, reusing the old one.", line);
283
+
284
pu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
285
pu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
286
pu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
313
- 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->machine_name, m->guid, pu->first_t, pu->last_t, pu->usages, pu->flags);
287
+ debug(D_REGISTRY, "REGISTRY: loaded person URL '%s' with name '%s' of machine '%s', first: %u, last: %u, usages: %u, flags: %02x",
288
+ string2str(u), string2str(pu->machine_name), m->guid, pu->first_t, pu->last_t, pu->usages, pu->flags);
289
+
290
+ string_freez(u);
291
+ break;
292
+
293
+ case 'P': // person
294
+ m = NULL;
295
+ // verify it is valid
296
+ if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
297
+ netdata_log_error("REGISTRY: person line %zu is wrong (len = %zu).", line, len);
298
+ continue;
299
+ }
300
+
301
+ s[1] = s[10] = s[19] = s[28] = '\0';
302
+ p = registry_person_allocate(&s[29], (time_t)strtoul(&s[2], NULL, 16));
303
+ p->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
304
+ p->usages = (uint32_t)strtoul(&s[20], NULL, 16);
305
+ debug(D_REGISTRY, "REGISTRY: loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
306
break;
307
308
case 'V': // machine URL
309
if(unlikely(!m)) {
318
- netdata_log_error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
310
+ netdata_log_error("REGISTRY: ignoring line %zu, no machine loaded: %s", line, s);
311
continue;
312
}
313
314
// verify it is valid
315
if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
324
- netdata_log_error("Registry person URL line %zu is wrong (len = %zu).", line, len);
316
+ netdata_log_error("REGISTRY: person URL line %zu is wrong (len = %zu).", line, len);
317
continue;
318
}
319
320
s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
329
- // u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
330
- u = registry_url_get(&s[32], strlen(&s[32]));
321
332
- REGISTRY_MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
322
+ url = &s[32];
323
+ if(*url != 'h' && *url != '*') {
324
+ netdata_log_error("REGISTRY: machine URL line %zu does not have a valid url: %s", line, url);
325
+ continue;
326
+ }
327
+
328
+ u = string_strdupz(url);
329
+
330
+ mu = registry_machine_url_find(m, u);
331
+ if(!mu)
332
+ mu = registry_machine_url_allocate(m, u, (time_t)strtoul(&s[2], NULL, 16));
333
+ else
334
+ netdata_log_error("REGISTRY: machine URL line %zu is duplicate, reusing the old one.", line);
335
+
336
mu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
337
mu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
338
mu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
336
- 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);
339
+ debug(D_REGISTRY, "Registry loaded machine URL '%s', machine '%s', first: %u, last: %u, usages: %u, flags: %02x",
340
+ string2str(u), m->guid, mu->first_t, mu->last_t, mu->usages, mu->flags);
341
+
342
+ string_freez(u);
343
+ break;
344
+
345
+ case 'M': // machine
346
+ p = NULL;
347
+ // verify it is valid
348
+ if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
349
+ netdata_log_error("REGISTRY: person line %zu is wrong (len = %zu).", line, len);
350
+ continue;
351
+ }
352
+
353
+ s[1] = s[10] = s[19] = s[28] = '\0';
354
+ m = registry_machine_allocate(&s[29], (time_t)strtoul(&s[2], NULL, 16));
355
+ m->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
356
+ m->usages = (uint32_t)strtoul(&s[20], NULL, 16);
357
+ debug(D_REGISTRY, "REGISTRY: loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
358
+ break;
359
+
360
+ case 'T': // totals
361
+ 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')) {
362
+ netdata_log_error("REGISTRY: totals line %zu is wrong (len = %zu).", line, len);
363
+ continue;
364
+ }
365
+ registry.persons_count = strtoull(&s[2], NULL, 16);
366
+ registry.machines_count = strtoull(&s[19], NULL, 16);
367
+ registry.usages_count = strtoull(&s[36], NULL, 16);
368
+ registry.persons_urls_count = strtoull(&s[70], NULL, 16);
369
+ registry.machines_urls_count = strtoull(&s[87], NULL, 16);
370
break;
371
372
default:
340
- netdata_log_error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
373
+ netdata_log_error("REGISTRY: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
374
break;
375
}
376
}