From: Pavel Rochnyak Date: Tue, 1 May 2018 12:13:51 +0000 (+0700) Subject: Merge pull request #2691 from lilydjwg/master X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=c3c9a56e37bac5d5be3b6beb0b33f89ea941295e;hp=e163fd022657f72711d507390fd28bdf76d8dbc1;p=collectd.git Merge pull request #2691 from lilydjwg/master ping plugin: support specifying the address family --- diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 6ec61f32..14e8e983 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1126,6 +1126,7 @@ # Timeout 0.9 # TTL 255 # SourceAddress "1.2.3.4" +# AddressFamily "any" # Device "eth0" # MaxMissed -1 # diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index e9715126..e45ae3d8 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -6185,6 +6185,11 @@ long string is used so that the packet size of an ICMPv4 packet is exactly Sets the source address to use. I may either be a numerical network address or a network hostname. +=item B I + +Sets the address family to use. I may be "any", "ipv4" or "ipv6". This +option will be ignored if you set a B. + =item B I Sets the outgoing network device to be used. I has to specify an diff --git a/src/ping.c b/src/ping.c index 1ffd72b7..b619e37a 100644 --- a/src/ping.c +++ b/src/ping.c @@ -71,6 +71,7 @@ typedef struct hostlist_s hostlist_t; */ static hostlist_t *hostlist_head = NULL; +static int ping_af = PING_DEF_AF; static char *ping_source = NULL; #ifdef HAVE_OPING_1_3 static char *ping_device = NULL; @@ -87,7 +88,7 @@ static int ping_thread_loop = 0; static int ping_thread_error = 0; static pthread_t ping_thread_id; -static const char *config_keys[] = {"Host", "SourceAddress", +static const char *config_keys[] = {"Host", "SourceAddress", "AddressFamily", #ifdef HAVE_OPING_1_3 "Device", #endif @@ -242,6 +243,12 @@ static void *ping_thread(void *arg) /* {{{ */ return (void *)-1; } + if (ping_af != PING_DEF_AF) { + if (ping_setopt(pingobj, PING_OPT_AF, &ping_af) != 0) + ERROR("ping plugin: Failed to set address family: %s", + ping_get_error(pingobj)); + } + if (ping_source != NULL) if (ping_setopt(pingobj, PING_OPT_SOURCE, (void *)ping_source) != 0) ERROR("ping plugin: Failed to set source address: %s", @@ -468,6 +475,23 @@ static int ping_config(const char *key, const char *value) /* {{{ */ hl->latency_squared = 0.0; hl->next = hostlist_head; hostlist_head = hl; + } else if (strcasecmp(key, "AddressFamily") == 0) { + char *af = NULL; + int status = config_set_string(key, &af, value); + if (status != 0) + return status; + + if (strncmp(af, "any", 3) == 0) { + ping_af = AF_UNSPEC; + } else if (strncmp(af, "ipv4", 4) == 0) { + ping_af = AF_INET; + } else if (strncmp(af, "ipv6", 4) == 0) { + ping_af = AF_INET6; + } else { + WARNING("ping plugin: Ignoring invalid AddressFamily value %s", af); + } + free(af); + } else if (strcasecmp(key, "SourceAddress") == 0) { int status = config_set_string(key, &ping_source, value); if (status != 0)