receiver.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrdpush.h"
  3. extern struct config stream_config;
  4. void destroy_receiver_state(struct receiver_state *rpt) {
  5. freez(rpt->key);
  6. freez(rpt->hostname);
  7. freez(rpt->registry_hostname);
  8. freez(rpt->machine_guid);
  9. freez(rpt->os);
  10. freez(rpt->timezone);
  11. freez(rpt->abbrev_timezone);
  12. freez(rpt->tags);
  13. freez(rpt->client_ip);
  14. freez(rpt->client_port);
  15. freez(rpt->program_name);
  16. freez(rpt->program_version);
  17. #ifdef ENABLE_HTTPS
  18. if(rpt->ssl.conn){
  19. SSL_free(rpt->ssl.conn);
  20. }
  21. #endif
  22. #ifdef ENABLE_COMPRESSION
  23. if (rpt->decompressor)
  24. rpt->decompressor->destroy(&rpt->decompressor);
  25. #endif
  26. freez(rpt);
  27. }
  28. static void rrdpush_receiver_thread_cleanup(void *ptr) {
  29. worker_unregister();
  30. static __thread int executed = 0;
  31. if(!executed) {
  32. executed = 1;
  33. struct receiver_state *rpt = (struct receiver_state *) ptr;
  34. // If the shutdown sequence has started, and this receiver is still attached to the host then we cannot touch
  35. // the host pointer as it is unpredictable when the RRDHOST is deleted. Do the cleanup from rrdhost_free().
  36. if (netdata_exit && rpt->host) {
  37. rpt->exited = 1;
  38. return;
  39. }
  40. // Make sure that we detach this thread and don't kill a freshly arriving receiver
  41. if (!netdata_exit && rpt->host) {
  42. netdata_mutex_lock(&rpt->host->receiver_lock);
  43. if (rpt->host->receiver == rpt)
  44. rpt->host->receiver = NULL;
  45. netdata_mutex_unlock(&rpt->host->receiver_lock);
  46. }
  47. info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
  48. destroy_receiver_state(rpt);
  49. }
  50. }
  51. #include "collectors/plugins.d/pluginsd_parser.h"
  52. PARSER_RC streaming_timestamp(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  53. {
  54. UNUSED(plugins_action);
  55. char *remote_time_txt = words[1];
  56. time_t remote_time = 0;
  57. RRDHOST *host = ((PARSER_USER_OBJECT *)user)->host;
  58. struct plugind *cd = ((PARSER_USER_OBJECT *)user)->cd;
  59. if (cd->version < VERSION_GAP_FILLING ) {
  60. error("STREAM %s from %s: Child negotiated version %u but sent TIMESTAMP!", host->hostname, cd->cmd,
  61. cd->version);
  62. return PARSER_RC_OK; // Ignore error and continue stream
  63. }
  64. if (remote_time_txt && *remote_time_txt) {
  65. remote_time = str2ull(remote_time_txt);
  66. time_t now = now_realtime_sec(), prev = rrdhost_last_entry_t(host);
  67. time_t gap = 0;
  68. if (prev == 0)
  69. info(
  70. "STREAM %s from %s: Initial connection (no gap to check), "
  71. "remote=%"PRId64" local=%"PRId64" slew=%"PRId64"",
  72. host->hostname,
  73. cd->cmd,
  74. (int64_t)remote_time,
  75. (int64_t)now,
  76. (int64_t)now - remote_time);
  77. else {
  78. gap = now - prev;
  79. info(
  80. "STREAM %s from %s: Checking for gaps... "
  81. "remote=%"PRId64" local=%"PRId64"..%"PRId64" slew=%"PRId64" %"PRId64"-sec gap",
  82. host->hostname,
  83. cd->cmd,
  84. (int64_t)remote_time,
  85. (int64_t)prev,
  86. (int64_t)now,
  87. (int64_t)(remote_time - now),
  88. (int64_t)gap);
  89. }
  90. char message[128];
  91. sprintf(
  92. message,
  93. "REPLICATE %"PRId64" %"PRId64"\n",
  94. (int64_t)(remote_time - gap),
  95. (int64_t)remote_time);
  96. int ret;
  97. #ifdef ENABLE_HTTPS
  98. SSL *conn = host->stream_ssl.conn ;
  99. if(conn && !host->stream_ssl.flags) {
  100. ret = SSL_write(conn, message, strlen(message));
  101. } else {
  102. ret = send(host->receiver->fd, message, strlen(message), MSG_DONTWAIT);
  103. }
  104. #else
  105. ret = send(host->receiver->fd, message, strlen(message), MSG_DONTWAIT);
  106. #endif
  107. if (ret != (int)strlen(message))
  108. error("Failed to send initial timestamp - gaps may appear in charts");
  109. return PARSER_RC_OK;
  110. }
  111. return PARSER_RC_ERROR;
  112. }
  113. #define CLAIMED_ID_MIN_WORDS 3
  114. PARSER_RC streaming_claimed_id(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  115. {
  116. UNUSED(plugins_action);
  117. int i;
  118. uuid_t uuid;
  119. RRDHOST *host = ((PARSER_USER_OBJECT *)user)->host;
  120. for (i = 0; words[i]; i++) ;
  121. if (i != CLAIMED_ID_MIN_WORDS) {
  122. error("Command CLAIMED_ID came malformed %d parameters are expected but %d received", CLAIMED_ID_MIN_WORDS - 1, i - 1);
  123. return PARSER_RC_ERROR;
  124. }
  125. // We don't need the parsed UUID
  126. // just do it to check the format
  127. if(uuid_parse(words[1], uuid)) {
  128. error("1st parameter (host GUID) to CLAIMED_ID command is not valid GUID. Received: \"%s\".", words[1]);
  129. return PARSER_RC_ERROR;
  130. }
  131. if(uuid_parse(words[2], uuid) && strcmp(words[2], "NULL")) {
  132. error("2nd parameter (Claim ID) to CLAIMED_ID command is not valid GUID. Received: \"%s\".", words[2]);
  133. return PARSER_RC_ERROR;
  134. }
  135. if(strcmp(words[1], host->machine_guid)) {
  136. error("Claim ID is for host \"%s\" but it came over connection for \"%s\"", words[1], host->machine_guid);
  137. return PARSER_RC_OK; //the message is OK problem must be somewhere else
  138. }
  139. rrdhost_aclk_state_lock(host);
  140. if (host->aclk_state.claimed_id)
  141. freez(host->aclk_state.claimed_id);
  142. host->aclk_state.claimed_id = strcmp(words[2], "NULL") ? strdupz(words[2]) : NULL;
  143. store_claim_id(&host->host_uuid, host->aclk_state.claimed_id ? &uuid : NULL);
  144. rrdhost_aclk_state_unlock(host);
  145. rrdpush_claimed_id(host);
  146. return PARSER_RC_OK;
  147. }
  148. #ifndef ENABLE_COMPRESSION
  149. /* The receiver socket is blocking, perform a single read into a buffer so that we can reassemble lines for parsing.
  150. */
  151. static int receiver_read(struct receiver_state *r, FILE *fp) {
  152. #ifdef ENABLE_HTTPS
  153. if (r->ssl.conn && !r->ssl.flags) {
  154. ERR_clear_error();
  155. int desired = sizeof(r->read_buffer) - r->read_len - 1;
  156. int ret = SSL_read(r->ssl.conn, r->read_buffer + r->read_len, desired);
  157. if (ret > 0 ) {
  158. r->read_len += ret;
  159. return 0;
  160. }
  161. // Don't treat SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE differently on blocking socket
  162. u_long err;
  163. char buf[256];
  164. while ((err = ERR_get_error()) != 0) {
  165. ERR_error_string_n(err, buf, sizeof(buf));
  166. error("STREAM %s [receive from %s] ssl error: %s", r->hostname, r->client_ip, buf);
  167. }
  168. return 1;
  169. }
  170. #endif
  171. if (!fgets(r->read_buffer, sizeof(r->read_buffer), fp))
  172. return 1;
  173. r->read_len = strlen(r->read_buffer);
  174. return 0;
  175. }
  176. #else
  177. /*
  178. * The receiver socket is blocking, perform a single read into a buffer so that we can reassemble lines for parsing.
  179. * if SSL encryption is on, then use SSL API for reading stream data.
  180. * Use line oriented fgets() in buffer from receiver_state is provided.
  181. * In other cases use fread to read binary data from socket.
  182. * Return zero on success and the number of bytes were read using pointer in the last argument.
  183. */
  184. static int read_stream(struct receiver_state *r, FILE *fp, char* buffer, size_t size, int* ret) {
  185. if (!ret)
  186. return 1;
  187. *ret = 0;
  188. #ifdef ENABLE_HTTPS
  189. if (r->ssl.conn && !r->ssl.flags) {
  190. ERR_clear_error();
  191. if (buffer != r->read_buffer + r->read_len) {
  192. *ret = SSL_read(r->ssl.conn, buffer, size);
  193. if (*ret > 0 )
  194. return 0;
  195. } else {
  196. // we need to receive data with LF to parse compression header
  197. size_t ofs = 0;
  198. int res = 0;
  199. while (ofs < size) {
  200. do {
  201. res = SSL_read(r->ssl.conn, buffer + ofs, 1);
  202. } while (res == 0);
  203. if (res < 0)
  204. break;
  205. if (buffer[ofs] == '\n')
  206. break;
  207. ofs += res;
  208. }
  209. if (res > 0) {
  210. ofs += res;
  211. *ret = ofs;
  212. buffer[ofs] = 0;
  213. return 0;
  214. }
  215. }
  216. // Don't treat SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE differently on blocking socket
  217. u_long err;
  218. char buf[256];
  219. while ((err = ERR_get_error()) != 0) {
  220. ERR_error_string_n(err, buf, sizeof(buf));
  221. error("STREAM %s [receive from %s] ssl error: %s", r->hostname, r->client_ip, buf);
  222. }
  223. return 1;
  224. }
  225. #endif
  226. if (buffer != r->read_buffer + r->read_len) {
  227. // read to external buffer
  228. *ret = fread(buffer, 1, size, fp);
  229. if (!*ret)
  230. return 1;
  231. } else {
  232. if (!fgets(r->read_buffer, sizeof(r->read_buffer), fp))
  233. return 1;
  234. *ret = strlen(r->read_buffer);
  235. }
  236. return 0;
  237. }
  238. /*
  239. * Get the next line of data for parsing.
  240. * Return data from the decompressor buffer if available.
  241. * Otherwise read next line from the socket and check for compression header.
  242. * Return the line was read If no compression header was found.
  243. * Otherwise read the entire block of compressed data, decompress it
  244. * and return it in receiver_state buffer.
  245. * Return zero on success.
  246. */
  247. static int receiver_read(struct receiver_state *r, FILE *fp) {
  248. // check any decompressed data present
  249. if (r->decompressor &&
  250. r->decompressor->decompressed_bytes_in_buffer(r->decompressor)) {
  251. size_t available = sizeof(r->read_buffer) - r->read_len;
  252. if (available) {
  253. size_t len = r->decompressor->get(r->decompressor,
  254. r->read_buffer + r->read_len, available);
  255. if (!len)
  256. return 1;
  257. r->read_len += len;
  258. }
  259. return 0;
  260. }
  261. int ret = 0;
  262. if (read_stream(r, fp, r->read_buffer + r->read_len, sizeof(r->read_buffer) - r->read_len - 1, &ret))
  263. return 1;
  264. if (!is_compressed_data(r->read_buffer, ret)) {
  265. r->read_len += ret;
  266. return 0;
  267. }
  268. if (unlikely(!r->decompressor))
  269. r->decompressor = create_decompressor();
  270. size_t bytes_to_read = r->decompressor->start(r->decompressor,
  271. r->read_buffer, ret);
  272. // Read the entire block of compressed data because
  273. // we're unable to decompress incomplete block
  274. char compressed[bytes_to_read];
  275. do {
  276. if (read_stream(r, fp, compressed, bytes_to_read, &ret))
  277. return 1;
  278. // Send input data to decompressor
  279. if (ret)
  280. r->decompressor->put(r->decompressor, compressed, ret);
  281. bytes_to_read -= ret;
  282. } while (bytes_to_read > 0);
  283. // Decompress
  284. size_t bytes_to_parse = r->decompressor->decompress(r->decompressor);
  285. if (!bytes_to_parse)
  286. return 1;
  287. // Fill read buffer with decompressed data
  288. r->read_len = r->decompressor->get(r->decompressor,
  289. r->read_buffer, sizeof(r->read_buffer));
  290. return 0;
  291. }
  292. #endif
  293. /* Produce a full line if one exists, statefully return where we start next time.
  294. * When we hit the end of the buffer with a partial line move it to the beginning for the next fill.
  295. */
  296. static char *receiver_next_line(struct receiver_state *r, int *pos) {
  297. int start = *pos, scan = *pos;
  298. if (scan >= r->read_len) {
  299. r->read_len = 0;
  300. return NULL;
  301. }
  302. while (scan < r->read_len && r->read_buffer[scan] != '\n')
  303. scan++;
  304. if (scan < r->read_len && r->read_buffer[scan] == '\n') {
  305. *pos = scan+1;
  306. r->read_buffer[scan] = 0;
  307. return &r->read_buffer[start];
  308. }
  309. memmove(r->read_buffer, &r->read_buffer[start], r->read_len - start);
  310. r->read_len -= start;
  311. return NULL;
  312. }
  313. static void streaming_parser_thread_cleanup(void *ptr) {
  314. PARSER *parser = (PARSER *)ptr;
  315. parser_destroy(parser);
  316. }
  317. size_t streaming_parser(struct receiver_state *rpt, struct plugind *cd, FILE *fp) {
  318. size_t result;
  319. PARSER_USER_OBJECT user = {
  320. .enabled = cd->enabled,
  321. .host = rpt->host,
  322. .opaque = rpt,
  323. .cd = cd,
  324. .trust_durations = 1
  325. };
  326. PARSER *parser = parser_init(rpt->host, &user, fp, PARSER_INPUT_SPLIT);
  327. // this keeps the parser with its current value
  328. // so, parser needs to be allocated before pushing it
  329. netdata_thread_cleanup_push(streaming_parser_thread_cleanup, parser);
  330. parser_add_keyword(parser, "TIMESTAMP", streaming_timestamp);
  331. parser_add_keyword(parser, "CLAIMED_ID", streaming_claimed_id);
  332. parser->plugins_action->begin_action = &pluginsd_begin_action;
  333. parser->plugins_action->flush_action = &pluginsd_flush_action;
  334. parser->plugins_action->end_action = &pluginsd_end_action;
  335. parser->plugins_action->disable_action = &pluginsd_disable_action;
  336. parser->plugins_action->variable_action = &pluginsd_variable_action;
  337. parser->plugins_action->dimension_action = &pluginsd_dimension_action;
  338. parser->plugins_action->label_action = &pluginsd_label_action;
  339. parser->plugins_action->overwrite_action = &pluginsd_overwrite_action;
  340. parser->plugins_action->chart_action = &pluginsd_chart_action;
  341. parser->plugins_action->set_action = &pluginsd_set_action;
  342. parser->plugins_action->clabel_commit_action = &pluginsd_clabel_commit_action;
  343. parser->plugins_action->clabel_action = &pluginsd_clabel_action;
  344. user.parser = parser;
  345. #ifdef ENABLE_COMPRESSION
  346. if (rpt->decompressor)
  347. rpt->decompressor->reset(rpt->decompressor);
  348. #endif
  349. do{
  350. if (receiver_read(rpt, fp))
  351. break;
  352. int pos = 0;
  353. char *line;
  354. while ((line = receiver_next_line(rpt, &pos))) {
  355. if (unlikely(netdata_exit || rpt->shutdown || parser_action(parser, line)))
  356. goto done;
  357. }
  358. rpt->last_msg_t = now_realtime_sec();
  359. }
  360. while(!netdata_exit);
  361. done:
  362. result = user.count;
  363. // free parser with the pop function
  364. netdata_thread_cleanup_pop(1);
  365. return result;
  366. }
  367. static int rrdpush_receive(struct receiver_state *rpt)
  368. {
  369. int history = default_rrd_history_entries;
  370. RRD_MEMORY_MODE mode = default_rrd_memory_mode;
  371. int health_enabled = default_health_enabled;
  372. int rrdpush_enabled = default_rrdpush_enabled;
  373. char *rrdpush_destination = default_rrdpush_destination;
  374. char *rrdpush_api_key = default_rrdpush_api_key;
  375. char *rrdpush_send_charts_matching = default_rrdpush_send_charts_matching;
  376. time_t alarms_delay = 60;
  377. rpt->update_every = (int)appconfig_get_number(&stream_config, rpt->machine_guid, "update every", rpt->update_every);
  378. if(rpt->update_every < 0) rpt->update_every = 1;
  379. history = (int)appconfig_get_number(&stream_config, rpt->key, "default history", history);
  380. history = (int)appconfig_get_number(&stream_config, rpt->machine_guid, "history", history);
  381. if(history < 5) history = 5;
  382. mode = rrd_memory_mode_id(appconfig_get(&stream_config, rpt->key, "default memory mode", rrd_memory_mode_name(mode)));
  383. mode = rrd_memory_mode_id(appconfig_get(&stream_config, rpt->machine_guid, "memory mode", rrd_memory_mode_name(mode)));
  384. #ifndef ENABLE_DBENGINE
  385. if (unlikely(mode == RRD_MEMORY_MODE_DBENGINE)) {
  386. close(rpt->fd);
  387. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "REJECTED -- DBENGINE MEMORY MODE NOT SUPPORTED");
  388. return 1;
  389. }
  390. #endif
  391. health_enabled = appconfig_get_boolean_ondemand(&stream_config, rpt->key, "health enabled by default", health_enabled);
  392. health_enabled = appconfig_get_boolean_ondemand(&stream_config, rpt->machine_guid, "health enabled", health_enabled);
  393. alarms_delay = appconfig_get_number(&stream_config, rpt->key, "default postpone alarms on connect seconds", alarms_delay);
  394. alarms_delay = appconfig_get_number(&stream_config, rpt->machine_guid, "postpone alarms on connect seconds", alarms_delay);
  395. rrdpush_enabled = appconfig_get_boolean(&stream_config, rpt->key, "default proxy enabled", rrdpush_enabled);
  396. rrdpush_enabled = appconfig_get_boolean(&stream_config, rpt->machine_guid, "proxy enabled", rrdpush_enabled);
  397. rrdpush_destination = appconfig_get(&stream_config, rpt->key, "default proxy destination", rrdpush_destination);
  398. rrdpush_destination = appconfig_get(&stream_config, rpt->machine_guid, "proxy destination", rrdpush_destination);
  399. rrdpush_api_key = appconfig_get(&stream_config, rpt->key, "default proxy api key", rrdpush_api_key);
  400. rrdpush_api_key = appconfig_get(&stream_config, rpt->machine_guid, "proxy api key", rrdpush_api_key);
  401. rrdpush_send_charts_matching = appconfig_get(&stream_config, rpt->key, "default proxy send charts matching", rrdpush_send_charts_matching);
  402. rrdpush_send_charts_matching = appconfig_get(&stream_config, rpt->machine_guid, "proxy send charts matching", rrdpush_send_charts_matching);
  403. #ifdef ENABLE_COMPRESSION
  404. unsigned int rrdpush_compression = default_compression_enabled;
  405. rrdpush_compression = appconfig_get_boolean(&stream_config, rpt->key, "enable compression", rrdpush_compression);
  406. rrdpush_compression = appconfig_get_boolean(&stream_config, rpt->machine_guid, "enable compression", rrdpush_compression);
  407. rpt->rrdpush_compression = (rrdpush_compression && default_compression_enabled);
  408. #endif //ENABLE_COMPRESSION
  409. (void)appconfig_set_default(&stream_config, rpt->machine_guid, "host tags", (rpt->tags)?rpt->tags:"");
  410. if (strcmp(rpt->machine_guid, localhost->machine_guid) == 0) {
  411. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "DENIED - ATTEMPT TO RECEIVE METRICS FROM MACHINE_GUID IDENTICAL TO PARENT");
  412. error("STREAM %s [receive from %s:%s]: denied to receive metrics, machine GUID [%s] is my own. Did you copy the parent/proxy machine GUID to a child, or is this an inter-agent loop?", rpt->hostname, rpt->client_ip, rpt->client_port, rpt->machine_guid);
  413. char initial_response[HTTP_HEADER_SIZE + 1];
  414. snprintfz(initial_response, HTTP_HEADER_SIZE, "%s", START_STREAMING_ERROR_SAME_LOCALHOST);
  415. #ifdef ENABLE_HTTPS
  416. rpt->host->stream_ssl.conn = rpt->ssl.conn;
  417. rpt->host->stream_ssl.flags = rpt->ssl.flags;
  418. if(send_timeout(&rpt->ssl, rpt->fd, initial_response, strlen(initial_response), 0, 60) != (ssize_t)strlen(initial_response)) {
  419. #else
  420. if(send_timeout(rpt->fd, initial_response, strlen(initial_response), 0, 60) != strlen(initial_response)) {
  421. #endif
  422. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "FAILED - CANNOT REPLY");
  423. error("STREAM %s [receive from [%s]:%s]: cannot send command.", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  424. close(rpt->fd);
  425. return 0;
  426. }
  427. close(rpt->fd);
  428. return 0;
  429. }
  430. if (rpt->host==NULL) {
  431. rpt->host = rrdhost_find_or_create(
  432. rpt->hostname
  433. , rpt->registry_hostname
  434. , rpt->machine_guid
  435. , rpt->os
  436. , rpt->timezone
  437. , rpt->abbrev_timezone
  438. , rpt->utc_offset
  439. , rpt->tags
  440. , rpt->program_name
  441. , rpt->program_version
  442. , rpt->update_every
  443. , history
  444. , mode
  445. , (unsigned int)(health_enabled != CONFIG_BOOLEAN_NO)
  446. , (unsigned int)(rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key)
  447. , rrdpush_destination
  448. , rrdpush_api_key
  449. , rrdpush_send_charts_matching
  450. , rpt->system_info
  451. );
  452. if(!rpt->host) {
  453. close(rpt->fd);
  454. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "FAILED - CANNOT ACQUIRE HOST");
  455. error("STREAM %s [receive from [%s]:%s]: failed to find/create host structure.", rpt->hostname, rpt->client_ip, rpt->client_port);
  456. return 1;
  457. }
  458. netdata_mutex_lock(&rpt->host->receiver_lock);
  459. if (rpt->host->receiver == NULL)
  460. rpt->host->receiver = rpt;
  461. else {
  462. error("Multiple receivers connected for %s concurrently, cancelling this one...", rpt->machine_guid);
  463. netdata_mutex_unlock(&rpt->host->receiver_lock);
  464. close(rpt->fd);
  465. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "FAILED - BEATEN TO HOST CREATION");
  466. return 1;
  467. }
  468. netdata_mutex_unlock(&rpt->host->receiver_lock);
  469. }
  470. else {
  471. rrd_wrlock();
  472. rrdhost_update(
  473. rpt->host,
  474. rpt->hostname,
  475. rpt->registry_hostname,
  476. rpt->machine_guid,
  477. rpt->os,
  478. rpt->timezone,
  479. rpt->abbrev_timezone,
  480. rpt->utc_offset,
  481. rpt->tags,
  482. rpt->program_name,
  483. rpt->program_version,
  484. rpt->update_every,
  485. history,
  486. mode,
  487. (unsigned int)(health_enabled != CONFIG_BOOLEAN_NO),
  488. (unsigned int)(rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key),
  489. rrdpush_destination,
  490. rrdpush_api_key,
  491. rrdpush_send_charts_matching,
  492. rpt->system_info);
  493. rrd_unlock();
  494. }
  495. #ifdef NETDATA_INTERNAL_CHECKS
  496. int ssl = 0;
  497. #ifdef ENABLE_HTTPS
  498. if (rpt->ssl.conn != NULL)
  499. ssl = 1;
  500. #endif
  501. info("STREAM %s [receive from [%s]:%s]: client willing to stream metrics for host '%s' with machine_guid '%s': update every = %d, history = %ld, memory mode = %s, health %s,%s tags '%s'"
  502. , rpt->hostname
  503. , rpt->client_ip
  504. , rpt->client_port
  505. , rpt->host->hostname
  506. , rpt->host->machine_guid
  507. , rpt->host->rrd_update_every
  508. , rpt->host->rrd_history_entries
  509. , rrd_memory_mode_name(rpt->host->rrd_memory_mode)
  510. , (health_enabled == CONFIG_BOOLEAN_NO)?"disabled":((health_enabled == CONFIG_BOOLEAN_YES)?"enabled":"auto")
  511. , ssl ? " SSL," : ""
  512. , rpt->host->tags?rpt->host->tags:""
  513. );
  514. #endif // NETDATA_INTERNAL_CHECKS
  515. struct plugind cd = {
  516. .enabled = 1,
  517. .update_every = default_rrd_update_every,
  518. .pid = 0,
  519. .serial_failures = 0,
  520. .successful_collections = 0,
  521. .obsolete = 0,
  522. .started_t = now_realtime_sec(),
  523. .next = NULL,
  524. .version = 0,
  525. };
  526. // put the client IP and port into the buffers used by plugins.d
  527. snprintfz(cd.id, CONFIG_MAX_NAME, "%s:%s", rpt->client_ip, rpt->client_port);
  528. snprintfz(cd.filename, FILENAME_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  529. snprintfz(cd.fullfilename, FILENAME_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  530. snprintfz(cd.cmd, PLUGINSD_CMD_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  531. info("STREAM %s [receive from [%s]:%s]: initializing communication...", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  532. char initial_response[HTTP_HEADER_SIZE];
  533. if (rpt->stream_version > 1) {
  534. if(rpt->stream_version >= STREAM_VERSION_COMPRESSION){
  535. #ifdef ENABLE_COMPRESSION
  536. if(!rpt->rrdpush_compression)
  537. rpt->stream_version = STREAM_VERSION_CLABELS;
  538. #else
  539. if(STREAMING_PROTOCOL_CURRENT_VERSION < rpt->stream_version) {
  540. rpt->stream_version = STREAMING_PROTOCOL_CURRENT_VERSION;
  541. }
  542. #endif
  543. }
  544. info("STREAM %s [receive from [%s]:%s]: Netdata is using the stream version %u.", rpt->host->hostname, rpt->client_ip, rpt->client_port, rpt->stream_version);
  545. sprintf(initial_response, "%s%u", START_STREAMING_PROMPT_VN, rpt->stream_version);
  546. } else if (rpt->stream_version == 1) {
  547. info("STREAM %s [receive from [%s]:%s]: Netdata is using the stream version %u.", rpt->host->hostname, rpt->client_ip, rpt->client_port, rpt->stream_version);
  548. sprintf(initial_response, "%s", START_STREAMING_PROMPT_V2);
  549. } else {
  550. info("STREAM %s [receive from [%s]:%s]: Netdata is using first stream protocol.", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  551. sprintf(initial_response, "%s", START_STREAMING_PROMPT);
  552. }
  553. debug(D_STREAM, "Initial response to %s: %s", rpt->client_ip, initial_response);
  554. #ifdef ENABLE_HTTPS
  555. rpt->host->stream_ssl.conn = rpt->ssl.conn;
  556. rpt->host->stream_ssl.flags = rpt->ssl.flags;
  557. if(send_timeout(&rpt->ssl, rpt->fd, initial_response, strlen(initial_response), 0, 60) != (ssize_t)strlen(initial_response)) {
  558. #else
  559. if(send_timeout(rpt->fd, initial_response, strlen(initial_response), 0, 60) != strlen(initial_response)) {
  560. #endif
  561. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "FAILED - CANNOT REPLY");
  562. error("STREAM %s [receive from [%s]:%s]: cannot send ready command.", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  563. close(rpt->fd);
  564. return 0;
  565. }
  566. // remove the non-blocking flag from the socket
  567. if(sock_delnonblock(rpt->fd) < 0)
  568. error("STREAM %s [receive from [%s]:%s]: cannot remove the non-blocking flag from socket %d", rpt->host->hostname, rpt->client_ip, rpt->client_port, rpt->fd);
  569. struct timeval timeout;
  570. timeout.tv_sec = 120;
  571. timeout.tv_usec = 0;
  572. if (unlikely(setsockopt(rpt->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) != 0))
  573. error("STREAM %s [receive from [%s]:%s]: cannot set timeout for socket %d", rpt->host->hostname, rpt->client_ip, rpt->client_port, rpt->fd);
  574. // convert the socket to a FILE *
  575. FILE *fp = fdopen(rpt->fd, "r");
  576. if(!fp) {
  577. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "FAILED - SOCKET ERROR");
  578. error("STREAM %s [receive from [%s]:%s]: failed to get a FILE for FD %d.", rpt->host->hostname, rpt->client_ip, rpt->client_port, rpt->fd);
  579. close(rpt->fd);
  580. return 0;
  581. }
  582. rrdhost_wrlock(rpt->host);
  583. /* if(rpt->host->connected_senders > 0) {
  584. rrdhost_unlock(rpt->host);
  585. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "REJECTED - ALREADY CONNECTED");
  586. info("STREAM %s [receive from [%s]:%s]: multiple streaming connections for the same host detected. Rejecting new connection.", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  587. fclose(fp);
  588. return 0;
  589. }
  590. */
  591. // rpt->host->connected_senders++;
  592. rpt->host->labels.labels_flag = (rpt->stream_version > 0)?LABEL_FLAG_UPDATE_STREAM:LABEL_FLAG_STOP_STREAM;
  593. if(health_enabled != CONFIG_BOOLEAN_NO) {
  594. if(alarms_delay > 0) {
  595. rpt->host->health_delay_up_to = now_realtime_sec() + alarms_delay;
  596. info(
  597. "Postponing health checks for %" PRId64 " seconds, on host '%s', because it was just connected.",
  598. (int64_t)alarms_delay,
  599. rpt->host->hostname);
  600. }
  601. }
  602. rpt->host->senders_connect_time = now_realtime_sec();
  603. rpt->host->senders_last_chart_command = 0;
  604. rpt->host->trigger_chart_obsoletion_check = 1;
  605. rrdhost_unlock(rpt->host);
  606. // call the plugins.d processor to receive the metrics
  607. info("STREAM %s [receive from [%s]:%s]: receiving metrics...", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  608. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "CONNECTED");
  609. cd.version = rpt->stream_version;
  610. #if defined(ENABLE_NEW_CLOUD_PROTOCOL)
  611. // in case we have cloud connection we inform cloud
  612. // new child connected
  613. if (netdata_cloud_setting)
  614. aclk_host_state_update(rpt->host, 1);
  615. #endif
  616. size_t count = streaming_parser(rpt, &cd, fp);
  617. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->hostname,
  618. "DISCONNECTED");
  619. error("STREAM %s [receive from [%s]:%s]: disconnected (completed %zu updates).", rpt->hostname, rpt->client_ip,
  620. rpt->client_port, count);
  621. #if defined(ENABLE_NEW_CLOUD_PROTOCOL)
  622. // in case we have cloud connection we inform cloud
  623. // new child connected
  624. if (netdata_cloud_setting)
  625. aclk_host_state_update(rpt->host, 0);
  626. #endif
  627. // During a shutdown there is cleanup code in rrdhost that will cancel the sender thread
  628. if (!netdata_exit && rpt->host) {
  629. rrd_rdlock();
  630. rrdhost_wrlock(rpt->host);
  631. netdata_mutex_lock(&rpt->host->receiver_lock);
  632. if (rpt->host->receiver == rpt) {
  633. rpt->host->senders_connect_time = 0;
  634. rpt->host->trigger_chart_obsoletion_check = 0;
  635. rpt->host->senders_disconnected_time = now_realtime_sec();
  636. rrdhost_flag_set(rpt->host, RRDHOST_FLAG_ORPHAN);
  637. if(health_enabled == CONFIG_BOOLEAN_AUTO)
  638. rpt->host->health_enabled = 0;
  639. }
  640. rrdhost_unlock(rpt->host);
  641. if (rpt->host->receiver == rpt) {
  642. rrdpush_sender_thread_stop(rpt->host);
  643. }
  644. netdata_mutex_unlock(&rpt->host->receiver_lock);
  645. rrd_unlock();
  646. }
  647. // cleanup
  648. fclose(fp);
  649. return (int)count;
  650. }
  651. void *rrdpush_receiver_thread(void *ptr) {
  652. netdata_thread_cleanup_push(rrdpush_receiver_thread_cleanup, ptr);
  653. struct receiver_state *rpt = (struct receiver_state *)ptr;
  654. info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
  655. worker_register("STREAMRCV");
  656. rrdpush_receive(rpt);
  657. worker_unregister();
  658. netdata_thread_cleanup_pop(1);
  659. return NULL;
  660. }