2 * collectd - src/ping.c
3 * Copyright (C) 2005-2009 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 verplant.org>
25 #include "configfile.h"
28 #include <netinet/in.h>
30 # include <netdb.h> /* NI_MAXHOST */
36 # define NI_MAXHOST 1025
50 double latency_squared;
52 struct hostlist_s *next;
54 typedef struct hostlist_s hostlist_t;
59 static hostlist_t *hostlist_head = NULL;
61 static int ping_ttl = PING_DEF_TTL;
62 static double ping_interval = 1.0;
63 static double ping_timeout = 0.9;
65 static int ping_thread_loop = 0;
66 static int ping_thread_error = 0;
67 static pthread_t ping_thread_id;
68 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
71 static const char *config_keys[] =
78 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
83 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
84 static void time_normalize (struct timespec *ts) /* {{{ */
86 while (ts->tv_nsec < 0)
95 ts->tv_nsec += 1000000000;
98 while (ts->tv_nsec >= 1000000000)
101 ts->tv_nsec -= 1000000000;
103 } /* }}} void time_normalize */
105 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
106 * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
107 static void time_calc (struct timespec *ts_dest, /* {{{ */
108 const struct timespec *ts_int,
109 const struct timeval *tv_begin,
110 const struct timeval *tv_end)
112 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
113 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
114 time_normalize (ts_dest);
116 /* Assure that `(begin + interval) > end'.
117 * This may seem overly complicated, but `tv_sec' is of type `time_t'
118 * which may be `unsigned. *sigh* */
119 if ((tv_end->tv_sec > ts_dest->tv_sec)
120 || ((tv_end->tv_sec == ts_dest->tv_sec)
121 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
123 ts_dest->tv_sec = tv_end->tv_sec;
124 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
127 time_normalize (ts_dest);
128 } /* }}} void time_calc */
130 static void *ping_thread (void *arg) /* {{{ */
132 static pingobj_t *pingobj = NULL;
134 struct timeval tv_begin;
135 struct timeval tv_end;
136 struct timespec ts_wait;
137 struct timespec ts_int;
142 pthread_mutex_lock (&ping_lock);
144 pingobj = ping_construct ();
147 ERROR ("ping plugin: ping_construct failed.");
148 ping_thread_error = 1;
149 pthread_mutex_unlock (&ping_lock);
150 return ((void *) -1);
153 ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
154 ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
156 /* Add all the hosts to the ping object. */
158 for (hl = hostlist_head; hl != NULL; hl = hl->next)
161 tmp_status = ping_host_add (pingobj, hl->host);
163 WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host);
170 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
171 ping_thread_error = 1;
172 pthread_mutex_unlock (&ping_lock);
173 return ((void *) -1);
176 /* Set up `ts_int' */
181 temp_nsec = modf (ping_interval, &temp_sec);
182 ts_int.tv_sec = (time_t) temp_sec;
183 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
186 while (ping_thread_loop > 0)
188 pingobj_iter_t *iter;
191 if (gettimeofday (&tv_begin, NULL) < 0)
193 ERROR ("ping plugin: gettimeofday failed");
194 ping_thread_error = 1;
198 pthread_mutex_unlock (&ping_lock);
200 status = ping_send (pingobj);
203 ERROR ("ping plugin: ping_send failed: %s", ping_get_error (pingobj));
204 pthread_mutex_lock (&ping_lock);
205 ping_thread_error = 1;
209 pthread_mutex_lock (&ping_lock);
211 if (ping_thread_loop <= 0)
214 for (iter = ping_iterator_get (pingobj);
216 iter = ping_iterator_next (iter))
218 char userhost[NI_MAXHOST];
222 param_size = sizeof (userhost);
223 status = ping_iterator_get_info (iter,
224 #ifdef PING_INFO_USERNAME
229 userhost, ¶m_size);
232 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
233 ping_get_error (pingobj));
237 for (hl = hostlist_head; hl != NULL; hl = hl->next)
238 if (strcmp (userhost, hl->host) == 0)
243 WARNING ("ping plugin: Cannot find host %s.", userhost);
247 param_size = sizeof (latency);
248 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
249 (void *) &latency, ¶m_size);
252 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
253 ping_get_error (pingobj));
261 hl->latency_total += latency;
262 hl->latency_squared += (latency * latency);
264 } /* }}} for (iter) */
266 if (gettimeofday (&tv_end, NULL) < 0)
268 ERROR ("ping plugin: gettimeofday failed");
269 ping_thread_error = 1;
273 /* Calculate the absolute time until which to wait and store it in
275 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
277 status = pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
278 if (ping_thread_loop <= 0)
280 } /* while (ping_thread_loop > 0) */
282 pthread_mutex_unlock (&ping_lock);
283 ping_destroy (pingobj);
286 } /* }}} void *ping_thread */
288 static int start_thread (void) /* {{{ */
292 pthread_mutex_lock (&ping_lock);
294 if (ping_thread_loop != 0)
296 pthread_mutex_unlock (&ping_lock);
300 ping_thread_loop = 1;
301 ping_thread_error = 0;
302 status = pthread_create (&ping_thread_id, /* attr = */ NULL,
303 ping_thread, /* arg = */ (void *) 0);
306 ping_thread_loop = 0;
307 ERROR ("ping plugin: Starting thread failed.");
308 pthread_mutex_unlock (&ping_lock);
312 pthread_mutex_unlock (&ping_lock);
314 } /* }}} int start_thread */
316 static int stop_thread (void) /* {{{ */
320 pthread_mutex_lock (&ping_lock);
322 if (ping_thread_loop == 0)
324 pthread_mutex_unlock (&ping_lock);
328 ping_thread_loop = 0;
329 pthread_cond_broadcast (&ping_cond);
330 pthread_mutex_unlock (&ping_lock);
332 status = pthread_join (ping_thread_id, /* return = */ NULL);
335 ERROR ("ping plugin: Stopping thread failed.");
339 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
340 ping_thread_error = 0;
343 } /* }}} int stop_thread */
345 static int ping_init (void) /* {{{ */
347 if (hostlist_head == NULL)
349 NOTICE ("ping plugin: No hosts have been configured.");
353 if (ping_timeout > ping_interval)
355 ping_timeout = 0.9 * ping_interval;
356 WARNING ("ping plugin: Timeout is greater than interval. "
357 "Will use a timeout of %gs.", ping_timeout);
360 if (start_thread () != 0)
364 } /* }}} int ping_init */
366 static int ping_config (const char *key, const char *value) /* {{{ */
368 if (strcasecmp (key, "Host") == 0)
373 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
377 ERROR ("ping plugin: malloc failed: %s",
378 sstrerror (errno, errbuf, sizeof (errbuf)));
382 host = strdup (value);
387 ERROR ("ping plugin: strdup failed: %s",
388 sstrerror (errno, errbuf, sizeof (errbuf)));
395 hl->latency_total = 0.0;
396 hl->latency_squared = 0.0;
397 hl->next = hostlist_head;
400 else if (strcasecmp (key, "TTL") == 0)
402 int ttl = atoi (value);
403 if ((ttl > 0) && (ttl <= 255))
406 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
408 else if (strcasecmp (key, "Interval") == 0)
416 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
419 else if (strcasecmp (key, "Timeout") == 0)
427 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
436 } /* }}} int ping_config */
438 static void submit (const char *host, const char *type, /* {{{ */
442 value_list_t vl = VALUE_LIST_INIT;
444 values[0].gauge = value;
448 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
449 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
450 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
451 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
452 sstrncpy (vl.type, type, sizeof (vl.type));
454 plugin_dispatch_values (&vl);
455 } /* }}} void ping_submit */
457 static int ping_read (void) /* {{{ */
461 if (ping_thread_error != 0)
463 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
467 for (hl = hostlist_head; hl != NULL; hl = hl->next)
471 hl->latency_total = 0.0;
472 hl->latency_squared = 0.0;
478 } /* if (ping_thread_error != 0) */
480 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
484 double latency_total;
485 double latency_squared;
487 double latency_average;
488 double latency_stddev;
492 /* Locking here works, because the structure of the linked list is only
493 * changed during configure and shutdown. */
494 pthread_mutex_lock (&ping_lock);
496 pkg_sent = hl->pkg_sent;
497 pkg_recv = hl->pkg_recv;
498 latency_total = hl->latency_total;
499 latency_squared = hl->latency_squared;
503 hl->latency_total = 0.0;
504 hl->latency_squared = 0.0;
506 pthread_mutex_unlock (&ping_lock);
508 /* This e. g. happens when starting up. */
511 DEBUG ("ping plugin: No packages for host %s have been sent.",
516 /* Calculate average. Beware of division by zero. */
518 latency_average = NAN;
520 latency_average = latency_total / ((double) pkg_recv);
522 /* Calculate standard deviation. Beware even more of division by zero. */
524 latency_stddev = NAN;
525 else if (pkg_recv == 1)
526 latency_stddev = 0.0;
528 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
529 - (latency_total * latency_total))
530 / ((double) (pkg_recv * (pkg_recv - 1))));
532 /* Calculate drop rate. */
533 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
535 submit (hl->host, "ping", latency_average);
536 submit (hl->host, "ping_stddev", latency_stddev);
537 submit (hl->host, "ping_droprate", droprate);
538 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
541 } /* }}} int ping_read */
543 static int ping_shutdown (void) /* {{{ */
547 INFO ("ping plugin: Shutting down thread.");
548 if (stop_thread () < 0)
565 } /* }}} int ping_shutdown */
567 void module_register (void)
569 plugin_register_config ("ping", ping_config,
570 config_keys, config_keys_num);
571 plugin_register_init ("ping", ping_init);
572 plugin_register_read ("ping", ping_read);
573 plugin_register_shutdown ("ping", ping_shutdown);
574 } /* void module_register */
576 /* vim: set sw=2 sts=2 et fdm=marker : */