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