send_data.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "exporting_engine.h"
  3. #ifdef ENABLE_HTTPS
  4. /**
  5. * Check if TLS is enabled in the configuration
  6. *
  7. * @param type buffer with response data.
  8. * @param options an instance data structure.
  9. * @return Returns 1 if TLS should be enabled, 0 otherwise.
  10. */
  11. static int exporting_tls_is_enabled(EXPORTING_CONNECTOR_TYPE type __maybe_unused, EXPORTING_OPTIONS options __maybe_unused)
  12. {
  13. return (type == EXPORTING_CONNECTOR_TYPE_GRAPHITE_HTTP ||
  14. type == EXPORTING_CONNECTOR_TYPE_JSON_HTTP ||
  15. type == EXPORTING_CONNECTOR_TYPE_OPENTSDB_HTTP ||
  16. type == EXPORTING_CONNECTOR_TYPE_PROMETHEUS_REMOTE_WRITE) &&
  17. options & EXPORTING_OPTION_USE_TLS;
  18. }
  19. #endif
  20. /**
  21. * Discard response
  22. *
  23. * Discards a response received by an exporting connector instance after logging a sample of it to error.log
  24. *
  25. * @param buffer buffer with response data.
  26. * @param instance an instance data structure.
  27. * @return Always returns 0.
  28. */
  29. int exporting_discard_response(BUFFER *buffer, struct instance *instance) {
  30. #if NETDATA_INTERNAL_CHECKS
  31. char sample[1024];
  32. const char *s = buffer_tostring(buffer);
  33. char *d = sample, *e = &sample[sizeof(sample) - 1];
  34. for(; *s && d < e ;s++) {
  35. char c = *s;
  36. if(unlikely(!isprint(c))) c = ' ';
  37. *d++ = c;
  38. }
  39. *d = '\0';
  40. netdata_log_debug(D_EXPORTING,
  41. "EXPORTING: received %zu bytes from %s connector instance. Ignoring them. Sample: '%s'",
  42. buffer_strlen(buffer),
  43. instance->config.name,
  44. sample);
  45. #else
  46. UNUSED(instance);
  47. #endif /* NETDATA_INTERNAL_CHECKS */
  48. buffer_flush(buffer);
  49. return 0;
  50. }
  51. /**
  52. * Receive response
  53. *
  54. * @param sock communication socket.
  55. * @param instance an instance data structure.
  56. */
  57. void simple_connector_receive_response(int *sock, struct instance *instance)
  58. {
  59. static BUFFER *response = NULL;
  60. if (!response)
  61. response = buffer_create(4096, &netdata_buffers_statistics.buffers_exporters);
  62. struct stats *stats = &instance->stats;
  63. #ifdef ENABLE_HTTPS
  64. uint32_t options = (uint32_t)instance->config.options;
  65. struct simple_connector_data *connector_specific_data = instance->connector_specific_data;
  66. if (options & EXPORTING_OPTION_USE_TLS)
  67. ERR_clear_error();
  68. #endif
  69. errno = 0;
  70. // loop through to collect all data
  71. while (*sock != -1 && errno != EWOULDBLOCK) {
  72. ssize_t r;
  73. #ifdef ENABLE_HTTPS
  74. if (SSL_connection(&connector_specific_data->ssl))
  75. r = netdata_ssl_read(&connector_specific_data->ssl, &response->buffer[response->len],
  76. (int) (response->size - response->len));
  77. else
  78. r = recv(*sock, &response->buffer[response->len], response->size - response->len, MSG_DONTWAIT);
  79. #else
  80. r = recv(*sock, &response->buffer[response->len], response->size - response->len, MSG_DONTWAIT);
  81. #endif
  82. if (likely(r > 0)) {
  83. // we received some data
  84. response->len += r;
  85. stats->received_bytes += r;
  86. stats->receptions++;
  87. }
  88. else if (r == 0) {
  89. netdata_log_error("EXPORTING: '%s' closed the socket", instance->config.destination);
  90. close(*sock);
  91. *sock = -1;
  92. }
  93. else {
  94. // failed to receive data
  95. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  96. netdata_log_error("EXPORTING: cannot receive data from '%s'.", instance->config.destination);
  97. close(*sock);
  98. *sock = -1;
  99. }
  100. }
  101. #ifdef UNIT_TESTING
  102. break;
  103. #endif
  104. }
  105. // if we received data, process them
  106. if (buffer_strlen(response))
  107. instance->check_response(response, instance);
  108. }
  109. /**
  110. * Send buffer to a server
  111. *
  112. * @param sock communication socket.
  113. * @param failures the number of communication failures.
  114. * @param instance an instance data structure.
  115. */
  116. void simple_connector_send_buffer(
  117. int *sock, int *failures, struct instance *instance, BUFFER *header, BUFFER *buffer, size_t buffered_metrics)
  118. {
  119. int flags = 0;
  120. #ifdef MSG_NOSIGNAL
  121. flags += MSG_NOSIGNAL;
  122. #endif
  123. #ifdef ENABLE_HTTPS
  124. uint32_t options = (uint32_t)instance->config.options;
  125. struct simple_connector_data *connector_specific_data = instance->connector_specific_data;
  126. if (options & EXPORTING_OPTION_USE_TLS)
  127. ERR_clear_error();
  128. #endif
  129. struct stats *stats = &instance->stats;
  130. ssize_t header_sent_bytes = 0;
  131. ssize_t buffer_sent_bytes = 0;
  132. size_t header_len = buffer_strlen(header);
  133. size_t buffer_len = buffer_strlen(buffer);
  134. #ifdef ENABLE_HTTPS
  135. if (SSL_connection(&connector_specific_data->ssl)) {
  136. if (header_len)
  137. header_sent_bytes = netdata_ssl_write(&connector_specific_data->ssl, buffer_tostring(header), header_len);
  138. if ((size_t)header_sent_bytes == header_len)
  139. buffer_sent_bytes = netdata_ssl_write(&connector_specific_data->ssl, buffer_tostring(buffer), buffer_len);
  140. }
  141. else {
  142. if (header_len)
  143. header_sent_bytes = send(*sock, buffer_tostring(header), header_len, flags);
  144. if ((size_t)header_sent_bytes == header_len)
  145. buffer_sent_bytes = send(*sock, buffer_tostring(buffer), buffer_len, flags);
  146. }
  147. #else
  148. if (header_len)
  149. header_sent_bytes = send(*sock, buffer_tostring(header), header_len, flags);
  150. if ((size_t)header_sent_bytes == header_len)
  151. buffer_sent_bytes = send(*sock, buffer_tostring(buffer), buffer_len, flags);
  152. #endif
  153. if ((size_t)buffer_sent_bytes == buffer_len) {
  154. // we sent the data successfully
  155. stats->transmission_successes++;
  156. stats->sent_metrics += buffered_metrics;
  157. stats->sent_bytes += buffer_sent_bytes;
  158. // reset the failures count
  159. *failures = 0;
  160. // empty the buffer
  161. buffer_flush(buffer);
  162. } else {
  163. // oops! we couldn't send (all or some of the) data
  164. netdata_log_error(
  165. "EXPORTING: failed to write data to '%s'. Willing to write %zu bytes, wrote %zd bytes. Will re-connect.",
  166. instance->config.destination,
  167. buffer_len,
  168. buffer_sent_bytes);
  169. stats->transmission_failures++;
  170. if(buffer_sent_bytes != -1)
  171. stats->sent_bytes += buffer_sent_bytes;
  172. // increment the counter we check for data loss
  173. (*failures)++;
  174. // close the socket - we will re-open it next time
  175. close(*sock);
  176. *sock = -1;
  177. }
  178. }
  179. /**
  180. * Simple connector worker
  181. *
  182. * Runs in a separate thread for every instance.
  183. *
  184. * @param instance_p an instance data structure.
  185. */
  186. void simple_connector_worker(void *instance_p)
  187. {
  188. struct instance *instance = (struct instance*)instance_p;
  189. struct simple_connector_data *connector_specific_data = instance->connector_specific_data;
  190. #ifdef ENABLE_HTTPS
  191. uint32_t options = (uint32_t)instance->config.options;
  192. if (options & EXPORTING_OPTION_USE_TLS)
  193. ERR_clear_error();
  194. #endif
  195. struct simple_connector_config *connector_specific_config = instance->config.connector_specific_config;
  196. int sock = -1;
  197. struct timeval timeout = { .tv_sec = (instance->config.timeoutms * 1000) / 1000000,
  198. .tv_usec = (instance->config.timeoutms * 1000) % 1000000 };
  199. int failures = 0;
  200. while (!instance->engine->exit) {
  201. struct stats *stats = &instance->stats;
  202. int send_stats = 0;
  203. if (instance->data_is_ready)
  204. send_stats = 1;
  205. uv_mutex_lock(&instance->mutex);
  206. if (!connector_specific_data->first_buffer->used || failures) {
  207. while (!instance->data_is_ready)
  208. uv_cond_wait(&instance->cond_var, &instance->mutex);
  209. instance->data_is_ready = 0;
  210. send_stats = 1;
  211. }
  212. if (unlikely(instance->engine->exit)) {
  213. uv_mutex_unlock(&instance->mutex);
  214. break;
  215. }
  216. // ------------------------------------------------------------------------
  217. // detach buffer
  218. size_t buffered_metrics;
  219. if (!connector_specific_data->previous_buffer ||
  220. (connector_specific_data->previous_buffer == connector_specific_data->first_buffer &&
  221. connector_specific_data->first_buffer->used == 1)) {
  222. BUFFER *header, *buffer;
  223. header = connector_specific_data->first_buffer->header;
  224. buffer = connector_specific_data->first_buffer->buffer;
  225. connector_specific_data->buffered_metrics = connector_specific_data->first_buffer->buffered_metrics;
  226. connector_specific_data->buffered_bytes = connector_specific_data->first_buffer->buffered_bytes;
  227. buffered_metrics = connector_specific_data->buffered_metrics;
  228. buffer_flush(connector_specific_data->header);
  229. connector_specific_data->first_buffer->header = connector_specific_data->header;
  230. connector_specific_data->header = header;
  231. buffer_flush(connector_specific_data->buffer);
  232. connector_specific_data->first_buffer->buffer = connector_specific_data->buffer;
  233. connector_specific_data->buffer = buffer;
  234. } else {
  235. buffered_metrics = connector_specific_data->buffered_metrics;
  236. }
  237. uv_mutex_unlock(&instance->mutex);
  238. // ------------------------------------------------------------------------
  239. // if we are connected, receive a response, without blocking
  240. if (likely(sock != -1))
  241. simple_connector_receive_response(&sock, instance);
  242. // ------------------------------------------------------------------------
  243. // if we are not connected, connect to a data collecting server
  244. if (unlikely(sock == -1)) {
  245. size_t reconnects = 0;
  246. sock = connect_to_one_of_urls(
  247. instance->config.destination,
  248. connector_specific_config->default_port,
  249. &timeout,
  250. &reconnects,
  251. connector_specific_data->connected_to,
  252. CONNECTED_TO_MAX);
  253. #ifdef ENABLE_HTTPS
  254. if (exporting_tls_is_enabled(instance->config.type, options) && sock != -1) {
  255. if (netdata_ssl_exporting_ctx) {
  256. if (sock_delnonblock(sock) < 0)
  257. netdata_log_error("Exporting cannot remove the non-blocking flag from socket %d", sock);
  258. if(netdata_ssl_open(&connector_specific_data->ssl, netdata_ssl_exporting_ctx, sock)) {
  259. if(netdata_ssl_connect(&connector_specific_data->ssl)) {
  260. netdata_log_info("Exporting established a SSL connection.");
  261. struct timeval tv;
  262. tv.tv_sec = timeout.tv_sec / 4;
  263. tv.tv_usec = 0;
  264. if (!tv.tv_sec)
  265. tv.tv_sec = 2;
  266. if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv)))
  267. netdata_log_error("Cannot set timeout to socket %d, this can block communication", sock);
  268. }
  269. }
  270. }
  271. }
  272. #endif
  273. stats->reconnects += reconnects;
  274. }
  275. if (unlikely(instance->engine->exit))
  276. break;
  277. // ------------------------------------------------------------------------
  278. // if we are connected, send our buffer to the data collecting server
  279. failures = 0;
  280. if (likely(sock != -1)) {
  281. simple_connector_send_buffer(
  282. &sock,
  283. &failures,
  284. instance,
  285. connector_specific_data->header,
  286. connector_specific_data->buffer,
  287. buffered_metrics);
  288. } else {
  289. netdata_log_error("EXPORTING: failed to update '%s'", instance->config.destination);
  290. stats->transmission_failures++;
  291. // increment the counter we check for data loss
  292. failures++;
  293. }
  294. if (!failures) {
  295. connector_specific_data->first_buffer->buffered_metrics =
  296. connector_specific_data->first_buffer->buffered_bytes = connector_specific_data->first_buffer->used = 0;
  297. connector_specific_data->first_buffer = connector_specific_data->first_buffer->next;
  298. }
  299. if (unlikely(instance->engine->exit))
  300. break;
  301. if (send_stats) {
  302. uv_mutex_lock(&instance->mutex);
  303. stats->buffered_metrics = connector_specific_data->total_buffered_metrics;
  304. send_internal_metrics(instance);
  305. stats->buffered_metrics = 0;
  306. // reset the internal monitoring chart counters
  307. connector_specific_data->total_buffered_metrics =
  308. stats->buffered_bytes =
  309. stats->receptions =
  310. stats->received_bytes =
  311. stats->sent_metrics =
  312. stats->sent_bytes =
  313. stats->transmission_successes =
  314. stats->transmission_failures =
  315. stats->reconnects =
  316. stats->data_lost_events =
  317. stats->lost_metrics =
  318. stats->lost_bytes = 0;
  319. uv_mutex_unlock(&instance->mutex);
  320. }
  321. #ifdef UNIT_TESTING
  322. return;
  323. #endif
  324. }
  325. #if ENABLE_PROMETHEUS_REMOTE_WRITE
  326. if (instance->config.type == EXPORTING_CONNECTOR_TYPE_PROMETHEUS_REMOTE_WRITE)
  327. clean_prometheus_remote_write(instance);
  328. #endif
  329. simple_connector_cleanup(instance);
  330. }