registry_db.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "daemon/common.h"
  3. #include "registry_internals.h"
  4. int registry_db_should_be_saved(void) {
  5. netdata_log_debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
  6. return registry.log_count > registry.save_registry_every_entries;
  7. }
  8. // ----------------------------------------------------------------------------
  9. // INTERNAL FUNCTIONS FOR SAVING REGISTRY OBJECTS
  10. static int registry_machine_save_url(REGISTRY_MACHINE_URL *mu, FILE *fp) {
  11. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_save_url('%s')", string2str(mu->url));
  12. int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
  13. mu->first_t,
  14. mu->last_t,
  15. mu->usages,
  16. mu->flags,
  17. string2str(mu->url)
  18. );
  19. // error handling is done at registry_db_save()
  20. return ret;
  21. }
  22. static int registry_machine_save(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *file) {
  23. REGISTRY_MACHINE *m = entry;
  24. FILE *fp = file;
  25. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_save('%s')", m->guid);
  26. int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
  27. m->first_t,
  28. m->last_t,
  29. m->usages,
  30. m->guid
  31. );
  32. if(ret >= 0) {
  33. for(REGISTRY_MACHINE_URL *mu = m->machine_urls; mu ; mu = mu->next) {
  34. int rc = registry_machine_save_url(mu, fp);
  35. if(rc < 0)
  36. return rc;
  37. ret += rc;
  38. }
  39. }
  40. // error handling is done at registry_db_save()
  41. return ret;
  42. }
  43. static inline int registry_person_save_url(REGISTRY_PERSON_URL *pu, FILE *fp) {
  44. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_person_save_url('%s')", string2str(pu->url));
  45. int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
  46. pu->first_t,
  47. pu->last_t,
  48. pu->usages,
  49. pu->flags,
  50. pu->machine->guid,
  51. string2str(pu->machine_name),
  52. string2str(pu->url)
  53. );
  54. // error handling is done at registry_db_save()
  55. return ret;
  56. }
  57. static inline int registry_person_save(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *file) {
  58. REGISTRY_PERSON *p = entry;
  59. FILE *fp = file;
  60. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_person_save('%s')", p->guid);
  61. int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
  62. p->first_t,
  63. p->last_t,
  64. p->usages,
  65. p->guid
  66. );
  67. if(ret >= 0) {
  68. for(REGISTRY_PERSON_URL *pu = p->person_urls; pu ;pu = pu->next) {
  69. int rc = registry_person_save_url(pu, fp);
  70. if(rc < 0)
  71. return rc;
  72. else
  73. ret += rc;
  74. }
  75. }
  76. // error handling is done at registry_db_save()
  77. return ret;
  78. }
  79. // ----------------------------------------------------------------------------
  80. // SAVE THE REGISTRY DATABASE
  81. int registry_db_save(void) {
  82. if(unlikely(!registry.enabled))
  83. return -1;
  84. if(unlikely(!registry_db_should_be_saved()))
  85. return -2;
  86. nd_log_limits_unlimited();
  87. char tmp_filename[FILENAME_MAX + 1];
  88. char old_filename[FILENAME_MAX + 1];
  89. snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
  90. snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
  91. netdata_log_debug(D_REGISTRY, "REGISTRY: Creating file '%s'", tmp_filename);
  92. FILE *fp = fopen(tmp_filename, "w");
  93. if(!fp) {
  94. netdata_log_error("REGISTRY: Cannot create file: %s", tmp_filename);
  95. nd_log_limits_reset();
  96. return -1;
  97. }
  98. // dictionary_walkthrough_read() has its own locking, so this is safe to do
  99. netdata_log_debug(D_REGISTRY, "REGISTRY: saving all machines");
  100. int bytes1 = dictionary_walkthrough_read(registry.machines, registry_machine_save, fp);
  101. if(bytes1 < 0) {
  102. netdata_log_error("REGISTRY: Cannot save registry machines - return value %d", bytes1);
  103. fclose(fp);
  104. nd_log_limits_reset();
  105. return bytes1;
  106. }
  107. netdata_log_debug(D_REGISTRY, "REGISTRY: saving machines took %d bytes", bytes1);
  108. netdata_log_debug(D_REGISTRY, "Saving all persons");
  109. int bytes2 = dictionary_walkthrough_read(registry.persons, registry_person_save, fp);
  110. if(bytes2 < 0) {
  111. netdata_log_error("REGISTRY: Cannot save registry persons - return value %d", bytes2);
  112. fclose(fp);
  113. nd_log_limits_reset();
  114. return bytes2;
  115. }
  116. netdata_log_debug(D_REGISTRY, "REGISTRY: saving persons took %d bytes", bytes2);
  117. // save the totals
  118. fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
  119. registry.persons_count,
  120. registry.machines_count,
  121. registry.usages_count + 1, // this is required - it is lost on db rotation
  122. 0LLU, //registry.urls_count,
  123. registry.persons_urls_count,
  124. registry.machines_urls_count
  125. );
  126. fclose(fp);
  127. errno = 0;
  128. // remove the .old db
  129. netdata_log_debug(D_REGISTRY, "REGISTRY: Removing old db '%s'", old_filename);
  130. if(unlink(old_filename) == -1 && errno != ENOENT)
  131. netdata_log_error("REGISTRY: cannot remove old registry file '%s'", old_filename);
  132. // rename the db to .old
  133. netdata_log_debug(D_REGISTRY, "REGISTRY: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
  134. if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
  135. netdata_log_error("REGISTRY: cannot move file '%s' to '%s'. Saving registry DB failed!", registry.db_filename, old_filename);
  136. else {
  137. // remove the database (it is saved in .old)
  138. netdata_log_debug(D_REGISTRY, "REGISTRY: removing db '%s'", registry.db_filename);
  139. if (unlink(registry.db_filename) == -1 && errno != ENOENT)
  140. netdata_log_error("REGISTRY: cannot remove old registry file '%s'", registry.db_filename);
  141. // move the .tmp to make it active
  142. netdata_log_debug(D_REGISTRY, "REGISTRY: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
  143. if (link(tmp_filename, registry.db_filename) == -1) {
  144. netdata_log_error("REGISTRY: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
  145. registry.db_filename);
  146. // move the .old back
  147. netdata_log_debug(D_REGISTRY, "REGISTRY: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
  148. if(link(old_filename, registry.db_filename) == -1)
  149. netdata_log_error("REGISTRY: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
  150. }
  151. else {
  152. netdata_log_debug(D_REGISTRY, "REGISTRY: removing tmp db '%s'", tmp_filename);
  153. if(unlink(tmp_filename) == -1)
  154. netdata_log_error("REGISTRY: cannot remove tmp registry file '%s'", tmp_filename);
  155. // it has been moved successfully
  156. // discard the current registry log
  157. registry_log_recreate();
  158. registry.log_count = 0;
  159. }
  160. }
  161. // continue operations
  162. nd_log_limits_reset();
  163. return -1;
  164. }
  165. // ----------------------------------------------------------------------------
  166. // LOAD THE REGISTRY DATABASE
  167. size_t registry_db_load(void) {
  168. char *s, buf[4096 + 1];
  169. REGISTRY_PERSON *p = NULL;
  170. REGISTRY_MACHINE *m = NULL;
  171. STRING *u = NULL;
  172. size_t line = 0;
  173. netdata_log_debug(D_REGISTRY, "REGISTRY: loading active db from: '%s'", registry.db_filename);
  174. FILE *fp = fopen(registry.db_filename, "r");
  175. if(!fp) {
  176. if (errno != ENOENT)
  177. netdata_log_error("REGISTRY: cannot open registry file: '%s'", registry.db_filename);
  178. return 0;
  179. }
  180. REGISTRY_MACHINE_URL *mu;
  181. size_t len = 0;
  182. buf[4096] = '\0';
  183. while((s = fgets_trim_len(buf, 4096, fp, &len))) {
  184. line++;
  185. netdata_log_debug(D_REGISTRY, "REGISTRY: read line %zu to length %zu: %s", line, len, s);
  186. switch(*s) {
  187. case 'U': // person URL
  188. if(unlikely(!p)) {
  189. netdata_log_error("REGISTRY: ignoring line %zu, no person loaded: %s", line, s);
  190. continue;
  191. }
  192. // verify it is valid
  193. if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
  194. netdata_log_error("REGISTRY: person URL line %zu is wrong (len = %zu).", line, len);
  195. continue;
  196. }
  197. s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
  198. // skip the name to find the url
  199. char *url = &s[69];
  200. while(*url && *url != '\t') url++;
  201. if(!*url) {
  202. netdata_log_error("REGISTRY: person URL line %zu does not have a url.", line);
  203. continue;
  204. }
  205. *url++ = '\0';
  206. if(*url != 'h' && *url != '*') {
  207. netdata_log_error("REGISTRY: person URL line %zu does not have a valid url: %s", line, url);
  208. continue;
  209. }
  210. u = string_strdupz(url);
  211. time_t first_t = (time_t)strtoul(&s[2], NULL, 16);
  212. m = registry_machine_find(&s[32]);
  213. if(!m) m = registry_machine_allocate(&s[32], first_t);
  214. mu = registry_machine_url_find(m, u);
  215. if(!mu) {
  216. netdata_log_error("REGISTRY: person URL line %zu was not linked to the machine it refers to", line);
  217. mu = registry_machine_url_allocate(m, u, first_t);
  218. }
  219. REGISTRY_PERSON_URL *pu = registry_person_url_index_find(p, u);
  220. if(!pu)
  221. pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
  222. else
  223. netdata_log_error("REGISTRY: person URL line %zu is duplicate, reusing the old one.", line);
  224. pu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
  225. pu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
  226. pu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
  227. netdata_log_debug(D_REGISTRY, "REGISTRY: loaded person URL '%s' with name '%s' of machine '%s', first: %u, last: %u, usages: %u, flags: %02x",
  228. string2str(u), string2str(pu->machine_name), m->guid, pu->first_t, pu->last_t, pu->usages, pu->flags);
  229. string_freez(u);
  230. break;
  231. case 'P': // person
  232. m = NULL;
  233. // verify it is valid
  234. if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
  235. netdata_log_error("REGISTRY: person line %zu is wrong (len = %zu).", line, len);
  236. continue;
  237. }
  238. s[1] = s[10] = s[19] = s[28] = '\0';
  239. p = registry_person_allocate(&s[29], (time_t)strtoul(&s[2], NULL, 16));
  240. p->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
  241. p->usages = (uint32_t)strtoul(&s[20], NULL, 16);
  242. netdata_log_debug(D_REGISTRY, "REGISTRY: loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
  243. break;
  244. case 'V': // machine URL
  245. if(unlikely(!m)) {
  246. netdata_log_error("REGISTRY: ignoring line %zu, no machine loaded: %s", line, s);
  247. continue;
  248. }
  249. // verify it is valid
  250. if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
  251. netdata_log_error("REGISTRY: person URL line %zu is wrong (len = %zu).", line, len);
  252. continue;
  253. }
  254. s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
  255. url = &s[32];
  256. if(*url != 'h' && *url != '*') {
  257. netdata_log_error("REGISTRY: machine URL line %zu does not have a valid url: %s", line, url);
  258. continue;
  259. }
  260. u = string_strdupz(url);
  261. mu = registry_machine_url_find(m, u);
  262. if(!mu)
  263. mu = registry_machine_url_allocate(m, u, (time_t)strtoul(&s[2], NULL, 16));
  264. else
  265. netdata_log_error("REGISTRY: machine URL line %zu is duplicate, reusing the old one.", line);
  266. mu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
  267. mu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
  268. mu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
  269. netdata_log_debug(D_REGISTRY, "Registry loaded machine URL '%s', machine '%s', first: %u, last: %u, usages: %u, flags: %02x",
  270. string2str(u), m->guid, mu->first_t, mu->last_t, mu->usages, mu->flags);
  271. string_freez(u);
  272. break;
  273. case 'M': // machine
  274. p = NULL;
  275. // verify it is valid
  276. if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
  277. netdata_log_error("REGISTRY: person line %zu is wrong (len = %zu).", line, len);
  278. continue;
  279. }
  280. s[1] = s[10] = s[19] = s[28] = '\0';
  281. m = registry_machine_allocate(&s[29], (time_t)strtoul(&s[2], NULL, 16));
  282. m->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
  283. m->usages = (uint32_t)strtoul(&s[20], NULL, 16);
  284. netdata_log_debug(D_REGISTRY, "REGISTRY: loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
  285. break;
  286. case 'T': // totals
  287. 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')) {
  288. netdata_log_error("REGISTRY: totals line %zu is wrong (len = %zu).", line, len);
  289. continue;
  290. }
  291. registry.persons_count = strtoull(&s[2], NULL, 16);
  292. registry.machines_count = strtoull(&s[19], NULL, 16);
  293. registry.usages_count = strtoull(&s[36], NULL, 16);
  294. registry.persons_urls_count = strtoull(&s[70], NULL, 16);
  295. registry.machines_urls_count = strtoull(&s[87], NULL, 16);
  296. break;
  297. default:
  298. netdata_log_error("REGISTRY: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
  299. break;
  300. }
  301. }
  302. fclose(fp);
  303. return line;
  304. }