graphite.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "graphite.h"
  3. /**
  4. * Initialize Graphite connector instance
  5. *
  6. * @param instance an instance data structure.
  7. * @return Returns 0 on success, 1 on failure.
  8. */
  9. int init_graphite_instance(struct instance *instance)
  10. {
  11. instance->worker = simple_connector_worker;
  12. struct simple_connector_config *connector_specific_config = callocz(1, sizeof(struct simple_connector_config));
  13. instance->config.connector_specific_config = (void *)connector_specific_config;
  14. connector_specific_config->default_port = 2003;
  15. struct simple_connector_data *connector_specific_data = callocz(1, sizeof(struct simple_connector_data));
  16. instance->connector_specific_data = connector_specific_data;
  17. #ifdef ENABLE_HTTPS
  18. connector_specific_data->ssl = NETDATA_SSL_UNSET_CONNECTION;
  19. if (instance->config.options & EXPORTING_OPTION_USE_TLS) {
  20. netdata_ssl_initialize_ctx(NETDATA_SSL_EXPORTING_CTX);
  21. }
  22. #endif
  23. instance->start_batch_formatting = NULL;
  24. instance->start_host_formatting = format_host_labels_graphite_plaintext;
  25. instance->start_chart_formatting = NULL;
  26. if (EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AS_COLLECTED)
  27. instance->metric_formatting = format_dimension_collected_graphite_plaintext;
  28. else
  29. instance->metric_formatting = format_dimension_stored_graphite_plaintext;
  30. instance->end_chart_formatting = NULL;
  31. instance->variables_formatting = NULL;
  32. instance->end_host_formatting = flush_host_labels;
  33. instance->end_batch_formatting = simple_connector_end_batch;
  34. if (instance->config.type == EXPORTING_CONNECTOR_TYPE_GRAPHITE_HTTP)
  35. instance->prepare_header = graphite_http_prepare_header;
  36. else
  37. instance->prepare_header = NULL;
  38. instance->check_response = exporting_discard_response;
  39. instance->buffer = (void *)buffer_create(0, &netdata_buffers_statistics.buffers_exporters);
  40. if (!instance->buffer) {
  41. error("EXPORTING: cannot create buffer for graphite exporting connector instance %s", instance->config.name);
  42. return 1;
  43. }
  44. simple_connector_init(instance);
  45. if (uv_mutex_init(&instance->mutex))
  46. return 1;
  47. if (uv_cond_init(&instance->cond_var))
  48. return 1;
  49. return 0;
  50. }
  51. /**
  52. * Copy a label value and substitute underscores in place of characters which can't be used in Graphite output
  53. *
  54. * @param dst a destination string.
  55. * @param src a source string.
  56. * @param len the maximum number of characters copied.
  57. */
  58. void sanitize_graphite_label_value(char *dst, const char *src, size_t len)
  59. {
  60. while (*src != '\0' && len) {
  61. if (isspace(*src) || *src == ';' || *src == '~')
  62. *dst++ = '_';
  63. else
  64. *dst++ = *src;
  65. src++;
  66. len--;
  67. }
  68. *dst = '\0';
  69. }
  70. /**
  71. * Format host labels for JSON connector
  72. *
  73. * @param instance an instance data structure.
  74. * @param host a data collecting host.
  75. * @return Always returns 0.
  76. */
  77. int format_host_labels_graphite_plaintext(struct instance *instance, RRDHOST *host)
  78. {
  79. if (!instance->labels_buffer)
  80. instance->labels_buffer = buffer_create(1024, &netdata_buffers_statistics.buffers_exporters);
  81. if (unlikely(!sending_labels_configured(instance)))
  82. return 0;
  83. rrdlabels_to_buffer(host->rrdlabels, instance->labels_buffer, ";", "=", "", "",
  84. exporting_labels_filter_callback, instance,
  85. NULL, sanitize_graphite_label_value);
  86. return 0;
  87. }
  88. /**
  89. * Format dimension using collected data for Graphite connector
  90. *
  91. * @param instance an instance data structure.
  92. * @param rd a dimension.
  93. * @return Always returns 0.
  94. */
  95. int format_dimension_collected_graphite_plaintext(struct instance *instance, RRDDIM *rd)
  96. {
  97. RRDSET *st = rd->rrdset;
  98. RRDHOST *host = st->rrdhost;
  99. char chart_name[RRD_ID_LENGTH_MAX + 1];
  100. exporting_name_copy(
  101. chart_name,
  102. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st),
  103. RRD_ID_LENGTH_MAX);
  104. char dimension_name[RRD_ID_LENGTH_MAX + 1];
  105. exporting_name_copy(
  106. dimension_name,
  107. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  108. RRD_ID_LENGTH_MAX);
  109. buffer_sprintf(
  110. instance->buffer,
  111. "%s.%s.%s.%s%s%s%s " COLLECTED_NUMBER_FORMAT " %llu\n",
  112. instance->config.prefix,
  113. (host == localhost) ? instance->config.hostname : rrdhost_hostname(host),
  114. chart_name,
  115. dimension_name,
  116. (host->tags) ? ";" : "",
  117. (host->tags) ? rrdhost_tags(host) : "",
  118. (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : "",
  119. rd->last_collected_value,
  120. (unsigned long long)rd->last_collected_time.tv_sec);
  121. return 0;
  122. }
  123. /**
  124. * Format dimension using a calculated value from stored data for Graphite connector
  125. *
  126. * @param instance an instance data structure.
  127. * @param rd a dimension.
  128. * @return Always returns 0.
  129. */
  130. int format_dimension_stored_graphite_plaintext(struct instance *instance, RRDDIM *rd)
  131. {
  132. RRDSET *st = rd->rrdset;
  133. RRDHOST *host = st->rrdhost;
  134. char chart_name[RRD_ID_LENGTH_MAX + 1];
  135. exporting_name_copy(
  136. chart_name,
  137. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st),
  138. RRD_ID_LENGTH_MAX);
  139. char dimension_name[RRD_ID_LENGTH_MAX + 1];
  140. exporting_name_copy(
  141. dimension_name,
  142. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  143. RRD_ID_LENGTH_MAX);
  144. time_t last_t;
  145. NETDATA_DOUBLE value = exporting_calculate_value_from_stored_data(instance, rd, &last_t);
  146. if(isnan(value))
  147. return 0;
  148. buffer_sprintf(
  149. instance->buffer,
  150. "%s.%s.%s.%s%s%s%s " NETDATA_DOUBLE_FORMAT " %llu\n",
  151. instance->config.prefix,
  152. (host == localhost) ? instance->config.hostname : rrdhost_hostname(host),
  153. chart_name,
  154. dimension_name,
  155. (host->tags) ? ";" : "",
  156. (host->tags) ? rrdhost_tags(host) : "",
  157. (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : "",
  158. value,
  159. (unsigned long long)last_t);
  160. return 0;
  161. }
  162. /**
  163. * Prepare HTTP header
  164. *
  165. * @param instance an instance data structure.
  166. * @return Returns 0 on success, 1 on failure.
  167. */
  168. void graphite_http_prepare_header(struct instance *instance)
  169. {
  170. struct simple_connector_data *simple_connector_data = instance->connector_specific_data;
  171. buffer_sprintf(
  172. simple_connector_data->last_buffer->header,
  173. "POST /api/put HTTP/1.1\r\n"
  174. "Host: %s\r\n"
  175. "%s"
  176. "Content-Type: application/graphite\r\n"
  177. "Content-Length: %lu\r\n"
  178. "\r\n",
  179. instance->config.destination,
  180. simple_connector_data->auth_string ? simple_connector_data->auth_string : "",
  181. (unsigned long int) buffer_strlen(simple_connector_data->last_buffer->buffer));
  182. return;
  183. }