send_data.c 13 KB

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