receiver.c 28 KB

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