{GPL, other}: Relicense to MIT license.
[collectd.git] / src / ping.c
index b536f42..216feda 100644 (file)
@@ -1,28 +1,34 @@
 /**
  * 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
- * 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 <octo at verplant.org>
+ *   Florian octo Forster <octo at collectd.org>
  **/
 
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
 #include "configfile.h"
+#include "utils_complain.h"
 
 #include <pthread.h>
 #include <netinet/in.h>
@@ -142,6 +148,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, &param_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, &param_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;
@@ -154,6 +251,8 @@ static void *ping_thread (void *arg) /* {{{ */
   hostlist_t *hl;
   int count;
 
+  c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
+
   pthread_mutex_lock (&ping_lock);
 
   pingobj = ping_construct ();
@@ -213,8 +312,8 @@ 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)
     {
@@ -230,10 +329,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);
@@ -241,87 +343,8 @@ 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, &param_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, &param_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)
-            WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host);
-        }
-      } /* }}} ping_max_missed */
-    } /* }}} for (iter) */
+    if (send_successful)
+      (void) ping_dispatch_all (pingobj);
 
     if (gettimeofday (&tv_end, NULL) < 0)
     {
@@ -361,7 +384,7 @@ static int start_thread (void) /* {{{ */
 
   ping_thread_loop = 1;
   ping_thread_error = 0;
-  status = pthread_create (&ping_thread_id, /* attr = */ NULL,
+  status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL,
       ping_thread, /* arg = */ (void *) 0);
   if (status != 0)
   {