aclk_collector_list.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // This is copied from Legacy ACLK, Original Author: amoss
  3. // TODO unmess this
  4. #include "aclk_collector_list.h"
  5. netdata_mutex_t collector_mutex = NETDATA_MUTEX_INITIALIZER;
  6. struct _collector *collector_list = NULL;
  7. /*
  8. * Free a collector structure
  9. */
  10. void _free_collector(struct _collector *collector)
  11. {
  12. if (likely(collector->plugin_name))
  13. freez(collector->plugin_name);
  14. if (likely(collector->module_name))
  15. freez(collector->module_name);
  16. if (likely(collector->hostname))
  17. freez(collector->hostname);
  18. freez(collector);
  19. }
  20. /*
  21. * This will report the collector list
  22. *
  23. */
  24. #ifdef ACLK_DEBUG
  25. static void _dump_collector_list()
  26. {
  27. struct _collector *tmp_collector;
  28. COLLECTOR_LOCK;
  29. info("DUMPING ALL COLLECTORS");
  30. if (unlikely(!collector_list || !collector_list->next)) {
  31. COLLECTOR_UNLOCK;
  32. info("DUMPING ALL COLLECTORS -- nothing found");
  33. return;
  34. }
  35. // Note that the first entry is "dummy"
  36. tmp_collector = collector_list->next;
  37. while (tmp_collector) {
  38. info(
  39. "COLLECTOR %s : [%s:%s] count = %u", tmp_collector->hostname,
  40. tmp_collector->plugin_name ? tmp_collector->plugin_name : "",
  41. tmp_collector->module_name ? tmp_collector->module_name : "", tmp_collector->count);
  42. tmp_collector = tmp_collector->next;
  43. }
  44. info("DUMPING ALL COLLECTORS DONE");
  45. COLLECTOR_UNLOCK;
  46. }
  47. #endif
  48. /*
  49. * This will cleanup the collector list
  50. *
  51. */
  52. void _reset_collector_list()
  53. {
  54. struct _collector *tmp_collector, *next_collector;
  55. COLLECTOR_LOCK;
  56. if (unlikely(!collector_list || !collector_list->next)) {
  57. COLLECTOR_UNLOCK;
  58. return;
  59. }
  60. // Note that the first entry is "dummy"
  61. tmp_collector = collector_list->next;
  62. collector_list->count = 0;
  63. collector_list->next = NULL;
  64. // We broke the link; we can unlock
  65. COLLECTOR_UNLOCK;
  66. while (tmp_collector) {
  67. next_collector = tmp_collector->next;
  68. _free_collector(tmp_collector);
  69. tmp_collector = next_collector;
  70. }
  71. }
  72. /*
  73. * Find a collector (if it exists)
  74. * Must lock before calling this
  75. * If last_collector is not null, it will return the previous collector in the linked
  76. * list (used in collector delete)
  77. */
  78. static struct _collector *_find_collector(
  79. const char *hostname, const char *plugin_name, const char *module_name, struct _collector **last_collector)
  80. {
  81. struct _collector *tmp_collector, *prev_collector;
  82. uint32_t plugin_hash;
  83. uint32_t module_hash;
  84. uint32_t hostname_hash;
  85. if (unlikely(!collector_list)) {
  86. collector_list = callocz(1, sizeof(struct _collector));
  87. return NULL;
  88. }
  89. if (unlikely(!collector_list->next))
  90. return NULL;
  91. plugin_hash = plugin_name ? simple_hash(plugin_name) : 1;
  92. module_hash = module_name ? simple_hash(module_name) : 1;
  93. hostname_hash = simple_hash(hostname);
  94. // Note that the first entry is "dummy"
  95. tmp_collector = collector_list->next;
  96. prev_collector = collector_list;
  97. while (tmp_collector) {
  98. if (plugin_hash == tmp_collector->plugin_hash && module_hash == tmp_collector->module_hash &&
  99. hostname_hash == tmp_collector->hostname_hash && (!strcmp(hostname, tmp_collector->hostname)) &&
  100. (!plugin_name || !tmp_collector->plugin_name || !strcmp(plugin_name, tmp_collector->plugin_name)) &&
  101. (!module_name || !tmp_collector->module_name || !strcmp(module_name, tmp_collector->module_name))) {
  102. if (unlikely(last_collector))
  103. *last_collector = prev_collector;
  104. return tmp_collector;
  105. }
  106. prev_collector = tmp_collector;
  107. tmp_collector = tmp_collector->next;
  108. }
  109. return tmp_collector;
  110. }
  111. /*
  112. * Called to delete a collector
  113. * It will reduce the count (chart_count) and will remove it
  114. * from the linked list if the count reaches zero
  115. * The structure will be returned to the caller to free
  116. * the resources
  117. *
  118. */
  119. struct _collector *_del_collector(const char *hostname, const char *plugin_name, const char *module_name)
  120. {
  121. struct _collector *tmp_collector, *prev_collector = NULL;
  122. tmp_collector = _find_collector(hostname, plugin_name, module_name, &prev_collector);
  123. if (likely(tmp_collector)) {
  124. --tmp_collector->count;
  125. if (unlikely(!tmp_collector->count))
  126. prev_collector->next = tmp_collector->next;
  127. }
  128. return tmp_collector;
  129. }
  130. /*
  131. * Add a new collector (plugin / module) to the list
  132. * If it already exists just update the chart count
  133. *
  134. * Lock before calling
  135. */
  136. struct _collector *_add_collector(const char *hostname, const char *plugin_name, const char *module_name)
  137. {
  138. struct _collector *tmp_collector;
  139. tmp_collector = _find_collector(hostname, plugin_name, module_name, NULL);
  140. if (unlikely(!tmp_collector)) {
  141. tmp_collector = callocz(1, sizeof(struct _collector));
  142. tmp_collector->hostname_hash = simple_hash(hostname);
  143. tmp_collector->plugin_hash = plugin_name ? simple_hash(plugin_name) : 1;
  144. tmp_collector->module_hash = module_name ? simple_hash(module_name) : 1;
  145. tmp_collector->hostname = strdupz(hostname);
  146. tmp_collector->plugin_name = plugin_name ? strdupz(plugin_name) : NULL;
  147. tmp_collector->module_name = module_name ? strdupz(module_name) : NULL;
  148. tmp_collector->next = collector_list->next;
  149. collector_list->next = tmp_collector;
  150. }
  151. tmp_collector->count++;
  152. debug(
  153. D_ACLK, "ADD COLLECTOR %s [%s:%s] -- chart %u", hostname, plugin_name ? plugin_name : "*",
  154. module_name ? module_name : "*", tmp_collector->count);
  155. return tmp_collector;
  156. }