rrdfunctions-exporters.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrdfunctions-internals.h"
  4. #include "rrdfunctions-exporters.h"
  5. void rrd_chart_functions_expose_rrdpush(RRDSET *st, BUFFER *wb) {
  6. if(!st->functions_view)
  7. return;
  8. struct rrd_host_function *t;
  9. dfe_start_read(st->functions_view, t) {
  10. if(t->options & RRD_FUNCTION_DYNCFG) continue;
  11. buffer_sprintf(wb
  12. , PLUGINSD_KEYWORD_FUNCTION " \"%s\" %d \"%s\" \"%s\" \"%s\" %d\n"
  13. , t_dfe.name
  14. , t->timeout
  15. , string2str(t->help)
  16. , string2str(t->tags)
  17. , http_id2access(t->access)
  18. ,
  19. t->priority
  20. );
  21. }
  22. dfe_done(t);
  23. }
  24. void rrd_global_functions_expose_rrdpush(RRDHOST *host, BUFFER *wb, bool dyncfg) {
  25. rrdhost_flag_clear(host, RRDHOST_FLAG_GLOBAL_FUNCTIONS_UPDATED);
  26. size_t configs = 0;
  27. struct rrd_host_function *tmp;
  28. dfe_start_read(host->functions, tmp) {
  29. if(tmp->options & RRD_FUNCTION_LOCAL) continue;
  30. if(tmp->options & RRD_FUNCTION_DYNCFG) {
  31. // we should not send dyncfg to this parent
  32. configs++;
  33. continue;
  34. }
  35. buffer_sprintf(wb
  36. , PLUGINSD_KEYWORD_FUNCTION " GLOBAL \"%s\" %d \"%s\" \"%s\" \"%s\" %d\n"
  37. , tmp_dfe.name
  38. , tmp->timeout
  39. , string2str(tmp->help)
  40. , string2str(tmp->tags)
  41. , http_id2access(tmp->access)
  42. , tmp->priority
  43. );
  44. }
  45. dfe_done(tmp);
  46. if(dyncfg && configs)
  47. dyncfg_add_streaming(wb);
  48. }
  49. static void functions2json(DICTIONARY *functions, BUFFER *wb) {
  50. struct rrd_host_function *t;
  51. dfe_start_read(functions, t) {
  52. if (!rrd_collector_running(t->collector)) continue;
  53. if(t->options & RRD_FUNCTION_DYNCFG) continue;
  54. buffer_json_member_add_object(wb, t_dfe.name);
  55. {
  56. buffer_json_member_add_string_or_empty(wb, "help", string2str(t->help));
  57. buffer_json_member_add_int64(wb, "timeout", (int64_t) t->timeout);
  58. char options[65];
  59. snprintfz(
  60. options, 64
  61. , "%s%s"
  62. , (t->options & RRD_FUNCTION_LOCAL) ? "LOCAL " : ""
  63. , (t->options & RRD_FUNCTION_GLOBAL) ? "GLOBAL" : ""
  64. );
  65. buffer_json_member_add_string_or_empty(wb, "options", options);
  66. buffer_json_member_add_string_or_empty(wb, "tags", string2str(t->tags));
  67. buffer_json_member_add_string(wb, "access", http_id2access(t->access));
  68. buffer_json_member_add_uint64(wb, "priority", t->priority);
  69. }
  70. buffer_json_object_close(wb);
  71. }
  72. dfe_done(t);
  73. }
  74. void chart_functions2json(RRDSET *st, BUFFER *wb) {
  75. if(!st || !st->functions_view) return;
  76. functions2json(st->functions_view, wb);
  77. }
  78. void host_functions2json(RRDHOST *host, BUFFER *wb) {
  79. if(!host || !host->functions) return;
  80. buffer_json_member_add_object(wb, "functions");
  81. struct rrd_host_function *t;
  82. dfe_start_read(host->functions, t) {
  83. if(!rrd_collector_running(t->collector)) continue;
  84. if(t->options & RRD_FUNCTION_DYNCFG) continue;
  85. buffer_json_member_add_object(wb, t_dfe.name);
  86. {
  87. buffer_json_member_add_string(wb, "help", string2str(t->help));
  88. buffer_json_member_add_int64(wb, "timeout", t->timeout);
  89. buffer_json_member_add_array(wb, "options");
  90. {
  91. if (t->options & RRD_FUNCTION_GLOBAL)
  92. buffer_json_add_array_item_string(wb, "GLOBAL");
  93. if (t->options & RRD_FUNCTION_LOCAL)
  94. buffer_json_add_array_item_string(wb, "LOCAL");
  95. }
  96. buffer_json_array_close(wb);
  97. buffer_json_member_add_string(wb, "tags", string2str(t->tags));
  98. buffer_json_member_add_string(wb, "access", http_id2access(t->access));
  99. buffer_json_member_add_uint64(wb, "priority", t->priority);
  100. }
  101. buffer_json_object_close(wb);
  102. }
  103. dfe_done(t);
  104. buffer_json_object_close(wb);
  105. }
  106. void chart_functions_to_dict(DICTIONARY *rrdset_functions_view, DICTIONARY *dst, void *value, size_t value_size) {
  107. if(!rrdset_functions_view || !dst) return;
  108. struct rrd_host_function *t;
  109. dfe_start_read(rrdset_functions_view, t) {
  110. if(!rrd_collector_running(t->collector)) continue;
  111. if(t->options & RRD_FUNCTION_DYNCFG) continue;
  112. dictionary_set(dst, t_dfe.name, value, value_size);
  113. }
  114. dfe_done(t);
  115. }
  116. void host_functions_to_dict(RRDHOST *host, DICTIONARY *dst, void *value, size_t value_size, STRING **help, STRING **tags, HTTP_ACCESS *access, int *priority) {
  117. if(!host || !host->functions || !dictionary_entries(host->functions) || !dst) return;
  118. struct rrd_host_function *t;
  119. dfe_start_read(host->functions, t) {
  120. if(!rrd_collector_running(t->collector)) continue;
  121. if(t->options & RRD_FUNCTION_DYNCFG) continue;
  122. if(help)
  123. *help = t->help;
  124. if(tags)
  125. *tags = t->tags;
  126. if(access)
  127. *access = t->access;
  128. if(priority)
  129. *priority = t->priority;
  130. dictionary_set(dst, t_dfe.name, value, value_size);
  131. }
  132. dfe_done(t);
  133. }