X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_tsdb.c;h=d1d65caa3d1906afa8825c588750007df22e51f5;hb=259ebe3df647b554f52ba9d49cd018b9698297bd;hp=0c87c473d7aaedbff3106e253683d4b7668b581e;hpb=6c17c8ccba745714c4d7cc10dc3ca57f2d0d293f;p=collectd.git diff --git a/src/write_tsdb.c b/src/write_tsdb.c index 0c87c473..d1d65caa 100644 --- a/src/write_tsdb.c +++ b/src/write_tsdb.c @@ -71,6 +71,8 @@ * Private variables */ struct wt_callback { + struct addrinfo *sock_info; + cdtime_t sock_info_last_update; int sock_fd; char *node; @@ -88,6 +90,8 @@ struct wt_callback { pthread_mutex_t send_lock; }; +static cdtime_t dnsttl = TIME_T_TO_CDTIME_T_STATIC(60); + /* * Functions */ @@ -145,8 +149,8 @@ static int wt_flush_nolock(cdtime_t timeout, struct wt_callback *cb) { } 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,19 +158,45 @@ 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->sock_info_last_update + dnsttl) < now) { + if (cb->sock_info) { + freeaddrinfo(cb->sock_info); + cb->sock_info = 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 (NULL == cb->sock_info) { + struct addrinfo ai_hints = { + .ai_family = AF_UNSPEC, + .ai_flags = AI_ADDRCONFIG, + .ai_socktype = SOCK_STREAM, + }; + + if ((cb->sock_info_last_update + dnsttl) >= now) { + DEBUG("write_tsdb plugin: too many getaddrinfo (%s, %s) failures", node, + service); + return (-1); + } + + cb->sock_info_last_update = now; + status = getaddrinfo(node, service, &ai_hints, &(cb->sock_info)); + if (status != 0) { + if (cb->sock_info) { + freeaddrinfo(cb->sock_info); + cb->sock_info = 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; + assert(cb->sock_info != NULL); + for (struct addrinfo *ai_ptr = cb->sock_info; 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); @@ -185,8 +215,6 @@ 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. " @@ -522,10 +550,6 @@ 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; pthread_mutex_init(&cb->send_lock, NULL); @@ -569,7 +593,11 @@ static int wt_config(oconfig_item_t *ci) { if (strcasecmp("Node", child->key) == 0) wt_config_tsd(child); - else { + if (strcasecmp("DNS_Cache_TTL", child->key) == 0) { + int ttl; + cf_util_get_int(child, &ttl); + dnsttl = TIME_T_TO_CDTIME_T(ttl); + } else { ERROR("write_tsdb plugin: Invalid configuration " "option: %s.", child->key);