From 259ebe3df647b554f52ba9d49cd018b9698297bd Mon Sep 17 00:00:00 2001 From: Yves Mettier Date: Wed, 14 Oct 2015 15:32:56 +0100 Subject: [PATCH] Prevent DNS flood when TSDB is not available --- src/collectd.conf.pod | 18 ++++++++++++++- src/write_tsdb.c | 64 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 27c4e16e..4d053527 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -8143,6 +8143,7 @@ packets. Synopsis: + DNS_Cache_TTL 60 Host "tsd-1.my.domain" Port "4242" @@ -8151,7 +8152,22 @@ Synopsis: The configuration consists of one or more EBEIE -blocks. Inside the B blocks, the following options are recognized: +blocks and global directives. + +Global directives are: + +=over 4 + +=item B I + +When Collectd connects to a TSDB node, it will request the DNS. This can become +a problem is the TSDN node is unavailable or badly configured because Collected +will request DNS in order to reconnect for every metric, which can flood your DNS. +So you can cache the last value for C seconds (default: 60s). + +=back + +Inside the B blocks, the following options are recognized: =over 4 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); -- 2.11.0