- pingobj_iter_t *iter;
-
- char host[512];
- double latency;
- size_t buf_len;
- int number_of_hosts;
-
- if (pingobj == NULL)
- return (-1);
-
- if (hosts != NULL)
- add_hosts ();
-
- if (ping_send (pingobj) < 0)
- {
- ERROR ("ping plugin: `ping_send' failed: %s",
- ping_get_error (pingobj));
- return (-1);
- }
-
- number_of_hosts = 0;
- for (iter = ping_iterator_get (pingobj);
- iter != NULL;
- iter = ping_iterator_next (iter))
- {
- buf_len = sizeof (host);
- if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
- host, &buf_len))
- {
- WARNING ("ping plugin: ping_iterator_get_info "
- "(PING_INFO_HOSTNAME) failed.");
- continue;
- }
-
- buf_len = sizeof (latency);
- if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
- &latency, &buf_len))
- {
- WARNING ("ping plugin: ping_iterator_get_info (%s, "
- "PING_INFO_LATENCY) failed.", host);
- continue;
- }
-
- DEBUG ("ping plugin: host = %s, latency = %f", host, latency);
- ping_submit (host, latency);
- number_of_hosts++;
- }
-
- if ((number_of_hosts == 0) && (getuid () != 0))
- {
- ERROR ("ping plugin: All hosts failed. Try starting collectd as root.");
- }
-
- return (number_of_hosts == 0 ? -1 : 0);
-} /* int ping_read */
+ if (hostlist_head == NULL)
+ {
+ NOTICE ("ping plugin: No hosts have been configured.");
+ return (-1);
+ }
+
+ if (ping_timeout > ping_interval)
+ {
+ ping_timeout = 0.9 * ping_interval;
+ WARNING ("ping plugin: Timeout is greater than interval. "
+ "Will use a timeout of %gs.", ping_timeout);
+ }
+
+ if (start_thread () != 0)
+ return (-1);
+
+ return (0);
+} /* }}} int ping_init */
+
+static int ping_config (const char *key, const char *value) /* {{{ */
+{
+ if (strcasecmp (key, "Host") == 0)
+ {
+ hostlist_t *hl;
+ char *host;
+
+ hl = (hostlist_t *) malloc (sizeof (hostlist_t));
+ if (hl == NULL)
+ {
+ char errbuf[1024];
+ ERROR ("ping plugin: malloc failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (1);
+ }
+
+ host = strdup (value);
+ if (host == NULL)
+ {
+ char errbuf[1024];
+ sfree (hl);
+ ERROR ("ping plugin: strdup failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (1);
+ }
+
+ hl->host = host;
+ hl->pkg_sent = 0;
+ hl->pkg_recv = 0;
+ hl->latency_total = 0.0;
+ hl->latency_squared = 0.0;
+ hl->next = hostlist_head;
+ hostlist_head = hl;
+ }
+ else if (strcasecmp (key, "TTL") == 0)
+ {
+ int ttl = atoi (value);
+ if ((ttl > 0) && (ttl <= 255))
+ ping_ttl = ttl;
+ else
+ WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
+ }
+ else if (strcasecmp (key, "Interval") == 0)
+ {
+ double tmp;
+
+ tmp = atof (value);
+ if (tmp > 0.0)
+ ping_interval = tmp;
+ else
+ WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
+ tmp, value);
+ }
+ else if (strcasecmp (key, "Timeout") == 0)
+ {
+ double tmp;
+
+ tmp = atof (value);
+ if (tmp > 0.0)
+ ping_timeout = tmp;
+ else
+ WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
+ tmp, value);
+ }
+ else
+ {
+ return (-1);
+ }
+
+ return (0);
+} /* }}} int ping_config */
+
+static void submit (const char *host, const char *type, /* {{{ */
+ gauge_t value)
+{
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
+
+ values[0].gauge = value;
+
+ vl.values = values;
+ vl.values_len = 1;
+ sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+ sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+ sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
+ sstrncpy (vl.type, type, sizeof (vl.type));
+
+ plugin_dispatch_values (&vl);
+} /* }}} void ping_submit */
+
+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)
+ {
+ hl->pkg_sent = 0;
+ hl->pkg_recv = 0;
+ hl->latency_total = 0.0;
+ hl->latency_squared = 0.0;
+ }
+
+ start_thread ();
+
+ return (-1);
+ } /* if (ping_thread_error != 0) */
+
+ for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
+ {
+ uint32_t pkg_sent;
+ uint32_t pkg_recv;
+ double latency_total;
+ double latency_squared;
+
+ double latency_average;
+ double latency_stddev;
+
+ double droprate;
+
+ /* Locking here works, because the structure of the linked list is only
+ * changed during configure and shutdown. */
+ pthread_mutex_lock (&ping_lock);
+
+ pkg_sent = hl->pkg_sent;
+ pkg_recv = hl->pkg_recv;
+ latency_total = hl->latency_total;
+ latency_squared = hl->latency_squared;
+
+ hl->pkg_sent = 0;
+ hl->pkg_recv = 0;
+ hl->latency_total = 0.0;
+ hl->latency_squared = 0.0;
+
+ pthread_mutex_unlock (&ping_lock);
+
+ /* This e. g. happens when starting up. */
+ if (pkg_sent == 0)
+ {
+ DEBUG ("ping plugin: No packages for host %s have been sent.",
+ hl->host);
+ continue;
+ }
+
+ /* Calculate average. Beware of division by zero. */
+ if (pkg_recv == 0)
+ latency_average = NAN;
+ else
+ latency_average = latency_total / ((double) pkg_recv);
+
+ /* Calculate standard deviation. Beware even more of division by zero. */
+ if (pkg_recv == 0)
+ latency_stddev = NAN;
+ else if (pkg_recv == 1)
+ latency_stddev = 0.0;
+ else
+ latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
+ - (latency_total * latency_total))
+ / ((double) (pkg_recv * (pkg_recv - 1))));
+
+ /* Calculate drop rate. */
+ droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
+
+ submit (hl->host, "ping", latency_average);
+ submit (hl->host, "ping_stddev", latency_stddev);
+ submit (hl->host, "ping_droprate", droprate);
+ } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
+
+ return (0);
+} /* }}} int ping_read */
+
+static int ping_shutdown (void) /* {{{ */
+{
+ hostlist_t *hl;
+
+ INFO ("ping plugin: Shutting down thread.");
+ if (stop_thread () < 0)
+ return (-1);
+
+ hl = hostlist_head;
+ while (hl != NULL)
+ {
+ hostlist_t *hl_next;
+
+ hl_next = hl->next;
+
+ sfree (hl->host);
+ sfree (hl);
+
+ hl = hl_next;
+ }
+
+ return (0);
+} /* }}} int ping_shutdown */