X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fping.c;h=5f66aab341f552cadb2d786bde09784d2272b2be;hb=db961f476426f5dd3ca1663ffc094f0fc7f6f8a2;hp=fca730f5e7698bb56e169b8559c036f81e424c3e;hpb=67a330cb7ba890a03142a089ed1aebea8842072f;p=collectd.git diff --git a/src/ping.c b/src/ping.c index fca730f5..5f66aab3 100644 --- a/src/ping.c +++ b/src/ping.c @@ -2,35 +2,43 @@ * collectd - src/ping.c * 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 - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: * Florian octo Forster **/ #include "collectd.h" + #include "common.h" #include "plugin.h" -#include "configfile.h" #include "utils_complain.h" -#include #include #if HAVE_NETDB_H # include /* NI_MAXHOST */ #endif +#ifdef HAVE_SYS_CAPABILITY_H +# include +#endif + #include #ifndef NI_MAXHOST @@ -68,6 +76,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; @@ -86,6 +95,7 @@ static const char *config_keys[] = #ifdef HAVE_OPING_1_3 "Device", #endif + "Size", "TTL", "Interval", "Timeout", @@ -145,11 +155,10 @@ static void time_calc (struct timespec *ts_dest, /* {{{ */ static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */ { - pingobj_iter_t *iter; hostlist_t *hl; int status; - for (iter = ping_iterator_get (pingobj); + for (pingobj_iter_t *iter = ping_iterator_get (pingobj); iter != NULL; iter = ping_iterator_next (iter)) { /* {{{ */ @@ -205,7 +214,8 @@ static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */ 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) + if ((ping_max_missed >= 0) + && (hl->pkg_missed >= ((uint32_t) 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 @@ -243,7 +253,6 @@ static void *ping_thread (void *arg) /* {{{ */ struct timespec ts_wait; struct timespec ts_int; - hostlist_t *hl; int count; c_complain_t complaint = C_COMPLAIN_INIT_STATIC; @@ -274,9 +283,12 @@ 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) + for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) { int tmp_status; tmp_status = ping_host_add (pingobj, hl->host); @@ -439,6 +451,20 @@ static int ping_init (void) /* {{{ */ "Will use a timeout of %gs.", ping_timeout); } +#if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW) + if (check_capability (CAP_NET_RAW) != 0) + { + if (getuid () == 0) + WARNING ("ping plugin: Running collectd as root, but the CAP_NET_RAW " + "capability is missing. The plugin's read function will probably " + "fail. Is your init system dropping capabilities?"); + else + WARNING ("ping plugin: collectd doesn't have the CAP_NET_RAW capability. " + "If you don't want to run collectd as root, try running \"setcap " + "cap_net_raw=ep\" on the collectd binary."); + } +#endif + return (start_thread ()); } /* }}} int ping_init */ @@ -469,7 +495,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]; @@ -530,6 +556,37 @@ 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) + { + 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 (size_t 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; @@ -576,15 +633,13 @@ static void submit (const char *host, const char *type, /* {{{ */ static int ping_read (void) /* {{{ */ { - hostlist_t *hl; - if (ping_thread_error != 0) { ERROR ("ping plugin: The ping thread had a problem. Restarting it."); stop_thread (); - for (hl = hostlist_head; hl != NULL; hl = hl->next) + for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) { hl->pkg_sent = 0; hl->pkg_recv = 0; @@ -597,7 +652,7 @@ static int ping_read (void) /* {{{ */ return (-1); } /* if (ping_thread_error != 0) */ - for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */ + for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */ { uint32_t pkg_sent; uint32_t pkg_recv; @@ -681,6 +736,11 @@ static int ping_shutdown (void) /* {{{ */ hl = hl_next; } + if (ping_data != NULL) { + free (ping_data); + ping_data = NULL; + } + return (0); } /* }}} int ping_shutdown */