3 #define BACKEND_SOURCE_DATA_AS_COLLECTED 0x00000001
4 #define BACKEND_SOURCE_DATA_AVERAGE 0x00000002
5 #define BACKEND_SOURCE_DATA_SUM 0x00000004
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, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
218 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);
221 static inline void 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) {
223 calculated_number value = backend_duration_average(st, rd, after, before, options);
225 buffer_sprintf(b, "%s.%s.%s.%s " CALCULATED_NUMBER_FORMAT " %u\n", prefix, hostname, st->id, rd->id, value, (uint32_t)before);
228 static inline void 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) {
233 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);
236 static inline void 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) {
238 calculated_number value = backend_duration_average(st, rd, after, before, options);
240 buffer_sprintf(b, "put %s.%s.%s %u " CALCULATED_NUMBER_FORMAT " host=%s\n", prefix, st->id, rd->id, (uint32_t)before, value, hostname);
243 void *backends_main(void *ptr) {
246 BUFFER *b = buffer_create(1);
247 void (*formatter)(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options);
249 info("BACKEND thread created with task id %d", gettid());
251 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
252 error("Cannot set pthread cancel type to DEFERRED.");
254 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
255 error("Cannot set pthread cancel state to ENABLE.");
257 int default_port = 0;
259 uint32_t options = BACKEND_SOURCE_DATA_AVERAGE;
260 int enabled = config_get_boolean("backend", "enable", 0);
261 const char *source = config_get("backend", "data source", "average");
262 const char *type = config_get("backend", "type", "graphite");
263 const char *destination = config_get("backend", "destination", "localhost");
264 const char *prefix = config_get("backend", "prefix", "netdata");
265 const char *hostname = config_get("backend", "hostname", localhost.hostname);
266 int frequency = (int)config_get_number("backend", "update every", 10);
267 int buffer_on_failures = (int)config_get_number("backend", "buffer on failures", 10);
269 if(!enabled || frequency < 1)
272 if(!strcmp(source, "as collected")) {
273 options = BACKEND_SOURCE_DATA_AS_COLLECTED;
275 else if(!strcmp(source, "average")) {
276 options = BACKEND_SOURCE_DATA_AVERAGE;
278 else if(!strcmp(source, "sum") || !strcmp(source, "volume")) {
279 options = BACKEND_SOURCE_DATA_SUM;
282 error("Invalid data source method '%s' for backend given. Disabling backed.", source);
286 if(!strcmp(type, "graphite") || !strcmp(type, "graphite:plaintext")) {
288 if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
289 formatter = format_dimension_collected_graphite_plaintext;
291 formatter = format_dimension_stored_graphite_plaintext;
293 else if(!strcmp(type, "opentsdb") || !strcmp(type, "opentsdb:telnet")) {
295 if(options == BACKEND_SOURCE_DATA_AS_COLLECTED)
296 formatter = format_dimension_collected_opentsdb_telnet;
298 formatter = format_dimension_stored_opentsdb_telnet;
301 error("Unknown backend type '%s'", type);
305 unsigned long long step_ut = frequency * 1000000ULL;
306 unsigned long long random_ut = time_usec() % (step_ut / 2);
307 time_t before = (time_t)((time_usec() - step_ut) / 10000000ULL);
308 time_t after = before;
311 info("BACKEND configured ('%s' on '%s' sending '%s' data, every %d seconds, as host '%s', with prefix '%s')", type, destination, source, frequency, hostname, prefix);
314 unsigned long long now_ut = time_usec();
315 unsigned long long next_ut = now_ut - (now_ut % step_ut) + step_ut;
316 before = (time_t)(next_ut / 1000000ULL);
318 // add a little delay (1/4 of the step) plus some randomness
319 next_ut += (step_ut / 4) + random_ut;
321 while(now_ut < next_ut) {
322 sleep_usec(next_ut - now_ut);
323 now_ut = time_usec();
327 int pthreadoldcancelstate;
329 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
330 error("Cannot set pthread cancel state to DISABLE.");
332 rrdhost_rdlock(&localhost);
333 for(st = localhost.rrdset_root; st ;st = st->next) {
334 pthread_rwlock_rdlock(&st->rwlock);
337 for(rd = st->dimensions; rd ;rd = rd->next) {
338 if(rd->last_collected_time.tv_sec >= after)
339 formatter(b, prefix, &localhost, hostname, st, rd, after, before, options);
342 pthread_rwlock_unlock(&st->rwlock);
344 rrdhost_unlock(&localhost);
346 if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
347 error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
349 if(unlikely(netdata_exit)) break;
351 // fprintf(stderr, "\nBACKEND BEGIN:\n%s\nBACKEND END\n", buffer_tostring(b)); // FIXME
352 //fprintf(stderr, "after = %lu, before = %lu\n", after, before);
354 if(unlikely(sock == -1)) {
355 const char *s = destination;
359 // skip separators, moving both s(tart) and e(nd)
360 while(isspace(*e) || *e == ',') s = ++e;
362 // move e(nd) to the first separator
363 while(*e && !isspace(*e) && *e != ',') e++;
365 // is there anything?
366 if(!*s || s == e) break;
369 strncpyz(buf, s, e - s);
370 sock = connect_to_one(buf, default_port);
371 if(sock != -1) break;
376 if(unlikely(netdata_exit)) break;
378 if(likely(sock != -1)) {
379 size_t len = buffer_strlen(b);
380 ssize_t written = write(sock, buffer_tostring(b), len);
381 if(written != -1 && (size_t)written == len) {
387 error("Failed to write data to database backend '%s'. Willing to write %zu bytes, wrote %zd bytes. Will re-connect.", destination, len, written);
396 error("Failed to update database backend '%s'", destination);
399 if(failures > buffer_on_failures) {
400 error("Reached %d backend failures. Flushing buffers to protect this host - this results in data loss on back-end server '%s'", failures, destination);
405 if(unlikely(netdata_exit)) break;
412 info("BACKEND thread exiting");