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 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 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 pthread_mutex_lock (&ping_lock);
420 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
421 ping_thread_error = 0;
422 pthread_mutex_unlock (&ping_lock);
425 } /* }}} int stop_thread */
427 static int ping_init (void) /* {{{ */
429 if (hostlist_head == NULL)
431 NOTICE ("ping plugin: No hosts have been configured.");
435 if (ping_timeout > ping_interval)
437 ping_timeout = 0.9 * ping_interval;
438 WARNING ("ping plugin: Timeout is greater than interval. "
439 "Will use a timeout of %gs.", ping_timeout);
442 if (start_thread () != 0)
446 } /* }}} int ping_init */
448 static int config_set_string (const char *name, /* {{{ */
449 char **var, const char *value)
453 tmp = strdup (value);
457 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
458 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
466 } /* }}} int config_set_string */
468 static int ping_config (const char *key, const char *value) /* {{{ */
470 if (strcasecmp (key, "Host") == 0)
475 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
479 ERROR ("ping plugin: malloc failed: %s",
480 sstrerror (errno, errbuf, sizeof (errbuf)));
484 host = strdup (value);
489 ERROR ("ping plugin: strdup failed: %s",
490 sstrerror (errno, errbuf, sizeof (errbuf)));
498 hl->latency_total = 0.0;
499 hl->latency_squared = 0.0;
500 hl->next = hostlist_head;
503 else if (strcasecmp (key, "SourceAddress") == 0)
505 int status = config_set_string (key, &ping_source, value);
509 #ifdef HAVE_OPING_1_3
510 else if (strcasecmp (key, "Device") == 0)
512 int status = config_set_string (key, &ping_device, value);
517 else if (strcasecmp (key, "TTL") == 0)
519 int ttl = atoi (value);
520 if ((ttl > 0) && (ttl <= 255))
523 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
525 else if (strcasecmp (key, "Interval") == 0)
533 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
536 else if (strcasecmp (key, "Timeout") == 0)
544 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
547 else if (strcasecmp (key, "MaxMissed") == 0)
549 ping_max_missed = atoi (value);
550 if (ping_max_missed < 0)
551 INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
559 } /* }}} int ping_config */
561 static void submit (const char *host, const char *type, /* {{{ */
565 value_list_t vl = VALUE_LIST_INIT;
567 values[0].gauge = value;
571 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
572 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
573 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
574 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
575 sstrncpy (vl.type, type, sizeof (vl.type));
577 plugin_dispatch_values (&vl);
578 } /* }}} void ping_submit */
580 static int ping_read (void) /* {{{ */
584 if (ping_thread_error != 0)
586 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
590 for (hl = hostlist_head; hl != NULL; hl = hl->next)
594 hl->latency_total = 0.0;
595 hl->latency_squared = 0.0;
601 } /* if (ping_thread_error != 0) */
603 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
607 double latency_total;
608 double latency_squared;
610 double latency_average;
611 double latency_stddev;
615 /* Locking here works, because the structure of the linked list is only
616 * changed during configure and shutdown. */
617 pthread_mutex_lock (&ping_lock);
619 pkg_sent = hl->pkg_sent;
620 pkg_recv = hl->pkg_recv;
621 latency_total = hl->latency_total;
622 latency_squared = hl->latency_squared;
626 hl->latency_total = 0.0;
627 hl->latency_squared = 0.0;
629 pthread_mutex_unlock (&ping_lock);
631 /* This e. g. happens when starting up. */
634 DEBUG ("ping plugin: No packages for host %s have been sent.",
639 /* Calculate average. Beware of division by zero. */
641 latency_average = NAN;
643 latency_average = latency_total / ((double) pkg_recv);
645 /* Calculate standard deviation. Beware even more of division by zero. */
647 latency_stddev = NAN;
648 else if (pkg_recv == 1)
649 latency_stddev = 0.0;
651 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
652 - (latency_total * latency_total))
653 / ((double) (pkg_recv * (pkg_recv - 1))));
655 /* Calculate drop rate. */
656 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
658 submit (hl->host, "ping", latency_average);
659 submit (hl->host, "ping_stddev", latency_stddev);
660 submit (hl->host, "ping_droprate", droprate);
661 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
664 } /* }}} int ping_read */
666 static int ping_shutdown (void) /* {{{ */
670 INFO ("ping plugin: Shutting down thread.");
671 if (stop_thread () < 0)
688 } /* }}} int ping_shutdown */
690 void module_register (void)
692 plugin_register_config ("ping", ping_config,
693 config_keys, config_keys_num);
694 plugin_register_init ("ping", ping_init);
695 plugin_register_read ("ping", ping_read);
696 plugin_register_shutdown ("ping", ping_shutdown);
697 } /* void module_register */
699 /* vim: set sw=2 sts=2 et fdm=marker : */