3 #define BACKEND_SOURCE_DATA_AS_COLLECTED 0x00000001
4 #define BACKEND_SOURCE_DATA_AVERAGE 0x00000002
5 #define BACKEND_SOURCE_DATA_SUM 0x00000003
7 int connect_to_socket4(const char *ip, int port) {
10 debug(D_LISTENER, "IPv4 connecting to ip '%s' port %d", ip, port);
12 sock = socket(AF_INET, SOCK_STREAM, 0);
14 error("IPv4 socket() on ip '%s' port %d failed.", ip, port);
18 struct sockaddr_in name;
19 memset(&name, 0, sizeof(struct sockaddr_in));
20 name.sin_family = AF_INET;
21 name.sin_port = htons(port);
23 int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
25 error("Failed to convert '%s' to a valid IPv4 address.", ip);
30 if(connect(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
32 error("IPv4 failed to connect to '%s', port %d", ip, port);
36 debug(D_LISTENER, "Connected to IPv4 ip '%s' port %d", ip, port);
40 int connect_to_socket6(const char *ip, int port) {
44 debug(D_LISTENER, "IPv6 connecting to ip '%s' port %d", ip, port);
46 sock = socket(AF_INET6, SOCK_STREAM, 0);
48 error("IPv6 socket() on ip '%s' port %d failed.", ip, port);
53 if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&ipv6only, sizeof(ipv6only)) != 0)
54 error("Cannot set IPV6_V6ONLY on ip '%s' port's %d.", ip, port);
56 struct sockaddr_in6 name;
57 memset(&name, 0, sizeof(struct sockaddr_in6));
58 name.sin6_family = AF_INET6;
59 name.sin6_port = htons ((uint16_t) port);
61 int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
63 error("Failed to convert IP '%s' to a valid IPv6 address.", ip);
68 name.sin6_scope_id = 0;
70 if(connect(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
72 error("IPv6 failed to connect to '%s', port %d", ip, port);
76 debug(D_LISTENER, "Connected to IPv6 ip '%s' port %d", ip, port);
81 static inline int connect_to_one(const char *definition, int default_port) {
82 struct addrinfo hints;
83 struct addrinfo *result = NULL, *rp = NULL;
85 char buffer[strlen(definition) + 1];
86 strcpy(buffer, definition);
89 snprintfz(buffer2, 10, "%d", default_port);
91 char *ip = buffer, *port = buffer2;
96 while(*e && *e != ']') e++;
103 while(*e && *e != ':') e++;
117 memset(&hints, 0, sizeof(struct addrinfo));
118 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
119 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
120 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
121 hints.ai_protocol = 0; /* Any protocol */
122 hints.ai_canonname = NULL;
123 hints.ai_addr = NULL;
124 hints.ai_next = NULL;
126 int r = getaddrinfo(ip, port, &hints, &result);
128 error("Cannot resolve host '%s', port '%s': %s\n", ip, port, gai_strerror(r));
133 for (rp = result; rp != NULL && fd == -1; rp = rp->ai_next) {
134 char rip[INET_ADDRSTRLEN + INET6_ADDRSTRLEN] = "INVALID";
137 switch (rp->ai_addr->sa_family) {
139 struct sockaddr_in *sin = (struct sockaddr_in *) rp->ai_addr;
140 inet_ntop(AF_INET, &sin->sin_addr, rip, INET_ADDRSTRLEN);
141 rport = ntohs(sin->sin_port);
142 fd = connect_to_socket4(rip, rport);
147 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) rp->ai_addr;
148 inet_ntop(AF_INET6, &sin6->sin6_addr, rip, INET6_ADDRSTRLEN);
149 rport = ntohs(sin6->sin6_port);
150 fd = connect_to_socket6(rip, rport);
156 freeaddrinfo(result);
161 static inline calculated_number backend_duration_average(RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
162 time_t first_t = rrdset_first_entry_t(st);
163 time_t last_t = rrdset_last_entry_t(st);
165 if(unlikely(before - after < st->update_every && after != after - after % st->update_every))
166 // when st->update_every is bigger than the frequency we send data to backend
167 // skip the iterations that are not aligned to the database
170 // align the time-frame
171 // for 'after' also skip the first value by adding st->update_every
172 after = after - after % st->update_every + st->update_every;
173 before = before - before % st->update_every;
175 if(unlikely(after < first_t))
178 if(unlikely(after > before))
179 // this can happen when the st->update_every > before - after
182 if(unlikely(before > last_t))
186 calculated_number sum = 0;
188 long start_at_slot = rrdset_time2slot(st, before),
189 stop_at_slot = rrdset_time2slot(st, after),
192 for(slot = start_at_slot; !stop_now ; slot--) {
193 if(unlikely(slot < 0)) slot = st->entries - 1;
194 if(unlikely(slot == stop_at_slot)) stop_now = 1;
196 storage_number n = rd->values[slot];
197 if(unlikely(!does_storage_number_exist(n))) continue;
199 calculated_number value = unpack_storage_number(n);
204 if(unlikely(!counter))
207 if(unlikely(options & BACKEND_SOURCE_DATA_SUM))
210 return sum / (calculated_number)counter;
213 static inline void format_dimension_collected_graphite_plaintext(BUFFER *b, const char *prefix, RRDHOST *host, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
217 buffer_sprintf(b, "%s.%s.%s.%s " COLLECTED_NUMBER_FORMAT " %u\n", prefix, host->hostname, st->id, rd->id, rd->last_collected_value, (uint32_t)rd->last_collected_time.tv_sec);
220 static inline void format_dimension_stored_graphite_plaintext(BUFFER *b, const char *prefix, RRDHOST *host, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
221 calculated_number value = backend_duration_average(st, rd, after, before, options);
223 buffer_sprintf(b, "%s.%s.%s.%s " CALCULATED_NUMBER_FORMAT " %u\n", prefix, host->hostname, st->id, rd->id, value, (uint32_t)before);
226 static inline void format_dimension_collected_opentsdb_telnet(BUFFER *b, const char *prefix, RRDHOST *host, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
230 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, host->hostname);
233 static inline void format_dimension_stored_opentsdb_telnet(BUFFER *b, const char *prefix, RRDHOST *host, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
234 calculated_number value = backend_duration_average(st, rd, after, before, options);
236 buffer_sprintf(b, "put %s.%s.%s %u " CALCULATED_NUMBER_FORMAT " host=%s\n", prefix, st->id, rd->id, (uint32_t)before, value, host->hostname);
239 void *backends_main(void *ptr) {
242 BUFFER *b = buffer_create(1);
243 void (*formatter)(BUFFER *b, const char *prefix, RRDHOST *host, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options);
245 info("BACKEND thread created with task id %d", gettid());
247 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
248 error("Cannot set pthread cancel type to DEFERRED.");
250 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
251 error("Cannot set pthread cancel state to ENABLE.");
253 int default_port = 0;
255 uint32_t options = BACKEND_SOURCE_DATA_AVERAGE;
256 int enabled = config_get_boolean("backend", "enable", 0);
257 const char *source = config_get("backend", "data source", "average");
258 const char *type = config_get("backend", "type", "graphite");
259 const char *destination = config_get("backend", "destination", "localhost");
260 const char *prefix = config_get("backend", "prefix", "netdata");
261 int frequency = (int)config_get_number("backend", "update every", 30);
262 int buffer_on_failures = (int)config_get_number("backend", "buffer on failures", 10);
264 if(!enabled || frequency < 1)
267 if(!strcmp(source, "as collected")) {
268 options = BACKEND_SOURCE_DATA_AS_COLLECTED;
270 else if(!strcmp(source, "average")) {
271 options = BACKEND_SOURCE_DATA_AVERAGE;
273 else if(!strcmp(source, "sum") || !strcmp(source, "volume")) {
274 options = BACKEND_SOURCE_DATA_SUM;
277 error("Invalid data source method '%s' for backend given. Disabling backed.", source);
281 if(!strcmp(type, "graphite") || !strcmp(type, "graphite:plaintext")) {
283 if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
284 formatter = format_dimension_collected_graphite_plaintext;
286 formatter = format_dimension_stored_graphite_plaintext;
288 else if(!strcmp(type, "opentsdb") || !strcmp(type, "opentsdb:telnet")) {
290 if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
291 formatter = format_dimension_collected_opentsdb_telnet;
293 formatter = format_dimension_stored_opentsdb_telnet;
296 error("Unknown backend type '%s'", type);
300 unsigned long long step_ut = frequency * 1000000ULL;
301 unsigned long long random_ut = time_usec() % (step_ut / 2);
302 time_t before = (time_t)((time_usec() - step_ut) / 10000000ULL);
303 time_t after = before;
307 unsigned long long now_ut = time_usec();
308 unsigned long long next_ut = now_ut - (now_ut % step_ut) + step_ut;
309 before = (time_t)(next_ut / 1000000ULL);
311 // add a little delay (1/4 of the step) plus some randomness
312 next_ut += (step_ut / 4) + random_ut;
314 while(now_ut < next_ut) {
315 sleep_usec(next_ut - now_ut);
316 now_ut = time_usec();
320 int pthreadoldcancelstate;
322 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
323 error("Cannot set pthread cancel state to DISABLE.");
325 rrdhost_rdlock(&localhost);
326 for(st = localhost.rrdset_root; st ;st = st->next) {
327 pthread_rwlock_rdlock(&st->rwlock);
330 for(rd = st->dimensions; rd ;rd = rd->next) {
331 if(rd->last_collected_time.tv_sec >= after)
332 formatter(b, prefix, &localhost, st, rd, after, before, options);
335 pthread_rwlock_unlock(&st->rwlock);
337 rrdhost_unlock(&localhost);
339 if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
340 error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
342 if(unlikely(netdata_exit)) break;
344 //fprintf(stderr, "\nBACKEND BEGIN:\n%s\nBACKEND END\n", buffer_tostring(b));
345 //fprintf(stderr, "after = %lu, before = %lu\n", after, before);
347 if(unlikely(sock == -1)) {
348 const char *s = destination;
352 // skip separators, moving both s(tart) and e(nd)
353 while(isspace(*e) || *e == ',') s = ++e;
355 // move e(nd) to the first separator
356 while(*e && !isspace(*e) && *e != ',') e++;
358 // is there anything?
359 if(!*s || s == e) break;
362 strncpyz(buf, s, e - s);
363 sock = connect_to_one(buf, default_port);
364 if(sock != -1) break;
369 if(unlikely(netdata_exit)) break;
371 if(likely(sock != -1)) {
372 size_t len = buffer_strlen(b);
373 ssize_t written = write(sock, buffer_tostring(b), len);
374 if(written != -1 && (size_t)written == len) {
380 error("Failed to write data to database backend '%s'. Willing to write %zu bytes, wrote %zd bytes. Will re-connect.", destination, len, written);
389 error("Failed to update database backend '%s'", destination);
392 if(failures > buffer_on_failures) {
393 error("Reached %d backend failures. Flushing buffers to protect this host - this results in data loss on back-end server '%s'", failures, destination);
398 if(unlikely(netdata_exit)) break;
405 info("BACKEND thread exiting");