receiver.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. freez(rpt);
  23. }
  24. static void rrdpush_receiver_thread_cleanup(void *ptr) {
  25. static __thread int executed = 0;
  26. if(!executed) {
  27. executed = 1;
  28. struct receiver_state *rpt = (struct receiver_state *) ptr;
  29. // If the shutdown sequence has started, and this receiver is still attached to the host then we cannot touch
  30. // the host pointer as it is unpredictable when the RRDHOST is deleted. Do the cleanup from rrdhost_free().
  31. if (netdata_exit && rpt->host) {
  32. rpt->exited = 1;
  33. return;
  34. }
  35. // Make sure that we detach this thread and don't kill a freshly arriving receiver
  36. if (!netdata_exit && rpt->host) {
  37. netdata_mutex_lock(&rpt->host->receiver_lock);
  38. if (rpt->host->receiver == rpt)
  39. rpt->host->receiver = NULL;
  40. netdata_mutex_unlock(&rpt->host->receiver_lock);
  41. }
  42. info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
  43. destroy_receiver_state(rpt);
  44. }
  45. }
  46. #include "collectors/plugins.d/pluginsd_parser.h"
  47. PARSER_RC streaming_timestamp(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  48. {
  49. UNUSED(plugins_action);
  50. char *remote_time_txt = words[1];
  51. time_t remote_time = 0;
  52. RRDHOST *host = ((PARSER_USER_OBJECT *)user)->host;
  53. struct plugind *cd = ((PARSER_USER_OBJECT *)user)->cd;
  54. if (cd->version < VERSION_GAP_FILLING ) {
  55. error("STREAM %s from %s: Child negotiated version %u but sent TIMESTAMP!", host->hostname, cd->cmd,
  56. cd->version);
  57. return PARSER_RC_OK; // Ignore error and continue stream
  58. }
  59. if (remote_time_txt && *remote_time_txt) {
  60. remote_time = str2ull(remote_time_txt);
  61. time_t now = now_realtime_sec(), prev = rrdhost_last_entry_t(host);
  62. time_t gap = 0;
  63. if (prev == 0)
  64. info("STREAM %s from %s: Initial connection (no gap to check), remote=%ld local=%ld slew=%ld",
  65. host->hostname, cd->cmd, remote_time, now, now-remote_time);
  66. else {
  67. gap = now - prev;
  68. info("STREAM %s from %s: Checking for gaps... remote=%ld local=%ld..%ld slew=%ld %ld-sec gap",
  69. host->hostname, cd->cmd, remote_time, prev, now, remote_time - now, gap);
  70. }
  71. char message[128];
  72. sprintf(message,"REPLICATE %ld %ld\n", remote_time - gap, remote_time);
  73. int ret;
  74. #ifdef ENABLE_HTTPS
  75. SSL *conn = host->stream_ssl.conn ;
  76. if(conn && !host->stream_ssl.flags) {
  77. ret = SSL_write(conn, message, strlen(message));
  78. } else {
  79. ret = send(host->receiver->fd, message, strlen(message), MSG_DONTWAIT);
  80. }
  81. #else
  82. ret = send(host->receiver->fd, message, strlen(message), MSG_DONTWAIT);
  83. #endif
  84. if (ret != (int)strlen(message))
  85. error("Failed to send initial timestamp - gaps may appear in charts");
  86. return PARSER_RC_OK;
  87. }
  88. return PARSER_RC_ERROR;
  89. }
  90. #define CLAIMED_ID_MIN_WORDS 3
  91. PARSER_RC streaming_claimed_id(char **words, void *user, PLUGINSD_ACTION *plugins_action)
  92. {
  93. UNUSED(plugins_action);
  94. int i;
  95. uuid_t uuid;
  96. RRDHOST *host = ((PARSER_USER_OBJECT *)user)->host;
  97. for (i = 0; words[i]; i++) ;
  98. if (i != CLAIMED_ID_MIN_WORDS) {
  99. error("Command CLAIMED_ID came malformed %d parameters are expected but %d received", CLAIMED_ID_MIN_WORDS - 1, i - 1);
  100. return PARSER_RC_ERROR;
  101. }
  102. // We don't need the parsed UUID
  103. // just do it to check the format
  104. if(uuid_parse(words[1], uuid)) {
  105. error("1st parameter (host GUID) to CLAIMED_ID command is not valid GUID. Received: \"%s\".", words[1]);
  106. return PARSER_RC_ERROR;
  107. }
  108. if(uuid_parse(words[2], uuid) && strcmp(words[2], "NULL")) {
  109. error("2nd parameter (Claim ID) to CLAIMED_ID command is not valid GUID. Received: \"%s\".", words[2]);
  110. return PARSER_RC_ERROR;
  111. }
  112. if(strcmp(words[1], host->machine_guid)) {
  113. error("Claim ID is for host \"%s\" but it came over connection for \"%s\"", words[1], host->machine_guid);
  114. return PARSER_RC_OK; //the message is OK problem must be somewhere else
  115. }
  116. rrdhost_aclk_state_lock(host);
  117. if (host->aclk_state.claimed_id)
  118. freez(host->aclk_state.claimed_id);
  119. host->aclk_state.claimed_id = strcmp(words[2], "NULL") ? strdupz(words[2]) : NULL;
  120. store_claim_id(&host->host_uuid, host->aclk_state.claimed_id ? &uuid : NULL);
  121. rrdhost_aclk_state_unlock(host);
  122. rrdpush_claimed_id(host);
  123. return PARSER_RC_OK;
  124. }
  125. /* The receiver socket is blocking, perform a single read into a buffer so that we can reassemble lines for parsing.
  126. */
  127. static int receiver_read(struct receiver_state *r, FILE *fp) {
  128. #ifdef ENABLE_HTTPS
  129. if (r->ssl.conn && !r->ssl.flags) {
  130. ERR_clear_error();
  131. int desired = sizeof(r->read_buffer) - r->read_len - 1;
  132. int ret = SSL_read(r->ssl.conn, r->read_buffer + r->read_len, desired);
  133. if (ret > 0 ) {
  134. r->read_len += ret;
  135. return 0;
  136. }
  137. // Don't treat SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE differently on blocking socket
  138. u_long err;
  139. char buf[256];
  140. while ((err = ERR_get_error()) != 0) {
  141. ERR_error_string_n(err, buf, sizeof(buf));
  142. error("STREAM %s [receive from %s] ssl error: %s", r->hostname, r->client_ip, buf);
  143. }
  144. return 1;
  145. }
  146. #endif
  147. if (!fgets(r->read_buffer, sizeof(r->read_buffer), fp))
  148. return 1;
  149. r->read_len = strlen(r->read_buffer);
  150. return 0;
  151. }
  152. /* Produce a full line if one exists, statefully return where we start next time.
  153. * When we hit the end of the buffer with a partial line move it to the beginning for the next fill.
  154. */
  155. static char *receiver_next_line(struct receiver_state *r, int *pos) {
  156. int start = *pos, scan = *pos;
  157. if (scan >= r->read_len) {
  158. r->read_len = 0;
  159. return NULL;
  160. }
  161. while (scan < r->read_len && r->read_buffer[scan] != '\n')
  162. scan++;
  163. if (scan < r->read_len && r->read_buffer[scan] == '\n') {
  164. *pos = scan+1;
  165. r->read_buffer[scan] = 0;
  166. return &r->read_buffer[start];
  167. }
  168. memmove(r->read_buffer, &r->read_buffer[start], r->read_len - start);
  169. r->read_len -= start;
  170. return NULL;
  171. }
  172. size_t streaming_parser(struct receiver_state *rpt, struct plugind *cd, FILE *fp) {
  173. size_t result;
  174. PARSER_USER_OBJECT *user = callocz(1, sizeof(*user));
  175. user->enabled = cd->enabled;
  176. user->host = rpt->host;
  177. user->opaque = rpt;
  178. user->cd = cd;
  179. user->trust_durations = 0;
  180. PARSER *parser = parser_init(rpt->host, user, fp, PARSER_INPUT_SPLIT);
  181. parser_add_keyword(parser, "TIMESTAMP", streaming_timestamp);
  182. parser_add_keyword(parser, "CLAIMED_ID", streaming_claimed_id);
  183. if (unlikely(!parser)) {
  184. error("Failed to initialize parser");
  185. cd->serial_failures++;
  186. freez(user);
  187. return 0;
  188. }
  189. parser->plugins_action->begin_action = &pluginsd_begin_action;
  190. parser->plugins_action->flush_action = &pluginsd_flush_action;
  191. parser->plugins_action->end_action = &pluginsd_end_action;
  192. parser->plugins_action->disable_action = &pluginsd_disable_action;
  193. parser->plugins_action->variable_action = &pluginsd_variable_action;
  194. parser->plugins_action->dimension_action = &pluginsd_dimension_action;
  195. parser->plugins_action->label_action = &pluginsd_label_action;
  196. parser->plugins_action->overwrite_action = &pluginsd_overwrite_action;
  197. parser->plugins_action->chart_action = &pluginsd_chart_action;
  198. parser->plugins_action->set_action = &pluginsd_set_action;
  199. parser->plugins_action->clabel_commit_action = &pluginsd_clabel_commit_action;
  200. parser->plugins_action->clabel_action = &pluginsd_clabel_action;
  201. user->parser = parser;
  202. do {
  203. if (receiver_read(rpt, fp))
  204. break;
  205. int pos = 0;
  206. char *line;
  207. while ((line = receiver_next_line(rpt, &pos))) {
  208. if (unlikely(netdata_exit || rpt->shutdown || parser_action(parser, line)))
  209. goto done;
  210. }
  211. rpt->last_msg_t = now_realtime_sec();
  212. }
  213. while(!netdata_exit);
  214. done:
  215. result= user->count;
  216. freez(user);
  217. parser_destroy(parser);
  218. return result;
  219. }
  220. static int rrdpush_receive(struct receiver_state *rpt)
  221. {
  222. int history = default_rrd_history_entries;
  223. RRD_MEMORY_MODE mode = default_rrd_memory_mode;
  224. int health_enabled = default_health_enabled;
  225. int rrdpush_enabled = default_rrdpush_enabled;
  226. char *rrdpush_destination = default_rrdpush_destination;
  227. char *rrdpush_api_key = default_rrdpush_api_key;
  228. char *rrdpush_send_charts_matching = default_rrdpush_send_charts_matching;
  229. time_t alarms_delay = 60;
  230. rpt->update_every = (int)appconfig_get_number(&stream_config, rpt->machine_guid, "update every", rpt->update_every);
  231. if(rpt->update_every < 0) rpt->update_every = 1;
  232. history = (int)appconfig_get_number(&stream_config, rpt->key, "default history", history);
  233. history = (int)appconfig_get_number(&stream_config, rpt->machine_guid, "history", history);
  234. if(history < 5) history = 5;
  235. mode = rrd_memory_mode_id(appconfig_get(&stream_config, rpt->key, "default memory mode", rrd_memory_mode_name(mode)));
  236. mode = rrd_memory_mode_id(appconfig_get(&stream_config, rpt->machine_guid, "memory mode", rrd_memory_mode_name(mode)));
  237. #ifndef ENABLE_DBENGINE
  238. if (unlikely(mode == RRD_MEMORY_MODE_DBENGINE)) {
  239. close(rpt->fd);
  240. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "REJECTED -- DBENGINE MEMORY MODE NOT SUPPORTED");
  241. return 1;
  242. }
  243. #endif
  244. health_enabled = appconfig_get_boolean_ondemand(&stream_config, rpt->key, "health enabled by default", health_enabled);
  245. health_enabled = appconfig_get_boolean_ondemand(&stream_config, rpt->machine_guid, "health enabled", health_enabled);
  246. alarms_delay = appconfig_get_number(&stream_config, rpt->key, "default postpone alarms on connect seconds", alarms_delay);
  247. alarms_delay = appconfig_get_number(&stream_config, rpt->machine_guid, "postpone alarms on connect seconds", alarms_delay);
  248. rrdpush_enabled = appconfig_get_boolean(&stream_config, rpt->key, "default proxy enabled", rrdpush_enabled);
  249. rrdpush_enabled = appconfig_get_boolean(&stream_config, rpt->machine_guid, "proxy enabled", rrdpush_enabled);
  250. rrdpush_destination = appconfig_get(&stream_config, rpt->key, "default proxy destination", rrdpush_destination);
  251. rrdpush_destination = appconfig_get(&stream_config, rpt->machine_guid, "proxy destination", rrdpush_destination);
  252. rrdpush_api_key = appconfig_get(&stream_config, rpt->key, "default proxy api key", rrdpush_api_key);
  253. rrdpush_api_key = appconfig_get(&stream_config, rpt->machine_guid, "proxy api key", rrdpush_api_key);
  254. rrdpush_send_charts_matching = appconfig_get(&stream_config, rpt->key, "default proxy send charts matching", rrdpush_send_charts_matching);
  255. rrdpush_send_charts_matching = appconfig_get(&stream_config, rpt->machine_guid, "proxy send charts matching", rrdpush_send_charts_matching);
  256. (void)appconfig_set_default(&stream_config, rpt->machine_guid, "host tags", (rpt->tags)?rpt->tags:"");
  257. if (strcmp(rpt->machine_guid, localhost->machine_guid) == 0) {
  258. 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");
  259. 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);
  260. close(rpt->fd);
  261. return 1;
  262. }
  263. if (rpt->host==NULL) {
  264. rpt->host = rrdhost_find_or_create(
  265. rpt->hostname
  266. , rpt->registry_hostname
  267. , rpt->machine_guid
  268. , rpt->os
  269. , rpt->timezone
  270. , rpt->abbrev_timezone
  271. , rpt->utc_offset
  272. , rpt->tags
  273. , rpt->program_name
  274. , rpt->program_version
  275. , rpt->update_every
  276. , history
  277. , mode
  278. , (unsigned int)(health_enabled != CONFIG_BOOLEAN_NO)
  279. , (unsigned int)(rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key)
  280. , rrdpush_destination
  281. , rrdpush_api_key
  282. , rrdpush_send_charts_matching
  283. , rpt->system_info
  284. );
  285. if(!rpt->host) {
  286. close(rpt->fd);
  287. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "FAILED - CANNOT ACQUIRE HOST");
  288. error("STREAM %s [receive from [%s]:%s]: failed to find/create host structure.", rpt->hostname, rpt->client_ip, rpt->client_port);
  289. return 1;
  290. }
  291. netdata_mutex_lock(&rpt->host->receiver_lock);
  292. if (rpt->host->receiver == NULL)
  293. rpt->host->receiver = rpt;
  294. else {
  295. error("Multiple receivers connected for %s concurrently, cancelling this one...", rpt->machine_guid);
  296. netdata_mutex_unlock(&rpt->host->receiver_lock);
  297. close(rpt->fd);
  298. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "FAILED - BEATEN TO HOST CREATION");
  299. return 1;
  300. }
  301. netdata_mutex_unlock(&rpt->host->receiver_lock);
  302. }
  303. #ifdef NETDATA_INTERNAL_CHECKS
  304. int ssl = 0;
  305. #ifdef ENABLE_HTTPS
  306. if (rpt->ssl.conn != NULL)
  307. ssl = 1;
  308. #endif
  309. 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'"
  310. , rpt->hostname
  311. , rpt->client_ip
  312. , rpt->client_port
  313. , rpt->host->hostname
  314. , rpt->host->machine_guid
  315. , rpt->host->rrd_update_every
  316. , rpt->host->rrd_history_entries
  317. , rrd_memory_mode_name(rpt->host->rrd_memory_mode)
  318. , (health_enabled == CONFIG_BOOLEAN_NO)?"disabled":((health_enabled == CONFIG_BOOLEAN_YES)?"enabled":"auto")
  319. , ssl ? " SSL," : ""
  320. , rpt->host->tags?rpt->host->tags:""
  321. );
  322. #endif // NETDATA_INTERNAL_CHECKS
  323. struct plugind cd = {
  324. .enabled = 1,
  325. .update_every = default_rrd_update_every,
  326. .pid = 0,
  327. .serial_failures = 0,
  328. .successful_collections = 0,
  329. .obsolete = 0,
  330. .started_t = now_realtime_sec(),
  331. .next = NULL,
  332. .version = 0,
  333. };
  334. // put the client IP and port into the buffers used by plugins.d
  335. snprintfz(cd.id, CONFIG_MAX_NAME, "%s:%s", rpt->client_ip, rpt->client_port);
  336. snprintfz(cd.filename, FILENAME_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  337. snprintfz(cd.fullfilename, FILENAME_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  338. snprintfz(cd.cmd, PLUGINSD_CMD_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  339. info("STREAM %s [receive from [%s]:%s]: initializing communication...", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  340. char initial_response[HTTP_HEADER_SIZE];
  341. if (rpt->stream_version > 1) {
  342. 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);
  343. sprintf(initial_response, "%s%u", START_STREAMING_PROMPT_VN, rpt->stream_version);
  344. } else if (rpt->stream_version == 1) {
  345. 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);
  346. sprintf(initial_response, "%s", START_STREAMING_PROMPT_V2);
  347. } else {
  348. info("STREAM %s [receive from [%s]:%s]: Netdata is using first stream protocol.", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  349. sprintf(initial_response, "%s", START_STREAMING_PROMPT);
  350. }
  351. debug(D_STREAM, "Initial response to %s: %s", rpt->client_ip, initial_response);
  352. #ifdef ENABLE_HTTPS
  353. rpt->host->stream_ssl.conn = rpt->ssl.conn;
  354. rpt->host->stream_ssl.flags = rpt->ssl.flags;
  355. if(send_timeout(&rpt->ssl, rpt->fd, initial_response, strlen(initial_response), 0, 60) != (ssize_t)strlen(initial_response)) {
  356. #else
  357. if(send_timeout(rpt->fd, initial_response, strlen(initial_response), 0, 60) != strlen(initial_response)) {
  358. #endif
  359. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "FAILED - CANNOT REPLY");
  360. error("STREAM %s [receive from [%s]:%s]: cannot send ready command.", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  361. close(rpt->fd);
  362. return 0;
  363. }
  364. // remove the non-blocking flag from the socket
  365. if(sock_delnonblock(rpt->fd) < 0)
  366. 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);
  367. // convert the socket to a FILE *
  368. FILE *fp = fdopen(rpt->fd, "r");
  369. if(!fp) {
  370. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "FAILED - SOCKET ERROR");
  371. 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);
  372. close(rpt->fd);
  373. return 0;
  374. }
  375. rrdhost_wrlock(rpt->host);
  376. /* if(rpt->host->connected_senders > 0) {
  377. rrdhost_unlock(rpt->host);
  378. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "REJECTED - ALREADY CONNECTED");
  379. 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);
  380. fclose(fp);
  381. return 0;
  382. }
  383. */
  384. // rpt->host->connected_senders++;
  385. rpt->host->labels.labels_flag = (rpt->stream_version > 0)?LABEL_FLAG_UPDATE_STREAM:LABEL_FLAG_STOP_STREAM;
  386. if(health_enabled != CONFIG_BOOLEAN_NO) {
  387. if(alarms_delay > 0) {
  388. rpt->host->health_delay_up_to = now_realtime_sec() + alarms_delay;
  389. info("Postponing health checks for %ld seconds, on host '%s', because it was just connected."
  390. , alarms_delay
  391. , rpt->host->hostname
  392. );
  393. }
  394. }
  395. rrdhost_unlock(rpt->host);
  396. // call the plugins.d processor to receive the metrics
  397. info("STREAM %s [receive from [%s]:%s]: receiving metrics...", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  398. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "CONNECTED");
  399. cd.version = rpt->stream_version;
  400. #if defined(ENABLE_ACLK)
  401. // in case we have cloud connection we inform cloud
  402. // new slave connected
  403. if (netdata_cloud_setting)
  404. aclk_host_state_update(rpt->host, 1);
  405. #endif
  406. size_t count = streaming_parser(rpt, &cd, fp);
  407. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->hostname,
  408. "DISCONNECTED");
  409. error("STREAM %s [receive from [%s]:%s]: disconnected (completed %zu updates).", rpt->hostname, rpt->client_ip,
  410. rpt->client_port, count);
  411. #if defined(ENABLE_ACLK)
  412. // in case we have cloud connection we inform cloud
  413. // new slave connected
  414. if (netdata_cloud_setting)
  415. aclk_host_state_update(rpt->host, 0);
  416. #endif
  417. // During a shutdown there is cleanup code in rrdhost that will cancel the sender thread
  418. if (!netdata_exit && rpt->host) {
  419. rrd_rdlock();
  420. rrdhost_wrlock(rpt->host);
  421. netdata_mutex_lock(&rpt->host->receiver_lock);
  422. if (rpt->host->receiver == rpt) {
  423. rpt->host->senders_disconnected_time = now_realtime_sec();
  424. rrdhost_flag_set(rpt->host, RRDHOST_FLAG_ORPHAN);
  425. if(health_enabled == CONFIG_BOOLEAN_AUTO)
  426. rpt->host->health_enabled = 0;
  427. }
  428. rrdhost_unlock(rpt->host);
  429. if (rpt->host->receiver == rpt) {
  430. rrdpush_sender_thread_stop(rpt->host);
  431. }
  432. netdata_mutex_unlock(&rpt->host->receiver_lock);
  433. rrd_unlock();
  434. }
  435. // cleanup
  436. fclose(fp);
  437. return (int)count;
  438. }
  439. void *rrdpush_receiver_thread(void *ptr) {
  440. netdata_thread_cleanup_push(rrdpush_receiver_thread_cleanup, ptr);
  441. struct receiver_state *rpt = (struct receiver_state *)ptr;
  442. info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
  443. rrdpush_receive(rpt);
  444. netdata_thread_cleanup_pop(1);
  445. return NULL;
  446. }