opentsdb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "opentsdb.h"
  3. #include "../json/json.h"
  4. /**
  5. * Initialize OpenTSDB telnet connector instance
  6. *
  7. * @param instance an instance data structure.
  8. * @return Returns 0 on success, 1 on failure.
  9. */
  10. int init_opentsdb_telnet_instance(struct instance *instance)
  11. {
  12. instance->worker = simple_connector_worker;
  13. struct simple_connector_config *connector_specific_config = callocz(1, sizeof(struct simple_connector_config));
  14. instance->config.connector_specific_config = (void *)connector_specific_config;
  15. connector_specific_config->default_port = 4242;
  16. struct simple_connector_data *connector_specific_data = callocz(1, sizeof(struct simple_connector_data));
  17. instance->connector_specific_data = connector_specific_data;
  18. #ifdef ENABLE_HTTPS
  19. connector_specific_data->ssl = NETDATA_SSL_UNSET_CONNECTION;
  20. if (instance->config.options & EXPORTING_OPTION_USE_TLS) {
  21. netdata_ssl_initialize_ctx(NETDATA_SSL_EXPORTING_CTX);
  22. }
  23. #endif
  24. instance->start_batch_formatting = NULL;
  25. instance->start_host_formatting = format_host_labels_opentsdb_telnet;
  26. instance->start_chart_formatting = NULL;
  27. if (EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AS_COLLECTED)
  28. instance->metric_formatting = format_dimension_collected_opentsdb_telnet;
  29. else
  30. instance->metric_formatting = format_dimension_stored_opentsdb_telnet;
  31. instance->end_chart_formatting = NULL;
  32. instance->variables_formatting = NULL;
  33. instance->end_host_formatting = flush_host_labels;
  34. instance->end_batch_formatting = simple_connector_end_batch;
  35. instance->prepare_header = NULL;
  36. instance->check_response = exporting_discard_response;
  37. instance->buffer = (void *)buffer_create(0, &netdata_buffers_statistics.buffers_exporters);
  38. if (!instance->buffer) {
  39. netdata_log_error("EXPORTING: cannot create buffer for opentsdb telnet exporting connector instance %s", instance->config.name);
  40. return 1;
  41. }
  42. simple_connector_init(instance);
  43. if (uv_mutex_init(&instance->mutex))
  44. return 1;
  45. if (uv_cond_init(&instance->cond_var))
  46. return 1;
  47. return 0;
  48. }
  49. /**
  50. * Initialize OpenTSDB HTTP connector instance
  51. *
  52. * @param instance an instance data structure.
  53. * @return Returns 0 on success, 1 on failure.
  54. */
  55. int init_opentsdb_http_instance(struct instance *instance)
  56. {
  57. instance->worker = simple_connector_worker;
  58. struct simple_connector_config *connector_specific_config = callocz(1, sizeof(struct simple_connector_config));
  59. instance->config.connector_specific_config = (void *)connector_specific_config;
  60. connector_specific_config->default_port = 4242;
  61. struct simple_connector_data *connector_specific_data = callocz(1, sizeof(struct simple_connector_data));
  62. #ifdef ENABLE_HTTPS
  63. connector_specific_data->ssl = NETDATA_SSL_UNSET_CONNECTION;
  64. if (instance->config.options & EXPORTING_OPTION_USE_TLS) {
  65. netdata_ssl_initialize_ctx(NETDATA_SSL_EXPORTING_CTX);
  66. }
  67. #endif
  68. instance->connector_specific_data = connector_specific_data;
  69. instance->start_batch_formatting = open_batch_json_http;
  70. instance->start_host_formatting = format_host_labels_opentsdb_http;
  71. instance->start_chart_formatting = NULL;
  72. if (EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AS_COLLECTED)
  73. instance->metric_formatting = format_dimension_collected_opentsdb_http;
  74. else
  75. instance->metric_formatting = format_dimension_stored_opentsdb_http;
  76. instance->end_chart_formatting = NULL;
  77. instance->variables_formatting = NULL;
  78. instance->end_host_formatting = flush_host_labels;
  79. instance->end_batch_formatting = close_batch_json_http;
  80. instance->prepare_header = opentsdb_http_prepare_header;
  81. instance->check_response = exporting_discard_response;
  82. instance->buffer = (void *)buffer_create(0, &netdata_buffers_statistics.buffers_exporters);
  83. if (!instance->buffer) {
  84. netdata_log_error("EXPORTING: cannot create buffer for opentsdb HTTP exporting connector instance %s", instance->config.name);
  85. return 1;
  86. }
  87. simple_connector_init(instance);
  88. if (uv_mutex_init(&instance->mutex))
  89. return 1;
  90. if (uv_cond_init(&instance->cond_var))
  91. return 1;
  92. return 0;
  93. }
  94. /**
  95. * Copy a label value and substitute underscores in place of characters which can't be used in OpenTSDB output
  96. *
  97. * @param dst a destination string.
  98. * @param src a source string.
  99. * @param len the maximum number of characters copied.
  100. */
  101. void sanitize_opentsdb_label_value(char *dst, const char *src, size_t len)
  102. {
  103. while (*src != '\0' && len) {
  104. if (isalpha(*src) || isdigit(*src) || *src == '-' || *src == '.' || *src == '/' || IS_UTF8_BYTE(*src))
  105. *dst++ = *src;
  106. else
  107. *dst++ = '_';
  108. src++;
  109. len--;
  110. }
  111. *dst = '\0';
  112. }
  113. /**
  114. * Format host labels for JSON connector
  115. *
  116. * @param instance an instance data structure.
  117. * @param host a data collecting host.
  118. * @return Always returns 0.
  119. */
  120. int format_host_labels_opentsdb_telnet(struct instance *instance, RRDHOST *host) {
  121. if(!instance->labels_buffer)
  122. instance->labels_buffer = buffer_create(1024, &netdata_buffers_statistics.buffers_exporters);
  123. if (unlikely(!sending_labels_configured(instance)))
  124. return 0;
  125. buffer_strcat(instance->labels_buffer, " ");
  126. rrdlabels_to_buffer(host->rrdlabels, instance->labels_buffer, "", "=", "", " ",
  127. exporting_labels_filter_callback, instance,
  128. NULL, sanitize_opentsdb_label_value);
  129. return 0;
  130. }
  131. /**
  132. * Format dimension using collected data for OpenTSDB telnet connector
  133. *
  134. * @param instance an instance data structure.
  135. * @param rd a dimension.
  136. * @return Always returns 0.
  137. */
  138. int format_dimension_collected_opentsdb_telnet(struct instance *instance, RRDDIM *rd)
  139. {
  140. RRDSET *st = rd->rrdset;
  141. RRDHOST *host = st->rrdhost;
  142. char chart_name[RRD_ID_LENGTH_MAX + 1];
  143. exporting_name_copy(
  144. chart_name,
  145. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st),
  146. RRD_ID_LENGTH_MAX);
  147. char dimension_name[RRD_ID_LENGTH_MAX + 1];
  148. exporting_name_copy(
  149. dimension_name,
  150. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  151. RRD_ID_LENGTH_MAX);
  152. buffer_sprintf(
  153. instance->buffer,
  154. "put %s.%s.%s %llu " COLLECTED_NUMBER_FORMAT " host=%s%s%s%s\n",
  155. instance->config.prefix,
  156. chart_name,
  157. dimension_name,
  158. (unsigned long long)rd->collector.last_collected_time.tv_sec,
  159. rd->collector.last_collected_value,
  160. (host == localhost) ? instance->config.hostname : rrdhost_hostname(host),
  161. (host->tags) ? " " : "",
  162. (host->tags) ? rrdhost_tags(host) : "",
  163. (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : "");
  164. return 0;
  165. }
  166. /**
  167. * Format dimension using a calculated value from stored data for OpenTSDB telnet connector
  168. *
  169. * @param instance an instance data structure.
  170. * @param rd a dimension.
  171. * @return Always returns 0.
  172. */
  173. int format_dimension_stored_opentsdb_telnet(struct instance *instance, RRDDIM *rd)
  174. {
  175. RRDSET *st = rd->rrdset;
  176. RRDHOST *host = st->rrdhost;
  177. char chart_name[RRD_ID_LENGTH_MAX + 1];
  178. exporting_name_copy(
  179. chart_name,
  180. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st),
  181. RRD_ID_LENGTH_MAX);
  182. char dimension_name[RRD_ID_LENGTH_MAX + 1];
  183. exporting_name_copy(
  184. dimension_name,
  185. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  186. RRD_ID_LENGTH_MAX);
  187. time_t last_t;
  188. NETDATA_DOUBLE value = exporting_calculate_value_from_stored_data(instance, rd, &last_t);
  189. if(isnan(value))
  190. return 0;
  191. buffer_sprintf(
  192. instance->buffer,
  193. "put %s.%s.%s %llu " NETDATA_DOUBLE_FORMAT " host=%s%s%s%s\n",
  194. instance->config.prefix,
  195. chart_name,
  196. dimension_name,
  197. (unsigned long long)last_t,
  198. value,
  199. (host == localhost) ? instance->config.hostname : rrdhost_hostname(host),
  200. (host->tags) ? " " : "",
  201. (host->tags) ? rrdhost_tags(host) : "",
  202. (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : "");
  203. return 0;
  204. }
  205. /**
  206. * Prepare HTTP header
  207. *
  208. * @param instance an instance data structure.
  209. * @return Returns 0 on success, 1 on failure.
  210. */
  211. void opentsdb_http_prepare_header(struct instance *instance)
  212. {
  213. struct simple_connector_data *simple_connector_data = instance->connector_specific_data;
  214. buffer_sprintf(
  215. simple_connector_data->last_buffer->header,
  216. "POST /api/put HTTP/1.1\r\n"
  217. "Host: %s\r\n"
  218. "%s"
  219. "Content-Type: application/json\r\n"
  220. "Content-Length: %lu\r\n"
  221. "\r\n",
  222. instance->config.destination,
  223. simple_connector_data->auth_string ? simple_connector_data->auth_string : "",
  224. (unsigned long int) buffer_strlen(simple_connector_data->last_buffer->buffer));
  225. return;
  226. }
  227. /**
  228. * Format host labels for OpenTSDB HTTP connector
  229. *
  230. * @param instance an instance data structure.
  231. * @param host a data collecting host.
  232. * @return Always returns 0.
  233. */
  234. int format_host_labels_opentsdb_http(struct instance *instance, RRDHOST *host) {
  235. if (!instance->labels_buffer)
  236. instance->labels_buffer = buffer_create(1024, &netdata_buffers_statistics.buffers_exporters);
  237. if (unlikely(!sending_labels_configured(instance)))
  238. return 0;
  239. rrdlabels_to_buffer(host->rrdlabels, instance->labels_buffer, ",", ":", "\"", "",
  240. exporting_labels_filter_callback, instance,
  241. NULL, sanitize_opentsdb_label_value);
  242. return 0;
  243. }
  244. /**
  245. * Format dimension using collected data for OpenTSDB HTTP connector
  246. *
  247. * @param instance an instance data structure.
  248. * @param rd a dimension.
  249. * @return Always returns 0.
  250. */
  251. int format_dimension_collected_opentsdb_http(struct instance *instance, RRDDIM *rd)
  252. {
  253. RRDSET *st = rd->rrdset;
  254. RRDHOST *host = st->rrdhost;
  255. char chart_name[RRD_ID_LENGTH_MAX + 1];
  256. exporting_name_copy(
  257. chart_name,
  258. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st),
  259. RRD_ID_LENGTH_MAX);
  260. char dimension_name[RRD_ID_LENGTH_MAX + 1];
  261. exporting_name_copy(
  262. dimension_name,
  263. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  264. RRD_ID_LENGTH_MAX);
  265. if (buffer_strlen((BUFFER *)instance->buffer) > 2)
  266. buffer_strcat(instance->buffer, ",\n");
  267. buffer_sprintf(
  268. instance->buffer,
  269. "{"
  270. "\"metric\":\"%s.%s.%s\","
  271. "\"timestamp\":%llu,"
  272. "\"value\":"COLLECTED_NUMBER_FORMAT","
  273. "\"tags\":{"
  274. "\"host\":\"%s%s%s\"%s"
  275. "}"
  276. "}",
  277. instance->config.prefix,
  278. chart_name,
  279. dimension_name,
  280. (unsigned long long)rd->collector.last_collected_time.tv_sec,
  281. rd->collector.last_collected_value,
  282. (host == localhost) ? instance->config.hostname : rrdhost_hostname(host),
  283. (host->tags) ? " " : "",
  284. (host->tags) ? rrdhost_tags(host) : "",
  285. instance->labels_buffer ? buffer_tostring(instance->labels_buffer) : "");
  286. return 0;
  287. }
  288. /**
  289. * Format dimension using a calculated value from stored data for OpenTSDB HTTP connector
  290. *
  291. * @param instance an instance data structure.
  292. * @param rd a dimension.
  293. * @return Always returns 0.
  294. */
  295. int format_dimension_stored_opentsdb_http(struct instance *instance, RRDDIM *rd)
  296. {
  297. RRDSET *st = rd->rrdset;
  298. RRDHOST *host = st->rrdhost;
  299. char chart_name[RRD_ID_LENGTH_MAX + 1];
  300. exporting_name_copy(
  301. chart_name,
  302. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st),
  303. RRD_ID_LENGTH_MAX);
  304. char dimension_name[RRD_ID_LENGTH_MAX + 1];
  305. exporting_name_copy(
  306. dimension_name,
  307. (instance->config.options & EXPORTING_OPTION_SEND_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  308. RRD_ID_LENGTH_MAX);
  309. time_t last_t;
  310. NETDATA_DOUBLE value = exporting_calculate_value_from_stored_data(instance, rd, &last_t);
  311. if(isnan(value))
  312. return 0;
  313. if (buffer_strlen((BUFFER *)instance->buffer) > 2)
  314. buffer_strcat(instance->buffer, ",\n");
  315. buffer_sprintf(
  316. instance->buffer,
  317. "{"
  318. "\"metric\":\"%s.%s.%s\","
  319. "\"timestamp\":%llu,"
  320. "\"value\":" NETDATA_DOUBLE_FORMAT ","
  321. "\"tags\":{"
  322. "\"host\":\"%s%s%s\"%s"
  323. "}"
  324. "}",
  325. instance->config.prefix,
  326. chart_name,
  327. dimension_name,
  328. (unsigned long long)last_t,
  329. value,
  330. (host == localhost) ? instance->config.hostname : rrdhost_hostname(host),
  331. (host->tags) ? " " : "",
  332. (host->tags) ? rrdhost_tags(host) : "",
  333. instance->labels_buffer ? buffer_tostring(instance->labels_buffer) : "");
  334. return 0;
  335. }