Code

collectd.conf(5): Document the per-plugin interval configuration.
[collectd.git] / src / ping.c
index e1540c37d94ca9321a4088c841f9b5c1673e56c7..7adbfe13e4d4288dcf5ebb11b55c34e9889970eb 100644 (file)
@@ -49,6 +49,7 @@ struct hostlist_s
 
   uint32_t pkg_sent;
   uint32_t pkg_recv;
+  uint32_t pkg_missed;
 
   double latency_total;
   double latency_squared;
@@ -69,6 +70,7 @@ static char  *ping_device = NULL;
 static int    ping_ttl = PING_DEF_TTL;
 static double ping_interval = 1.0;
 static double ping_timeout = 0.9;
+static int    ping_max_missed = -1;
 
 static int             ping_thread_loop = 0;
 static int             ping_thread_error = 0;
@@ -85,7 +87,8 @@ static const char *config_keys[] =
 #endif
   "TTL",
   "Interval",
-  "Timeout"
+  "Timeout",
+  "MaxMissed"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
@@ -181,10 +184,11 @@ static void *ping_thread (void *arg) /* {{{ */
   count = 0;
   for (hl = hostlist_head; hl != NULL; hl = hl->next)
   {
-    int status;
-    status = ping_host_add (pingobj, hl->host);
-    if (status != 0)
-      WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host);
+    int tmp_status;
+    tmp_status = ping_host_add (pingobj, hl->host);
+    if (tmp_status != 0)
+      WARNING ("ping plugin: ping_host_add (%s) failed: %s",
+          hl->host, ping_get_error (pingobj));
     else
       count++;
   }
@@ -214,7 +218,9 @@ static void *ping_thread (void *arg) /* {{{ */
 
     if (gettimeofday (&tv_begin, NULL) < 0)
     {
-      ERROR ("ping plugin: gettimeofday failed");
+      char errbuf[1024];
+      ERROR ("ping plugin: gettimeofday failed: %s",
+          sstrerror (errno, errbuf, sizeof (errbuf)));
       ping_thread_error = 1;
       break;
     }
@@ -284,12 +290,44 @@ static void *ping_thread (void *arg) /* {{{ */
         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 (gettimeofday (&tv_end, NULL) < 0)
     {
-      ERROR ("ping plugin: gettimeofday failed");
+      char errbuf[1024];
+      ERROR ("ping plugin: gettimeofday failed: %s",
+          sstrerror (errno, errbuf, sizeof (errbuf)));
       ping_thread_error = 1;
       break;
     }
@@ -436,6 +474,7 @@ static int ping_config (const char *key, const char *value) /* {{{ */
     hl->host = host;
     hl->pkg_sent = 0;
     hl->pkg_recv = 0;
+    hl->pkg_missed = 0;
     hl->latency_total = 0.0;
     hl->latency_squared = 0.0;
     hl->next = hostlist_head;
@@ -485,6 +524,12 @@ static int ping_config (const char *key, const char *value) /* {{{ */
       WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
           tmp, value);
   }
+  else if (strcasecmp (key, "MaxMissed") == 0)
+  {
+    ping_max_missed = atoi (value);
+    if (ping_max_missed < 0)
+      INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
+  }
   else
   {
     return (-1);
@@ -497,7 +542,7 @@ static void submit (const char *host, const char *type, /* {{{ */
     gauge_t value)
 {
   value_t values[1];
-  value_list_t vl = VALUE_LIST_INIT;
+  value_list_t vl = VALUE_LIST_INIT (plugin_interval);
 
   values[0].gauge = value;
 
@@ -622,12 +667,14 @@ static int ping_shutdown (void) /* {{{ */
   return (0);
 } /* }}} int ping_shutdown */
 
-void module_register (void)
+void module_register (plugin_loaddata_t *data)
 {
+  PLUGIN_INIT_INTERVAL (data);
+
   plugin_register_config ("ping", ping_config,
       config_keys, config_keys_num);
   plugin_register_init ("ping", ping_init);
-  plugin_register_read ("ping", ping_read);
+  plugin_register_read ("ping", ping_read, plugin_interval);
   plugin_register_shutdown ("ping", ping_shutdown);
 } /* void module_register */