receiver.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 unpredicable 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 somewehere 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. health_enabled = appconfig_get_boolean_ondemand(&stream_config, rpt->key, "health enabled by default", health_enabled);
  234. health_enabled = appconfig_get_boolean_ondemand(&stream_config, rpt->machine_guid, "health enabled", health_enabled);
  235. alarms_delay = appconfig_get_number(&stream_config, rpt->key, "default postpone alarms on connect seconds", alarms_delay);
  236. alarms_delay = appconfig_get_number(&stream_config, rpt->machine_guid, "postpone alarms on connect seconds", alarms_delay);
  237. rrdpush_enabled = appconfig_get_boolean(&stream_config, rpt->key, "default proxy enabled", rrdpush_enabled);
  238. rrdpush_enabled = appconfig_get_boolean(&stream_config, rpt->machine_guid, "proxy enabled", rrdpush_enabled);
  239. rrdpush_destination = appconfig_get(&stream_config, rpt->key, "default proxy destination", rrdpush_destination);
  240. rrdpush_destination = appconfig_get(&stream_config, rpt->machine_guid, "proxy destination", rrdpush_destination);
  241. rrdpush_api_key = appconfig_get(&stream_config, rpt->key, "default proxy api key", rrdpush_api_key);
  242. rrdpush_api_key = appconfig_get(&stream_config, rpt->machine_guid, "proxy api key", rrdpush_api_key);
  243. rrdpush_send_charts_matching = appconfig_get(&stream_config, rpt->key, "default proxy send charts matching", rrdpush_send_charts_matching);
  244. rrdpush_send_charts_matching = appconfig_get(&stream_config, rpt->machine_guid, "proxy send charts matching", rrdpush_send_charts_matching);
  245. (void)appconfig_set_default(&stream_config, rpt->machine_guid, "host tags", (rpt->tags)?rpt->tags:"");
  246. if (strcmp(rpt->machine_guid, localhost->machine_guid) == 0) {
  247. 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");
  248. 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);
  249. close(rpt->fd);
  250. return 1;
  251. }
  252. if (rpt->host==NULL) {
  253. rpt->host = rrdhost_find_or_create(
  254. rpt->hostname
  255. , rpt->registry_hostname
  256. , rpt->machine_guid
  257. , rpt->os
  258. , rpt->timezone
  259. , rpt->tags
  260. , rpt->program_name
  261. , rpt->program_version
  262. , rpt->update_every
  263. , history
  264. , mode
  265. , (unsigned int)(health_enabled != CONFIG_BOOLEAN_NO)
  266. , (unsigned int)(rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key)
  267. , rrdpush_destination
  268. , rrdpush_api_key
  269. , rrdpush_send_charts_matching
  270. , rpt->system_info
  271. );
  272. if(!rpt->host) {
  273. close(rpt->fd);
  274. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "FAILED - CANNOT ACQUIRE HOST");
  275. error("STREAM %s [receive from [%s]:%s]: failed to find/create host structure.", rpt->hostname, rpt->client_ip, rpt->client_port);
  276. return 1;
  277. }
  278. netdata_mutex_lock(&rpt->host->receiver_lock);
  279. if (rpt->host->receiver == NULL)
  280. rpt->host->receiver = rpt;
  281. else {
  282. error("Multiple receivers connected for %s concurrently, cancelling this one...", rpt->machine_guid);
  283. netdata_mutex_unlock(&rpt->host->receiver_lock);
  284. close(rpt->fd);
  285. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->machine_guid, rpt->hostname, "FAILED - BEATEN TO HOST CREATION");
  286. return 1;
  287. }
  288. netdata_mutex_unlock(&rpt->host->receiver_lock);
  289. }
  290. int ssl = 0;
  291. #ifdef ENABLE_HTTPS
  292. if (rpt->ssl.conn != NULL)
  293. ssl = 1;
  294. #endif
  295. #ifdef NETDATA_INTERNAL_CHECKS
  296. 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'"
  297. , rpt->hostname
  298. , rpt->client_ip
  299. , rpt->client_port
  300. , rpt->host->hostname
  301. , rpt->host->machine_guid
  302. , rpt->host->rrd_update_every
  303. , rpt->host->rrd_history_entries
  304. , rrd_memory_mode_name(rpt->host->rrd_memory_mode)
  305. , (health_enabled == CONFIG_BOOLEAN_NO)?"disabled":((health_enabled == CONFIG_BOOLEAN_YES)?"enabled":"auto")
  306. , ssl ? " SSL," : ""
  307. , rpt->host->tags?rpt->host->tags:""
  308. );
  309. #endif // NETDATA_INTERNAL_CHECKS
  310. struct plugind cd = {
  311. .enabled = 1,
  312. .update_every = default_rrd_update_every,
  313. .pid = 0,
  314. .serial_failures = 0,
  315. .successful_collections = 0,
  316. .obsolete = 0,
  317. .started_t = now_realtime_sec(),
  318. .next = NULL,
  319. .version = 0,
  320. };
  321. // put the client IP and port into the buffers used by plugins.d
  322. snprintfz(cd.id, CONFIG_MAX_NAME, "%s:%s", rpt->client_ip, rpt->client_port);
  323. snprintfz(cd.filename, FILENAME_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  324. snprintfz(cd.fullfilename, FILENAME_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  325. snprintfz(cd.cmd, PLUGINSD_CMD_MAX, "%s:%s", rpt->client_ip, rpt->client_port);
  326. info("STREAM %s [receive from [%s]:%s]: initializing communication...", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  327. char initial_response[HTTP_HEADER_SIZE];
  328. if (rpt->stream_version > 1) {
  329. 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);
  330. sprintf(initial_response, "%s%u", START_STREAMING_PROMPT_VN, rpt->stream_version);
  331. } else if (rpt->stream_version == 1) {
  332. 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);
  333. sprintf(initial_response, "%s", START_STREAMING_PROMPT_V2);
  334. } else {
  335. info("STREAM %s [receive from [%s]:%s]: Netdata is using first stream protocol.", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  336. sprintf(initial_response, "%s", START_STREAMING_PROMPT);
  337. }
  338. debug(D_STREAM, "Initial response to %s: %s", rpt->client_ip, initial_response);
  339. #ifdef ENABLE_HTTPS
  340. rpt->host->stream_ssl.conn = rpt->ssl.conn;
  341. rpt->host->stream_ssl.flags = rpt->ssl.flags;
  342. if(send_timeout(&rpt->ssl, rpt->fd, initial_response, strlen(initial_response), 0, 60) != (ssize_t)strlen(initial_response)) {
  343. #else
  344. if(send_timeout(rpt->fd, initial_response, strlen(initial_response), 0, 60) != strlen(initial_response)) {
  345. #endif
  346. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "FAILED - CANNOT REPLY");
  347. error("STREAM %s [receive from [%s]:%s]: cannot send ready command.", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  348. close(rpt->fd);
  349. return 0;
  350. }
  351. // remove the non-blocking flag from the socket
  352. if(sock_delnonblock(rpt->fd) < 0)
  353. 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);
  354. // convert the socket to a FILE *
  355. FILE *fp = fdopen(rpt->fd, "r");
  356. if(!fp) {
  357. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "FAILED - SOCKET ERROR");
  358. 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);
  359. close(rpt->fd);
  360. return 0;
  361. }
  362. rrdhost_wrlock(rpt->host);
  363. /* if(rpt->host->connected_senders > 0) {
  364. rrdhost_unlock(rpt->host);
  365. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "REJECTED - ALREADY CONNECTED");
  366. 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);
  367. fclose(fp);
  368. return 0;
  369. }
  370. */
  371. // rpt->host->connected_senders++;
  372. rpt->host->labels.labels_flag = (rpt->stream_version > 0)?LABEL_FLAG_UPDATE_STREAM:LABEL_FLAG_STOP_STREAM;
  373. if(health_enabled != CONFIG_BOOLEAN_NO) {
  374. if(alarms_delay > 0) {
  375. rpt->host->health_delay_up_to = now_realtime_sec() + alarms_delay;
  376. info("Postponing health checks for %ld seconds, on host '%s', because it was just connected."
  377. , alarms_delay
  378. , rpt->host->hostname
  379. );
  380. }
  381. }
  382. rrdhost_unlock(rpt->host);
  383. // call the plugins.d processor to receive the metrics
  384. info("STREAM %s [receive from [%s]:%s]: receiving metrics...", rpt->host->hostname, rpt->client_ip, rpt->client_port);
  385. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->host->hostname, "CONNECTED");
  386. cd.version = rpt->stream_version;
  387. #ifdef ENABLE_ACLK
  388. // in case we have cloud connection we inform cloud
  389. // new slave connected
  390. if (netdata_cloud_setting)
  391. aclk_host_state_update(rpt->host, ACLK_CMD_CHILD_CONNECT);
  392. #endif
  393. size_t count = streaming_parser(rpt, &cd, fp);
  394. log_stream_connection(rpt->client_ip, rpt->client_port, rpt->key, rpt->host->machine_guid, rpt->hostname,
  395. "DISCONNECTED");
  396. error("STREAM %s [receive from [%s]:%s]: disconnected (completed %zu updates).", rpt->hostname, rpt->client_ip,
  397. rpt->client_port, count);
  398. #ifdef ENABLE_ACLK
  399. // in case we have cloud connection we inform cloud
  400. // new slave connected
  401. if (netdata_cloud_setting)
  402. aclk_host_state_update(rpt->host, ACLK_CMD_CHILD_DISCONNECT);
  403. #endif
  404. // During a shutdown there is cleanup code in rrdhost that will cancel the sender thread
  405. if (!netdata_exit && rpt->host) {
  406. rrd_rdlock();
  407. rrdhost_wrlock(rpt->host);
  408. netdata_mutex_lock(&rpt->host->receiver_lock);
  409. if (rpt->host->receiver == rpt) {
  410. rpt->host->senders_disconnected_time = now_realtime_sec();
  411. rrdhost_flag_set(rpt->host, RRDHOST_FLAG_ORPHAN);
  412. if(health_enabled == CONFIG_BOOLEAN_AUTO)
  413. rpt->host->health_enabled = 0;
  414. }
  415. rrdhost_unlock(rpt->host);
  416. if (rpt->host->receiver == rpt) {
  417. rrdpush_sender_thread_stop(rpt->host);
  418. }
  419. netdata_mutex_unlock(&rpt->host->receiver_lock);
  420. rrd_unlock();
  421. }
  422. // cleanup
  423. fclose(fp);
  424. return (int)count;
  425. }
  426. void *rrdpush_receiver_thread(void *ptr) {
  427. netdata_thread_cleanup_push(rrdpush_receiver_thread_cleanup, ptr);
  428. struct receiver_state *rpt = (struct receiver_state *)ptr;
  429. info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
  430. rrdpush_receive(rpt);
  431. netdata_thread_cleanup_pop(1);
  432. return NULL;
  433. }