]> arthur.barton.de Git - netdata.git/blobdiff - src/backends.c
backend now sends all metrics for all hosts
[netdata.git] / src / backends.c
index 966476dbed7e225f77ec46d6753c242486e71d85..a3717bc066f483732f052302b8626417c3c66a4d 100644 (file)
@@ -8,9 +8,8 @@ static inline calculated_number backend_calculate_value_from_stored_data(RRDSET
     time_t first_t = rrdset_first_entry_t(st);
     time_t last_t = rrdset_last_entry_t(st);
 
-    if(unlikely(before - after < st->update_every && after != after - after % st->update_every))
-        // when st->update_every is bigger than the frequency we send data to backend
-        // skip the iterations that are not aligned to the database
+    if(unlikely(before < first_t || after > last_t))
+        // the chart has not been updated in the wanted timeframe
         return NAN;
 
     // align the time-frame
@@ -95,19 +94,41 @@ static inline int format_dimension_stored_opentsdb_telnet(BUFFER *b, const char
 }
 
 static inline int process_graphite_response(BUFFER *b) {
-    info("Received %zu bytes from graphite backend. Ignoring them.", buffer_strlen(b));
+    char sample[1024];
+    const char *s = buffer_tostring(b);
+    char *d = sample, *e = &sample[sizeof(sample) - 1];
+
+    for(; *s && d < e ;s++) {
+        char c = *s;
+        if(unlikely(!isprint(c))) c = ' ';
+        *d++ = c;
+    }
+    *d = '\0';
+
+    info("Received %zu bytes from graphite backend. Ignoring them. Sample: '%s'", buffer_strlen(b), sample);
     buffer_flush(b);
     return 0;
 }
 
 static inline int process_opentsdb_response(BUFFER *b) {
-    info("Received %zu bytes from opentsdb backend. Ignoring them.", buffer_strlen(b));
+    char sample[1024];
+    const char *s = buffer_tostring(b);
+    char *d = sample, *e = &sample[sizeof(sample) - 1];
+
+    for(; *s && d < e ;s++) {
+        char c = *s;
+        if(unlikely(!isprint(c))) c = ' ';
+        *d++ = c;
+    }
+    *d = '\0';
+
+    info("Received %zu bytes from opentsdb backend. Ignoring them. Sample: '%s'", buffer_strlen(b), sample);
     buffer_flush(b);
     return 0;
 }
 
 void *backends_main(void *ptr) {
-    (void)ptr;
+    struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
 
     BUFFER *b = buffer_create(1), *response = buffer_create(1);
     int (*backend_request_formatter)(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) = NULL;
@@ -131,19 +152,20 @@ void *backends_main(void *ptr) {
     int default_port = 0;
     int sock = -1;
     uint32_t options;
-    int enabled = config_get_boolean("backend", "enabled", 0);
-    const char *source = config_get("backend", "data source", "average");
-    const char *type = config_get("backend", "type", "graphite");
+    int enabled             = config_get_boolean("backend", "enabled", 0);
+    const char *source      = config_get("backend", "data source", "average");
+    const char *type        = config_get("backend", "type", "graphite");
     const char *destination = config_get("backend", "destination", "localhost");
-    const char *prefix = config_get("backend", "prefix", "netdata");
-    const char *hostname = config_get("backend", "hostname", localhost.hostname);
-    int frequency = (int)config_get_number("backend", "update every", 10);
-    int buffer_on_failures = (int)config_get_number("backend", "buffer on failures", 10);
-    long timeoutms = config_get_number("backend", "timeout ms", frequency * 2 * 1000);
+    const char *prefix      = config_get("backend", "prefix", "netdata");
+    const char *hostname    = config_get("backend", "hostname", localhost->hostname);
+    int frequency           = (int)config_get_number("backend", "update every", 10);
+    int buffer_on_failures  = (int)config_get_number("backend", "buffer on failures", 10);
+    long timeoutms          = config_get_number("backend", "timeout ms", frequency * 2 * 1000);
 
     // ------------------------------------------------------------------------
     // validate configuration options
     // and prepare for sending data to our backend
+
     if(!enabled || frequency < 1)
         goto cleanup;
 
@@ -161,6 +183,17 @@ void *backends_main(void *ptr) {
         goto cleanup;
     }
 
+    if(timeoutms < 1) {
+        error("BACKED invalid timeout %ld ms given. Assuming %d ms.", timeoutms, frequency * 2 * 1000);
+        timeoutms = frequency * 2 * 1000;
+    }
+    timeout.tv_sec  = (timeoutms * 1000) / 1000000;
+    timeout.tv_usec = (timeoutms * 1000) % 1000000;
+
+
+    // ------------------------------------------------------------------------
+    // select the backend type
+
     if(!strcmp(type, "graphite") || !strcmp(type, "graphite:plaintext")) {
         default_port = 2003;
         if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
@@ -189,15 +222,9 @@ void *backends_main(void *ptr) {
         goto cleanup;
     }
 
-    if(timeoutms < 1) {
-        error("BACKED invalid timeout %ld ms given. Assuming %d ms.", timeoutms, frequency * 2 * 1000);
-        timeoutms = frequency * 2 * 1000;
-    }
-    timeout.tv_sec  = (timeoutms * 1000) / 1000000;
-    timeout.tv_usec = (timeoutms * 1000) % 1000000;
 
     // ------------------------------------------------------------------------
-    // prepare the charts for monitoring the backend
+    // prepare the charts for monitoring the backend operation
 
     struct rusage thread;
 
@@ -206,7 +233,9 @@ void *backends_main(void *ptr) {
             chart_lost_metrics = 0,
             chart_sent_metrics = 0,
             chart_buffered_bytes = 0,
+            chart_received_bytes = 0,
             chart_sent_bytes = 0,
+            chart_receptions = 0,
             chart_transmission_successes = 0,
             chart_transmission_failures = 0,
             chart_data_lost_events = 0,
@@ -214,43 +243,39 @@ void *backends_main(void *ptr) {
             chart_backend_reconnects = 0,
             chart_backend_latency = 0;
 
-    RRDSET *chart_metrics = rrdset_find("netdata.backend_metrics");
-    if(!chart_metrics) {
-        chart_metrics = rrdset_create("netdata", "backend_metrics", NULL, "backend", NULL, "Netdata Buffered Metrics", "metrics", 130600, frequency, RRDSET_TYPE_LINE);
-        rrddim_add(chart_metrics, "buffered", NULL,   1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_metrics, "lost",     NULL,   1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_metrics, "sent",     NULL,   1, 1, RRDDIM_ABSOLUTE);
-    }
-
-    RRDSET *chart_bytes = rrdset_find("netdata.backend_bytes");
-    if(!chart_bytes) {
-        chart_bytes = rrdset_create("netdata", "backend_bytes", NULL, "backend", NULL, "Netdata Backend Data Size", "KB", 130610, frequency, RRDSET_TYPE_AREA);
-        rrddim_add(chart_bytes, "buffered", NULL,  1, 1024, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_bytes, "lost",     NULL,  1, 1024, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_bytes, "sent",     NULL,  1, 1024, RRDDIM_ABSOLUTE);
-    }
-
-    RRDSET *chart_ops = rrdset_find("netdata.backend_ops");
-    if(!chart_ops) {
-        chart_ops = rrdset_create("netdata", "backend_ops", NULL, "backend", NULL, "Netdata Backend Operations", "operations", 130630, frequency, RRDSET_TYPE_LINE);
-        rrddim_add(chart_ops, "write",     NULL,  1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_ops, "discard",   NULL,  1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_ops, "reconnect", NULL,  1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_ops, "failure",   NULL,  1, 1, RRDDIM_ABSOLUTE);
-    }
-
-    RRDSET *chart_latency = rrdset_find("netdata.backend_latency");
-    if(!chart_latency) {
-        chart_latency = rrdset_create("netdata", "backend_latency", NULL, "backend", NULL, "Netdata Backend Latency", "ms", 130620, frequency, RRDSET_TYPE_AREA);
-        rrddim_add(chart_latency, "latency",   NULL,  1, 1000, RRDDIM_ABSOLUTE);
-    }
+    RRDSET *chart_metrics = rrdset_create_localhost("netdata", "backend_metrics", NULL, "backend", NULL, "Netdata Buffered Metrics", "metrics", 130600, frequency, RRDSET_TYPE_LINE);
+    rrddim_add(chart_metrics, "buffered", NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_metrics, "lost",     NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_metrics, "sent",     NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
+
+    RRDSET *chart_bytes = rrdset_create_localhost("netdata", "backend_bytes", NULL, "backend", NULL, "Netdata Backend Data Size", "KB", 130610, frequency, RRDSET_TYPE_AREA);
+    rrddim_add(chart_bytes, "buffered", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_bytes, "lost",     NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_bytes, "sent",     NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_bytes, "received", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
+
+    RRDSET *chart_ops = rrdset_create_localhost("netdata", "backend_ops", NULL, "backend", NULL, "Netdata Backend Operations", "operations", 130630, frequency, RRDSET_TYPE_LINE);
+    rrddim_add(chart_ops, "write",     NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_ops, "discard",   NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_ops, "reconnect", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_ops, "failure",   NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
+    rrddim_add(chart_ops, "read",      NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
+
+    /*
+     * this is misleading - we can only measure the time we need to send data
+     * this time is not related to the time required for the data to travel to
+     * the backend database and the time that server needed to process them
+     *
+     * issue #1432 and https://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html
+     *
+    RRDSET *chart_latency = rrdset_create_localhost("netdata", "backend_latency", NULL, "backend", NULL, "Netdata Backend Latency", "ms", 130620, frequency, RRDSET_TYPE_AREA);
+    rrddim_add(chart_latency, "latency",   NULL,  1, 1000, RRD_ALGORITHM_ABSOLUTE);
+    */
+
+    RRDSET *chart_rusage = rrdset_create_localhost("netdata", "backend_thread_cpu", NULL, "backend", NULL, "NetData Backend Thread CPU usage", "milliseconds/s", 130630, frequency, RRDSET_TYPE_STACKED);
+    rrddim_add(chart_rusage, "user",   NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
+    rrddim_add(chart_rusage, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
 
-    RRDSET *chart_rusage = rrdset_find("netdata.backend_thread_cpu");
-    if(!chart_rusage) {
-        chart_rusage = rrdset_create("netdata", "backend_thread_cpu", NULL, "backend", NULL, "NetData Backend Thread CPU usage", "milliseconds/s", 130630, frequency, RRDSET_TYPE_STACKED);
-        rrddim_add(chart_rusage, "user",   NULL, 1, 1000, RRDDIM_INCREMENTAL);
-        rrddim_add(chart_rusage, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
-    }
 
     // ------------------------------------------------------------------------
     // prepare the backend main loop
@@ -258,49 +283,52 @@ void *backends_main(void *ptr) {
     info("BACKEND configured ('%s' on '%s' sending '%s' data, every %d seconds, as host '%s', with prefix '%s')", type, destination, source, frequency, hostname, prefix);
 
     usec_t step_ut = frequency * USEC_PER_SEC;
-    usec_t random_ut = now_realtime_usec() % (step_ut / 2);
-    time_t before = (time_t)((now_realtime_usec() - step_ut) / USEC_PER_SEC);
-    time_t after = before;
+    time_t after = now_realtime_sec();
     int failures = 0;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
 
     for(;;) {
-        // ------------------------------------------------------------------------
-        // wait for the next iteration point
 
-        usec_t now_ut = now_realtime_usec();
-        usec_t next_ut = now_ut - (now_ut % step_ut) + step_ut;
-        before = (time_t)(next_ut / USEC_PER_SEC);
+        // ------------------------------------------------------------------------
+        // Wait for the next iteration point.
+        heartbeat_next(&hb, step_ut);
+        time_t before = now_realtime_sec();
 
-        // add a little delay (1/4 of the step) plus some randomness
-        next_ut += (step_ut / 4) + random_ut;
-
-        while(now_ut < next_ut) {
-            sleep_usec(next_ut - now_ut);
-            now_ut = now_realtime_usec();
-        }
 
         // ------------------------------------------------------------------------
         // add to the buffer the data we need to send to the backend
 
-        RRDSET *st;
         int pthreadoldcancelstate;
 
         if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
             error("Cannot set pthread cancel state to DISABLE.");
 
-        rrdhost_rdlock(&localhost);
-        for(st = localhost.rrdset_root; st ;st = st->next) {
-            pthread_rwlock_rdlock(&st->rwlock);
+        RRDHOST *host;
+        for(host = localhost; host ; host = host->next) {
+            // for each host
+
+            rrdhost_rdlock(host);
+
+            RRDSET *st;
+            for(st = host->rrdset_root; st; st = st->next) {
+                // for each chart
 
-            RRDDIM *rd;
-            for(rd = st->dimensions; rd ;rd = rd->next) {
-                if(rd->last_collected_time.tv_sec >= after)
-                    chart_buffered_metrics += backend_request_formatter(b, prefix, &localhost, hostname, st, rd, after, before, options);
+                pthread_rwlock_rdlock(&st->rwlock);
+
+                RRDDIM *rd;
+                for(rd = st->dimensions; rd; rd = rd->next) {
+                    // for each dimension
+
+                    if(rd->last_collected_time.tv_sec >= after)
+                        chart_buffered_metrics += backend_request_formatter(b, prefix, host, (host == localhost)?hostname:host->hostname, st, rd, after, before, options);
+                }
+
+                pthread_rwlock_unlock(&st->rwlock);
             }
 
-            pthread_rwlock_unlock(&st->rwlock);
+            rrdhost_unlock(host);
         }
-        rrdhost_unlock(&localhost);
 
         if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
             error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
@@ -310,6 +338,7 @@ void *backends_main(void *ptr) {
         chart_buffered_bytes = (collected_number)buffer_strlen(b);
 
         // reset the monitoring chart counters
+        chart_received_bytes =
         chart_sent_bytes =
         chart_sent_metrics =
         chart_lost_metrics =
@@ -325,62 +354,50 @@ void *backends_main(void *ptr) {
         //fprintf(stderr, "\nBACKEND BEGIN:\n%s\nBACKEND END\n", buffer_tostring(b)); // FIXME
         //fprintf(stderr, "after = %lu, before = %lu\n", after, before);
 
+        // prepare for the next iteration
+        // to add incrementally data to buffer
+        after = before;
+
         // ------------------------------------------------------------------------
         // if we are connected, receive a response, without blocking
 
         if(likely(sock != -1)) {
-            if(likely(fcntl(sock, F_SETFL, O_NONBLOCK) >= 0)) {
-                errno = 0;
-
-                // loop through to collect all data
-                while(sock != -1 && errno != EWOULDBLOCK) {
-                    buffer_need_bytes(response, 4096);
-
-                    ssize_t r = recv(sock, &response->buffer[response->len], response->size - response->len
-                                     , O_NONBLOCK);
-                    if(likely(r > 0)) {
-                        // we received some data
-                        response->len += r;
-                    }
-                    else if(r == 0) {
-                        error("Backend '%s' closed the socket", destination);
-                        close(sock);
-                        sock = -1;
-                    }
-                    else {
-                        // failed to receive data
-                        if(errno != EAGAIN && errno != EWOULDBLOCK)
-                            error("Cannot receive data from backend '%s'.", destination);
-                    }
+            errno = 0;
+
+            // loop through to collect all data
+            while(sock != -1 && errno != EWOULDBLOCK) {
+                buffer_need_bytes(response, 4096);
+
+                ssize_t r = recv(sock, &response->buffer[response->len], response->size - response->len, MSG_DONTWAIT);
+                if(likely(r > 0)) {
+                    // we received some data
+                    response->len += r;
+                    chart_received_bytes += r;
+                    chart_receptions++;
                 }
-
-                if(sock != -1) {
-                    // unset O_NONBLOCK
-                    int val = fcntl(sock, F_GETFL, 0);
-                    if(likely(val >= 0)) {
-                        int flags = O_NONBLOCK;
-                        val &= ~flags;
-                        if(unlikely(fcntl(sock, F_SETFL, val) < 0))
-                            error("Cannot unset O_NONBLOCK from backend's '%s' socket.", destination);
+                else if(r == 0) {
+                    error("Backend '%s' closed the socket", destination);
+                    close(sock);
+                    sock = -1;
+                }
+                else {
+                    // failed to receive data
+                    if(errno != EAGAIN && errno != EWOULDBLOCK) {
+                        error("Cannot receive data from backend '%s'.", destination);
                     }
-                    else
-                        error("Cannot get active flags from backend's '%s' socket.", destination);
                 }
-
-                // if we received data, process them
-                if(buffer_strlen(response))
-                    backend_response_checker(response);
-            }
-            else {
-                error("Cannot set O_NONBLOCK from backend's '%s' socket. Receiving data from backend skipped.", destination);
             }
+
+            // if we received data, process them
+            if(buffer_strlen(response))
+                backend_response_checker(response);
         }
 
         // ------------------------------------------------------------------------
         // if we are not connected, connect to a backend server
 
         if(unlikely(sock == -1)) {
-            usec_t start_ut = now_realtime_usec();
+            usec_t start_ut = now_monotonic_usec();
             const char *s = destination;
             while(*s) {
                 const char *e = s;
@@ -401,7 +418,7 @@ void *backends_main(void *ptr) {
                 if(sock != -1) break;
                 s = e;
             }
-            chart_backend_latency += now_realtime_usec() - start_ut;
+            chart_backend_latency += now_monotonic_usec() - start_ut;
         }
 
         if(unlikely(netdata_exit)) break;
@@ -411,14 +428,14 @@ void *backends_main(void *ptr) {
 
         if(likely(sock != -1)) {
             size_t len = buffer_strlen(b);
-            usec_t start_ut = now_realtime_usec();
+            usec_t start_ut = now_monotonic_usec();
             int flags = 0;
 #ifdef MSG_NOSIGNAL
             flags += MSG_NOSIGNAL;
 #endif
 
             ssize_t written = send(sock, buffer_tostring(b), len, flags);
-            chart_backend_latency += now_realtime_usec() - start_ut;
+            chart_backend_latency += now_monotonic_usec() - start_ut;
             if(written != -1 && (size_t)written == len) {
                 // we sent the data successfully
                 chart_transmission_successes++;
@@ -446,12 +463,6 @@ void *backends_main(void *ptr) {
                 close(sock);
                 sock = -1;
             }
-
-            // either the buffer is empty
-            // or is holding the data we couldn't send
-            // so, make sure the next iteration will continue
-            // from where we are now
-            after = before;
         }
         else {
             error("Failed to update database backend '%s'", destination);
@@ -476,31 +487,35 @@ void *backends_main(void *ptr) {
         // ------------------------------------------------------------------------
         // update the monitoring charts
 
-        if(chart_ops->counter_done) rrdset_next(chart_ops);
+        if(likely(chart_ops->counter_done)) rrdset_next(chart_ops);
+        rrddim_set(chart_ops, "read",         chart_receptions);
         rrddim_set(chart_ops, "write",        chart_transmission_successes);
         rrddim_set(chart_ops, "discard",      chart_data_lost_events);
         rrddim_set(chart_ops, "failure",      chart_transmission_failures);
         rrddim_set(chart_ops, "reconnect",    chart_backend_reconnects);
         rrdset_done(chart_ops);
 
-        if(chart_metrics->counter_done) rrdset_next(chart_metrics);
+        if(likely(chart_metrics->counter_done)) rrdset_next(chart_metrics);
         rrddim_set(chart_metrics, "buffered", chart_buffered_metrics);
         rrddim_set(chart_metrics, "lost",     chart_lost_metrics);
         rrddim_set(chart_metrics, "sent",     chart_sent_metrics);
         rrdset_done(chart_metrics);
 
-        if(chart_bytes->counter_done) rrdset_next(chart_bytes);
+        if(likely(chart_bytes->counter_done)) rrdset_next(chart_bytes);
         rrddim_set(chart_bytes, "buffered",   chart_buffered_bytes);
         rrddim_set(chart_bytes, "lost",       chart_lost_bytes);
         rrddim_set(chart_bytes, "sent",       chart_sent_bytes);
+        rrddim_set(chart_bytes, "received",   chart_received_bytes);
         rrdset_done(chart_bytes);
 
-        if(chart_latency->counter_done) rrdset_next(chart_latency);
+        /*
+        if(likely(chart_latency->counter_done)) rrdset_next(chart_latency);
         rrddim_set(chart_latency, "latency",  chart_backend_latency);
         rrdset_done(chart_latency);
+        */
 
         getrusage(RUSAGE_THREAD, &thread);
-        if(chart_rusage->counter_done) rrdset_next(chart_rusage);
+        if(likely(chart_rusage->counter_done)) rrdset_next(chart_rusage);
         rrddim_set(chart_rusage, "user",   thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
         rrddim_set(chart_rusage, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
         rrdset_done(chart_rusage);
@@ -520,6 +535,7 @@ cleanup:
 
     info("BACKEND thread exiting");
 
+    static_thread->enabled = 0;
     pthread_exit(NULL);
     return NULL;
 }