receiver.c 21 KB

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