X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fping.c;h=da63d3a58c7bb9707b0b0d5763f0ee579b2c4dd5;hb=73a6f82a63747f088352a61e201beac2d185b2ac;hp=df2f6da6e6bc69158ebad65e8c47615cd10bfcad;hpb=7bab45b57d6ef99ae55c499c51478dd63c3b1397;p=collectd.git diff --git a/src/ping.c b/src/ping.c index df2f6da6..e16ba077 100644 --- a/src/ping.c +++ b/src/ping.c @@ -30,7 +30,6 @@ #include "configfile.h" #include "utils_complain.h" -#include #include #if HAVE_NETDB_H # include /* NI_MAXHOST */ @@ -73,6 +72,7 @@ static char *ping_source = NULL; #ifdef HAVE_OPING_1_3 static char *ping_device = NULL; #endif +static char *ping_data = NULL; static int ping_ttl = PING_DEF_TTL; static double ping_interval = 1.0; static double ping_timeout = 0.9; @@ -91,6 +91,7 @@ static const char *config_keys[] = #ifdef HAVE_OPING_1_3 "Device", #endif + "Size", "TTL", "Interval", "Timeout", @@ -242,7 +243,7 @@ static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */ static void *ping_thread (void *arg) /* {{{ */ { - static pingobj_t *pingobj = NULL; + pingobj_t *pingobj = NULL; struct timeval tv_begin; struct timeval tv_end; @@ -280,6 +281,9 @@ static void *ping_thread (void *arg) /* {{{ */ ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout); ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl); + if (ping_data != NULL) + ping_setopt (pingobj, PING_OPT_DATA, (void *) ping_data); + /* Add all the hosts to the ping object. */ count = 0; for (hl = hostlist_head; hl != NULL; hl = hl->next) @@ -394,7 +398,7 @@ static int start_thread (void) /* {{{ */ pthread_mutex_unlock (&ping_lock); return (-1); } - + pthread_mutex_unlock (&ping_lock); return (0); } /* }}} int start_thread */ @@ -422,8 +426,10 @@ static int stop_thread (void) /* {{{ */ status = -1; } + pthread_mutex_lock (&ping_lock); memset (&ping_thread_id, 0, sizeof (ping_thread_id)); ping_thread_error = 0; + pthread_mutex_unlock (&ping_lock); return (status); } /* }}} int stop_thread */ @@ -476,7 +482,7 @@ static int ping_config (const char *key, const char *value) /* {{{ */ hostlist_t *hl; char *host; - hl = (hostlist_t *) malloc (sizeof (hostlist_t)); + hl = malloc (sizeof (*hl)); if (hl == NULL) { char errbuf[1024]; @@ -537,6 +543,39 @@ static int ping_config (const char *key, const char *value) /* {{{ */ WARNING ("ping plugin: Ignoring invalid interval %g (%s)", tmp, value); } + else if (strcasecmp (key, "Size") == 0) { + size_t size = (size_t) atoi (value); + + /* Max IP packet size - (IPv6 + ICMP) = 65535 - (40 + 8) = 65487 */ + if (size <= 65487) + { + size_t i; + + sfree (ping_data); + ping_data = malloc (size + 1); + if (ping_data == NULL) + { + ERROR ("ping plugin: malloc failed."); + return (1); + } + + /* Note: By default oping is using constant string + * "liboping -- ICMP ping library " + * which is exactly 56 bytes. + * + * Optimally we would follow the ping(1) behaviour, but we + * cannot use byte 00 or start data payload at exactly same + * location, due to oping library limitations. */ + for (i = 0; i < size; i++) /* {{{ */ + { + /* This restricts data pattern to be only composed of easily + * printable characters, and not NUL character. */ + ping_data[i] = ('0' + i % 64); + } /* }}} for (i = 0; i < size; i++) */ + ping_data[size] = 0; + } else + WARNING ("ping plugin: Ignoring invalid Size %zu.", size); + } else if (strcasecmp (key, "Timeout") == 0) { double tmp; @@ -688,6 +727,11 @@ static int ping_shutdown (void) /* {{{ */ hl = hl_next; } + if (ping_data != NULL) { + free (ping_data); + ping_data = NULL; + } + return (0); } /* }}} int ping_shutdown */