#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.
//
const char *backend_prefix = "netdata";
int backend_send_names = 1;
int backend_update_every = 10;
uint32_t backend_options = BACKEND_SOURCE_DATA_AVERAGE;
// ----------------------------------------------------------------------------
// 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
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
, time_t *first_timestamp // the first point of the database used in this response
, time_t *last_timestamp // the timestamp that should be reported to backend
) {
// 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;
// step back a little, to make sure we have complete data collection
// for all metrics
after -= update_every * 2;
before -= update_every * 2;
// align the time-frame
after = after - (after % update_every);
before = before - (before % update_every);
// for before, loose another iteration
// the latest point will be reported the next time
before -= update_every;
if(unlikely(after > before))
// this can happen when update_every > before - after
after = before;
if(unlikely(after < first_t))
after = first_t;
if(unlikely(before > last_t))
before = last_t;
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;
}
*first_timestamp = after;
*last_timestamp = before;
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_BITS) == BACKEND_SOURCE_DATA_SUM))
return sum;
return sum / (calculated_number)