X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fstatsd.c;h=9050596f9d3f1894830d479a4d8d09bed27c4c22;hp=5142613739e9c77f2542d64b352752af9ad7eb3c;hb=a811574a6acbf87f23948411876a231fecaeb491;hpb=4380983e0a45e2d1c1207dbea95863d1dcc844c6 diff --git a/src/statsd.c b/src/statsd.c index 51426137..9050596f 100644 --- a/src/statsd.c +++ b/src/statsd.c @@ -26,10 +26,10 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" -#include "utils_avltree.h" -#include "utils_latency.h" +#include "utils/avltree/avltree.h" +#include "utils/common/common.h" +#include "utils/latency/latency.h" #include #include @@ -61,29 +61,29 @@ struct statsd_metric_s { }; typedef struct statsd_metric_s statsd_metric_t; -static c_avl_tree_t *metrics_tree = NULL; +static c_avl_tree_t *metrics_tree; static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_t network_thread; -static _Bool network_thread_running = 0; -static _Bool network_thread_shutdown = 0; +static bool network_thread_running; +static bool network_thread_shutdown; -static char *conf_node = NULL; -static char *conf_service = NULL; +static char *conf_node; +static char *conf_service; -static _Bool conf_delete_counters = 0; -static _Bool conf_delete_timers = 0; -static _Bool conf_delete_gauges = 0; -static _Bool conf_delete_sets = 0; +static bool conf_delete_counters; +static bool conf_delete_timers; +static bool conf_delete_gauges; +static bool conf_delete_sets; -static double *conf_timer_percentile = NULL; -static size_t conf_timer_percentile_num = 0; +static double *conf_timer_percentile; +static size_t conf_timer_percentile_num; -static _Bool conf_counter_sum = 0; -static _Bool conf_timer_lower = 0; -static _Bool conf_timer_upper = 0; -static _Bool conf_timer_sum = 0; -static _Bool conf_timer_count = 0; +static bool conf_counter_sum; +static bool conf_timer_lower; +static bool conf_timer_upper; +static bool conf_timer_sum; +static bool conf_timer_count; /* Must hold metrics_lock when calling this function. */ static statsd_metric_t *statsd_metric_lookup_unsafe(char const *name, /* {{{ */ @@ -352,9 +352,8 @@ static int statsd_handle_set(char const *name, /* {{{ */ status = c_avl_insert(metric->set, set_key, /* value = */ NULL); if (status < 0) { pthread_mutex_unlock(&metrics_lock); - if (status < 0) - ERROR("statsd plugin: c_avl_insert (\"%s\") failed with status %i.", - set_key, status); + ERROR("statsd plugin: c_avl_insert (\"%s\") failed with status %i.", + set_key, status); sfree(set_key); return -1; } else if (status > 0) /* key already exists */ @@ -446,13 +445,11 @@ static void statsd_network_read(int fd) /* {{{ */ status = recv(fd, buffer, sizeof(buffer), /* flags = */ MSG_DONTWAIT); if (status < 0) { - char errbuf[1024]; if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) return; - ERROR("statsd plugin: recv(2) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("statsd plugin: recv(2) failed: %s", STRERRNO); return; } @@ -492,28 +489,33 @@ static int statsd_network_init(struct pollfd **ret_fds, /* {{{ */ int fd; struct pollfd *tmp; - char dbg_node[NI_MAXHOST]; - char dbg_service[NI_MAXSERV]; + char str_node[NI_MAXHOST]; + char str_service[NI_MAXSERV]; fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); if (fd < 0) { - char errbuf[1024]; - ERROR("statsd plugin: socket(2) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("statsd plugin: socket(2) failed: %s", STRERRNO); continue; } - getnameinfo(ai_ptr->ai_addr, ai_ptr->ai_addrlen, dbg_node, sizeof(dbg_node), - dbg_service, sizeof(dbg_service), + /* allow multiple sockets to use the same PORT number */ + int yes = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) { + ERROR("statsd plugin: setsockopt (reuseaddr): %s", STRERRNO); + close(fd); + continue; + } + + getnameinfo(ai_ptr->ai_addr, ai_ptr->ai_addrlen, str_node, sizeof(str_node), + str_service, sizeof(str_service), NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV); - DEBUG("statsd plugin: Trying to bind to [%s]:%s ...", dbg_node, - dbg_service); + DEBUG("statsd plugin: Trying to bind to [%s]:%s ...", str_node, + str_service); status = bind(fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen); if (status != 0) { - char errbuf[1024]; - ERROR("statsd plugin: bind(2) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("statsd plugin: bind(2) to [%s]:%s failed: %s", str_node, + str_service, STRERRNO); close(fd); continue; } @@ -531,6 +533,7 @@ static int statsd_network_init(struct pollfd **ret_fds, /* {{{ */ memset(tmp, 0, sizeof(*tmp)); tmp->fd = fd; tmp->events = POLLIN | POLLPRI; + INFO("statsd plugin: Listening on [%s]:%s.", str_node, str_service); } freeaddrinfo(ai_list); @@ -561,13 +564,11 @@ static void *statsd_network_thread(void *args) /* {{{ */ while (!network_thread_shutdown) { status = poll(fds, (nfds_t)fds_num, /* timeout = */ -1); if (status < 0) { - char errbuf[1024]; if ((errno == EINTR) || (errno == EAGAIN)) continue; - ERROR("statsd plugin: poll(2) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("statsd plugin: poll(2) failed: %s", STRERRNO); break; } @@ -605,9 +606,8 @@ static int statsd_config_timer_percentile(oconfig_item_t *ci) /* {{{ */ return ERANGE; } - tmp = - realloc(conf_timer_percentile, - sizeof(*conf_timer_percentile) * (conf_timer_percentile_num + 1)); + tmp = realloc(conf_timer_percentile, sizeof(*conf_timer_percentile) * + (conf_timer_percentile_num + 1)); if (tmp == NULL) { ERROR("statsd plugin: realloc failed."); return ENOMEM; @@ -669,14 +669,12 @@ static int statsd_init(void) /* {{{ */ /* attr = */ NULL, statsd_network_thread, /* args = */ NULL); if (status != 0) { - char errbuf[1024]; pthread_mutex_unlock(&metrics_lock); - ERROR("statsd plugin: pthread_create failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + ERROR("statsd plugin: pthread_create failed: %s", STRERRNO); return status; } } - network_thread_running = 1; + network_thread_running = true; pthread_mutex_unlock(&metrics_lock); @@ -727,7 +725,7 @@ static int statsd_metric_submit_unsafe(char const *name, if (metric->type == STATSD_GAUGE) vl.values[0].gauge = (gauge_t)metric->value; else if (metric->type == STATSD_TIMER) { - _Bool have_events = (metric->updates_num > 0); + bool have_events = (metric->updates_num > 0); /* Make sure all timer metrics share the *same* timestamp. */ vl.time = cdtime(); @@ -886,11 +884,11 @@ static int statsd_shutdown(void) /* {{{ */ void *value; if (network_thread_running) { - network_thread_shutdown = 1; + network_thread_shutdown = true; pthread_kill(network_thread, SIGTERM); pthread_join(network_thread, /* retval = */ NULL); } - network_thread_running = 0; + network_thread_running = false; pthread_mutex_lock(&metrics_lock);