send_data.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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);
  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 (exporting_tls_is_enabled(instance->config.type, options) &&
  74. connector_specific_data->conn &&
  75. connector_specific_data->flags == NETDATA_SSL_HANDSHAKE_COMPLETE) {
  76. r = (ssize_t)SSL_read(connector_specific_data->conn,
  77. &response->buffer[response->len],
  78. (int) (response->size - response->len));
  79. if (likely(r > 0)) {
  80. // we received some data
  81. response->len += r;
  82. stats->received_bytes += r;
  83. stats->receptions++;
  84. continue;
  85. } else {
  86. int sslerrno = SSL_get_error(connector_specific_data->conn, (int) r);
  87. u_long sslerr = ERR_get_error();
  88. char buf[256];
  89. switch (sslerrno) {
  90. case SSL_ERROR_WANT_READ:
  91. case SSL_ERROR_WANT_WRITE:
  92. goto endloop;
  93. default:
  94. ERR_error_string_n(sslerr, buf, sizeof(buf));
  95. error("SSL error (%s)",
  96. ERR_error_string((long)SSL_get_error(connector_specific_data->conn, (int)r), NULL));
  97. goto endloop;
  98. }
  99. }
  100. } else {
  101. r = recv(*sock, &response->buffer[response->len], response->size - response->len, MSG_DONTWAIT);
  102. }
  103. #else
  104. r = recv(*sock, &response->buffer[response->len], response->size - response->len, MSG_DONTWAIT);
  105. #endif
  106. if (likely(r > 0)) {
  107. // we received some data
  108. response->len += r;
  109. stats->received_bytes += r;
  110. stats->receptions++;
  111. } else if (r == 0) {
  112. error("EXPORTING: '%s' closed the socket", instance->config.destination);
  113. close(*sock);
  114. *sock = -1;
  115. } else {
  116. // failed to receive data
  117. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  118. error("EXPORTING: cannot receive data from '%s'.", instance->config.destination);
  119. }
  120. }
  121. #ifdef UNIT_TESTING
  122. break;
  123. #endif
  124. }
  125. #ifdef ENABLE_HTTPS
  126. endloop:
  127. #endif
  128. // if we received data, process them
  129. if (buffer_strlen(response))
  130. instance->check_response(response, instance);
  131. }
  132. /**
  133. * Send buffer to a server
  134. *
  135. * @param sock communication socket.
  136. * @param failures the number of communication failures.
  137. * @param instance an instance data structure.
  138. */
  139. void simple_connector_send_buffer(
  140. int *sock, int *failures, struct instance *instance, BUFFER *header, BUFFER *buffer, size_t buffered_metrics)
  141. {
  142. int flags = 0;
  143. #ifdef MSG_NOSIGNAL
  144. flags += MSG_NOSIGNAL;
  145. #endif
  146. #ifdef ENABLE_HTTPS
  147. uint32_t options = (uint32_t)instance->config.options;
  148. struct simple_connector_data *connector_specific_data = instance->connector_specific_data;
  149. if (options & EXPORTING_OPTION_USE_TLS)
  150. ERR_clear_error();
  151. #endif
  152. struct stats *stats = &instance->stats;
  153. ssize_t header_sent_bytes = 0;
  154. ssize_t buffer_sent_bytes = 0;
  155. size_t header_len = buffer_strlen(header);
  156. size_t buffer_len = buffer_strlen(buffer);
  157. #ifdef ENABLE_HTTPS
  158. if (exporting_tls_is_enabled(instance->config.type, options) &&
  159. connector_specific_data->conn &&
  160. connector_specific_data->flags == NETDATA_SSL_HANDSHAKE_COMPLETE) {
  161. if (header_len)
  162. header_sent_bytes = (ssize_t)SSL_write(connector_specific_data->conn, buffer_tostring(header), header_len);
  163. if ((size_t)header_sent_bytes == header_len)
  164. buffer_sent_bytes = (ssize_t)SSL_write(connector_specific_data->conn, buffer_tostring(buffer), buffer_len);
  165. } else {
  166. if (header_len)
  167. header_sent_bytes = send(*sock, buffer_tostring(header), header_len, flags);
  168. if ((size_t)header_sent_bytes == header_len)
  169. buffer_sent_bytes = send(*sock, buffer_tostring(buffer), buffer_len, flags);
  170. }
  171. #else
  172. if (header_len)
  173. header_sent_bytes = send(*sock, buffer_tostring(header), header_len, flags);
  174. if ((size_t)header_sent_bytes == header_len)
  175. buffer_sent_bytes = send(*sock, buffer_tostring(buffer), buffer_len, flags);
  176. #endif
  177. if ((size_t)buffer_sent_bytes == buffer_len) {
  178. // we sent the data successfully
  179. stats->transmission_successes++;
  180. stats->sent_metrics += buffered_metrics;
  181. stats->sent_bytes += buffer_sent_bytes;
  182. // reset the failures count
  183. *failures = 0;
  184. // empty the buffer
  185. buffer_flush(buffer);
  186. } else {
  187. // oops! we couldn't send (all or some of the) data
  188. error(
  189. "EXPORTING: failed to write data to '%s'. Willing to write %zu bytes, wrote %zd bytes. Will re-connect.",
  190. instance->config.destination,
  191. buffer_len,
  192. buffer_sent_bytes);
  193. stats->transmission_failures++;
  194. if(buffer_sent_bytes != -1)
  195. stats->sent_bytes += buffer_sent_bytes;
  196. // increment the counter we check for data loss
  197. (*failures)++;
  198. // close the socket - we will re-open it next time
  199. close(*sock);
  200. *sock = -1;
  201. }
  202. }
  203. /**
  204. * Simple connector worker
  205. *
  206. * Runs in a separate thread for every instance.
  207. *
  208. * @param instance_p an instance data structure.
  209. */
  210. void simple_connector_worker(void *instance_p)
  211. {
  212. struct instance *instance = (struct instance*)instance_p;
  213. struct simple_connector_data *connector_specific_data = instance->connector_specific_data;
  214. #ifdef ENABLE_HTTPS
  215. uint32_t options = (uint32_t)instance->config.options;
  216. if (options & EXPORTING_OPTION_USE_TLS)
  217. ERR_clear_error();
  218. #endif
  219. struct simple_connector_config *connector_specific_config = instance->config.connector_specific_config;
  220. int sock = -1;
  221. struct timeval timeout = { .tv_sec = (instance->config.timeoutms * 1000) / 1000000,
  222. .tv_usec = (instance->config.timeoutms * 1000) % 1000000 };
  223. int failures = 0;
  224. while (!instance->engine->exit) {
  225. struct stats *stats = &instance->stats;
  226. int send_stats = 0;
  227. if (instance->data_is_ready)
  228. send_stats = 1;
  229. uv_mutex_lock(&instance->mutex);
  230. if (!connector_specific_data->first_buffer->used || failures) {
  231. while (!instance->data_is_ready)
  232. uv_cond_wait(&instance->cond_var, &instance->mutex);
  233. instance->data_is_ready = 0;
  234. send_stats = 1;
  235. }
  236. if (unlikely(instance->engine->exit)) {
  237. uv_mutex_unlock(&instance->mutex);
  238. break;
  239. }
  240. // ------------------------------------------------------------------------
  241. // detach buffer
  242. size_t buffered_metrics;
  243. if (!connector_specific_data->previous_buffer ||
  244. (connector_specific_data->previous_buffer == connector_specific_data->first_buffer &&
  245. connector_specific_data->first_buffer->used == 1)) {
  246. BUFFER *header, *buffer;
  247. header = connector_specific_data->first_buffer->header;
  248. buffer = connector_specific_data->first_buffer->buffer;
  249. connector_specific_data->buffered_metrics = connector_specific_data->first_buffer->buffered_metrics;
  250. connector_specific_data->buffered_bytes = connector_specific_data->first_buffer->buffered_bytes;
  251. buffered_metrics = connector_specific_data->buffered_metrics;
  252. buffer_flush(connector_specific_data->header);
  253. connector_specific_data->first_buffer->header = connector_specific_data->header;
  254. connector_specific_data->header = header;
  255. buffer_flush(connector_specific_data->buffer);
  256. connector_specific_data->first_buffer->buffer = connector_specific_data->buffer;
  257. connector_specific_data->buffer = buffer;
  258. } else {
  259. buffered_metrics = connector_specific_data->buffered_metrics;
  260. }
  261. uv_mutex_unlock(&instance->mutex);
  262. // ------------------------------------------------------------------------
  263. // if we are connected, receive a response, without blocking
  264. if (likely(sock != -1))
  265. simple_connector_receive_response(&sock, instance);
  266. // ------------------------------------------------------------------------
  267. // if we are not connected, connect to a data collecting server
  268. if (unlikely(sock == -1)) {
  269. size_t reconnects = 0;
  270. sock = connect_to_one_of(
  271. instance->config.destination,
  272. connector_specific_config->default_port,
  273. &timeout,
  274. &reconnects,
  275. connector_specific_data->connected_to,
  276. CONNECTED_TO_MAX);
  277. #ifdef ENABLE_HTTPS
  278. if (exporting_tls_is_enabled(instance->config.type, options) && sock != -1) {
  279. if (netdata_exporting_ctx) {
  280. if (sock_delnonblock(sock) < 0)
  281. error("Exporting cannot remove the non-blocking flag from socket %d", sock);
  282. if (connector_specific_data->conn == NULL) {
  283. connector_specific_data->conn = SSL_new(netdata_exporting_ctx);
  284. if (connector_specific_data->conn == NULL) {
  285. error("Failed to allocate SSL structure to socket %d.", sock);
  286. connector_specific_data->flags = NETDATA_SSL_NO_HANDSHAKE;
  287. }
  288. } else {
  289. SSL_clear(connector_specific_data->conn);
  290. }
  291. if (connector_specific_data->conn) {
  292. if (SSL_set_fd(connector_specific_data->conn, sock) != 1) {
  293. error("Failed to set the socket to the SSL on socket fd %d.", sock);
  294. connector_specific_data->flags = NETDATA_SSL_NO_HANDSHAKE;
  295. } else {
  296. connector_specific_data->flags = NETDATA_SSL_HANDSHAKE_COMPLETE;
  297. SSL_set_connect_state(connector_specific_data->conn);
  298. int err = SSL_connect(connector_specific_data->conn);
  299. if (err != 1) {
  300. err = SSL_get_error(connector_specific_data->conn, err);
  301. error(
  302. "SSL cannot connect with the server: %s ",
  303. ERR_error_string((long)SSL_get_error(connector_specific_data->conn, err), NULL));
  304. connector_specific_data->flags = NETDATA_SSL_NO_HANDSHAKE;
  305. } else {
  306. info("Exporting established a SSL connection.");
  307. struct timeval tv;
  308. tv.tv_sec = timeout.tv_sec / 4;
  309. tv.tv_usec = 0;
  310. if (!tv.tv_sec)
  311. tv.tv_sec = 2;
  312. if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv)))
  313. error("Cannot set timeout to socket %d, this can block communication", sock);
  314. }
  315. }
  316. }
  317. }
  318. }
  319. #endif
  320. stats->reconnects += reconnects;
  321. }
  322. if (unlikely(instance->engine->exit))
  323. break;
  324. // ------------------------------------------------------------------------
  325. // if we are connected, send our buffer to the data collecting server
  326. failures = 0;
  327. if (likely(sock != -1)) {
  328. simple_connector_send_buffer(
  329. &sock,
  330. &failures,
  331. instance,
  332. connector_specific_data->header,
  333. connector_specific_data->buffer,
  334. buffered_metrics);
  335. } else {
  336. error("EXPORTING: failed to update '%s'", instance->config.destination);
  337. stats->transmission_failures++;
  338. // increment the counter we check for data loss
  339. failures++;
  340. }
  341. if (!failures) {
  342. connector_specific_data->first_buffer->buffered_metrics =
  343. connector_specific_data->first_buffer->buffered_bytes = connector_specific_data->first_buffer->used = 0;
  344. connector_specific_data->first_buffer = connector_specific_data->first_buffer->next;
  345. }
  346. if (unlikely(instance->engine->exit))
  347. break;
  348. if (send_stats) {
  349. uv_mutex_lock(&instance->mutex);
  350. stats->buffered_metrics = connector_specific_data->total_buffered_metrics;
  351. send_internal_metrics(instance);
  352. stats->buffered_metrics = 0;
  353. // reset the internal monitoring chart counters
  354. connector_specific_data->total_buffered_metrics =
  355. stats->buffered_bytes =
  356. stats->receptions =
  357. stats->received_bytes =
  358. stats->sent_metrics =
  359. stats->sent_bytes =
  360. stats->transmission_successes =
  361. stats->transmission_failures =
  362. stats->reconnects =
  363. stats->data_lost_events =
  364. stats->lost_metrics =
  365. stats->lost_bytes = 0;
  366. uv_mutex_unlock(&instance->mutex);
  367. }
  368. #ifdef UNIT_TESTING
  369. return;
  370. #endif
  371. }
  372. #if ENABLE_PROMETHEUS_REMOTE_WRITE
  373. if (instance->config.type == EXPORTING_CONNECTOR_TYPE_PROMETHEUS_REMOTE_WRITE)
  374. clean_prometheus_remote_write(instance);
  375. #endif
  376. simple_connector_cleanup(instance);
  377. }