#include "common.h"
// ----------------------------------------------------------------------------
// How backends work in netdata:
//
// 1. There is an independent thread that runs at the required interval
// (for example, once every 10 seconds)
//
// 2. Every time it wakes, it calls the backend formatting functions to build
// a buffer of data. This is a very fast, memory only operation.
//
// 3. If the buffer already includes data, the new data are appended.
// If the buffer becomes too big, because the data cannot be sent, a
// log is written and the buffer is discarded.
//
// 4. Then it tries to send all the data. It blocks until all the data are sent
// or the socket returns an error.
// If the time required for this is above the interval, it starts skipping
// intervals, but the calculated values include the entire database, without
// gaps (it remembers the timestamps and continues from where it stopped).
//
// 5. repeats the above forever.
//
#define BACKEND_SOURCE_DATA_AS_COLLECTED 0x00000001
#define BACKEND_SOURCE_DATA_AVERAGE 0x00000002
#define BACKEND_SOURCE_DATA_SUM 0x00000004
int backend_send_names = 1;
// ----------------------------------------------------------------------------
// helper functions for backends
static inline size_t backend_name_copy(char *d, const char *s, size_t usable) {
size_t n;
for(n = 0; *s && n < usable ; d++, s++, n++) {
char c = *s;
if(c != '.' && !isalnum(c)) *d = '_';
else *d = c;
}
*d = '\0';
return n;
}
// calculate the SUM or AVERAGE of a dimension, for any timeframe
// may return NAN if the database does not have any value in the give timeframe
static inline calculated_number backend_calculate_value_from_stored_data(
RRDSET *st // the chart
, RRDDIM *rd // the dimension
, time_t after // the start timestamp
, time_t before // the end timestamp
, uint32_t options // BACKEND_SOURCE_* bitmap
) {
// find the edges of the rrd database for this chart
time_t first_t = rrdset_first_entry_t(st);
time_t last_t = rrdset_last_entry_t(st);
time_t update_every = st->update_every;
// align the time-frame
// for 'after' also skip the first value by adding update_every
after = after - after % update_every + update_every;
before = before - before % update_every;
if(unlikely(after = last_t + update_every)) {
// we missed an update, report the last one
after -= update_every;
before -= update_every;
}
if(unlikely(before < first_t || after > last_t)) {
// the chart has not been updated in the wanted timeframe
debug(D_BACKEND, "BACKEND: %s.%s.%s: aligned timeframe %lu to %lu is outside the chart's database range %lu to %lu",
st->rrdhost->hostname, st->id, rd->id,
(unsigned long)after, (unsigned long)before,
(unsigned long)first_t, (unsigned long)last_t
);
return NAN;
}
if(unlikely(after < first_t))
after = first_t;
if(unlikely(after > before))
// this can happen when update_every > before - after
before = after;
if(unlikely(before > last_t))
before = last_t;
size_t counter = 0;
calculated_number sum = 0;
long start_at_slot = rrdset_time2slot(st, before),
stop_at_slot = rrdset_time2slot(st, after),
slot, stop_now = 0;
for(slot = start_at_slot; !stop_now ; slot--) {
if(unlikely(slot < 0)) slot = st->entries - 1;
if(unlikely(slot == stop_at_slot)) stop_now = 1;
storage_number n = rd->values[slot];
if(unlikely(!does_storage_number_exist(n))) {
// not collected
continue;
}
calculated_number value = unpack_storage_number(n);
sum += value;
counter++;
}
if(unlikely(!counter)) {
debug(D_BACKEND, "BACKEND: %s.%s.%s: no values stored in database for range %lu to %lu",
st->rrdhost->hostname, st->id, rd->id,
(unsigned long)after, (unsigned long)before
);
return NAN;
}
if(unlikely(options & BACKEND_SOURCE_DATA_SUM))
return sum;
return sum / (calculated_number)counter;
}
// discard a response received by a backend
// after logging a simple of it to error.log
static inline int discard_response(BUFFER *b, const char *backend) {
char sample[1024];
const char *s = buffer_tostring(b);
char *d = sample, *e = &sample[sizeof(sample) - 1];
for(; *s