]> arthur.barton.de Git - netdata.git/blob - src/backends.c
Merge pull request #1815 from lfdominguez/master
[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     int default_port = 0;
232     int sock = -1;
233     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
234
235     BUFFER *b = buffer_create(1), *response = buffer_create(1);
236     int (*backend_request_formatter)(BUFFER *, const char *, RRDHOST *, const char *, RRDSET *, RRDDIM *, time_t, time_t, uint32_t) = NULL;
237     int (*backend_response_checker)(BUFFER *) = NULL;
238
239     info("BACKEND thread created with task id %d", gettid());
240
241     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
242         error("Cannot set pthread cancel type to DEFERRED.");
243
244     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
245         error("Cannot set pthread cancel state to ENABLE.");
246
247     // ------------------------------------------------------------------------
248     // collect configuration options
249
250     struct timeval timeout = {
251             .tv_sec = 0,
252             .tv_usec = 0
253     };
254     uint32_t options;
255     int enabled             = config_get_boolean(CONFIG_SECTION_BACKEND, "enabled", 0);
256     const char *source      = config_get(CONFIG_SECTION_BACKEND, "data source", "average");
257     const char *type        = config_get(CONFIG_SECTION_BACKEND, "type", "graphite");
258     const char *destination = config_get(CONFIG_SECTION_BACKEND, "destination", "localhost");
259     const char *prefix      = config_get(CONFIG_SECTION_BACKEND, "prefix", "netdata");
260     const char *hostname    = config_get(CONFIG_SECTION_BACKEND, "hostname", localhost->hostname);
261     int frequency           = (int)config_get_number(CONFIG_SECTION_BACKEND, "update every", 10);
262     int buffer_on_failures  = (int)config_get_number(CONFIG_SECTION_BACKEND, "buffer on failures", 10);
263     long timeoutms          = config_get_number(CONFIG_SECTION_BACKEND, "timeout ms", frequency * 2 * 1000);
264
265     // ------------------------------------------------------------------------
266     // validate configuration options
267     // and prepare for sending data to our backend
268
269     if(!enabled || frequency < 1)
270         goto cleanup;
271
272     if(!strcmp(source, "as collected")) {
273         options = BACKEND_SOURCE_DATA_AS_COLLECTED;
274     }
275     else if(!strcmp(source, "average")) {
276         options = BACKEND_SOURCE_DATA_AVERAGE;
277     }
278     else if(!strcmp(source, "sum") || !strcmp(source, "volume")) {
279         options = BACKEND_SOURCE_DATA_SUM;
280     }
281     else {
282         error("Invalid data source method '%s' for backend given. Disabling backed.", source);
283         goto cleanup;
284     }
285
286     if(timeoutms < 1) {
287         error("BACKED invalid timeout %ld ms given. Assuming %d ms.", timeoutms, frequency * 2 * 1000);
288         timeoutms = frequency * 2 * 1000;
289     }
290     timeout.tv_sec  = (timeoutms * 1000) / 1000000;
291     timeout.tv_usec = (timeoutms * 1000) % 1000000;
292
293
294     // ------------------------------------------------------------------------
295     // select the backend type
296
297     if(!strcmp(type, "graphite") || !strcmp(type, "graphite:plaintext")) {
298         default_port = 2003;
299         if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
300             backend_request_formatter = format_dimension_collected_graphite_plaintext;
301         else
302             backend_request_formatter = format_dimension_stored_graphite_plaintext;
303
304         backend_response_checker = process_graphite_response;
305     }
306     else if(!strcmp(type, "opentsdb") || !strcmp(type, "opentsdb:telnet")) {
307         default_port = 4242;
308         if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
309             backend_request_formatter = format_dimension_collected_opentsdb_telnet;
310         else
311             backend_request_formatter = format_dimension_stored_opentsdb_telnet;
312
313         backend_response_checker = process_opentsdb_response;
314     }
315     else if (!strcmp(type, "json") || !strcmp(type, "json:plaintext"))
316     {
317         default_port = 5448;
318
319         if (options == BACKEND_SOURCE_DATA_AS_COLLECTED)
320         {
321             backend_request_formatter = format_dimension_collected_json_plaintext;
322         }
323         else
324         {
325             backend_request_formatter = format_dimension_stored_json_plaintext;
326         }
327
328         backend_response_checker = process_json_response;
329     }
330     else {
331         error("Unknown backend type '%s'", type);
332         goto cleanup;
333     }
334
335     if(backend_request_formatter == NULL || backend_response_checker == NULL) {
336         error("backend is misconfigured - disabling it.");
337         goto cleanup;
338     }
339
340
341     // ------------------------------------------------------------------------
342     // prepare the charts for monitoring the backend operation
343
344     struct rusage thread;
345
346     collected_number
347             chart_buffered_metrics = 0,
348             chart_lost_metrics = 0,
349             chart_sent_metrics = 0,
350             chart_buffered_bytes = 0,
351             chart_received_bytes = 0,
352             chart_sent_bytes = 0,
353             chart_receptions = 0,
354             chart_transmission_successes = 0,
355             chart_transmission_failures = 0,
356             chart_data_lost_events = 0,
357             chart_lost_bytes = 0,
358             chart_backend_reconnects = 0,
359             chart_backend_latency = 0;
360
361     RRDSET *chart_metrics = rrdset_create_localhost("netdata", "backend_metrics", NULL, "backend", NULL, "Netdata Buffered Metrics", "metrics", 130600, frequency, RRDSET_TYPE_LINE);
362     rrddim_add(chart_metrics, "buffered", NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
363     rrddim_add(chart_metrics, "lost",     NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
364     rrddim_add(chart_metrics, "sent",     NULL,  1, 1, RRD_ALGORITHM_ABSOLUTE);
365
366     RRDSET *chart_bytes = rrdset_create_localhost("netdata", "backend_bytes", NULL, "backend", NULL, "Netdata Backend Data Size", "KB", 130610, frequency, RRDSET_TYPE_AREA);
367     rrddim_add(chart_bytes, "buffered", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
368     rrddim_add(chart_bytes, "lost",     NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
369     rrddim_add(chart_bytes, "sent",     NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
370     rrddim_add(chart_bytes, "received", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
371
372     RRDSET *chart_ops = rrdset_create_localhost("netdata", "backend_ops", NULL, "backend", NULL, "Netdata Backend Operations", "operations", 130630, frequency, RRDSET_TYPE_LINE);
373     rrddim_add(chart_ops, "write",     NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
374     rrddim_add(chart_ops, "discard",   NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
375     rrddim_add(chart_ops, "reconnect", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
376     rrddim_add(chart_ops, "failure",   NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
377     rrddim_add(chart_ops, "read",      NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
378
379     /*
380      * this is misleading - we can only measure the time we need to send data
381      * this time is not related to the time required for the data to travel to
382      * the backend database and the time that server needed to process them
383      *
384      * issue #1432 and https://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html
385      *
386     RRDSET *chart_latency = rrdset_create_localhost("netdata", "backend_latency", NULL, "backend", NULL, "Netdata Backend Latency", "ms", 130620, frequency, RRDSET_TYPE_AREA);
387     rrddim_add(chart_latency, "latency",   NULL,  1, 1000, RRD_ALGORITHM_ABSOLUTE);
388     */
389
390     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);
391     rrddim_add(chart_rusage, "user",   NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
392     rrddim_add(chart_rusage, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
393
394
395     // ------------------------------------------------------------------------
396     // prepare the backend main loop
397
398     info("BACKEND configured ('%s' on '%s' sending '%s' data, every %d seconds, as host '%s', with prefix '%s')", type, destination, source, frequency, hostname, prefix);
399
400     usec_t step_ut = frequency * USEC_PER_SEC;
401     time_t after = now_realtime_sec();
402     int failures = 0;
403     heartbeat_t hb;
404     heartbeat_init(&hb);
405
406     for(;;) {
407
408         // ------------------------------------------------------------------------
409         // Wait for the next iteration point.
410         heartbeat_next(&hb, step_ut);
411         time_t before = now_realtime_sec();
412
413
414         // ------------------------------------------------------------------------
415         // add to the buffer the data we need to send to the backend
416
417         int pthreadoldcancelstate;
418
419         if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
420             error("Cannot set pthread cancel state to DISABLE.");
421
422         rrd_rdlock();
423         RRDHOST *host;
424         rrdhost_foreach_read(host) {
425             if(host->rrd_memory_mode == RRD_MEMORY_MODE_NONE)
426                 continue;
427
428             rrdhost_rdlock(host);
429
430             RRDSET *st;
431             rrdset_foreach_read(st, host) {
432                 rrdset_rdlock(st);
433
434                 RRDDIM *rd;
435                 rrddim_foreach_read(rd, st) {
436                     if(rd->last_collected_time.tv_sec >= after)
437                         chart_buffered_metrics += backend_request_formatter(b, prefix, host, (host == localhost)?hostname:host->hostname, st, rd, after, before, options);
438                 }
439                 rrdset_unlock(st);
440             }
441             rrdhost_unlock(host);
442         }
443         rrd_unlock();
444
445         if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
446             error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
447
448         // ------------------------------------------------------------------------
449
450         chart_buffered_bytes = (collected_number)buffer_strlen(b);
451
452         // reset the monitoring chart counters
453         chart_received_bytes =
454         chart_sent_bytes =
455         chart_sent_metrics =
456         chart_lost_metrics =
457         chart_transmission_successes =
458         chart_transmission_failures =
459         chart_data_lost_events =
460         chart_lost_bytes =
461         chart_backend_reconnects =
462         chart_backend_latency = 0;
463
464         if(unlikely(netdata_exit)) break;
465
466         //fprintf(stderr, "\nBACKEND BEGIN:\n%s\nBACKEND END\n", buffer_tostring(b)); // FIXME
467         //fprintf(stderr, "after = %lu, before = %lu\n", after, before);
468
469         // prepare for the next iteration
470         // to add incrementally data to buffer
471         after = before;
472
473         // ------------------------------------------------------------------------
474         // if we are connected, receive a response, without blocking
475
476         if(likely(sock != -1)) {
477             errno = 0;
478
479             // loop through to collect all data
480             while(sock != -1 && errno != EWOULDBLOCK) {
481                 buffer_need_bytes(response, 4096);
482
483                 ssize_t r = recv(sock, &response->buffer[response->len], response->size - response->len, MSG_DONTWAIT);
484                 if(likely(r > 0)) {
485                     // we received some data
486                     response->len += r;
487                     chart_received_bytes += r;
488                     chart_receptions++;
489                 }
490                 else if(r == 0) {
491                     error("Backend '%s' closed the socket", destination);
492                     close(sock);
493                     sock = -1;
494                 }
495                 else {
496                     // failed to receive data
497                     if(errno != EAGAIN && errno != EWOULDBLOCK) {
498                         error("Cannot receive data from backend '%s'.", destination);
499                     }
500                 }
501             }
502
503             // if we received data, process them
504             if(buffer_strlen(response))
505                 backend_response_checker(response);
506         }
507
508         // ------------------------------------------------------------------------
509         // if we are not connected, connect to a backend server
510
511         if(unlikely(sock == -1)) {
512             usec_t start_ut = now_monotonic_usec();
513             size_t reconnects = 0;
514
515             sock = connect_to_one_of(destination, default_port, &timeout, &reconnects, NULL, 0);
516
517             chart_backend_reconnects += reconnects;
518             chart_backend_latency += now_monotonic_usec() - start_ut;
519         }
520
521         if(unlikely(netdata_exit)) break;
522
523         // ------------------------------------------------------------------------
524         // if we are connected, send our buffer to the backend server
525
526         if(likely(sock != -1)) {
527             size_t len = buffer_strlen(b);
528             usec_t start_ut = now_monotonic_usec();
529             int flags = 0;
530 #ifdef MSG_NOSIGNAL
531             flags += MSG_NOSIGNAL;
532 #endif
533
534             ssize_t written = send(sock, buffer_tostring(b), len, flags);
535             chart_backend_latency += now_monotonic_usec() - start_ut;
536             if(written != -1 && (size_t)written == len) {
537                 // we sent the data successfully
538                 chart_transmission_successes++;
539                 chart_sent_bytes += written;
540                 chart_sent_metrics = chart_buffered_metrics;
541
542                 // reset the failures count
543                 failures = 0;
544
545                 // empty the buffer
546                 buffer_flush(b);
547             }
548             else {
549                 // oops! we couldn't send (all or some of the) data
550                 error("Failed to write data to database backend '%s'. Willing to write %zu bytes, wrote %zd bytes. Will re-connect.", destination, len, written);
551                 chart_transmission_failures++;
552
553                 if(written != -1)
554                     chart_sent_bytes += written;
555
556                 // increment the counter we check for data loss
557                 failures++;
558
559                 // close the socket - we will re-open it next time
560                 close(sock);
561                 sock = -1;
562             }
563         }
564         else {
565             error("Failed to update database backend '%s'", destination);
566             chart_transmission_failures++;
567
568             // increment the counter we check for data loss
569             failures++;
570         }
571
572         if(failures > buffer_on_failures) {
573             // too bad! we are going to lose data
574             chart_lost_bytes += buffer_strlen(b);
575             error("Reached %d backend failures. Flushing buffers to protect this host - this results in data loss on back-end server '%s'", failures, destination);
576             buffer_flush(b);
577             failures = 0;
578             chart_data_lost_events++;
579             chart_lost_metrics = chart_buffered_metrics;
580         }
581
582         if(unlikely(netdata_exit)) break;
583
584         // ------------------------------------------------------------------------
585         // update the monitoring charts
586
587         if(likely(chart_ops->counter_done)) rrdset_next(chart_ops);
588         rrddim_set(chart_ops, "read",         chart_receptions);
589         rrddim_set(chart_ops, "write",        chart_transmission_successes);
590         rrddim_set(chart_ops, "discard",      chart_data_lost_events);
591         rrddim_set(chart_ops, "failure",      chart_transmission_failures);
592         rrddim_set(chart_ops, "reconnect",    chart_backend_reconnects);
593         rrdset_done(chart_ops);
594
595         if(likely(chart_metrics->counter_done)) rrdset_next(chart_metrics);
596         rrddim_set(chart_metrics, "buffered", chart_buffered_metrics);
597         rrddim_set(chart_metrics, "lost",     chart_lost_metrics);
598         rrddim_set(chart_metrics, "sent",     chart_sent_metrics);
599         rrdset_done(chart_metrics);
600
601         if(likely(chart_bytes->counter_done)) rrdset_next(chart_bytes);
602         rrddim_set(chart_bytes, "buffered",   chart_buffered_bytes);
603         rrddim_set(chart_bytes, "lost",       chart_lost_bytes);
604         rrddim_set(chart_bytes, "sent",       chart_sent_bytes);
605         rrddim_set(chart_bytes, "received",   chart_received_bytes);
606         rrdset_done(chart_bytes);
607
608         /*
609         if(likely(chart_latency->counter_done)) rrdset_next(chart_latency);
610         rrddim_set(chart_latency, "latency",  chart_backend_latency);
611         rrdset_done(chart_latency);
612         */
613
614         getrusage(RUSAGE_THREAD, &thread);
615         if(likely(chart_rusage->counter_done)) rrdset_next(chart_rusage);
616         rrddim_set(chart_rusage, "user",   thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
617         rrddim_set(chart_rusage, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
618         rrdset_done(chart_rusage);
619
620         if(likely(buffer_strlen(b) == 0))
621             chart_buffered_metrics = 0;
622
623         if(unlikely(netdata_exit)) break;
624     }
625
626 cleanup:
627     if(sock != -1)
628         close(sock);
629
630     buffer_free(b);
631     buffer_free(response);
632
633     info("BACKEND thread exiting");
634
635     static_thread->enabled = 0;
636     pthread_exit(NULL);
637     return NULL;
638 }