]> arthur.barton.de Git - netdata.git/blob - src/backends.c
JSON output modified according to @ktsaou.
[netdata.git] / src / backends.c
1 #include "common.h"
2
3 #define BACKEND_SOURCE_DATA_AS_COLLECTED 0x00000001
4 #define BACKEND_SOURCE_DATA_AVERAGE      0x00000002
5 #define BACKEND_SOURCE_DATA_SUM          0x00000004
6
7 static inline calculated_number backend_calculate_value_from_stored_data(RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
8     time_t first_t = rrdset_first_entry_t(st);
9     time_t last_t = rrdset_last_entry_t(st);
10
11     if(unlikely(before < first_t || after > last_t))
12         // the chart has not been updated in the wanted timeframe
13         return NAN;
14
15     // align the time-frame
16     // for 'after' also skip the first value by adding st->update_every
17     after  = after  - after  % st->update_every + st->update_every;
18     before = before - before % st->update_every;
19
20     if(unlikely(after < first_t))
21         after = first_t;
22
23     if(unlikely(after > before))
24         // this can happen when the st->update_every > before - after
25         before = after;
26
27     if(unlikely(before > last_t))
28         before = last_t;
29
30     size_t counter = 0;
31     calculated_number sum = 0;
32
33     long    start_at_slot = rrdset_time2slot(st, before),
34             stop_at_slot  = rrdset_time2slot(st, after),
35             slot, stop_now = 0;
36
37     for(slot = start_at_slot; !stop_now ; slot--) {
38         if(unlikely(slot < 0)) slot = st->entries - 1;
39         if(unlikely(slot == stop_at_slot)) stop_now = 1;
40
41         storage_number n = rd->values[slot];
42         if(unlikely(!does_storage_number_exist(n))) continue;
43
44         calculated_number value = unpack_storage_number(n);
45         sum += value;
46         counter++;
47     }
48
49     if(unlikely(!counter))
50         return NAN;
51
52     if(unlikely(options & BACKEND_SOURCE_DATA_SUM))
53         return sum;
54
55     return sum / (calculated_number)counter;
56 }
57
58 static inline int format_dimension_collected_graphite_plaintext(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
59     (void)host;
60     (void)after;
61     (void)before;
62     (void)options;
63     buffer_sprintf(b, "%s.%s.%s.%s " COLLECTED_NUMBER_FORMAT " %u\n", prefix, hostname, st->id, rd->id, rd->last_collected_value, (uint32_t)rd->last_collected_time.tv_sec);
64     return 1;
65 }
66
67 static inline int format_dimension_stored_graphite_plaintext(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
68     (void)host;
69     calculated_number value = backend_calculate_value_from_stored_data(st, rd, after, before, options);
70     if(!isnan(value)) {
71         buffer_sprintf(b, "%s.%s.%s.%s " CALCULATED_NUMBER_FORMAT " %u\n", prefix, hostname, st->id, rd->id, value, (uint32_t) before);
72         return 1;
73     }
74     return 0;
75 }
76
77 static inline int format_dimension_collected_opentsdb_telnet(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
78     (void)host;
79     (void)after;
80     (void)before;
81     (void)options;
82     buffer_sprintf(b, "put %s.%s.%s %u " COLLECTED_NUMBER_FORMAT " host=%s\n", prefix, st->id, rd->id, (uint32_t)rd->last_collected_time.tv_sec, rd->last_collected_value, hostname);
83     return 1;
84 }
85
86 static inline int format_dimension_stored_opentsdb_telnet(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
87     (void)host;
88     calculated_number value = backend_calculate_value_from_stored_data(st, rd, after, before, options);
89     if(!isnan(value)) {
90         buffer_sprintf(b, "put %s.%s.%s %u " CALCULATED_NUMBER_FORMAT " host=%s\n", prefix, st->id, rd->id, (uint32_t) before, value, hostname);
91         return 1;
92     }
93     return 0;
94 }
95
96 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) {
97     (void)host;
98     (void)after;
99     (void)before;
100     (void)options;
101     buffer_sprintf(b, "{"
102         "\"prefix\":\"%s\","
103         "\"hostname\":\"%s\","
104
105         "\"chart_id\":\"%s\","
106         "\"chart_name\":\"%s\","
107         "\"family\":\"%s\","
108         "\"context\": \"%s\","
109         "\"type\":\"%s\","
110         "\"units\": \"%s\","
111
112         "\"id\":\"%s\","
113         "\"name\":\"%s\","
114         "\"value\":" COLLECTED_NUMBER_FORMAT ","
115
116         "\"timestamp\": %u}\n", 
117             prefix,
118             hostname,
119             
120             st->id,
121             st->name,
122             st->family,
123             st->context,
124             st->type,
125             st->units,
126
127             rd->id,
128             rd->name,
129             rd->last_collected_value, 
130             
131             (uint32_t)rd->last_collected_time.tv_sec
132     );
133
134     return 1;
135 }
136
137 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) {
138     (void)host;
139     calculated_number value = backend_calculate_value_from_stored_data(st, rd, after, before, options);
140     if(!isnan(value)) {
141         buffer_sprintf(b, "{"
142             "\"prefix\":\"%s\","
143             "\"hostname\":\"%s\","
144
145             "\"chart_id\":\"%s\","
146             "\"chart_name\":\"%s\","
147             "\"family\":\"%s\","
148             "\"context\": \"%s\","
149             "\"type\":\"%s\","
150             "\"units\": \"%s\","
151
152             "\"id\":\"%s\","
153             "\"name\":\"%s\","
154             "\"value\":" CALCULATED_NUMBER_FORMAT ","
155
156             "\"timestamp\": %u}\n", 
157                 prefix,
158                 hostname,
159                 
160                 st->id,
161                 st->name,
162                 st->family,
163                 st->context,
164                 st->type,
165                 st->units,
166
167                 rd->id,
168                 rd->name,
169                 value, 
170                 
171                 (uint32_t)before
172         );
173         
174         return 1;
175     }
176     return 0;
177 }
178
179 static inline int process_graphite_response(BUFFER *b) {
180     char sample[1024];
181     const char *s = buffer_tostring(b);
182     char *d = sample, *e = &sample[sizeof(sample) - 1];
183
184     for(; *s && d < e ;s++) {
185         char c = *s;
186         if(unlikely(!isprint(c))) c = ' ';
187         *d++ = c;
188     }
189     *d = '\0';
190
191     info("Received %zu bytes from graphite backend. Ignoring them. Sample: '%s'", buffer_strlen(b), sample);
192     buffer_flush(b);
193     return 0;
194 }
195
196 static inline int process_json_response(BUFFER *b) {
197     char sample[1024];
198     const char *s = buffer_tostring(b);
199     char *d = sample, *e = &sample[sizeof(sample) - 1];
200
201     for(; *s && d < e ;s++) {
202         char c = *s;
203         if(unlikely(!isprint(c))) c = ' ';
204         *d++ = c;
205     }
206     *d = '\0';
207
208     info("Received %zu bytes from json backend. Ignoring them. Sample: '%s'", buffer_strlen(b), sample);
209     buffer_flush(b);
210     return 0;
211 }
212
213 static inline int process_opentsdb_response(BUFFER *b) {
214     char sample[1024];
215     const char *s = buffer_tostring(b);
216     char *d = sample, *e = &sample[sizeof(sample) - 1];
217
218     for(; *s && d < e ;s++) {
219         char c = *s;
220         if(unlikely(!isprint(c))) c = ' ';
221         *d++ = c;
222     }
223     *d = '\0';
224
225     info("Received %zu bytes from opentsdb backend. Ignoring them. Sample: '%s'", buffer_strlen(b), sample);
226     buffer_flush(b);
227     return 0;
228 }
229
230 void *backends_main(void *ptr) {
231     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
232
233     BUFFER *b = buffer_create(1), *response = buffer_create(1);
234     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;
235     int (*backend_response_checker)(BUFFER *b) = NULL;
236
237     info("BACKEND thread created with task id %d", gettid());
238
239     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
240         error("Cannot set pthread cancel type to DEFERRED.");
241
242     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
243         error("Cannot set pthread cancel state to ENABLE.");
244
245     // ------------------------------------------------------------------------
246     // collect configuration options
247
248     struct timeval timeout = {
249             .tv_sec = 0,
250             .tv_usec = 0
251     };
252     int default_port = 0;
253     int sock = -1;
254     uint32_t options;
255     int enabled = config_get_boolean("backend", "enabled", 0);
256     const char *source = config_get("backend", "data source", "average");
257     const char *type = config_get("backend", "type", "graphite");
258     const char *destination = config_get("backend", "destination", "localhost");
259     const char *prefix = config_get("backend", "prefix", "netdata");
260     const char *hostname = config_get("backend", "hostname", localhost.hostname);
261     int frequency = (int)config_get_number("backend", "update every", 10);
262     int buffer_on_failures = (int)config_get_number("backend", "buffer on failures", 10);
263     long timeoutms = config_get_number("backend", "timeout ms", frequency * 2 * 1000);
264
265     // ------------------------------------------------------------------------
266     // validate configuration options
267     // and prepare for sending data to our backend
268     if(!enabled || frequency < 1)
269         goto cleanup;
270
271     if(!strcmp(source, "as collected")) {
272         options = BACKEND_SOURCE_DATA_AS_COLLECTED;
273     }
274     else if(!strcmp(source, "average")) {
275         options = BACKEND_SOURCE_DATA_AVERAGE;
276     }
277     else if(!strcmp(source, "sum") || !strcmp(source, "volume")) {
278         options = BACKEND_SOURCE_DATA_SUM;
279     }
280     else {
281         error("Invalid data source method '%s' for backend given. Disabling backed.", source);
282         goto cleanup;
283     }
284
285     if(!strcmp(type, "graphite") || !strcmp(type, "graphite:plaintext")) {
286         default_port = 2003;
287         if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
288             backend_request_formatter = format_dimension_collected_graphite_plaintext;
289         else
290             backend_request_formatter = format_dimension_stored_graphite_plaintext;
291
292         backend_response_checker = process_graphite_response;
293     }
294     else if(!strcmp(type, "opentsdb") || !strcmp(type, "opentsdb:telnet")) {
295         default_port = 4242;
296         if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
297             backend_request_formatter = format_dimension_collected_opentsdb_telnet;
298         else
299             backend_request_formatter = format_dimension_stored_opentsdb_telnet;
300
301         backend_response_checker = process_opentsdb_response;
302     }
303     else if (!strcmp(type, "json") || !strcmp(type, "json:plaintext"))
304     {
305         default_port = 5448;
306
307         if (options == BACKEND_SOURCE_DATA_AS_COLLECTED)
308         {
309             backend_request_formatter = format_dimension_collected_json_plaintext;
310         }
311         else
312         {
313             backend_request_formatter = format_dimension_stored_json_plaintext;
314         }
315
316         backend_response_checker = process_json_response;
317     }
318     else {
319         error("Unknown backend type '%s'", type);
320         goto cleanup;
321     }
322
323     if(backend_request_formatter == NULL || backend_response_checker == NULL) {
324         error("backend is misconfigured - disabling it.");
325         goto cleanup;
326     }
327
328     if(timeoutms < 1) {
329         error("BACKED invalid timeout %ld ms given. Assuming %d ms.", timeoutms, frequency * 2 * 1000);
330         timeoutms = frequency * 2 * 1000;
331     }
332     timeout.tv_sec  = (timeoutms * 1000) / 1000000;
333     timeout.tv_usec = (timeoutms * 1000) % 1000000;
334
335     // ------------------------------------------------------------------------
336     // prepare the charts for monitoring the backend
337
338     struct rusage thread;
339
340     collected_number
341             chart_buffered_metrics = 0,
342             chart_lost_metrics = 0,
343             chart_sent_metrics = 0,
344             chart_buffered_bytes = 0,
345             chart_received_bytes = 0,
346             chart_sent_bytes = 0,
347             chart_receptions = 0,
348             chart_transmission_successes = 0,
349             chart_transmission_failures = 0,
350             chart_data_lost_events = 0,
351             chart_lost_bytes = 0,
352             chart_backend_reconnects = 0,
353             chart_backend_latency = 0;
354
355     RRDSET *chart_metrics = rrdset_find("netdata.backend_metrics");
356     if(!chart_metrics) {
357         chart_metrics = rrdset_create("netdata", "backend_metrics", NULL, "backend", NULL, "Netdata Buffered Metrics", "metrics", 130600, frequency, RRDSET_TYPE_LINE);
358         rrddim_add(chart_metrics, "buffered", NULL,  1, 1, RRDDIM_ABSOLUTE);
359         rrddim_add(chart_metrics, "lost",     NULL,  1, 1, RRDDIM_ABSOLUTE);
360         rrddim_add(chart_metrics, "sent",     NULL,  1, 1, RRDDIM_ABSOLUTE);
361     }
362
363     RRDSET *chart_bytes = rrdset_find("netdata.backend_bytes");
364     if(!chart_bytes) {
365         chart_bytes = rrdset_create("netdata", "backend_bytes", NULL, "backend", NULL, "Netdata Backend Data Size", "KB", 130610, frequency, RRDSET_TYPE_AREA);
366         rrddim_add(chart_bytes, "buffered", NULL, 1, 1024, RRDDIM_ABSOLUTE);
367         rrddim_add(chart_bytes, "lost",     NULL, 1, 1024, RRDDIM_ABSOLUTE);
368         rrddim_add(chart_bytes, "sent",     NULL, 1, 1024, RRDDIM_ABSOLUTE);
369         rrddim_add(chart_bytes, "received", NULL, 1, 1024, RRDDIM_ABSOLUTE);
370     }
371
372     RRDSET *chart_ops = rrdset_find("netdata.backend_ops");
373     if(!chart_ops) {
374         chart_ops = rrdset_create("netdata", "backend_ops", NULL, "backend", NULL, "Netdata Backend Operations", "operations", 130630, frequency, RRDSET_TYPE_LINE);
375         rrddim_add(chart_ops, "write",     NULL, 1, 1, RRDDIM_ABSOLUTE);
376         rrddim_add(chart_ops, "discard",   NULL, 1, 1, RRDDIM_ABSOLUTE);
377         rrddim_add(chart_ops, "reconnect", NULL, 1, 1, RRDDIM_ABSOLUTE);
378         rrddim_add(chart_ops, "failure",   NULL, 1, 1, RRDDIM_ABSOLUTE);
379         rrddim_add(chart_ops, "read",      NULL, 1, 1, RRDDIM_ABSOLUTE);
380     }
381
382     /*
383      * this is misleading - we can only measure the time we need to send data
384      * this time is not related to the time required for the data to travel to
385      * the backend database and the time that server needed to process them
386      *
387      * issue #1432 and https://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html
388      *
389     RRDSET *chart_latency = rrdset_find("netdata.backend_latency");
390     if(!chart_latency) {
391         chart_latency = rrdset_create("netdata", "backend_latency", NULL, "backend", NULL, "Netdata Backend Latency", "ms", 130620, frequency, RRDSET_TYPE_AREA);
392         rrddim_add(chart_latency, "latency",   NULL,  1, 1000, RRDDIM_ABSOLUTE);
393     }
394     */
395
396     RRDSET *chart_rusage = rrdset_find("netdata.backend_thread_cpu");
397     if(!chart_rusage) {
398         chart_rusage = rrdset_create("netdata", "backend_thread_cpu", NULL, "backend", NULL, "NetData Backend Thread CPU usage", "milliseconds/s", 130630, frequency, RRDSET_TYPE_STACKED);
399         rrddim_add(chart_rusage, "user",   NULL, 1, 1000, RRDDIM_INCREMENTAL);
400         rrddim_add(chart_rusage, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
401     }
402
403     // ------------------------------------------------------------------------
404     // prepare the backend main loop
405
406     info("BACKEND configured ('%s' on '%s' sending '%s' data, every %d seconds, as host '%s', with prefix '%s')", type, destination, source, frequency, hostname, prefix);
407
408     usec_t step_ut = frequency * USEC_PER_SEC;
409     time_t after = now_realtime_sec();
410     int failures = 0;
411     heartbeat_t hb;
412     heartbeat_init(&hb);
413
414     for(;;) {
415         // ------------------------------------------------------------------------
416         // Wait for the next iteration point.
417         heartbeat_next(&hb, step_ut);
418         time_t before = now_realtime_sec();
419
420         // ------------------------------------------------------------------------
421         // add to the buffer the data we need to send to the backend
422         RRDSET *st;
423         int pthreadoldcancelstate;
424
425         if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
426             error("Cannot set pthread cancel state to DISABLE.");
427
428         rrdhost_rdlock(&localhost);
429         for(st = localhost.rrdset_root; st ;st = st->next) {
430             pthread_rwlock_rdlock(&st->rwlock);
431
432             RRDDIM *rd;
433             for(rd = st->dimensions; rd ;rd = rd->next) {
434                 if(rd->last_collected_time.tv_sec >= after)
435                     chart_buffered_metrics += backend_request_formatter(b, prefix, &localhost, hostname, st, rd, after, before, options);
436             }
437
438             pthread_rwlock_unlock(&st->rwlock);
439         }
440         rrdhost_unlock(&localhost);
441
442         if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
443             error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
444
445         // ------------------------------------------------------------------------
446
447         chart_buffered_bytes = (collected_number)buffer_strlen(b);
448
449         // reset the monitoring chart counters
450         chart_received_bytes =
451         chart_sent_bytes =
452         chart_sent_metrics =
453         chart_lost_metrics =
454         chart_transmission_successes =
455         chart_transmission_failures =
456         chart_data_lost_events =
457         chart_lost_bytes =
458         chart_backend_reconnects =
459         chart_backend_latency = 0;
460
461         if(unlikely(netdata_exit)) break;
462
463         //fprintf(stderr, "\nBACKEND BEGIN:\n%s\nBACKEND END\n", buffer_tostring(b)); // FIXME
464         //fprintf(stderr, "after = %lu, before = %lu\n", after, before);
465
466         // prepare for the next iteration
467         // to add incrementally data to buffer
468         after = before;
469
470         // ------------------------------------------------------------------------
471         // if we are connected, receive a response, without blocking
472
473         if(likely(sock != -1)) {
474             errno = 0;
475
476             // loop through to collect all data
477             while(sock != -1 && errno != EWOULDBLOCK) {
478                 buffer_need_bytes(response, 4096);
479
480                 ssize_t r = recv(sock, &response->buffer[response->len], response->size - response->len, MSG_DONTWAIT);
481                 if(likely(r > 0)) {
482                     // we received some data
483                     response->len += r;
484                     chart_received_bytes += r;
485                     chart_receptions++;
486                 }
487                 else if(r == 0) {
488                     error("Backend '%s' closed the socket", destination);
489                     close(sock);
490                     sock = -1;
491                 }
492                 else {
493                     // failed to receive data
494                     if(errno != EAGAIN && errno != EWOULDBLOCK) {
495                         error("Cannot receive data from backend '%s'.", destination);
496                     }
497                 }
498             }
499
500             // if we received data, process them
501             if(buffer_strlen(response))
502                 backend_response_checker(response);
503         }
504
505         // ------------------------------------------------------------------------
506         // if we are not connected, connect to a backend server
507
508         if(unlikely(sock == -1)) {
509             usec_t start_ut = now_monotonic_usec();
510             const char *s = destination;
511             while(*s) {
512                 const char *e = s;
513
514                 // skip separators, moving both s(tart) and e(nd)
515                 while(isspace(*e) || *e == ',') s = ++e;
516
517                 // move e(nd) to the first separator
518                 while(*e && !isspace(*e) && *e != ',') e++;
519
520                 // is there anything?
521                 if(!*s || s == e) break;
522
523                 char buf[e - s + 1];
524                 strncpyz(buf, s, e - s);
525                 chart_backend_reconnects++;
526                 sock = connect_to(buf, default_port, &timeout);
527                 if(sock != -1) break;
528                 s = e;
529             }
530             chart_backend_latency += now_monotonic_usec() - start_ut;
531         }
532
533         if(unlikely(netdata_exit)) break;
534
535         // ------------------------------------------------------------------------
536         // if we are connected, send our buffer to the backend server
537
538         if(likely(sock != -1)) {
539             size_t len = buffer_strlen(b);
540             usec_t start_ut = now_monotonic_usec();
541             int flags = 0;
542 #ifdef MSG_NOSIGNAL
543             flags += MSG_NOSIGNAL;
544 #endif
545
546             ssize_t written = send(sock, buffer_tostring(b), len, flags);
547             chart_backend_latency += now_monotonic_usec() - start_ut;
548             if(written != -1 && (size_t)written == len) {
549                 // we sent the data successfully
550                 chart_transmission_successes++;
551                 chart_sent_bytes += written;
552                 chart_sent_metrics = chart_buffered_metrics;
553
554                 // reset the failures count
555                 failures = 0;
556
557                 // empty the buffer
558                 buffer_flush(b);
559             }
560             else {
561                 // oops! we couldn't send (all or some of the) data
562                 error("Failed to write data to database backend '%s'. Willing to write %zu bytes, wrote %zd bytes. Will re-connect.", destination, len, written);
563                 chart_transmission_failures++;
564
565                 if(written != -1)
566                     chart_sent_bytes += written;
567
568                 // increment the counter we check for data loss
569                 failures++;
570
571                 // close the socket - we will re-open it next time
572                 close(sock);
573                 sock = -1;
574             }
575         }
576         else {
577             error("Failed to update database backend '%s'", destination);
578             chart_transmission_failures++;
579
580             // increment the counter we check for data loss
581             failures++;
582         }
583
584         if(failures > buffer_on_failures) {
585             // too bad! we are going to lose data
586             chart_lost_bytes += buffer_strlen(b);
587             error("Reached %d backend failures. Flushing buffers to protect this host - this results in data loss on back-end server '%s'", failures, destination);
588             buffer_flush(b);
589             failures = 0;
590             chart_data_lost_events++;
591             chart_lost_metrics = chart_buffered_metrics;
592         }
593
594         if(unlikely(netdata_exit)) break;
595
596         // ------------------------------------------------------------------------
597         // update the monitoring charts
598
599         if(chart_ops->counter_done) rrdset_next(chart_ops);
600         rrddim_set(chart_ops, "read",         chart_receptions);
601         rrddim_set(chart_ops, "write",        chart_transmission_successes);
602         rrddim_set(chart_ops, "discard",      chart_data_lost_events);
603         rrddim_set(chart_ops, "failure",      chart_transmission_failures);
604         rrddim_set(chart_ops, "reconnect",    chart_backend_reconnects);
605         rrdset_done(chart_ops);
606
607         if(chart_metrics->counter_done) rrdset_next(chart_metrics);
608         rrddim_set(chart_metrics, "buffered", chart_buffered_metrics);
609         rrddim_set(chart_metrics, "lost",     chart_lost_metrics);
610         rrddim_set(chart_metrics, "sent",     chart_sent_metrics);
611         rrdset_done(chart_metrics);
612
613         if(chart_bytes->counter_done) rrdset_next(chart_bytes);
614         rrddim_set(chart_bytes, "buffered",   chart_buffered_bytes);
615         rrddim_set(chart_bytes, "lost",       chart_lost_bytes);
616         rrddim_set(chart_bytes, "sent",       chart_sent_bytes);
617         rrddim_set(chart_bytes, "received",   chart_received_bytes);
618         rrdset_done(chart_bytes);
619
620         /*
621         if(chart_latency->counter_done) rrdset_next(chart_latency);
622         rrddim_set(chart_latency, "latency",  chart_backend_latency);
623         rrdset_done(chart_latency);
624         */
625
626         getrusage(RUSAGE_THREAD, &thread);
627         if(chart_rusage->counter_done) rrdset_next(chart_rusage);
628         rrddim_set(chart_rusage, "user",   thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
629         rrddim_set(chart_rusage, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
630         rrdset_done(chart_rusage);
631
632         if(likely(buffer_strlen(b) == 0))
633             chart_buffered_metrics = 0;
634
635         if(unlikely(netdata_exit)) break;
636     }
637
638 cleanup:
639     if(sock != -1)
640         close(sock);
641
642     buffer_free(b);
643     buffer_free(response);
644
645     info("BACKEND thread exiting");
646
647     static_thread->enabled = 0;
648     pthread_exit(NULL);
649     return NULL;
650 }