rrdvar.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_HEALTH_INTERNALS
  3. #include "rrd.h"
  4. // ----------------------------------------------------------------------------
  5. // RRDVAR management
  6. inline int rrdvar_fix_name(char *variable) {
  7. int fixed = 0;
  8. while(*variable) {
  9. if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
  10. *variable++ = '_';
  11. fixed++;
  12. }
  13. else
  14. variable++;
  15. }
  16. return fixed;
  17. }
  18. int rrdvar_compare(void* a, void* b) {
  19. if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
  20. else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
  21. else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
  22. }
  23. static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
  24. RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl_t *)(rv));
  25. if(ret != rv)
  26. debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
  27. return ret;
  28. }
  29. static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
  30. RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl_t *)(rv));
  31. if(!ret)
  32. error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
  33. return ret;
  34. }
  35. static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
  36. RRDVAR tmp;
  37. tmp.name = (char *)name;
  38. tmp.hash = (hash)?hash:simple_hash(tmp.name);
  39. return (RRDVAR *)avl_search_lock(tree, (avl_t *)&tmp);
  40. }
  41. inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
  42. (void)host;
  43. if(!rv) return;
  44. if(tree) {
  45. debug(D_VARIABLES, "Deleting variable '%s'", rv->name);
  46. if(unlikely(!rrdvar_index_del(tree, rv)))
  47. error("RRDVAR: Attempted to delete variable '%s' from host '%s', but it is not found.", rv->name, host->hostname);
  48. }
  49. if(rv->options & RRDVAR_OPTION_ALLOCATED)
  50. freez(rv->value);
  51. freez(rv->name);
  52. freez(rv);
  53. }
  54. inline RRDVAR *rrdvar_create_and_index(const char *scope __maybe_unused, avl_tree_lock *tree, const char *name,
  55. RRDVAR_TYPE type, RRDVAR_OPTIONS options, void *value) {
  56. char *variable = strdupz(name);
  57. rrdvar_fix_name(variable);
  58. uint32_t hash = simple_hash(variable);
  59. RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
  60. if(unlikely(!rv)) {
  61. debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
  62. rv = callocz(1, sizeof(RRDVAR));
  63. rv->name = variable;
  64. rv->hash = hash;
  65. rv->type = type;
  66. rv->options = options;
  67. rv->value = value;
  68. rv->last_updated = now_realtime_sec();
  69. RRDVAR *ret = rrdvar_index_add(tree, rv);
  70. if(unlikely(ret != rv)) {
  71. debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
  72. freez(rv);
  73. freez(variable);
  74. rv = NULL;
  75. }
  76. else
  77. debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
  78. }
  79. else {
  80. debug(D_VARIABLES, "Variable '%s' is already found in scope '%s'.", variable, scope);
  81. // already exists
  82. freez(variable);
  83. // this is important
  84. // it must return NULL - not the existing variable - or double-free will happen
  85. rv = NULL;
  86. }
  87. return rv;
  88. }
  89. void rrdvar_free_remaining_variables(RRDHOST *host, avl_tree_lock *tree_lock) {
  90. // This is not bullet proof - avl should support some means to destroy it
  91. // with a callback for each item already in the index
  92. RRDVAR *rv, *last = NULL;
  93. while((rv = (RRDVAR *)tree_lock->avl_tree.root)) {
  94. if(unlikely(rv == last)) {
  95. error("RRDVAR: INTERNAL ERROR: Cannot cleanup tree of RRDVARs");
  96. break;
  97. }
  98. last = rv;
  99. rrdvar_free(host, tree_lock, rv);
  100. }
  101. }
  102. // ----------------------------------------------------------------------------
  103. // CUSTOM HOST VARIABLES
  104. inline int rrdvar_callback_for_all_host_variables(RRDHOST *host, int (*callback)(void * /*rrdvar*/, void * /*data*/), void *data) {
  105. return avl_traverse_lock(&host->rrdvar_root_index, callback, data);
  106. }
  107. static RRDVAR *rrdvar_custom_variable_create(const char *scope, avl_tree_lock *tree_lock, const char *name) {
  108. NETDATA_DOUBLE *v = callocz(1, sizeof(NETDATA_DOUBLE));
  109. *v = NAN;
  110. RRDVAR *rv = rrdvar_create_and_index(scope, tree_lock, name, RRDVAR_TYPE_CALCULATED, RRDVAR_OPTION_CUSTOM_HOST_VAR|RRDVAR_OPTION_ALLOCATED, v);
  111. if(unlikely(!rv)) {
  112. freez(v);
  113. debug(D_VARIABLES, "Requested variable '%s' already exists - possibly 2 plugins are updating it at the same time.", name);
  114. char *variable = strdupz(name);
  115. rrdvar_fix_name(variable);
  116. uint32_t hash = simple_hash(variable);
  117. // find the existing one to return it
  118. rv = rrdvar_index_find(tree_lock, variable, hash);
  119. freez(variable);
  120. }
  121. return rv;
  122. }
  123. RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name) {
  124. return rrdvar_custom_variable_create("host", &host->rrdvar_root_index, name);
  125. }
  126. void rrdvar_custom_host_variable_set(RRDHOST *host, RRDVAR *rv, NETDATA_DOUBLE value) {
  127. if(rv->type != RRDVAR_TYPE_CALCULATED || !(rv->options & RRDVAR_OPTION_CUSTOM_HOST_VAR) || !(rv->options & RRDVAR_OPTION_ALLOCATED))
  128. error("requested to set variable '%s' to value " NETDATA_DOUBLE_FORMAT " but the variable is not a custom one.", rv->name, value);
  129. else {
  130. NETDATA_DOUBLE *v = rv->value;
  131. if(*v != value) {
  132. *v = value;
  133. rv->last_updated = now_realtime_sec();
  134. // if the host is streaming, send this variable upstream immediately
  135. rrdpush_sender_send_this_host_variable_now(host, rv);
  136. }
  137. }
  138. }
  139. int foreach_host_variable_callback(RRDHOST *host, int (*callback)(RRDVAR * /*rv*/, void * /*data*/), void *data) {
  140. return avl_traverse_lock(&host->rrdvar_root_index, (int (*)(void *, void *))callback, data);
  141. }
  142. // ----------------------------------------------------------------------------
  143. // RRDVAR lookup
  144. NETDATA_DOUBLE rrdvar2number(RRDVAR *rv) {
  145. switch(rv->type) {
  146. case RRDVAR_TYPE_CALCULATED: {
  147. NETDATA_DOUBLE *n = (NETDATA_DOUBLE *)rv->value;
  148. return *n;
  149. }
  150. case RRDVAR_TYPE_TIME_T: {
  151. time_t *n = (time_t *)rv->value;
  152. return *n;
  153. }
  154. case RRDVAR_TYPE_COLLECTED: {
  155. collected_number *n = (collected_number *)rv->value;
  156. return *n;
  157. }
  158. case RRDVAR_TYPE_TOTAL: {
  159. total_number *n = (total_number *)rv->value;
  160. return *n;
  161. }
  162. case RRDVAR_TYPE_INT: {
  163. int *n = (int *)rv->value;
  164. return *n;
  165. }
  166. default:
  167. error("I don't know how to convert RRDVAR type %u to NETDATA_DOUBLE", rv->type);
  168. return NAN;
  169. }
  170. }
  171. int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, NETDATA_DOUBLE *result) {
  172. RRDSET *st = rc->rrdset;
  173. if(!st) return 0;
  174. RRDHOST *host = st->rrdhost;
  175. RRDVAR *rv;
  176. rv = rrdvar_index_find(&st->rrdvar_root_index, variable, hash);
  177. if(rv) {
  178. *result = rrdvar2number(rv);
  179. return 1;
  180. }
  181. rv = rrdvar_index_find(&st->rrdfamily->rrdvar_root_index, variable, hash);
  182. if(rv) {
  183. *result = rrdvar2number(rv);
  184. return 1;
  185. }
  186. rv = rrdvar_index_find(&host->rrdvar_root_index, variable, hash);
  187. if(rv) {
  188. *result = rrdvar2number(rv);
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. // ----------------------------------------------------------------------------
  194. // RRDVAR to JSON
  195. struct variable2json_helper {
  196. BUFFER *buf;
  197. size_t counter;
  198. RRDVAR_OPTIONS options;
  199. };
  200. static int single_variable2json(void *entry, void *data) {
  201. struct variable2json_helper *helper = (struct variable2json_helper *)data;
  202. RRDVAR *rv = (RRDVAR *)entry;
  203. NETDATA_DOUBLE value = rrdvar2number(rv);
  204. if (helper->options == RRDVAR_OPTION_DEFAULT || rv->options & helper->options) {
  205. if(unlikely(isnan(value) || isinf(value)))
  206. buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rv->name);
  207. else
  208. buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5" NETDATA_DOUBLE_MODIFIER, helper->counter?",":"", rv->name, (NETDATA_DOUBLE)value);
  209. helper->counter++;
  210. }
  211. return 0;
  212. }
  213. void health_api_v1_chart_custom_variables2json(RRDSET *st, BUFFER *buf) {
  214. struct variable2json_helper helper = {
  215. .buf = buf,
  216. .counter = 0,
  217. .options = RRDVAR_OPTION_CUSTOM_CHART_VAR
  218. };
  219. buffer_sprintf(buf, "{");
  220. avl_traverse_lock(&st->rrdvar_root_index, single_variable2json, (void *)&helper);
  221. buffer_strcat(buf, "\n\t\t\t}");
  222. }
  223. void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf) {
  224. RRDHOST *host = st->rrdhost;
  225. struct variable2json_helper helper = {
  226. .buf = buf,
  227. .counter = 0,
  228. .options = RRDVAR_OPTION_DEFAULT
  229. };
  230. buffer_sprintf(buf, "{\n\t\"chart\": \"%s\",\n\t\"chart_name\": \"%s\",\n\t\"chart_context\": \"%s\",\n\t\"chart_variables\": {", st->id, st->name, st->context);
  231. avl_traverse_lock(&st->rrdvar_root_index, single_variable2json, (void *)&helper);
  232. buffer_sprintf(buf, "\n\t},\n\t\"family\": \"%s\",\n\t\"family_variables\": {", st->family);
  233. helper.counter = 0;
  234. avl_traverse_lock(&st->rrdfamily->rrdvar_root_index, single_variable2json, (void *)&helper);
  235. buffer_sprintf(buf, "\n\t},\n\t\"host\": \"%s\",\n\t\"host_variables\": {", host->hostname);
  236. helper.counter = 0;
  237. avl_traverse_lock(&host->rrdvar_root_index, single_variable2json, (void *)&helper);
  238. buffer_strcat(buf, "\n\t}\n}\n");
  239. }