X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fwrite_tsdb.c;h=f8f4cb9104efb1753a875402510ecabc0eb1adc3;hp=9cb3c97de611323b3c731ba7f5bef3883cd2c866;hb=54619dc85fd308b21ed09a0271e5c7383c7921b9;hpb=35a6c9c5fcd87cb78451a974c4d5b5707926845c diff --git a/src/write_tsdb.c b/src/write_tsdb.c index 9cb3c97d..f8f4cb91 100644 --- a/src/write_tsdb.c +++ b/src/write_tsdb.c @@ -43,10 +43,10 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" - +#include "utils/common/common.h" #include "utils_cache.h" +#include "utils_random.h" #include @@ -71,14 +71,16 @@ * Private variables */ struct wt_callback { + struct addrinfo *ai; + cdtime_t ai_last_update; int sock_fd; char *node; char *service; char *host_tags; - _Bool store_rates; - _Bool always_append_ds; + bool store_rates; + bool always_append_ds; char send_buf[WT_SEND_BUF_SIZE]; size_t send_buf_free; @@ -86,8 +88,15 @@ struct wt_callback { cdtime_t send_buf_init_time; pthread_mutex_t send_lock; + + bool connect_failed_log_enabled; + int connect_dns_failed_attempts_remaining; + cdtime_t next_random_ttl; }; +static cdtime_t resolve_interval; +static cdtime_t resolve_jitter; + /* * Functions */ @@ -102,10 +111,9 @@ static int wt_send_buffer(struct wt_callback *cb) { ssize_t status = 0; status = swrite(cb->sock_fd, cb->send_buf, strlen(cb->send_buf)); - if (status < 0) { - char errbuf[1024]; + if (status != 0) { ERROR("write_tsdb plugin: send failed with status %zi (%s)", status, - sstrerror(errno, errbuf, sizeof(errbuf))); + STRERRNO); close(cb->sock_fd); cb->sock_fd = -1; @@ -121,7 +129,7 @@ static int wt_flush_nolock(cdtime_t timeout, struct wt_callback *cb) { int status; DEBUG("write_tsdb plugin: wt_flush_nolock: timeout = %.3f; " - "send_buf_fill = %zu;", + "send_buf_fill = %" PRIsz ";", (double)timeout, cb->send_buf_fill); /* timeout == 0 => flush unconditionally */ @@ -144,9 +152,16 @@ static int wt_flush_nolock(cdtime_t timeout, struct wt_callback *cb) { return status; } +static cdtime_t new_random_ttl(void) { + if (resolve_jitter == 0) + return 0; + + return (cdtime_t)cdrand_range(0, (long)resolve_jitter); +} + static int wt_callback_init(struct wt_callback *cb) { - struct addrinfo *ai_list; int status; + cdtime_t now; const char *node = cb->node ? cb->node : WT_DEFAULT_NODE; const char *service = cb->service ? cb->service : WT_DEFAULT_SERVICE; @@ -154,28 +169,68 @@ static int wt_callback_init(struct wt_callback *cb) { if (cb->sock_fd > 0) return 0; - struct addrinfo ai_hints = {.ai_family = AF_UNSPEC, - .ai_flags = AI_ADDRCONFIG, - .ai_socktype = SOCK_STREAM}; + now = cdtime(); + if (cb->ai) { + /* When we are here, we still have the IP in cache. + * If we have remaining attempts without calling the DNS, we update the + * last_update date so we keep the info until next time. + * If there is no more attempts, we need to flush the cache. + */ + + if ((cb->ai_last_update + resolve_interval + cb->next_random_ttl) < now) { + cb->next_random_ttl = new_random_ttl(); + if (cb->connect_dns_failed_attempts_remaining > 0) { + /* Warning : this is run under send_lock mutex. + * This is why we do not use another mutex here. + * */ + cb->ai_last_update = now; + cb->connect_dns_failed_attempts_remaining--; + } else { + freeaddrinfo(cb->ai); + cb->ai = NULL; + } + } + } - status = getaddrinfo(node, service, &ai_hints, &ai_list); - if (status != 0) { - ERROR("write_tsdb plugin: getaddrinfo (%s, %s) failed: %s", node, service, - gai_strerror(status)); - return -1; + if (cb->ai == NULL) { + if ((cb->ai_last_update + resolve_interval + cb->next_random_ttl) >= now) { + DEBUG("write_tsdb plugin: too many getaddrinfo(%s, %s) failures", node, + service); + return -1; + } + cb->ai_last_update = now; + cb->next_random_ttl = new_random_ttl(); + + struct addrinfo ai_hints = { + .ai_family = AF_UNSPEC, + .ai_flags = AI_ADDRCONFIG, + .ai_socktype = SOCK_STREAM, + }; + + status = getaddrinfo(node, service, &ai_hints, &cb->ai); + if (status != 0) { + if (cb->ai) { + freeaddrinfo(cb->ai); + cb->ai = NULL; + } + if (cb->connect_failed_log_enabled) { + ERROR("write_tsdb plugin: getaddrinfo(%s, %s) failed: %s", node, + service, gai_strerror(status)); + cb->connect_failed_log_enabled = 0; + } + return -1; + } } - assert(ai_list != NULL); - for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; - ai_ptr = ai_ptr->ai_next) { - cb->sock_fd = - socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); + assert(cb->ai != NULL); + for (struct addrinfo *ai = cb->ai; ai != NULL; ai = ai->ai_next) { + cb->sock_fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (cb->sock_fd < 0) continue; set_sock_opts(cb->sock_fd); - status = connect(cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen); + status = connect(cb->sock_fd, ai->ai_addr, ai->ai_addrlen); if (status != 0) { close(cb->sock_fd); cb->sock_fd = -1; @@ -185,16 +240,19 @@ static int wt_callback_init(struct wt_callback *cb) { break; } - freeaddrinfo(ai_list); - if (cb->sock_fd < 0) { - char errbuf[1024]; ERROR("write_tsdb plugin: Connecting to %s:%s failed. " "The last error was: %s", - node, service, sstrerror(errno, errbuf, sizeof(errbuf))); + node, service, STRERRNO); return -1; } + if (0 == cb->connect_failed_log_enabled) { + WARNING("write_tsdb plugin: Connecting to %s:%s succeeded.", node, service); + cb->connect_failed_log_enabled = 1; + } + cb->connect_dns_failed_attempts_remaining = 1; + wt_reset_buffer(cb); return 0; @@ -255,7 +313,7 @@ static int wt_flush(cdtime_t timeout, static int wt_format_values(char *ret, size_t ret_len, int ds_num, const data_set_t *ds, const value_list_t *vl, - _Bool store_rates) { + bool store_rates) { size_t offset = 0; int status; gauge_t *rates = NULL; @@ -266,7 +324,7 @@ static int wt_format_values(char *ret, size_t ret_len, int ds_num, #define BUFFER_ADD(...) \ do { \ - status = ssnprintf(ret + offset, ret_len - offset, __VA_ARGS__); \ + status = snprintf(ret + offset, ret_len - offset, __VA_ARGS__); \ if (status < 1) { \ sfree(rates); \ return -1; \ @@ -289,7 +347,7 @@ static int wt_format_values(char *ret, size_t ret_len, int ds_num, } BUFFER_ADD(GAUGE_FORMAT, rates[ds_num]); } else if (ds->ds[ds_num].type == DS_TYPE_COUNTER) - BUFFER_ADD("%llu", vl->values[ds_num].counter); + BUFFER_ADD("%" PRIu64, (uint64_t)vl->values[ds_num].counter); else if (ds->ds[ds_num].type == DS_TYPE_DERIVE) BUFFER_ADD("%" PRIi64, vl->values[ds_num].derive); else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE) @@ -329,36 +387,36 @@ static int wt_format_name(char *ret, int ret_len, const value_list_t *vl, if (ds_name != NULL) { if (vl->plugin_instance[0] == '\0') { if (vl->type_instance[0] == '\0') { - ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin, vl->type, - ds_name); + snprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin, vl->type, + ds_name); } else { - ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin, vl->type, - vl->type_instance, ds_name); + snprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin, vl->type, + vl->type_instance, ds_name); } } else { /* vl->plugin_instance != "" */ if (vl->type_instance[0] == '\0') { - ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin, - vl->plugin_instance, vl->type, ds_name); + snprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin, + vl->plugin_instance, vl->type, ds_name); } else { - ssnprintf(ret, ret_len, "%s%s.%s.%s.%s.%s", prefix, vl->plugin, - vl->plugin_instance, vl->type, vl->type_instance, ds_name); + snprintf(ret, ret_len, "%s%s.%s.%s.%s.%s", prefix, vl->plugin, + vl->plugin_instance, vl->type, vl->type_instance, ds_name); } } } else { /* ds_name == NULL */ if (vl->plugin_instance[0] == '\0') { if (vl->type_instance[0] == '\0') { - ssnprintf(ret, ret_len, "%s%s.%s", prefix, vl->plugin, vl->type); + snprintf(ret, ret_len, "%s%s.%s", prefix, vl->plugin, vl->type); } else { - ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin, - vl->type_instance, vl->type); + snprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin, + vl->type_instance, vl->type); } } else { /* vl->plugin_instance != "" */ if (vl->type_instance[0] == '\0') { - ssnprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin, - vl->plugin_instance, vl->type); + snprintf(ret, ret_len, "%s%s.%s.%s", prefix, vl->plugin, + vl->plugin_instance, vl->type); } else { - ssnprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin, - vl->plugin_instance, vl->type, vl->type_instance); + snprintf(ret, ret_len, "%s%s.%s.%s.%s", prefix, vl->plugin, + vl->plugin_instance, vl->type, vl->type_instance); } } } @@ -397,8 +455,8 @@ static int wt_send_message(const char *key, const char *value, cdtime_t time, } status = - ssnprintf(message, sizeof(message), "put %s %.0f %s fqdn=%s %s %s\r\n", - key, CDTIME_T_TO_DOUBLE(time), value, host, tags, host_tags); + snprintf(message, sizeof(message), "put %s %.0f %s fqdn=%s %s %s\r\n", + key, CDTIME_T_TO_DOUBLE(time), value, host, tags, host_tags); sfree(temp); if (status < 0) return -1; @@ -406,7 +464,7 @@ static int wt_send_message(const char *key, const char *value, cdtime_t time, if (message_len >= sizeof(message)) { ERROR("write_tsdb plugin: message buffer too small: " - "Need %zu bytes.", + "Need %" PRIsz " bytes.", message_len + 1); return -1; } @@ -439,8 +497,8 @@ static int wt_send_message(const char *key, const char *value, cdtime_t time, cb->send_buf_fill += message_len; cb->send_buf_free -= message_len; - DEBUG("write_tsdb plugin: [%s]:%s buf %zu/%zu (%.1f %%) \"%s\"", cb->node, - cb->service, cb->send_buf_fill, sizeof(cb->send_buf), + DEBUG("write_tsdb plugin: [%s]:%s buf %" PRIsz "/%" PRIsz " (%.1f %%) \"%s\"", + cb->node, cb->service, cb->send_buf_fill, sizeof(cb->send_buf), 100.0 * ((double)cb->send_buf_fill) / ((double)sizeof(cb->send_buf)), message); @@ -523,10 +581,8 @@ static int wt_config_tsd(oconfig_item_t *ci) { return -1; } cb->sock_fd = -1; - cb->node = NULL; - cb->service = NULL; - cb->host_tags = NULL; - cb->store_rates = 0; + cb->connect_failed_log_enabled = 1; + cb->next_random_ttl = new_random_ttl(); pthread_mutex_init(&cb->send_lock, NULL); @@ -550,9 +606,9 @@ static int wt_config_tsd(oconfig_item_t *ci) { } } - ssnprintf(callback_name, sizeof(callback_name), "write_tsdb/%s/%s", - cb->node != NULL ? cb->node : WT_DEFAULT_NODE, - cb->service != NULL ? cb->service : WT_DEFAULT_SERVICE); + snprintf(callback_name, sizeof(callback_name), "write_tsdb/%s/%s", + cb->node != NULL ? cb->node : WT_DEFAULT_NODE, + cb->service != NULL ? cb->service : WT_DEFAULT_SERVICE); user_data_t user_data = {.data = cb, .free_func = wt_callback_free}; @@ -565,11 +621,18 @@ static int wt_config_tsd(oconfig_item_t *ci) { } static int wt_config(oconfig_item_t *ci) { + if ((resolve_interval == 0) && (resolve_jitter == 0)) + resolve_interval = resolve_jitter = plugin_get_interval(); + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp("Node", child->key) == 0) wt_config_tsd(child); + else if (strcasecmp("ResolveInterval", child->key) == 0) + cf_util_get_cdtime(child, &resolve_interval); + else if (strcasecmp("ResolveJitter", child->key) == 0) + cf_util_get_cdtime(child, &resolve_jitter); else { ERROR("write_tsdb plugin: Invalid configuration " "option: %s.", @@ -583,5 +646,3 @@ static int wt_config(oconfig_item_t *ci) { void module_register(void) { plugin_register_complex_config("write_tsdb", wt_config); } - -/* vim: set sw=4 ts=4 sts=4 tw=78 et : */