X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fping.c;h=e1540c37d94ca9321a4088c841f9b5c1673e56c7;hb=4212d170f8eb1a72608fc8f5f5c34837badc64ba;hp=de9c45bb38d822f4221065343bc1c7e494e37028;hpb=a2bf02b4f5bf4a22395b57bba6715dada3859612;p=collectd.git diff --git a/src/ping.c b/src/ping.c index de9c45bb..e1540c37 100644 --- a/src/ping.c +++ b/src/ping.c @@ -36,6 +36,10 @@ # define NI_MAXHOST 1025 #endif +#if defined(OPING_VERSION) && (OPING_VERSION >= 1003000) +# define HAVE_OPING_1_3 +#endif + /* * Private data types */ @@ -58,6 +62,10 @@ 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; @@ -71,6 +79,10 @@ 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" @@ -137,7 +149,7 @@ static void *ping_thread (void *arg) /* {{{ */ struct timespec ts_int; hostlist_t *hl; - int status; + int count; pthread_mutex_lock (&ping_lock); @@ -150,22 +162,34 @@ 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) + int status; + status = ping_host_add (pingobj, hl->host); + if (status != 0) WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host); 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; @@ -363,6 +387,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) @@ -397,6 +441,20 @@ static int ping_config (const char *key, const char *value) /* {{{ */ 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);