6 * 3 threads are involved for all stream operations
8 * 1. a random data collection thread, calling rrdset_done_push()
9 * this is called for each chart.
11 * the output of this work is kept in a BUFFER in RRDHOST
12 * the sender thread is signalled via a pipe (also in RRDHOST)
14 * 2. a sender thread running at the sending netdata
15 * this is spawned automatically on the first chart to be pushed
17 * It tries to push the metrics to the remote netdata, as fast
18 * as possible (i.e. immediately after they are collected).
20 * 3. a receiver thread, running at the receiving netdata
21 * this is spawned automatically when the sender connects to
26 #define START_STREAMING_PROMPT "Hit me baby, push them over..."
28 int default_rrdpush_enabled = 0;
29 char *default_rrdpush_destination = NULL;
30 char *default_rrdpush_api_key = NULL;
33 default_rrdpush_enabled = appconfig_get_boolean(&stream_config, CONFIG_SECTION_STREAM, "enabled", default_rrdpush_enabled);
34 default_rrdpush_destination = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "destination", "");
35 default_rrdpush_api_key = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "api key", "");
36 rrdhost_free_orphan_time = config_get_number(CONFIG_SECTION_GLOBAL, "cleanup orphan hosts after seconds", rrdhost_free_orphan_time);
38 if(default_rrdpush_enabled && (!default_rrdpush_destination || !*default_rrdpush_destination || !default_rrdpush_api_key || !*default_rrdpush_api_key)) {
39 error("STREAM [send]: cannot enable sending thread - information is missing.");
40 default_rrdpush_enabled = 0;
43 return default_rrdpush_enabled;
46 #define CONNECTED_TO_SIZE 100
48 // data collection happens from multiple threads
49 // each of these threads calls rrdset_done()
50 // which in turn calls rrdset_done_push()
51 // which uses this pipe to notify the streaming thread
52 // that there are more data ready to be sent
56 // to have the remote netdata re-sync the charts
57 // to its current clock, we send for this many
58 // iterations a BEGIN line without microseconds
59 // this is for the first iterations of each chart
60 static unsigned int remote_clock_resync_iterations = 60;
62 #define rrdpush_lock(host) netdata_mutex_lock(&((host)->rrdpush_mutex))
63 #define rrdpush_unlock(host) netdata_mutex_unlock(&((host)->rrdpush_mutex))
65 // checks if the current chart definition has been sent
66 static inline int need_to_send_chart_definition(RRDSET *st) {
68 rrddim_foreach_read(rd, st)
75 // sends the current chart definition
76 static inline void send_chart_definition(RRDSET *st) {
77 buffer_sprintf(st->rrdhost->rrdpush_buffer, "CHART '%s' '%s' '%s' '%s' '%s' '%s' '%s' %ld %d\n"
84 , rrdset_type_name(st->chart_type)
90 rrddim_foreach_read(rd, st) {
91 buffer_sprintf(st->rrdhost->rrdpush_buffer, "DIMENSION '%s' '%s' '%s' " COLLECTED_NUMBER_FORMAT " " COLLECTED_NUMBER_FORMAT " '%s %s'\n"
94 , rrd_algorithm_name(rd->algorithm)
97 , rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?"hidden":""
98 , rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)?"noreset":""
104 // sends the current chart dimensions
105 static inline void send_chart_metrics(RRDSET *st) {
106 buffer_sprintf(st->rrdhost->rrdpush_buffer, "BEGIN %s %llu\n", st->id, (st->counter_done > remote_clock_resync_iterations)?st->usec_since_last_update:0);
109 rrddim_foreach_read(rd, st) {
110 if(rd->updated && rd->exposed)
111 buffer_sprintf(st->rrdhost->rrdpush_buffer, "SET %s = " COLLECTED_NUMBER_FORMAT "\n"
113 , rd->collected_value
117 buffer_strcat(st->rrdhost->rrdpush_buffer, "END\n");
120 void rrdpush_sender_thread_spawn(RRDHOST *host);
122 void rrdset_done_push(RRDSET *st) {
123 RRDHOST *host = st->rrdhost;
125 if(unlikely(!rrdset_flag_check(st, RRDSET_FLAG_ENABLED)))
130 if(unlikely(host->rrdpush_enabled && !host->rrdpush_spawn))
131 rrdpush_sender_thread_spawn(host);
133 if(unlikely(!host->rrdpush_buffer || !host->rrdpush_connected)) {
134 if(unlikely(!host->rrdpush_error_shown))
135 error("STREAM %s [send]: not ready - discarding collected metrics.", host->hostname);
137 host->rrdpush_error_shown = 1;
139 rrdpush_unlock(host);
142 else if(unlikely(host->rrdpush_error_shown)) {
143 info("STREAM %s [send]: ready - sending metrics...", host->hostname);
144 host->rrdpush_error_shown = 0;
147 if(need_to_send_chart_definition(st))
148 send_chart_definition(st);
150 send_chart_metrics(st);
152 // signal the sender there are more data
153 if(write(host->rrdpush_pipe[PIPE_WRITE], " ", 1) == -1)
154 error("STREAM %s [send]: cannot write to internal pipe", host->hostname);
156 rrdpush_unlock(host);
159 // ----------------------------------------------------------------------------
160 // rrdpush sender thread
162 // resets all the chart, so that their definitions
163 // will be resent to the central netdata
164 static void rrdpush_sender_thread_reset_all_charts(RRDHOST *host) {
165 rrdhost_rdlock(host);
168 rrdset_foreach_read(st, host) {
170 // make it re-align the current time
171 // on the remote host
172 st->counter_done = 0;
177 rrddim_foreach_read(rd, st)
183 rrdhost_unlock(host);
186 static inline void rrdpush_sender_thread_data_flush(RRDHOST *host) {
189 if(buffer_strlen(host->rrdpush_buffer))
190 error("STREAM %s [send]: discarding %zu bytes of metrics already in the buffer.", host->hostname, buffer_strlen(host->rrdpush_buffer));
192 buffer_flush(host->rrdpush_buffer);
194 rrdpush_sender_thread_reset_all_charts(host);
196 rrdpush_unlock(host);
199 static void rrdpush_sender_thread_cleanup_locked_all(RRDHOST *host) {
200 host->rrdpush_connected = 0;
202 if(host->rrdpush_socket != -1) {
203 close(host->rrdpush_socket);
204 host->rrdpush_socket = -1;
208 if(host->rrdpush_pipe[PIPE_READ] != -1) {
209 close(host->rrdpush_pipe[PIPE_READ]);
210 host->rrdpush_pipe[PIPE_READ] = -1;
213 if(host->rrdpush_pipe[PIPE_WRITE] != -1) {
214 close(host->rrdpush_pipe[PIPE_WRITE]);
215 host->rrdpush_pipe[PIPE_WRITE] = -1;
218 buffer_free(host->rrdpush_buffer);
219 host->rrdpush_buffer = NULL;
221 host->rrdpush_spawn = 0;
223 rrdhost_flag_set(host, RRDHOST_ORPHAN);
226 void rrdpush_sender_thread_stop(RRDHOST *host) {
228 rrdhost_wrlock(host);
230 if(host->rrdpush_spawn) {
231 info("STREAM %s [send]: stopping sending thread...", host->hostname);
232 pthread_cancel(host->rrdpush_thread);
233 rrdpush_sender_thread_cleanup_locked_all(host);
236 rrdhost_unlock(host);
237 rrdpush_unlock(host);
240 void *rrdpush_sender_thread(void *ptr) {
241 RRDHOST *host = (RRDHOST *)ptr;
243 info("STREAM %s [send]: thread created (task id %d)", host->hostname, gettid());
245 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
246 error("STREAM %s [send]: cannot set pthread cancel type to DEFERRED.", host->hostname);
248 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
249 error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
251 int timeout = (int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "timeout seconds", 60);
252 int default_port = (int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "default port", 19999);
253 size_t max_size = (size_t)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "buffer size bytes", 1024 * 1024);
254 unsigned int reconnect_delay = (unsigned int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "reconnect delay seconds", 5);
255 remote_clock_resync_iterations = (unsigned int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "initial clock resync iterations", remote_clock_resync_iterations);
256 char connected_to[CONNECTED_TO_SIZE + 1] = "";
258 if(!host->rrdpush_enabled || !host->rrdpush_destination || !*host->rrdpush_destination || !host->rrdpush_api_key || !*host->rrdpush_api_key)
261 // initialize rrdpush globals
262 host->rrdpush_buffer = buffer_create(1);
263 host->rrdpush_connected = 0;
264 if(pipe(host->rrdpush_pipe) == -1) fatal("STREAM %s [send]: cannot create required pipe.", host->hostname);
266 // initialize local variables
268 size_t reconnects_counter = 0;
269 size_t sent_bytes = 0;
270 size_t sent_connection = 0;
272 struct timeval tv = {
277 struct pollfd fds[2], *ifd, *ofd;
283 for(; host->rrdpush_enabled && !netdata_exit ;) {
285 if(unlikely(host->rrdpush_socket == -1)) {
286 // stop appending data into rrdpush_buffer
287 // they will be lost, so there is no point to do it
288 host->rrdpush_connected = 0;
290 info("STREAM %s [send to %s]: connecting...", host->hostname, host->rrdpush_destination);
291 host->rrdpush_socket = connect_to_one_of(host->rrdpush_destination, default_port, &tv, &reconnects_counter, connected_to, CONNECTED_TO_SIZE);
293 if(unlikely(host->rrdpush_socket == -1)) {
294 error("STREAM %s [send to %s]: failed to connect", host->hostname, host->rrdpush_destination);
295 sleep(reconnect_delay);
299 info("STREAM %s [send to %s]: initializing communication...", host->hostname, connected_to);
302 snprintfz(http, 1000,
303 "STREAM key=%s&hostname=%s&machine_guid=%s&os=%s&update_every=%d HTTP/1.1\r\n"
304 "User-Agent: netdata-push-service/%s\r\n"
305 "Accept: */*\r\n\r\n"
306 , host->rrdpush_api_key
310 , default_rrd_update_every
314 if(send_timeout(host->rrdpush_socket, http, strlen(http), 0, timeout) == -1) {
315 close(host->rrdpush_socket);
316 host->rrdpush_socket = -1;
317 error("STREAM %s [send to %s]: failed to send http header to netdata", host->hostname, connected_to);
318 sleep(reconnect_delay);
322 info("STREAM %s [send to %s]: waiting response from remote netdata...", host->hostname, connected_to);
324 if(recv_timeout(host->rrdpush_socket, http, 1000, 0, timeout) == -1) {
325 close(host->rrdpush_socket);
326 host->rrdpush_socket = -1;
327 error("STREAM %s [send to %s]: failed to initialize communication", host->hostname, connected_to);
328 sleep(reconnect_delay);
332 if(strncmp(http, START_STREAMING_PROMPT, strlen(START_STREAMING_PROMPT))) {
333 close(host->rrdpush_socket);
334 host->rrdpush_socket = -1;
335 error("STREAM %s [send to %s]: server is not replying properly.", host->hostname, connected_to);
336 sleep(reconnect_delay);
340 info("STREAM %s [send to %s]: established communication - sending metrics...", host->hostname, connected_to);
342 if(fcntl(host->rrdpush_socket, F_SETFL, O_NONBLOCK) < 0)
343 error("STREAM %s [send to %s]: cannot set non-blocking mode for socket.", host->hostname, connected_to);
345 rrdpush_sender_thread_data_flush(host);
348 // allow appending data into rrdpush_buffer
349 host->rrdpush_connected = 1;
352 ifd->fd = host->rrdpush_pipe[PIPE_READ];
353 ifd->events = POLLIN;
356 ofd->fd = host->rrdpush_socket;
358 if(begin < buffer_strlen(host->rrdpush_buffer)) {
359 ofd->events = POLLOUT;
367 if(netdata_exit) break;
368 int retval = poll(fds, fdmax, timeout * 1000);
369 if(netdata_exit) break;
371 if(unlikely(retval == -1)) {
372 if(errno == EAGAIN || errno == EINTR)
375 error("STREAM %s [send to %s]: failed to poll().", host->hostname, connected_to);
376 close(host->rrdpush_socket);
377 host->rrdpush_socket = -1;
380 else if(unlikely(!retval)) {
385 if(ifd->revents & POLLIN) {
386 char buffer[1000 + 1];
387 if(read(host->rrdpush_pipe[PIPE_READ], buffer, 1000) == -1)
388 error("STREAM %s [send to %s]: cannot read from internal pipe.", host->hostname, connected_to);
391 if(ofd->revents & POLLOUT && begin < buffer_strlen(host->rrdpush_buffer)) {
393 // BEGIN RRDPUSH LOCKED SESSION
395 // during this session, data collectors
396 // will not be able to append data to our buffer
397 // but the socket is in non-blocking mode
398 // so, we will not block at send()
400 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
401 error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
405 ssize_t ret = send(host->rrdpush_socket, &host->rrdpush_buffer->buffer[begin], buffer_strlen(host->rrdpush_buffer) - begin, MSG_DONTWAIT);
407 if(errno != EAGAIN && errno != EINTR) {
408 error("STREAM %s [send to %s]: failed to send metrics - closing connection - we have sent %zu bytes on this connection.", host->hostname, connected_to, sent_connection);
409 close(host->rrdpush_socket);
410 host->rrdpush_socket = -1;
414 sent_connection += ret;
417 if(begin == buffer_strlen(host->rrdpush_buffer)) {
420 buffer_flush(host->rrdpush_buffer);
425 rrdpush_unlock(host);
427 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
428 error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
430 // END RRDPUSH LOCKED SESSION
433 // protection from overflow
434 if(host->rrdpush_buffer->len > max_size) {
436 error("STREAM %s [send to %s]: too many data pending - buffer is %zu bytes long, %zu unsent - we have sent %zu bytes in total, %zu on this connection. Closing connection to flush the data.", host->hostname, connected_to, host->rrdpush_buffer->len, host->rrdpush_buffer->len - begin, sent_bytes, sent_connection);
437 if(host->rrdpush_socket != -1) {
438 close(host->rrdpush_socket);
439 host->rrdpush_socket = -1;
445 debug(D_WEB_CLIENT, "STREAM %s [send]: sending thread exits.", host->hostname);
447 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
448 error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
451 rrdhost_wrlock(host);
452 rrdpush_sender_thread_cleanup_locked_all(host);
453 rrdhost_unlock(host);
454 rrdpush_unlock(host);
456 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
457 error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
464 // ----------------------------------------------------------------------------
465 // rrdpush receiver thread
467 int rrdpush_receive(int fd, const char *key, const char *hostname, const char *machine_guid, const char *os, int update_every, char *client_ip, char *client_port) {
469 int history = default_rrd_history_entries;
470 RRD_MEMORY_MODE mode = default_rrd_memory_mode;
471 int health_enabled = default_health_enabled;
472 int rrdpush_enabled = default_rrdpush_enabled;
473 char *rrdpush_destination = default_rrdpush_destination;
474 char *rrdpush_api_key = default_rrdpush_api_key;
475 time_t alarms_delay = 60;
477 update_every = (int)appconfig_get_number(&stream_config, machine_guid, "update every", update_every);
478 if(update_every < 0) update_every = 1;
480 history = (int)appconfig_get_number(&stream_config, key, "default history", history);
481 history = (int)appconfig_get_number(&stream_config, machine_guid, "history", history);
482 if(history < 5) history = 5;
484 mode = rrd_memory_mode_id(appconfig_get(&stream_config, key, "default memory mode", rrd_memory_mode_name(mode)));
485 mode = rrd_memory_mode_id(appconfig_get(&stream_config, machine_guid, "memory mode", rrd_memory_mode_name(mode)));
487 health_enabled = appconfig_get_boolean_ondemand(&stream_config, key, "health enabled by default", health_enabled);
488 health_enabled = appconfig_get_boolean_ondemand(&stream_config, machine_guid, "health enabled", health_enabled);
490 alarms_delay = appconfig_get_number(&stream_config, key, "default postpone alarms on connect seconds", alarms_delay);
491 alarms_delay = appconfig_get_number(&stream_config, machine_guid, "postpone alarms on connect seconds", alarms_delay);
493 rrdpush_enabled = appconfig_get_boolean(&stream_config, key, "default proxy enabled", rrdpush_enabled);
494 rrdpush_enabled = appconfig_get_boolean(&stream_config, machine_guid, "proxy enabled", rrdpush_enabled);
496 rrdpush_destination = appconfig_get(&stream_config, key, "default proxy destination", rrdpush_destination);
497 rrdpush_destination = appconfig_get(&stream_config, machine_guid, "proxy destination", rrdpush_destination);
499 rrdpush_api_key = appconfig_get(&stream_config, key, "default proxy api key", rrdpush_api_key);
500 rrdpush_api_key = appconfig_get(&stream_config, machine_guid, "proxy api key", rrdpush_api_key);
502 if(!strcmp(machine_guid, "localhost"))
505 host = rrdhost_find_or_create(
512 , (health_enabled != CONFIG_BOOLEAN_NO)
513 , (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key)
514 , rrdpush_destination
520 error("STREAM %s [receive from [%s]:%s]: failed to find/create host structure.", hostname, client_ip, client_port);
524 #ifdef NETDATA_INTERNAL_CHECKS
525 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"
531 , host->rrd_update_every
532 , host->rrd_history_entries
533 , rrd_memory_mode_name(host->rrd_memory_mode)
534 , (health_enabled == CONFIG_BOOLEAN_NO)?"disabled":((health_enabled == CONFIG_BOOLEAN_YES)?"enabled":"auto")
536 #endif // NETDATA_INTERNAL_CHECKS
538 struct plugind cd = {
540 .update_every = default_rrd_update_every,
542 .serial_failures = 0,
543 .successful_collections = 0,
545 .started_t = now_realtime_sec(),
549 // put the client IP and port into the buffers used by plugins.d
550 snprintfz(cd.id, CONFIG_MAX_NAME, "%s:%s", client_ip, client_port);
551 snprintfz(cd.filename, FILENAME_MAX, "%s:%s", client_ip, client_port);
552 snprintfz(cd.fullfilename, FILENAME_MAX, "%s:%s", client_ip, client_port);
553 snprintfz(cd.cmd, PLUGINSD_CMD_MAX, "%s:%s", client_ip, client_port);
555 info("STREAM %s [receive from [%s]:%s]: initializing communication...", host->hostname, client_ip, client_port);
556 if(send_timeout(fd, START_STREAMING_PROMPT, strlen(START_STREAMING_PROMPT), 0, 60) != strlen(START_STREAMING_PROMPT)) {
557 error("STREAM %s [receive from [%s]:%s]: cannot send ready command.", host->hostname, client_ip, client_port);
562 // remove the non-blocking flag from the socket
563 if(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK) == -1)
564 error("STREAM %s [receive from [%s]:%s]: cannot remove the non-blocking flag from socket %d", host->hostname, client_ip, client_port, fd);
566 // convert the socket to a FILE *
567 FILE *fp = fdopen(fd, "r");
569 error("STREAM %s [receive from [%s]:%s]: failed to get a FILE for FD %d.", host->hostname, client_ip, client_port, fd);
574 rrdhost_wrlock(host);
575 host->connected_senders++;
576 if(health_enabled != CONFIG_BOOLEAN_NO)
577 host->health_delay_up_to = now_realtime_sec() + alarms_delay;
578 rrdhost_unlock(host);
580 // call the plugins.d processor to receive the metrics
581 info("STREAM %s [receive from [%s]:%s]: receiving metrics...", host->hostname, client_ip, client_port);
582 size_t count = pluginsd_process(host, &cd, fp, 1);
583 error("STREAM %s [receive from [%s]:%s]: disconnected (completed updates %zu).", host->hostname, client_ip, client_port, count);
585 rrdhost_wrlock(host);
586 host->senders_disconnected_time = now_realtime_sec();
587 host->connected_senders--;
588 if(!host->connected_senders) {
589 if(health_enabled == CONFIG_BOOLEAN_AUTO)
590 host->health_enabled = 0;
592 rrdhost_unlock(host);
594 rrdpush_sender_thread_stop(host);
602 struct rrdpush_thread {
613 void *rrdpush_receiver_thread(void *ptr) {
614 struct rrdpush_thread *rpt = (struct rrdpush_thread *)ptr;
616 if (pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
617 error("STREAM %s [receive]: cannot set pthread cancel type to DEFERRED.", rpt->hostname);
619 if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
620 error("STREAM %s [receive]: cannot set pthread cancel state to ENABLE.", rpt->hostname);
623 info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
624 rrdpush_receive(rpt->fd, rpt->key, rpt->hostname, rpt->machine_guid, rpt->os, rpt->update_every, rpt->client_ip, rpt->client_port);
625 info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
628 freez(rpt->hostname);
629 freez(rpt->machine_guid);
631 freez(rpt->client_ip);
632 freez(rpt->client_port);
639 void rrdpush_sender_thread_spawn(RRDHOST *host) {
640 rrdhost_wrlock(host);
642 if(!host->rrdpush_spawn) {
643 if(pthread_create(&host->rrdpush_thread, NULL, rrdpush_sender_thread, (void *) host))
644 error("STREAM %s [send]: failed to create new thread for client.", host->hostname);
646 else if(pthread_detach(host->rrdpush_thread))
647 error("STREAM %s [send]: cannot request detach newly created thread.", host->hostname);
649 rrdhost_flag_clear(host, RRDHOST_ORPHAN);
650 host->rrdpush_spawn = 1;
653 rrdhost_unlock(host);
656 int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url) {
659 info("STREAM [receive from [%s]:%s]: new client connection.", w->client_ip, w->client_port);
661 char *key = NULL, *hostname = NULL, *machine_guid = NULL, *os = "unknown";
662 int update_every = default_rrd_update_every;
663 char buf[GUID_LEN + 1];
666 char *value = mystrsep(&url, "?&");
667 if(!value || !*value) continue;
669 char *name = mystrsep(&value, "=");
670 if(!name || !*name) continue;
671 if(!value || !*value) continue;
673 if(!strcmp(name, "key"))
675 else if(!strcmp(name, "hostname"))
677 else if(!strcmp(name, "machine_guid"))
678 machine_guid = value;
679 else if(!strcmp(name, "update_every"))
680 update_every = (int)strtoul(value, NULL, 0);
681 else if(!strcmp(name, "os"))
686 error("STREAM [receive from [%s]:%s]: request without an API key. Forbidding access.", w->client_ip, w->client_port);
687 buffer_flush(w->response.data);
688 buffer_sprintf(w->response.data, "You need an API key for this request.");
692 if(!hostname || !*hostname) {
693 error("STREAM [receive from [%s]:%s]: request without a hostname. Forbidding access.", w->client_ip, w->client_port);
694 buffer_flush(w->response.data);
695 buffer_sprintf(w->response.data, "You need to send a hostname too.");
699 if(!machine_guid || !*machine_guid) {
700 error("STREAM [receive from [%s]:%s]: request without a machine GUID. Forbidding access.", w->client_ip, w->client_port);
701 buffer_flush(w->response.data);
702 buffer_sprintf(w->response.data, "You need to send a machine GUID too.");
706 if(regenerate_guid(key, buf) == -1) {
707 error("STREAM [receive from [%s]:%s]: API key '%s' is not valid GUID. Forbidding access.", w->client_ip, w->client_port, key);
708 buffer_flush(w->response.data);
709 buffer_sprintf(w->response.data, "Your API key is invalid.");
713 if(regenerate_guid(machine_guid, buf) == -1) {
714 error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not GUID. Forbidding access.", w->client_ip, w->client_port, key);
715 buffer_flush(w->response.data);
716 buffer_sprintf(w->response.data, "Your machine GUID is invalid.");
720 if(!appconfig_get_boolean(&stream_config, key, "enabled", 0)) {
721 error("STREAM [receive from [%s]:%s]: API key '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, machine_guid);
722 buffer_flush(w->response.data);
723 buffer_sprintf(w->response.data, "Your API key is not permitted access.");
727 if(!appconfig_get_boolean(&stream_config, machine_guid, "enabled", 1)) {
728 error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, machine_guid);
729 buffer_flush(w->response.data);
730 buffer_sprintf(w->response.data, "Your machine guide is not permitted access.");
734 struct rrdpush_thread *rpt = mallocz(sizeof(struct rrdpush_thread));
736 rpt->key = strdupz(key);
737 rpt->hostname = strdupz(hostname);
738 rpt->machine_guid = strdupz(machine_guid);
739 rpt->os = strdupz(os);
740 rpt->client_ip = strdupz(w->client_ip);
741 rpt->client_port = strdupz(w->client_port);
742 rpt->update_every = update_every;
745 debug(D_SYSTEM, "STREAM [receive from [%s]:%s]: starting receiving thread.", w->client_ip, w->client_port);
747 if(pthread_create(&thread, NULL, rrdpush_receiver_thread, (void *)rpt))
748 error("STREAM [receive from [%s]:%s]: failed to create new thread for client.", w->client_ip, w->client_port);
750 else if(pthread_detach(thread))
751 error("STREAM [receive from [%s]:%s]: cannot request detach newly created thread.", w->client_ip, w->client_port);
753 // prevent the caller from closing the streaming socket
755 w->ifd = w->ofd = -1;
759 buffer_flush(w->response.data);