X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fping.c;h=ab1459e3a938544d29fab7b2c7777205b3b48f1f;hb=9e7b3a035836474cb4af253b248de30d5eb97f4d;hp=de9c45bb38d822f4221065343bc1c7e494e37028;hpb=085a993e0f187ad73d3a28cea508ec6e56c39516;p=collectd.git diff --git a/src/ping.c b/src/ping.c index de9c45bb..ab1459e3 100644 --- a/src/ping.c +++ b/src/ping.c @@ -1,6 +1,6 @@ /** * collectd - src/ping.c - * Copyright (C) 2005-2009 Florian octo Forster + * Copyright (C) 2005-2012 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -16,13 +16,14 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster **/ #include "collectd.h" #include "common.h" #include "plugin.h" #include "configfile.h" +#include "utils_complain.h" #include #include @@ -36,6 +37,10 @@ # define NI_MAXHOST 1025 #endif +#if defined(OPING_VERSION) && (OPING_VERSION >= 1003000) +# define HAVE_OPING_1_3 +#endif + /* * Private data types */ @@ -45,6 +50,7 @@ struct hostlist_s uint32_t pkg_sent; uint32_t pkg_recv; + uint32_t pkg_missed; double latency_total; double latency_squared; @@ -58,9 +64,14 @@ typedef struct hostlist_s hostlist_t; */ static hostlist_t *hostlist_head = NULL; +static char *ping_source = NULL; +#ifdef HAVE_OPING_1_3 +static char *ping_device = NULL; +#endif static int ping_ttl = PING_DEF_TTL; static double ping_interval = 1.0; static double ping_timeout = 0.9; +static int ping_max_missed = -1; static int ping_thread_loop = 0; static int ping_thread_error = 0; @@ -71,9 +82,14 @@ static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER; static const char *config_keys[] = { "Host", + "SourceAddress", +#ifdef HAVE_OPING_1_3 + "Device", +#endif "TTL", "Interval", - "Timeout" + "Timeout", + "MaxMissed" }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); @@ -127,6 +143,97 @@ static void time_calc (struct timespec *ts_dest, /* {{{ */ time_normalize (ts_dest); } /* }}} void time_calc */ +static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */ +{ + pingobj_iter_t *iter; + hostlist_t *hl; + int status; + + for (iter = ping_iterator_get (pingobj); + iter != NULL; + iter = ping_iterator_next (iter)) + { /* {{{ */ + char userhost[NI_MAXHOST]; + double latency; + size_t param_size; + + param_size = sizeof (userhost); + status = ping_iterator_get_info (iter, +#ifdef PING_INFO_USERNAME + PING_INFO_USERNAME, +#else + PING_INFO_HOSTNAME, +#endif + userhost, ¶m_size); + if (status != 0) + { + WARNING ("ping plugin: ping_iterator_get_info failed: %s", + ping_get_error (pingobj)); + continue; + } + + for (hl = hostlist_head; hl != NULL; hl = hl->next) + if (strcmp (userhost, hl->host) == 0) + break; + + if (hl == NULL) + { + WARNING ("ping plugin: Cannot find host %s.", userhost); + continue; + } + + param_size = sizeof (latency); + status = ping_iterator_get_info (iter, PING_INFO_LATENCY, + (void *) &latency, ¶m_size); + if (status != 0) + { + WARNING ("ping plugin: ping_iterator_get_info failed: %s", + ping_get_error (pingobj)); + continue; + } + + hl->pkg_sent++; + if (latency >= 0.0) + { + hl->pkg_recv++; + hl->latency_total += latency; + hl->latency_squared += (latency * latency); + + /* reset missed packages counter */ + hl->pkg_missed = 0; + } else + hl->pkg_missed++; + + /* if the host did not answer our last N packages, trigger a resolv. */ + if (ping_max_missed >= 0 && hl->pkg_missed >= ping_max_missed) + { /* {{{ */ + /* we reset the missed package counter here, since we only want to + * trigger a resolv every N packages and not every package _AFTER_ N + * missed packages */ + hl->pkg_missed = 0; + + WARNING ("ping plugin: host %s has not answered %d PING requests," + " triggering resolve", hl->host, ping_max_missed); + + /* we trigger the resolv simply be removeing and adding the host to our + * ping object */ + status = ping_host_remove (pingobj, hl->host); + if (status != 0) + { + WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host); + } + else + { + status = ping_host_add (pingobj, hl->host); + if (status != 0) + ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host); + } + } /* }}} ping_max_missed */ + } /* }}} for (iter) */ + + return (0); +} /* }}} int ping_dispatch_all */ + static void *ping_thread (void *arg) /* {{{ */ { static pingobj_t *pingobj = NULL; @@ -137,7 +244,9 @@ static void *ping_thread (void *arg) /* {{{ */ struct timespec ts_int; hostlist_t *hl; - int status; + int count; + + c_complain_t complaint = C_COMPLAIN_INIT_STATIC; pthread_mutex_lock (&ping_lock); @@ -150,22 +259,35 @@ static void *ping_thread (void *arg) /* {{{ */ return ((void *) -1); } + if (ping_source != NULL) + if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0) + ERROR ("ping plugin: Failed to set source address: %s", + ping_get_error (pingobj)); + +#ifdef HAVE_OPING_1_3 + if (ping_device != NULL) + if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0) + ERROR ("ping plugin: Failed to set device: %s", + ping_get_error (pingobj)); +#endif + ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout); ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl); /* Add all the hosts to the ping object. */ - status = 0; + count = 0; for (hl = hostlist_head; hl != NULL; hl = hl->next) { int tmp_status; tmp_status = ping_host_add (pingobj, hl->host); if (tmp_status != 0) - WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host); + WARNING ("ping plugin: ping_host_add (%s) failed: %s", + hl->host, ping_get_error (pingobj)); else - status++; + count++; } - if (status == 0) + if (count == 0) { ERROR ("ping plugin: No host could be added to ping object. Giving up."); ping_thread_error = 1; @@ -185,12 +307,14 @@ static void *ping_thread (void *arg) /* {{{ */ while (ping_thread_loop > 0) { - pingobj_iter_t *iter; int status; + _Bool send_successful = 0; if (gettimeofday (&tv_begin, NULL) < 0) { - ERROR ("ping plugin: gettimeofday failed"); + char errbuf[1024]; + ERROR ("ping plugin: gettimeofday failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); ping_thread_error = 1; break; } @@ -200,10 +324,13 @@ static void *ping_thread (void *arg) /* {{{ */ status = ping_send (pingobj); if (status < 0) { - ERROR ("ping plugin: ping_send failed: %s", ping_get_error (pingobj)); - pthread_mutex_lock (&ping_lock); - ping_thread_error = 1; - break; + c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s", + ping_get_error (pingobj)); + } + else + { + c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded."); + send_successful = 1; } pthread_mutex_lock (&ping_lock); @@ -211,61 +338,14 @@ static void *ping_thread (void *arg) /* {{{ */ if (ping_thread_loop <= 0) break; - for (iter = ping_iterator_get (pingobj); - iter != NULL; - iter = ping_iterator_next (iter)) - { /* {{{ */ - char userhost[NI_MAXHOST]; - double latency; - size_t param_size; - - param_size = sizeof (userhost); - status = ping_iterator_get_info (iter, -#ifdef PING_INFO_USERNAME - PING_INFO_USERNAME, -#else - PING_INFO_HOSTNAME, -#endif - userhost, ¶m_size); - if (status != 0) - { - WARNING ("ping plugin: ping_iterator_get_info failed: %s", - ping_get_error (pingobj)); - continue; - } - - for (hl = hostlist_head; hl != NULL; hl = hl->next) - if (strcmp (userhost, hl->host) == 0) - break; - - if (hl == NULL) - { - WARNING ("ping plugin: Cannot find host %s.", userhost); - continue; - } - - param_size = sizeof (latency); - status = ping_iterator_get_info (iter, PING_INFO_LATENCY, - (void *) &latency, ¶m_size); - if (status != 0) - { - WARNING ("ping plugin: ping_iterator_get_info failed: %s", - ping_get_error (pingobj)); - continue; - } - - hl->pkg_sent++; - if (latency >= 0.0) - { - hl->pkg_recv++; - hl->latency_total += latency; - hl->latency_squared += (latency * latency); - } - } /* }}} for (iter) */ + if (send_successful) + (void) ping_dispatch_all (pingobj); if (gettimeofday (&tv_end, NULL) < 0) { - ERROR ("ping plugin: gettimeofday failed"); + char errbuf[1024]; + ERROR ("ping plugin: gettimeofday failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); ping_thread_error = 1; break; } @@ -363,6 +443,26 @@ static int ping_init (void) /* {{{ */ return (0); } /* }}} int ping_init */ +static int config_set_string (const char *name, /* {{{ */ + char **var, const char *value) +{ + char *tmp; + + tmp = strdup (value); + if (tmp == NULL) + { + char errbuf[1024]; + ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s", + name, value, sstrerror (errno, errbuf, sizeof (errbuf))); + return (1); + } + + if (*var != NULL) + free (*var); + *var = tmp; + return (0); +} /* }}} int config_set_string */ + static int ping_config (const char *key, const char *value) /* {{{ */ { if (strcasecmp (key, "Host") == 0) @@ -392,11 +492,26 @@ static int ping_config (const char *key, const char *value) /* {{{ */ hl->host = host; hl->pkg_sent = 0; hl->pkg_recv = 0; + hl->pkg_missed = 0; hl->latency_total = 0.0; hl->latency_squared = 0.0; hl->next = hostlist_head; hostlist_head = hl; } + else if (strcasecmp (key, "SourceAddress") == 0) + { + int status = config_set_string (key, &ping_source, value); + if (status != 0) + return (status); + } +#ifdef HAVE_OPING_1_3 + else if (strcasecmp (key, "Device") == 0) + { + int status = config_set_string (key, &ping_device, value); + if (status != 0) + return (status); + } +#endif else if (strcasecmp (key, "TTL") == 0) { int ttl = atoi (value); @@ -427,6 +542,12 @@ static int ping_config (const char *key, const char *value) /* {{{ */ WARNING ("ping plugin: Ignoring invalid timeout %g (%s)", tmp, value); } + else if (strcasecmp (key, "MaxMissed") == 0) + { + ping_max_missed = atoi (value); + if (ping_max_missed < 0) + INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts"); + } else { return (-1);