opentsdb.c 13 KB

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