send_data.c 13 KB

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