]> arthur.barton.de Git - netdata.git/blobdiff - src/backends.c
Added json as new backend with the structure:
[netdata.git] / src / backends.c
index eb6918a98ba0b230bd103c6b660b346504ba9edb..9f7e54cc3435fba8ff580b92cd51b7b9c6ff0025 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
@@ -94,6 +93,37 @@ static inline int format_dimension_stored_opentsdb_telnet(BUFFER *b, const char
     return 0;
 }
 
+static inline int format_dimension_collected_json_plaintext(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
+    (void)host;
+    (void)after;
+    (void)before;
+    (void)options;
+    buffer_sprintf(b, "{"
+        "\"prefix\":\"%s\","
+        "\"hostname\":\"%s\","
+        "\"id\":\"%s\","
+        "\"subid\":\"%s\","
+        "\"value\":" COLLECTED_NUMBER_FORMAT ","
+        "\"timestamp\": %u}\n", prefix, hostname, st->id, rd->id, rd->last_collected_value, (uint32_t)rd->last_collected_time.tv_sec);
+    return 1;
+}
+
+static inline int format_dimension_stored_json_plaintext(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
+    (void)host;
+    calculated_number value = backend_calculate_value_from_stored_data(st, rd, after, before, options);
+    if(!isnan(value)) {
+        buffer_sprintf(b, "{"
+            "\"prefix\":\"%s\","
+            "\"hostname\":\"%s\","
+            "\"id\":\"%s\","
+            "\"subid\":\"%s\","
+            "\"value\":" CALCULATED_NUMBER_FORMAT ","
+            "\"timestamp\": %u}\n", prefix, hostname, st->id, rd->id, value, (uint32_t) before);
+        return 1;
+    }
+    return 0;
+}
+
 static inline int process_graphite_response(BUFFER *b) {
     char sample[1024];
     const char *s = buffer_tostring(b);
@@ -111,6 +141,23 @@ static inline int process_graphite_response(BUFFER *b) {
     return 0;
 }
 
+static inline int process_json_response(BUFFER *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 json backend. Ignoring them. Sample: '%s'", buffer_strlen(b), sample);
+    buffer_flush(b);
+    return 0;
+}
+
 static inline int process_opentsdb_response(BUFFER *b) {
     char sample[1024];
     const char *s = buffer_tostring(b);
@@ -129,7 +176,7 @@ static inline int process_opentsdb_response(BUFFER *b) {
 }
 
 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;
@@ -201,6 +248,21 @@ void *backends_main(void *ptr) {
 
         backend_response_checker = process_opentsdb_response;
     }
+    else if (!strcmp(type, "json") || !strcmp(type, "json:plaintext"))
+    {
+        default_port = 5448;
+
+        if (options == BACKEND_SOURCE_DATA_AS_COLLECTED)
+        {
+            backend_request_formatter = format_dimension_collected_json_plaintext;
+        }
+        else
+        {
+            backend_request_formatter = format_dimension_stored_json_plaintext;
+        }
+
+        backend_response_checker = process_json_response;
+    }
     else {
         error("Unknown backend type '%s'", type);
         goto cleanup;
@@ -265,11 +327,19 @@ void *backends_main(void *ptr) {
         rrddim_add(chart_ops, "read",      NULL, 1, 1, RRDDIM_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_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_rusage = rrdset_find("netdata.backend_thread_cpu");
     if(!chart_rusage) {
@@ -284,30 +354,19 @@ 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);
-
-        // 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();
-        }
+        // Wait for the next iteration point.
+        heartbeat_next(&hb, step_ut);
+        time_t before = now_realtime_sec();
 
         // ------------------------------------------------------------------------
         // add to the buffer the data we need to send to the backend
-
         RRDSET *st;
         int pthreadoldcancelstate;
 
@@ -352,64 +411,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;
-                        chart_received_bytes += r;
-                        chart_receptions++;
-                    }
-                    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;
@@ -430,7 +475,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;
@@ -440,14 +485,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++;
@@ -475,12 +520,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);
@@ -526,9 +565,11 @@ void *backends_main(void *ptr) {
         rrddim_set(chart_bytes, "received",   chart_received_bytes);
         rrdset_done(chart_bytes);
 
+        /*
         if(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);
@@ -551,6 +592,7 @@ cleanup:
 
     info("BACKEND thread exiting");
 
+    static_thread->enabled = 0;
     pthread_exit(NULL);
     return NULL;
 }