summaryrefslogtreecommitdiffstats
path: root/src/rrdpush.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rrdpush.c')
-rw-r--r--src/rrdpush.c860
1 files changed, 521 insertions, 339 deletions
diff --git a/src/rrdpush.c b/src/rrdpush.c
index c1d052fd8..2d10c3ca9 100644
--- a/src/rrdpush.c
+++ b/src/rrdpush.c
@@ -59,72 +59,100 @@ int rrdpush_init() {
// this is for the first iterations of each chart
unsigned int remote_clock_resync_iterations = 60;
-#define rrdpush_lock(host) netdata_mutex_lock(&((host)->rrdpush_mutex))
-#define rrdpush_unlock(host) netdata_mutex_unlock(&((host)->rrdpush_mutex))
+#define rrdpush_buffer_lock(host) netdata_mutex_lock(&((host)->rrdpush_sender_buffer_mutex))
+#define rrdpush_buffer_unlock(host) netdata_mutex_unlock(&((host)->rrdpush_sender_buffer_mutex))
// checks if the current chart definition has been sent
static inline int need_to_send_chart_definition(RRDSET *st) {
+ rrdset_check_rdlock(st);
+
if(unlikely(!(rrdset_flag_check(st, RRDSET_FLAG_EXPOSED_UPSTREAM))))
return 1;
RRDDIM *rd;
rrddim_foreach_read(rd, st)
- if(!rd->exposed)
+ if(unlikely(!rd->exposed))
return 1;
return 0;
}
// sends the current chart definition
-static inline void send_chart_definition(RRDSET *st) {
+static inline void rrdpush_send_chart_definition_nolock(RRDSET *st) {
+ RRDHOST *host = st->rrdhost;
+
rrdset_flag_set(st, RRDSET_FLAG_EXPOSED_UPSTREAM);
- buffer_sprintf(st->rrdhost->rrdpush_buffer, "CHART \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" %ld %d \"%s %s %s\"\n"
- , st->id
- , st->name
- , st->title
- , st->units
- , st->family
- , st->context
- , rrdset_type_name(st->chart_type)
- , st->priority
- , st->update_every
- , rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)?"obsolete":""
- , rrdset_flag_check(st, RRDSET_FLAG_DETAIL)?"detail":""
- , rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)?"store_first":""
+ // send the chart
+ buffer_sprintf(
+ host->rrdpush_sender_buffer
+ , "CHART \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" %ld %d \"%s %s %s\" \"%s\" \"%s\"\n"
+ , st->id
+ , st->name
+ , st->title
+ , st->units
+ , st->family
+ , st->context
+ , rrdset_type_name(st->chart_type)
+ , st->priority
+ , st->update_every
+ , rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)?"obsolete":""
+ , rrdset_flag_check(st, RRDSET_FLAG_DETAIL)?"detail":""
+ , rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)?"store_first":""
+ , (st->plugin_name)?st->plugin_name:""
+ , (st->module_name)?st->module_name:""
);
+ // send the dimensions
RRDDIM *rd;
rrddim_foreach_read(rd, st) {
- buffer_sprintf(st->rrdhost->rrdpush_buffer, "DIMENSION \"%s\" \"%s\" \"%s\" " COLLECTED_NUMBER_FORMAT " " COLLECTED_NUMBER_FORMAT " \"%s %s\"\n"
- , rd->id
- , rd->name
- , rrd_algorithm_name(rd->algorithm)
- , rd->multiplier
- , rd->divisor
- , rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?"hidden":""
- , rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)?"noreset":""
+ buffer_sprintf(
+ host->rrdpush_sender_buffer
+ , "DIMENSION \"%s\" \"%s\" \"%s\" " COLLECTED_NUMBER_FORMAT " " COLLECTED_NUMBER_FORMAT " \"%s %s\"\n"
+ , rd->id
+ , rd->name
+ , rrd_algorithm_name(rd->algorithm)
+ , rd->multiplier
+ , rd->divisor
+ , rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?"hidden":""
+ , rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)?"noreset":""
);
rd->exposed = 1;
}
+ // send the chart local custom variables
+ RRDSETVAR *rs;
+ for(rs = st->variables; rs ;rs = rs->next) {
+ if(unlikely(rs->options && RRDVAR_OPTION_ALLOCATED)) {
+ calculated_number *value = (calculated_number *) rs->value;
+
+ buffer_sprintf(
+ host->rrdpush_sender_buffer
+ , "VARIABLE CHART %s = " CALCULATED_NUMBER_FORMAT "\n"
+ , rs->variable
+ , *value
+ );
+ }
+ }
+
st->upstream_resync_time = st->last_collected_time.tv_sec + (remote_clock_resync_iterations * st->update_every);
}
// sends the current chart dimensions
-static inline void send_chart_metrics(RRDSET *st) {
- buffer_sprintf(st->rrdhost->rrdpush_buffer, "BEGIN \"%s\" %llu\n", st->id, (st->upstream_resync_time > st->last_collected_time.tv_sec)?st->usec_since_last_update:0);
+static inline void rrdpush_send_chart_metrics_nolock(RRDSET *st) {
+ buffer_sprintf(st->rrdhost->rrdpush_sender_buffer, "BEGIN \"%s\" %llu\n", st->id, (st->upstream_resync_time > st->last_collected_time.tv_sec)?st->usec_since_last_update:0);
RRDDIM *rd;
rrddim_foreach_read(rd, st) {
if(rd->updated && rd->exposed)
- buffer_sprintf(st->rrdhost->rrdpush_buffer, "SET \"%s\" = " COLLECTED_NUMBER_FORMAT "\n"
- , rd->id
- , rd->collected_value
+ buffer_sprintf(st->rrdhost->rrdpush_sender_buffer
+ , "SET \"%s\" = " COLLECTED_NUMBER_FORMAT "\n"
+ , rd->id
+ , rd->collected_value
);
}
- buffer_strcat(st->rrdhost->rrdpush_buffer, "END\n");
+ buffer_strcat(st->rrdhost->rrdpush_sender_buffer, "END\n");
}
static void rrdpush_sender_thread_spawn(RRDHOST *host);
@@ -133,9 +161,9 @@ void rrdset_push_chart_definition(RRDSET *st) {
RRDHOST *host = st->rrdhost;
rrdset_rdlock(st);
- rrdpush_lock(host);
- send_chart_definition(st);
- rrdpush_unlock(host);
+ rrdpush_buffer_lock(host);
+ rrdpush_send_chart_definition_nolock(st);
+ rrdpush_buffer_unlock(host);
rrdset_unlock(st);
}
@@ -145,40 +173,81 @@ void rrdset_done_push(RRDSET *st) {
if(unlikely(!rrdset_flag_check(st, RRDSET_FLAG_ENABLED)))
return;
- rrdpush_lock(host);
+ rrdpush_buffer_lock(host);
- if(unlikely(host->rrdpush_enabled && !host->rrdpush_spawn))
+ if(unlikely(host->rrdpush_send_enabled && !host->rrdpush_sender_spawn))
rrdpush_sender_thread_spawn(host);
- if(unlikely(!host->rrdpush_buffer || !host->rrdpush_connected)) {
- if(unlikely(!host->rrdpush_error_shown))
+ if(unlikely(!host->rrdpush_sender_buffer || !host->rrdpush_sender_connected)) {
+ if(unlikely(!host->rrdpush_sender_error_shown))
error("STREAM %s [send]: not ready - discarding collected metrics.", host->hostname);
- host->rrdpush_error_shown = 1;
+ host->rrdpush_sender_error_shown = 1;
- rrdpush_unlock(host);
+ rrdpush_buffer_unlock(host);
return;
}
- else if(unlikely(host->rrdpush_error_shown)) {
- info("STREAM %s [send]: ready - sending metrics...", host->hostname);
- host->rrdpush_error_shown = 0;
+ else if(unlikely(host->rrdpush_sender_error_shown)) {
+ info("STREAM %s [send]: sending metrics...", host->hostname);
+ host->rrdpush_sender_error_shown = 0;
}
if(need_to_send_chart_definition(st))
- send_chart_definition(st);
+ rrdpush_send_chart_definition_nolock(st);
- send_chart_metrics(st);
+ rrdpush_send_chart_metrics_nolock(st);
// signal the sender there are more data
- if(write(host->rrdpush_pipe[PIPE_WRITE], " ", 1) == -1)
+ if(host->rrdpush_sender_pipe[PIPE_WRITE] != -1 && write(host->rrdpush_sender_pipe[PIPE_WRITE], " ", 1) == -1)
error("STREAM %s [send]: cannot write to internal pipe", host->hostname);
- rrdpush_unlock(host);
+ rrdpush_buffer_unlock(host);
}
// ----------------------------------------------------------------------------
// rrdpush sender thread
+static inline void rrdpush_sender_add_host_variable_to_buffer_nolock(RRDHOST *host, RRDVAR *rv) {
+ calculated_number *value = (calculated_number *)rv->value;
+
+ buffer_sprintf(
+ host->rrdpush_sender_buffer
+ , "VARIABLE HOST %s = " CALCULATED_NUMBER_FORMAT "\n"
+ , rv->name
+ , *value
+ );
+
+ debug(D_STREAM, "RRDVAR pushed HOST VARIABLE %s = " CALCULATED_NUMBER_FORMAT, rv->name, *value);
+}
+
+void rrdpush_sender_send_this_host_variable_now(RRDHOST *host, RRDVAR *rv) {
+ if(host->rrdpush_send_enabled && host->rrdpush_sender_spawn && host->rrdpush_sender_connected) {
+ rrdpush_buffer_lock(host);
+ rrdpush_sender_add_host_variable_to_buffer_nolock(host, rv);
+ rrdpush_buffer_unlock(host);
+ }
+}
+
+static int rrdpush_sender_thread_custom_host_variables_callback(void *rrdvar_ptr, void *host_ptr) {
+ RRDVAR *rv = (RRDVAR *)rrdvar_ptr;
+ RRDHOST *host = (RRDHOST *)host_ptr;
+
+ if(unlikely(rv->type == RRDVAR_TYPE_CALCULATED_ALLOCATED)) {
+ rrdpush_sender_add_host_variable_to_buffer_nolock(host, rv);
+
+ // return 1, so that the traversal will return the number of variables sent
+ return 1;
+ }
+
+ // returning a negative number will break the traversal
+ return 0;
+}
+
+static void rrdpush_sender_thread_send_custom_host_variables(RRDHOST *host) {
+ int ret = rrdvar_callback_for_all_host_variables(host, rrdpush_sender_thread_custom_host_variables_callback, host);
+ debug(D_STREAM, "RRDVAR sent %d VARIABLES", ret);
+}
+
// resets all the chart, so that their definitions
// will be resent to the central netdata
static void rrdpush_sender_thread_reset_all_charts(RRDHOST *host) {
@@ -186,6 +255,7 @@ static void rrdpush_sender_thread_reset_all_charts(RRDHOST *host) {
RRDSET *st;
rrdset_foreach_read(st, host) {
+ rrdset_flag_clear(st, RRDSET_FLAG_EXPOSED_UPSTREAM);
st->upstream_resync_time = 0;
@@ -202,68 +272,198 @@ static void rrdpush_sender_thread_reset_all_charts(RRDHOST *host) {
}
static inline void rrdpush_sender_thread_data_flush(RRDHOST *host) {
- rrdpush_lock(host);
+ rrdpush_buffer_lock(host);
- if(buffer_strlen(host->rrdpush_buffer))
- error("STREAM %s [send]: discarding %zu bytes of metrics already in the buffer.", host->hostname, buffer_strlen(host->rrdpush_buffer));
+ if(buffer_strlen(host->rrdpush_sender_buffer))
+ error("STREAM %s [send]: discarding %zu bytes of metrics already in the buffer.", host->hostname, buffer_strlen(host->rrdpush_sender_buffer));
- buffer_flush(host->rrdpush_buffer);
+ buffer_flush(host->rrdpush_sender_buffer);
rrdpush_sender_thread_reset_all_charts(host);
+ rrdpush_sender_thread_send_custom_host_variables(host);
- rrdpush_unlock(host);
+ rrdpush_buffer_unlock(host);
}
-static void rrdpush_sender_thread_cleanup_locked_all(RRDHOST *host) {
- host->rrdpush_connected = 0;
+void rrdpush_sender_thread_stop(RRDHOST *host) {
+ rrdpush_buffer_lock(host);
+ rrdhost_wrlock(host);
+
+ pthread_t thr = 0;
- if(host->rrdpush_socket != -1) {
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
+ if(host->rrdpush_sender_spawn) {
+ info("STREAM %s [send]: signaling sending thread to stop...", host->hostname);
+
+ // signal the thread that we want to join it
+ host->rrdpush_sender_join = 1;
+
+ // copy the thread id, so that we will be waiting for the right one
+ // even if a new one has been spawn
+ thr = host->rrdpush_sender_thread;
+
+ // signal it to cancel
+ int ret = pthread_cancel(host->rrdpush_sender_thread);
+ if(ret != 0)
+ error("STREAM %s [send]: pthread_cancel() returned error.", host->hostname);
}
- // close the pipe
- if(host->rrdpush_pipe[PIPE_READ] != -1) {
- close(host->rrdpush_pipe[PIPE_READ]);
- host->rrdpush_pipe[PIPE_READ] = -1;
+ rrdhost_unlock(host);
+ rrdpush_buffer_unlock(host);
+
+ if(thr != 0) {
+ info("STREAM %s [send]: waiting for the sending thread to stop...", host->hostname);
+
+ void *result;
+ int ret = pthread_join(thr, &result);
+ if(ret != 0)
+ error("STREAM %s [send]: pthread_join() returned error.", host->hostname);
+
+ info("STREAM %s [send]: sending thread has exited.", host->hostname);
+ }
+}
+
+static inline void rrdpush_sender_thread_close_socket(RRDHOST *host) {
+ host->rrdpush_sender_connected = 0;
+
+ if(host->rrdpush_sender_socket != -1) {
+ close(host->rrdpush_sender_socket);
+ host->rrdpush_sender_socket = -1;
+ }
+}
+
+static int rrdpush_sender_thread_connect_to_master(RRDHOST *host, int default_port, int timeout, size_t *reconnects_counter, char *connected_to, size_t connected_to_size) {
+ struct timeval tv = {
+ .tv_sec = timeout,
+ .tv_usec = 0
+ };
+
+ // make sure the socket is closed
+ rrdpush_sender_thread_close_socket(host);
+
+ debug(D_STREAM, "STREAM: Attempting to connect...");
+ info("STREAM %s [send to %s]: connecting...", host->hostname, host->rrdpush_send_destination);
+
+ host->rrdpush_sender_socket = connect_to_one_of(
+ host->rrdpush_send_destination
+ , default_port
+ , &tv
+ , reconnects_counter
+ , connected_to
+ , connected_to_size
+ );
+
+ if(unlikely(host->rrdpush_sender_socket == -1)) {
+ error("STREAM %s [send to %s]: failed to connect", host->hostname, host->rrdpush_send_destination);
+ return 0;
+ }
+
+ info("STREAM %s [send to %s]: initializing communication...", host->hostname, connected_to);
+
+ #define HTTP_HEADER_SIZE 8192
+ char http[HTTP_HEADER_SIZE + 1];
+ snprintfz(http, HTTP_HEADER_SIZE,
+ "STREAM key=%s&hostname=%s&registry_hostname=%s&machine_guid=%s&update_every=%d&os=%s&timezone=%s&tags=%s HTTP/1.1\r\n"
+ "User-Agent: netdata-push-service/%s\r\n"
+ "Accept: */*\r\n\r\n"
+ , host->rrdpush_send_api_key
+ , host->hostname
+ , host->registry_hostname
+ , host->machine_guid
+ , default_rrd_update_every
+ , host->os
+ , host->timezone
+ , (host->tags)?host->tags:""
+ , program_version
+ );
+
+ if(send_timeout(host->rrdpush_sender_socket, http, strlen(http), 0, timeout) == -1) {
+ error("STREAM %s [send to %s]: failed to send HTTP header to remote netdata.", host->hostname, connected_to);
+ rrdpush_sender_thread_close_socket(host);
+ return 0;
}
- if(host->rrdpush_pipe[PIPE_WRITE] != -1) {
- close(host->rrdpush_pipe[PIPE_WRITE]);
- host->rrdpush_pipe[PIPE_WRITE] = -1;
+ info("STREAM %s [send to %s]: waiting response from remote netdata...", host->hostname, connected_to);
+
+ if(recv_timeout(host->rrdpush_sender_socket, http, HTTP_HEADER_SIZE, 0, timeout) == -1) {
+ error("STREAM %s [send to %s]: remote netdata does not respond.", host->hostname, connected_to);
+ rrdpush_sender_thread_close_socket(host);
+ return 0;
+ }
+
+ if(strncmp(http, START_STREAMING_PROMPT, strlen(START_STREAMING_PROMPT)) != 0) {
+ error("STREAM %s [send to %s]: server is not replying properly (is it a netdata?).", host->hostname, connected_to);
+ rrdpush_sender_thread_close_socket(host);
+ return 0;
}
- buffer_free(host->rrdpush_buffer);
- host->rrdpush_buffer = NULL;
+ info("STREAM %s [send to %s]: established communication - ready to send metrics...", host->hostname, connected_to);
- host->rrdpush_spawn = 0;
+ if(sock_setnonblock(host->rrdpush_sender_socket) < 0)
+ error("STREAM %s [send to %s]: cannot set non-blocking mode for socket.", host->hostname, connected_to);
+
+ if(sock_enlarge_out(host->rrdpush_sender_socket) < 0)
+ error("STREAM %s [send to %s]: cannot enlarge the socket buffer.", host->hostname, connected_to);
+
+ debug(D_STREAM, "STREAM: Connected on fd %d...", host->rrdpush_sender_socket);
+
+ return 1;
}
-void rrdpush_sender_thread_stop(RRDHOST *host) {
- rrdpush_lock(host);
+static void rrdpush_sender_thread_cleanup_callback(void *ptr) {
+ RRDHOST *host = (RRDHOST *)ptr;
+
+ rrdpush_buffer_lock(host);
rrdhost_wrlock(host);
- if(host->rrdpush_spawn) {
- info("STREAM %s [send]: stopping sending thread...", host->hostname);
- pthread_cancel(host->rrdpush_thread);
- rrdpush_sender_thread_cleanup_locked_all(host);
+ info("STREAM %s [send]: sending thread cleans up...", host->hostname);
+
+ rrdpush_sender_thread_close_socket(host);
+
+ // close the pipe
+ if(host->rrdpush_sender_pipe[PIPE_READ] != -1) {
+ close(host->rrdpush_sender_pipe[PIPE_READ]);
+ host->rrdpush_sender_pipe[PIPE_READ] = -1;
+ }
+
+ if(host->rrdpush_sender_pipe[PIPE_WRITE] != -1) {
+ close(host->rrdpush_sender_pipe[PIPE_WRITE]);
+ host->rrdpush_sender_pipe[PIPE_WRITE] = -1;
}
+ buffer_free(host->rrdpush_sender_buffer);
+ host->rrdpush_sender_buffer = NULL;
+
+ if(!host->rrdpush_sender_join) {
+ info("STREAM %s [send]: sending thread detaches itself.", host->hostname);
+ if(pthread_detach(pthread_self()))
+ error("STREAM %s [send]: pthread_detach() failed.", host->hostname);
+ }
+
+ host->rrdpush_sender_spawn = 0;
+
+ info("STREAM %s [send]: sending thread now exits.", host->hostname);
+
rrdhost_unlock(host);
- rrdpush_unlock(host);
+ rrdpush_buffer_unlock(host);
}
void *rrdpush_sender_thread(void *ptr) {
RRDHOST *host = (RRDHOST *)ptr;
- info("STREAM %s [send]: thread created (task id %d)", host->hostname, gettid());
+ if(!host->rrdpush_send_enabled || !host->rrdpush_send_destination || !*host->rrdpush_send_destination || !host->rrdpush_send_api_key || !*host->rrdpush_send_api_key) {
+ error("STREAM %s [send]: thread created (task id %d), but host has streaming disabled.", host->hostname, gettid());
+ pthread_exit(NULL);
+ return NULL;
+ }
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
- error("STREAM %s [send]: cannot set pthread cancel type to DEFERRED.", host->hostname);
+ info("STREAM %s [send]: thread created (task id %d)", host->hostname, gettid());
if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
+ if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
+ error("STREAM %s [send]: cannot set pthread cancel type to DEFERRED.", host->hostname);
+
int timeout = (int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "timeout seconds", 60);
int default_port = (int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "default port", 19999);
size_t max_size = (size_t)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "buffer size bytes", 1024 * 1024);
@@ -271,24 +471,17 @@ void *rrdpush_sender_thread(void *ptr) {
remote_clock_resync_iterations = (unsigned int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "initial clock resync iterations", remote_clock_resync_iterations);
char connected_to[CONNECTED_TO_SIZE + 1] = "";
- if(!host->rrdpush_enabled || !host->rrdpush_destination || !*host->rrdpush_destination || !host->rrdpush_api_key || !*host->rrdpush_api_key)
- goto cleanup;
-
// initialize rrdpush globals
- host->rrdpush_buffer = buffer_create(1);
- host->rrdpush_connected = 0;
- if(pipe(host->rrdpush_pipe) == -1) fatal("STREAM %s [send]: cannot create required pipe.", host->hostname);
+ host->rrdpush_sender_buffer = buffer_create(1);
+ host->rrdpush_sender_connected = 0;
+ if(pipe(host->rrdpush_sender_pipe) == -1) fatal("STREAM %s [send]: cannot create required pipe.", host->hostname);
// initialize local variables
size_t begin = 0;
size_t reconnects_counter = 0;
size_t sent_bytes = 0;
- size_t sent_connection = 0;
+ size_t sent_bytes_on_this_connection = 0;
- struct timeval tv = {
- .tv_sec = timeout,
- .tv_usec = 0
- };
time_t last_sent_t = 0;
struct pollfd fds[2], *ifd, *ofd;
@@ -297,260 +490,206 @@ void *rrdpush_sender_thread(void *ptr) {
ifd = &fds[0];
ofd = &fds[1];
- for(; host->rrdpush_enabled && !netdata_exit ;) {
- debug(D_STREAM, "STREAM: Checking if we need to timeout the connection...");
- if(host->rrdpush_socket != -1 && now_monotonic_sec() - last_sent_t > timeout) {
- error("STREAM %s [send to %s]: could not send metrics for %d seconds - closing connection - we have sent %zu bytes on this connection.", host->hostname, connected_to, timeout, sent_connection);
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
- }
+ size_t not_connected_loops = 0;
- if(unlikely(host->rrdpush_socket == -1)) {
- debug(D_STREAM, "STREAM: Attempting to connect...");
+ pthread_cleanup_push(rrdpush_sender_thread_cleanup_callback, host);
- // stop appending data into rrdpush_buffer
- // they will be lost, so there is no point to do it
- host->rrdpush_connected = 0;
+ for(; host->rrdpush_send_enabled && !netdata_exit ;) {
+ // check for outstanding cancellation requests
+ pthread_testcancel();
- info("STREAM %s [send to %s]: connecting...", host->hostname, host->rrdpush_destination);
- host->rrdpush_socket = connect_to_one_of(host->rrdpush_destination, default_port, &tv, &reconnects_counter, connected_to, CONNECTED_TO_SIZE);
+ // if we don't have socket open, lets wait a bit
+ if(unlikely(host->rrdpush_sender_socket == -1)) {
+ if(not_connected_loops == 0 && sent_bytes_on_this_connection > 0) {
+ // fast re-connection on first disconnect
+ sleep_usec(USEC_PER_MS * 500); // milliseconds
+ }
+ else {
+ // slow re-connection on repeating errors
+ sleep_usec(USEC_PER_SEC * reconnect_delay); // seconds
+ }
- if(unlikely(host->rrdpush_socket == -1)) {
- error("STREAM %s [send to %s]: failed to connect", host->hostname, host->rrdpush_destination);
- sleep(reconnect_delay);
- continue;
- }
+ if(rrdpush_sender_thread_connect_to_master(host, default_port, timeout, &reconnects_counter, connected_to, CONNECTED_TO_SIZE)) {
+ last_sent_t = now_monotonic_sec();
- info("STREAM %s [send to %s]: initializing communication...", host->hostname, connected_to);
+ // reset the buffer, to properly send charts and metrics
+ rrdpush_sender_thread_data_flush(host);
- #define HTTP_HEADER_SIZE 8192
- char http[HTTP_HEADER_SIZE + 1];
- snprintfz(http, HTTP_HEADER_SIZE,
- "STREAM key=%s&hostname=%s&registry_hostname=%s&machine_guid=%s&update_every=%d&os=%s&tags=%s HTTP/1.1\r\n"
- "User-Agent: netdata-push-service/%s\r\n"
- "Accept: */*\r\n\r\n"
- , host->rrdpush_api_key
- , host->hostname
- , host->registry_hostname
- , host->machine_guid
- , default_rrd_update_every
- , host->os
- , (host->tags)?host->tags:""
- , program_version
- );
+ // make sure the next reconnection will be immediate
+ not_connected_loops = 0;
- if(send_timeout(host->rrdpush_socket, http, strlen(http), 0, timeout) == -1) {
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
- error("STREAM %s [send to %s]: failed to send http header to netdata", host->hostname, connected_to);
- sleep(reconnect_delay);
- continue;
- }
+ // reset the bytes we have sent for this session
+ sent_bytes_on_this_connection = 0;
- info("STREAM %s [send to %s]: waiting response from remote netdata...", host->hostname, connected_to);
+ // let the data collection threads know we are ready
+ host->rrdpush_sender_connected = 1;
+ }
+ else {
+ // increase the failed connections counter
+ not_connected_loops++;
- if(recv_timeout(host->rrdpush_socket, http, HTTP_HEADER_SIZE, 0, timeout) == -1) {
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
- error("STREAM %s [send to %s]: failed to initialize communication", host->hostname, connected_to);
- sleep(reconnect_delay);
- continue;
- }
+ // reset the number of bytes sent
+ sent_bytes_on_this_connection = 0;
+ }
- if(strncmp(http, START_STREAMING_PROMPT, strlen(START_STREAMING_PROMPT))) {
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
- error("STREAM %s [send to %s]: server is not replying properly.", host->hostname, connected_to);
- sleep(reconnect_delay);
+ // loop through
continue;
}
-
- info("STREAM %s [send to %s]: established communication - sending metrics...", host->hostname, connected_to);
- last_sent_t = now_monotonic_sec();
-
- if(sock_setnonblock(host->rrdpush_socket) < 0)
- error("STREAM %s [send to %s]: cannot set non-blocking mode for socket.", host->hostname, connected_to);
-
- if(sock_enlarge_out(host->rrdpush_socket) < 0)
- error("STREAM %s [send to %s]: cannot enlarge the socket buffer.", host->hostname, connected_to);
-
- rrdpush_sender_thread_data_flush(host);
- sent_connection = 0;
-
- // allow appending data into rrdpush_buffer
- host->rrdpush_connected = 1;
-
- debug(D_STREAM, "STREAM: Connected on fd %d...", host->rrdpush_socket);
- }
-
- ifd->fd = host->rrdpush_pipe[PIPE_READ];
- ifd->events = POLLIN;
- ifd->revents = 0;
-
- ofd->fd = host->rrdpush_socket;
- ofd->revents = 0;
- if(ofd->fd != -1 && begin < buffer_strlen(host->rrdpush_buffer)) {
- debug(D_STREAM, "STREAM: Requesting data output on streaming socket %d...", ofd->fd);
- ofd->events = POLLOUT;
- fdmax = 2;
- }
- else {
- debug(D_STREAM, "STREAM: Not requesting data output on streaming socket %d (nothing to send now)...", ofd->fd);
- ofd->events = 0;
- fdmax = 1;
- }
-
- debug(D_STREAM, "STREAM: Waiting for poll() events (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_buffer));
- if(netdata_exit) break;
- int retval = poll(fds, fdmax, 1000);
- if(netdata_exit) break;
-
- if(unlikely(retval == -1)) {
- debug(D_STREAM, "STREAM: poll() failed (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_buffer));
-
- if(errno == EAGAIN || errno == EINTR) {
- debug(D_STREAM, "STREAM: poll() failed with EAGAIN or EINTR...");
- continue;
+ else if(unlikely(now_monotonic_sec() - last_sent_t > timeout)) {
+ error("STREAM %s [send to %s]: could not send metrics for %d seconds - closing connection - we have sent %zu bytes on this connection.", host->hostname, connected_to, timeout, sent_bytes_on_this_connection);
+ rrdpush_sender_thread_close_socket(host);
}
- error("STREAM %s [send to %s]: failed to poll().", host->hostname, connected_to);
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
- break;
- }
- else if(likely(retval)) {
- if (ifd->revents & POLLIN || ifd->revents & POLLPRI) {
- debug(D_STREAM, "STREAM: Data added to send buffer (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_buffer));
+ ifd->fd = host->rrdpush_sender_pipe[PIPE_READ];
+ ifd->events = POLLIN;
+ ifd->revents = 0;
- char buffer[1000 + 1];
- if (read(host->rrdpush_pipe[PIPE_READ], buffer, 1000) == -1)
- error("STREAM %s [send to %s]: cannot read from internal pipe.", host->hostname, connected_to);
+ ofd->fd = host->rrdpush_sender_socket;
+ ofd->revents = 0;
+ if(ofd->fd != -1 && begin < buffer_strlen(host->rrdpush_sender_buffer)) {
+ debug(D_STREAM, "STREAM: Requesting data output on streaming socket %d...", ofd->fd);
+ ofd->events = POLLOUT;
+ fdmax = 2;
+ }
+ else {
+ debug(D_STREAM, "STREAM: Not requesting data output on streaming socket %d (nothing to send now)...", ofd->fd);
+ ofd->events = 0;
+ fdmax = 1;
}
- if (ofd->revents & POLLOUT) {
- if (begin < buffer_strlen(host->rrdpush_buffer)) {
- debug(D_STREAM, "STREAM: Sending data (current buffer length %zu bytes, begin = %zu)...", buffer_strlen(host->rrdpush_buffer), begin);
+ debug(D_STREAM, "STREAM: Waiting for poll() events (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_sender_buffer));
+ if(unlikely(netdata_exit)) break;
+ int retval = poll(fds, fdmax, 1000);
+ if(unlikely(netdata_exit)) break;
- // BEGIN RRDPUSH LOCKED SESSION
+ if(unlikely(retval == -1)) {
+ debug(D_STREAM, "STREAM: poll() failed (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_sender_buffer));
- // during this session, data collectors
- // will not be able to append data to our buffer
- // but the socket is in non-blocking mode
- // so, we will not block at send()
+ if(errno == EAGAIN || errno == EINTR) {
+ debug(D_STREAM, "STREAM: poll() failed with EAGAIN or EINTR...");
+ }
+ else {
+ error("STREAM %s [send to %s]: failed to poll(). Closing socket.", host->hostname, connected_to);
+ rrdpush_sender_thread_close_socket(host);
+ }
- if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
- error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
+ continue;
+ }
+ else if(likely(retval)) {
+ if (ifd->revents & POLLIN || ifd->revents & POLLPRI) {
+ debug(D_STREAM, "STREAM: Data added to send buffer (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_sender_buffer));
- debug(D_STREAM, "STREAM: Getting exclusive lock on host...");
- rrdpush_lock(host);
+ char buffer[1000 + 1];
+ if (read(host->rrdpush_sender_pipe[PIPE_READ], buffer, 1000) == -1)
+ error("STREAM %s [send to %s]: cannot read from internal pipe.", host->hostname, connected_to);
+ }
- debug(D_STREAM, "STREAM: Sending data, starting from %zu, size %zu...", begin, buffer_strlen(host->rrdpush_buffer));
- ssize_t ret = send(host->rrdpush_socket, &host->rrdpush_buffer->buffer[begin], buffer_strlen(host->rrdpush_buffer) - begin, MSG_DONTWAIT);
- if (unlikely(ret == -1)) {
- if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) {
- debug(D_STREAM, "STREAM: Send failed - closing socket...");
- 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);
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
- }
- else {
- debug(D_STREAM, "STREAM: Send failed - will retry...");
+ if (ofd->revents & POLLOUT) {
+ if (begin < buffer_strlen(host->rrdpush_sender_buffer)) {
+ debug(D_STREAM, "STREAM: Sending data (current buffer length %zu bytes, begin = %zu)...", buffer_strlen(host->rrdpush_sender_buffer), begin);
+
+ // BEGIN RRDPUSH LOCKED SESSION
+
+ // during this session, data collectors
+ // will not be able to append data to our buffer
+ // but the socket is in non-blocking mode
+ // so, we will not block at send()
+
+ if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
+ error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
+
+ debug(D_STREAM, "STREAM: Getting exclusive lock on host...");
+ rrdpush_buffer_lock(host);
+
+ debug(D_STREAM, "STREAM: Sending data, starting from %zu, size %zu...", begin, buffer_strlen(host->rrdpush_sender_buffer));
+ ssize_t ret = send(host->rrdpush_sender_socket, &host->rrdpush_sender_buffer->buffer[begin], buffer_strlen(host->rrdpush_sender_buffer) - begin, MSG_DONTWAIT);
+ if (unlikely(ret == -1)) {
+ if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) {
+ debug(D_STREAM, "STREAM: Send failed - closing socket...");
+ 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_bytes_on_this_connection);
+ rrdpush_sender_thread_close_socket(host);
+ }
+ else {
+ debug(D_STREAM, "STREAM: Send failed - will retry...");
+ }
}
- }
- else if (likely(ret > 0)) {
- // DEBUG - dump the scring to see it
- //char c = host->rrdpush_buffer->buffer[begin + ret];
- //host->rrdpush_buffer->buffer[begin + ret] = '\0';
- //debug(D_STREAM, "STREAM: sent from %zu to %zd:\n%s\n", begin, ret, &host->rrdpush_buffer->buffer[begin]);
- //host->rrdpush_buffer->buffer[begin + ret] = c;
-
- sent_connection += ret;
- sent_bytes += ret;
- begin += ret;
-
- if (begin == buffer_strlen(host->rrdpush_buffer)) {
- // we send it all
-
- debug(D_STREAM, "STREAM: Sent %zd bytes (the whole buffer)...", ret);
- buffer_flush(host->rrdpush_buffer);
- begin = 0;
+ else if (likely(ret > 0)) {
+ // DEBUG - dump the string to see it
+ //char c = host->rrdpush_sender_buffer->buffer[begin + ret];
+ //host->rrdpush_sender_buffer->buffer[begin + ret] = '\0';
+ //debug(D_STREAM, "STREAM: sent from %zu to %zd:\n%s\n", begin, ret, &host->rrdpush_sender_buffer->buffer[begin]);
+ //host->rrdpush_sender_buffer->buffer[begin + ret] = c;
+
+ sent_bytes_on_this_connection += ret;
+ sent_bytes += ret;
+ begin += ret;
+
+ if (begin == buffer_strlen(host->rrdpush_sender_buffer)) {
+ // we send it all
+
+ debug(D_STREAM, "STREAM: Sent %zd bytes (the whole buffer)...", ret);
+ buffer_flush(host->rrdpush_sender_buffer);
+ begin = 0;
+ }
+ else {
+ debug(D_STREAM, "STREAM: Sent %zd bytes (part of the data buffer)...", ret);
+ }
+
+ last_sent_t = now_monotonic_sec();
}
else {
- debug(D_STREAM, "STREAM: Sent %zd bytes (part of the data buffer)...", ret);
+ debug(D_STREAM, "STREAM: send() returned %zd - closing the socket...", ret);
+ error("STREAM %s [send to %s]: failed to send metrics (send() returned %zd) - closing connection - we have sent %zu bytes on this connection.",
+ host->hostname, connected_to, ret, sent_bytes_on_this_connection);
+ rrdpush_sender_thread_close_socket(host);
}
- last_sent_t = now_monotonic_sec();
+ debug(D_STREAM, "STREAM: Releasing exclusive lock on host...");
+ rrdpush_buffer_unlock(host);
+
+ if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
+ error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
+
+ // END RRDPUSH LOCKED SESSION
}
else {
- debug(D_STREAM, "STREAM: send() returned %zd - closing the socket...", ret);
- error("STREAM %s [send to %s]: failed to send metrics (send() returned %zd) - closing connection - we have sent %zu bytes on this connection.",
- host->hostname, connected_to, ret, sent_connection);
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
+ debug(D_STREAM, "STREAM: we have sent the entire buffer, but we received POLLOUT...");
}
-
- debug(D_STREAM, "STREAM: Releasing exclusive lock on host...");
- rrdpush_unlock(host);
-
- if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
- error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
-
- // END RRDPUSH LOCKED SESSION
- }
- else {
- debug(D_STREAM, "STREAM: we have sent the entire buffer, but we received POLLOUT...");
}
- }
- if(unlikely(ofd->revents & POLLERR)) {
- debug(D_STREAM, "STREAM: Send failed (POLLERR) - closing socket...");
- error("STREAM %s [send to %s]: connection reports errors (POLLERR), closing it - we have sent %zu bytes on this connection.", host->hostname, connected_to, sent_connection);
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
- }
- else if(unlikely(ofd->revents & POLLHUP)) {
- debug(D_STREAM, "STREAM: Send failed (POLLHUP) - closing socket...");
- error("STREAM %s [send to %s]: connection closed by remote end (POLLHUP) - we have sent %zu bytes on this connection.", host->hostname, connected_to, sent_connection);
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
+ if(host->rrdpush_sender_socket != -1) {
+ char *error = NULL;
+
+ if (unlikely(ofd->revents & POLLERR))
+ error = "socket reports errors (POLLERR)";
+
+ else if (unlikely(ofd->revents & POLLHUP))
+ error = "connection closed by remote end (POLLHUP)";
+
+ else if (unlikely(ofd->revents & POLLNVAL))
+ error = "connection is invalid (POLLNVAL)";
+
+ if(unlikely(error)) {
+ debug(D_STREAM, "STREAM: %s - closing socket...", error);
+ error("STREAM %s [send to %s]: %s - reopening socket - we have sent %zu bytes on this connection.", host->hostname, connected_to, error, sent_bytes_on_this_connection);
+ rrdpush_sender_thread_close_socket(host);
+ }
+ }
}
- else if(unlikely(ofd->revents & POLLNVAL)) {
- debug(D_STREAM, "STREAM: Send failed (POLLNVAL) - closing socket...");
- error("STREAM %s [send to %s]: connection is invalid (POLLNVAL), closing it - we have sent %zu bytes on this connection.", host->hostname, connected_to, sent_connection);
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
+ else {
+ debug(D_STREAM, "STREAM: poll() timed out.");
}
- }
- else {
- debug(D_STREAM, "STREAM: poll() timed out.");
- }
- // protection from overflow
- if(buffer_strlen(host->rrdpush_buffer) > max_size) {
- debug(D_STREAM, "STREAM: Buffer is too big (%zu bytes), bigger than the max (%zu) - flushing it...", buffer_strlen(host->rrdpush_buffer), max_size);
- errno = 0;
- 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);
- if(host->rrdpush_socket != -1) {
- close(host->rrdpush_socket);
- host->rrdpush_socket = -1;
+ // protection from overflow
+ if(buffer_strlen(host->rrdpush_sender_buffer) > max_size) {
+ debug(D_STREAM, "STREAM: Buffer is too big (%zu bytes), bigger than the max (%zu) - flushing it...", buffer_strlen(host->rrdpush_sender_buffer), max_size);
+ errno = 0;
+ 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_sender_buffer->len, host->rrdpush_sender_buffer->len - begin, sent_bytes, sent_bytes_on_this_connection);
+ rrdpush_sender_thread_close_socket(host);
}
}
- }
-
-cleanup:
- debug(D_WEB_CLIENT, "STREAM %s [send]: sending thread exits.", host->hostname);
-
- if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
- error("STREAM %s [send]: cannot set pthread cancel state to DISABLE.", host->hostname);
-
- rrdpush_lock(host);
- rrdhost_wrlock(host);
- rrdpush_sender_thread_cleanup_locked_all(host);
- rrdhost_unlock(host);
- rrdpush_unlock(host);
- if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
- error("STREAM %s [send]: cannot set pthread cancel state to ENABLE.", host->hostname);
+ pthread_cleanup_pop(1);
pthread_exit(NULL);
return NULL;
@@ -560,7 +699,11 @@ cleanup:
// ----------------------------------------------------------------------------
// rrdpush receiver thread
-static int rrdpush_receive(int fd, const char *key, const char *hostname, const char *registry_hostname, const char *machine_guid, const char *os, const char *tags, int update_every, char *client_ip, char *client_port) {
+static void log_stream_connection(const char *client_ip, const char *client_port, const char *api_key, const char *machine_guid, const char *host, const char *msg) {
+ log_access("STREAM: %d '[%s]:%s' '%s' host '%s' api key '%s' machine guid '%s'", gettid(), client_ip, client_port, msg, host, api_key, machine_guid);
+}
+
+static int rrdpush_receive(int fd, const char *key, const char *hostname, const char *registry_hostname, const char *machine_guid, const char *os, const char *timezone, const char *tags, int update_every, char *client_ip, char *client_port) {
RRDHOST *host;
int history = default_rrd_history_entries;
RRD_MEMORY_MODE mode = default_rrd_memory_mode;
@@ -606,6 +749,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
, registry_hostname
, machine_guid
, os
+ , timezone
, tags
, update_every
, history
@@ -618,6 +762,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
if(!host) {
close(fd);
+ log_stream_connection(client_ip, client_port, key, machine_guid, hostname, "FAILED - CANNOT ACQUIRE HOST");
error("STREAM %s [receive from [%s]:%s]: failed to find/create host structure.", hostname, client_ip, client_port);
return 1;
}
@@ -656,6 +801,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
info("STREAM %s [receive from [%s]:%s]: initializing communication...", host->hostname, client_ip, client_port);
if(send_timeout(fd, START_STREAMING_PROMPT, strlen(START_STREAMING_PROMPT), 0, 60) != strlen(START_STREAMING_PROMPT)) {
+ log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "FAILED - CANNOT REPLY");
error("STREAM %s [receive from [%s]:%s]: cannot send ready command.", host->hostname, client_ip, client_port);
close(fd);
return 0;
@@ -668,6 +814,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
// convert the socket to a FILE *
FILE *fp = fdopen(fd, "r");
if(!fp) {
+ log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "FAILED - SOCKET ERROR");
error("STREAM %s [receive from [%s]:%s]: failed to get a FILE for FD %d.", host->hostname, client_ip, client_port, fd);
close(fd);
return 0;
@@ -677,8 +824,9 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
if(host->connected_senders > 0)
info("STREAM %s [receive from [%s]:%s]: multiple streaming connections for the same host detected. If multiple netdata are pushing metrics for the same charts, at the same time, the result is unexpected.", host->hostname, client_ip, client_port);
+ rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
host->connected_senders++;
- rrdhost_flag_clear(host, RRDHOST_ORPHAN);
+ host->senders_disconnected_time = 0;
if(health_enabled != CONFIG_BOOLEAN_NO) {
if(alarms_delay > 0) {
host->health_delay_up_to = now_realtime_sec() + alarms_delay;
@@ -692,20 +840,25 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
// call the plugins.d processor to receive the metrics
info("STREAM %s [receive from [%s]:%s]: receiving metrics...", host->hostname, client_ip, client_port);
+ log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "CONNECTED");
+
size_t count = pluginsd_process(host, &cd, fp, 1);
- error("STREAM %s [receive from [%s]:%s]: disconnected (completed updates %zu).", host->hostname, client_ip, client_port, count);
+
+ log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "DISCONNECTED");
+ error("STREAM %s [receive from [%s]:%s]: disconnected (completed %zu updates).", host->hostname, client_ip, client_port, count);
rrdhost_wrlock(host);
host->senders_disconnected_time = now_realtime_sec();
host->connected_senders--;
if(!host->connected_senders) {
- rrdhost_flag_set(host, RRDHOST_ORPHAN);
+ rrdhost_flag_set(host, RRDHOST_FLAG_ORPHAN);
if(health_enabled == CONFIG_BOOLEAN_AUTO)
host->health_enabled = 0;
}
rrdhost_unlock(host);
- rrdpush_sender_thread_stop(host);
+ if(host->connected_senders == 0)
+ rrdpush_sender_thread_stop(host);
// cleanup
fclose(fp);
@@ -720,6 +873,7 @@ struct rrdpush_thread {
char *registry_hostname;
char *machine_guid;
char *os;
+ char *timezone;
char *tags;
char *client_ip;
char *client_port;
@@ -737,7 +891,7 @@ static void *rrdpush_receiver_thread(void *ptr) {
info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
- rrdpush_receive(rpt->fd, rpt->key, rpt->hostname, rpt->registry_hostname, rpt->machine_guid, rpt->os, rpt->tags, rpt->update_every, rpt->client_ip, rpt->client_port);
+ rrdpush_receive(rpt->fd, rpt->key, rpt->hostname, rpt->registry_hostname, rpt->machine_guid, rpt->os, rpt->timezone, rpt->tags, rpt->update_every, rpt->client_ip, rpt->client_port);
info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
freez(rpt->key);
@@ -745,6 +899,7 @@ static void *rrdpush_receiver_thread(void *ptr) {
freez(rpt->registry_hostname);
freez(rpt->machine_guid);
freez(rpt->os);
+ freez(rpt->timezone);
freez(rpt->tags);
freez(rpt->client_ip);
freez(rpt->client_port);
@@ -757,25 +912,30 @@ static void *rrdpush_receiver_thread(void *ptr) {
static void rrdpush_sender_thread_spawn(RRDHOST *host) {
rrdhost_wrlock(host);
- if(!host->rrdpush_spawn) {
- if(pthread_create(&host->rrdpush_thread, NULL, rrdpush_sender_thread, (void *) host))
+ if(!host->rrdpush_sender_spawn) {
+ if(pthread_create(&host->rrdpush_sender_thread, NULL, rrdpush_sender_thread, (void *) host))
error("STREAM %s [send]: failed to create new thread for client.", host->hostname);
-
- else if(pthread_detach(host->rrdpush_thread))
- error("STREAM %s [send]: cannot request detach newly created thread.", host->hostname);
-
- host->rrdpush_spawn = 1;
+ else
+ host->rrdpush_sender_spawn = 1;
}
rrdhost_unlock(host);
}
+int rrdpush_receiver_permission_denied(struct web_client *w) {
+ // we always respond with the same message and error code
+ // to prevent an attacker from gaining info about the error
+ buffer_flush(w->response.data);
+ buffer_sprintf(w->response.data, "You are not permitted to access this. Check the logs for more info.");
+ return 401;
+}
+
int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url) {
(void)host;
info("STREAM [receive from [%s]:%s]: new client connection.", w->client_ip, w->client_port);
- char *key = NULL, *hostname = NULL, *registry_hostname = NULL, *machine_guid = NULL, *os = "unknown", *tags = NULL;
+ char *key = NULL, *hostname = NULL, *registry_hostname = NULL, *machine_guid = NULL, *os = "unknown", *timezone = "unknown", *tags = NULL;
int update_every = default_rrd_update_every;
char buf[GUID_LEN + 1];
@@ -799,6 +959,8 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
update_every = (int)strtoul(value, NULL, 0);
else if(!strcmp(name, "os"))
os = value;
+ else if(!strcmp(name, "timezone"))
+ timezone = value;
else if(!strcmp(name, "tags"))
tags = value;
else
@@ -806,52 +968,71 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
}
if(!key || !*key) {
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO KEY");
error("STREAM [receive from [%s]:%s]: request without an API key. Forbidding access.", w->client_ip, w->client_port);
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "You need an API key for this request.");
- return 401;
+ return rrdpush_receiver_permission_denied(w);
}
if(!hostname || !*hostname) {
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO HOSTNAME");
error("STREAM [receive from [%s]:%s]: request without a hostname. Forbidding access.", w->client_ip, w->client_port);
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "You need to send a hostname too.");
- return 400;
+ return rrdpush_receiver_permission_denied(w);
}
if(!machine_guid || !*machine_guid) {
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO MACHINE GUID");
error("STREAM [receive from [%s]:%s]: request without a machine GUID. Forbidding access.", w->client_ip, w->client_port);
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "You need to send a machine GUID too.");
- return 400;
+ return rrdpush_receiver_permission_denied(w);
}
if(regenerate_guid(key, buf) == -1) {
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - INVALID KEY");
error("STREAM [receive from [%s]:%s]: API key '%s' is not valid GUID (use the command uuidgen to generate one). Forbidding access.", w->client_ip, w->client_port, key);
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "Your API key is invalid.");
- return 401;
+ return rrdpush_receiver_permission_denied(w);
}
if(regenerate_guid(machine_guid, buf) == -1) {
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - INVALID MACHINE GUID");
error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not GUID. Forbidding access.", w->client_ip, w->client_port, machine_guid);
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "Your machine GUID is invalid.");
- return 404;
+ return rrdpush_receiver_permission_denied(w);
}
if(!appconfig_get_boolean(&stream_config, key, "enabled", 0)) {
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - KEY NOT ENABLED");
error("STREAM [receive from [%s]:%s]: API key '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, key);
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "Your API key is not permitted access.");
- return 401;
+ return rrdpush_receiver_permission_denied(w);
+ }
+
+ {
+ SIMPLE_PATTERN *key_allow_from = simple_pattern_create(appconfig_get(&stream_config, key, "allow from", "*"), SIMPLE_PATTERN_EXACT);
+ if(key_allow_from) {
+ if(!simple_pattern_matches(key_allow_from, w->client_ip)) {
+ simple_pattern_free(key_allow_from);
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname) ? hostname : "-", "ACCESS DENIED - KEY NOT ALLOWED FROM THIS IP");
+ error("STREAM [receive from [%s]:%s]: API key '%s' is not permitted from this IP. Forbidding access.", w->client_ip, w->client_port, key);
+ return rrdpush_receiver_permission_denied(w);
+ }
+ simple_pattern_free(key_allow_from);
+ }
}
if(!appconfig_get_boolean(&stream_config, machine_guid, "enabled", 1)) {
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - MACHINE GUID NOT ENABLED");
error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, machine_guid);
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "Your machine guide is not permitted access.");
- return 404;
+ return rrdpush_receiver_permission_denied(w);
+ }
+
+ {
+ SIMPLE_PATTERN *machine_allow_from = simple_pattern_create(appconfig_get(&stream_config, machine_guid, "allow from", "*"), SIMPLE_PATTERN_EXACT);
+ if(machine_allow_from) {
+ if(!simple_pattern_matches(machine_allow_from, w->client_ip)) {
+ simple_pattern_free(machine_allow_from);
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname) ? hostname : "-", "ACCESS DENIED - MACHINE GUID NOT ALLOWED FROM THIS IP");
+ error("STREAM [receive from [%s]:%s]: Machine GUID '%s' is not permitted from this IP. Forbidding access.", w->client_ip, w->client_port, machine_guid);
+ return rrdpush_receiver_permission_denied(w);
+ }
+ simple_pattern_free(machine_allow_from);
+ }
}
struct rrdpush_thread *rpt = mallocz(sizeof(struct rrdpush_thread));
@@ -861,6 +1042,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
rpt->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
rpt->machine_guid = strdupz(machine_guid);
rpt->os = strdupz(os);
+ rpt->timezone = strdupz(timezone);
rpt->tags = (tags)?strdupz(tags):NULL;
rpt->client_ip = strdupz(w->client_ip);
rpt->client_port = strdupz(w->client_port);