2 * collectd - src/ping.c
3 * Copyright (C) 2005-2012 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
25 #include "configfile.h"
26 #include "utils_complain.h"
29 #include <netinet/in.h>
31 # include <netdb.h> /* NI_MAXHOST */
37 # define NI_MAXHOST 1025
40 #if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
41 # define HAVE_OPING_1_3
56 double latency_squared;
58 struct hostlist_s *next;
60 typedef struct hostlist_s hostlist_t;
65 static hostlist_t *hostlist_head = NULL;
67 static char *ping_source = NULL;
69 static char *ping_device = NULL;
71 static int ping_ttl = PING_DEF_TTL;
72 static double ping_interval = 1.0;
73 static double ping_timeout = 0.9;
74 static int ping_max_missed = -1;
76 static int ping_thread_loop = 0;
77 static int ping_thread_error = 0;
78 static pthread_t ping_thread_id;
79 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
80 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
82 static const char *config_keys[] =
94 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
99 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
100 static void time_normalize (struct timespec *ts) /* {{{ */
102 while (ts->tv_nsec < 0)
111 ts->tv_nsec += 1000000000;
114 while (ts->tv_nsec >= 1000000000)
117 ts->tv_nsec -= 1000000000;
119 } /* }}} void time_normalize */
121 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
122 * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
123 static void time_calc (struct timespec *ts_dest, /* {{{ */
124 const struct timespec *ts_int,
125 const struct timeval *tv_begin,
126 const struct timeval *tv_end)
128 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
129 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
130 time_normalize (ts_dest);
132 /* Assure that `(begin + interval) > end'.
133 * This may seem overly complicated, but `tv_sec' is of type `time_t'
134 * which may be `unsigned. *sigh* */
135 if ((tv_end->tv_sec > ts_dest->tv_sec)
136 || ((tv_end->tv_sec == ts_dest->tv_sec)
137 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
139 ts_dest->tv_sec = tv_end->tv_sec;
140 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
143 time_normalize (ts_dest);
144 } /* }}} void time_calc */
146 static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
148 pingobj_iter_t *iter;
152 for (iter = ping_iterator_get (pingobj);
154 iter = ping_iterator_next (iter))
156 char userhost[NI_MAXHOST];
160 param_size = sizeof (userhost);
161 status = ping_iterator_get_info (iter,
162 #ifdef PING_INFO_USERNAME
167 userhost, ¶m_size);
170 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
171 ping_get_error (pingobj));
175 for (hl = hostlist_head; hl != NULL; hl = hl->next)
176 if (strcmp (userhost, hl->host) == 0)
181 WARNING ("ping plugin: Cannot find host %s.", userhost);
185 param_size = sizeof (latency);
186 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
187 (void *) &latency, ¶m_size);
190 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
191 ping_get_error (pingobj));
199 hl->latency_total += latency;
200 hl->latency_squared += (latency * latency);
202 /* reset missed packages counter */
207 /* if the host did not answer our last N packages, trigger a resolv. */
208 if (ping_max_missed >= 0 && hl->pkg_missed >= ping_max_missed)
210 /* we reset the missed package counter here, since we only want to
211 * trigger a resolv every N packages and not every package _AFTER_ N
215 WARNING ("ping plugin: host %s has not answered %d PING requests,"
216 " triggering resolve", hl->host, ping_max_missed);
218 /* we trigger the resolv simply be removeing and adding the host to our
220 status = ping_host_remove (pingobj, hl->host);
223 WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
227 status = ping_host_add (pingobj, hl->host);
229 ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
231 } /* }}} ping_max_missed */
232 } /* }}} for (iter) */
235 } /* }}} int ping_dispatch_all */
237 static void *ping_thread (void *arg) /* {{{ */
239 static pingobj_t *pingobj = NULL;
241 struct timeval tv_begin;
242 struct timeval tv_end;
243 struct timespec ts_wait;
244 struct timespec ts_int;
249 c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
251 pthread_mutex_lock (&ping_lock);
253 pingobj = ping_construct ();
256 ERROR ("ping plugin: ping_construct failed.");
257 ping_thread_error = 1;
258 pthread_mutex_unlock (&ping_lock);
259 return ((void *) -1);
262 if (ping_source != NULL)
263 if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
264 ERROR ("ping plugin: Failed to set source address: %s",
265 ping_get_error (pingobj));
267 #ifdef HAVE_OPING_1_3
268 if (ping_device != NULL)
269 if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
270 ERROR ("ping plugin: Failed to set device: %s",
271 ping_get_error (pingobj));
274 ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
275 ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
277 /* Add all the hosts to the ping object. */
279 for (hl = hostlist_head; hl != NULL; hl = hl->next)
282 tmp_status = ping_host_add (pingobj, hl->host);
284 WARNING ("ping plugin: ping_host_add (%s) failed: %s",
285 hl->host, ping_get_error (pingobj));
292 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
293 ping_thread_error = 1;
294 pthread_mutex_unlock (&ping_lock);
295 return ((void *) -1);
298 /* Set up `ts_int' */
303 temp_nsec = modf (ping_interval, &temp_sec);
304 ts_int.tv_sec = (time_t) temp_sec;
305 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
308 while (ping_thread_loop > 0)
311 _Bool send_successful = 0;
313 if (gettimeofday (&tv_begin, NULL) < 0)
316 ERROR ("ping plugin: gettimeofday failed: %s",
317 sstrerror (errno, errbuf, sizeof (errbuf)));
318 ping_thread_error = 1;
322 pthread_mutex_unlock (&ping_lock);
324 status = ping_send (pingobj);
327 c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
328 ping_get_error (pingobj));
332 c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
336 pthread_mutex_lock (&ping_lock);
338 if (ping_thread_loop <= 0)
342 (void) ping_dispatch_all (pingobj);
344 if (gettimeofday (&tv_end, NULL) < 0)
347 ERROR ("ping plugin: gettimeofday failed: %s",
348 sstrerror (errno, errbuf, sizeof (errbuf)));
349 ping_thread_error = 1;
353 /* Calculate the absolute time until which to wait and store it in
355 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
357 status = pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
358 if (ping_thread_loop <= 0)
360 } /* while (ping_thread_loop > 0) */
362 pthread_mutex_unlock (&ping_lock);
363 ping_destroy (pingobj);
366 } /* }}} void *ping_thread */
368 static int start_thread (void) /* {{{ */
372 pthread_mutex_lock (&ping_lock);
374 if (ping_thread_loop != 0)
376 pthread_mutex_unlock (&ping_lock);
380 ping_thread_loop = 1;
381 ping_thread_error = 0;
382 status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL,
383 ping_thread, /* arg = */ (void *) 0);
386 ping_thread_loop = 0;
387 ERROR ("ping plugin: Starting thread failed.");
388 pthread_mutex_unlock (&ping_lock);
392 pthread_mutex_unlock (&ping_lock);
394 } /* }}} int start_thread */
396 static int stop_thread (void) /* {{{ */
400 pthread_mutex_lock (&ping_lock);
402 if (ping_thread_loop == 0)
404 pthread_mutex_unlock (&ping_lock);
408 ping_thread_loop = 0;
409 pthread_cond_broadcast (&ping_cond);
410 pthread_mutex_unlock (&ping_lock);
412 status = pthread_join (ping_thread_id, /* return = */ NULL);
415 ERROR ("ping plugin: Stopping thread failed.");
419 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
420 ping_thread_error = 0;
423 } /* }}} int stop_thread */
425 static int ping_init (void) /* {{{ */
427 if (hostlist_head == NULL)
429 NOTICE ("ping plugin: No hosts have been configured.");
433 if (ping_timeout > ping_interval)
435 ping_timeout = 0.9 * ping_interval;
436 WARNING ("ping plugin: Timeout is greater than interval. "
437 "Will use a timeout of %gs.", ping_timeout);
440 if (start_thread () != 0)
444 } /* }}} int ping_init */
446 static int config_set_string (const char *name, /* {{{ */
447 char **var, const char *value)
451 tmp = strdup (value);
455 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
456 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
464 } /* }}} int config_set_string */
466 static int ping_config (const char *key, const char *value) /* {{{ */
468 if (strcasecmp (key, "Host") == 0)
473 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
477 ERROR ("ping plugin: malloc failed: %s",
478 sstrerror (errno, errbuf, sizeof (errbuf)));
482 host = strdup (value);
487 ERROR ("ping plugin: strdup failed: %s",
488 sstrerror (errno, errbuf, sizeof (errbuf)));
496 hl->latency_total = 0.0;
497 hl->latency_squared = 0.0;
498 hl->next = hostlist_head;
501 else if (strcasecmp (key, "SourceAddress") == 0)
503 int status = config_set_string (key, &ping_source, value);
507 #ifdef HAVE_OPING_1_3
508 else if (strcasecmp (key, "Device") == 0)
510 int status = config_set_string (key, &ping_device, value);
515 else if (strcasecmp (key, "TTL") == 0)
517 int ttl = atoi (value);
518 if ((ttl > 0) && (ttl <= 255))
521 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
523 else if (strcasecmp (key, "Interval") == 0)
531 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
534 else if (strcasecmp (key, "Timeout") == 0)
542 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
545 else if (strcasecmp (key, "MaxMissed") == 0)
547 ping_max_missed = atoi (value);
548 if (ping_max_missed < 0)
549 INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
557 } /* }}} int ping_config */
559 static void submit (const char *host, const char *type, /* {{{ */
563 value_list_t vl = VALUE_LIST_INIT;
565 values[0].gauge = value;
569 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
570 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
571 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
572 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
573 sstrncpy (vl.type, type, sizeof (vl.type));
575 plugin_dispatch_values (&vl);
576 } /* }}} void ping_submit */
578 static int ping_read (void) /* {{{ */
582 if (ping_thread_error != 0)
584 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
588 for (hl = hostlist_head; hl != NULL; hl = hl->next)
592 hl->latency_total = 0.0;
593 hl->latency_squared = 0.0;
599 } /* if (ping_thread_error != 0) */
601 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
605 double latency_total;
606 double latency_squared;
608 double latency_average;
609 double latency_stddev;
613 /* Locking here works, because the structure of the linked list is only
614 * changed during configure and shutdown. */
615 pthread_mutex_lock (&ping_lock);
617 pkg_sent = hl->pkg_sent;
618 pkg_recv = hl->pkg_recv;
619 latency_total = hl->latency_total;
620 latency_squared = hl->latency_squared;
624 hl->latency_total = 0.0;
625 hl->latency_squared = 0.0;
627 pthread_mutex_unlock (&ping_lock);
629 /* This e. g. happens when starting up. */
632 DEBUG ("ping plugin: No packages for host %s have been sent.",
637 /* Calculate average. Beware of division by zero. */
639 latency_average = NAN;
641 latency_average = latency_total / ((double) pkg_recv);
643 /* Calculate standard deviation. Beware even more of division by zero. */
645 latency_stddev = NAN;
646 else if (pkg_recv == 1)
647 latency_stddev = 0.0;
649 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
650 - (latency_total * latency_total))
651 / ((double) (pkg_recv * (pkg_recv - 1))));
653 /* Calculate drop rate. */
654 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
656 submit (hl->host, "ping", latency_average);
657 submit (hl->host, "ping_stddev", latency_stddev);
658 submit (hl->host, "ping_droprate", droprate);
659 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
662 } /* }}} int ping_read */
664 static int ping_shutdown (void) /* {{{ */
668 INFO ("ping plugin: Shutting down thread.");
669 if (stop_thread () < 0)
686 } /* }}} int ping_shutdown */
688 void module_register (void)
690 plugin_register_config ("ping", ping_config,
691 config_keys, config_keys_num);
692 plugin_register_init ("ping", ping_init);
693 plugin_register_read ("ping", ping_read);
694 plugin_register_shutdown ("ping", ping_shutdown);
695 } /* void module_register */
697 /* vim: set sw=2 sts=2 et fdm=marker : */