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"
29 #include <netinet/in.h>
30 #include "liboping/oping.h"
33 # include <netdb.h> /* NI_MAXHOST */
37 # define NI_MAXHOST 1025
51 double latency_squared;
53 struct hostlist_s *next;
55 typedef struct hostlist_s hostlist_t;
60 static hostlist_t *hostlist_head = NULL;
62 static int ping_ttl = PING_DEF_TTL;
63 static double ping_interval = 1.0;
64 static double ping_timeout = 0.9;
66 static int ping_thread_loop = 0;
67 static int ping_thread_error = 0;
68 static pthread_t ping_thread_id;
69 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
70 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
72 static const char *config_keys[] =
79 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
84 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
85 static void time_normalize (struct timespec *ts) /* {{{ */
87 while (ts->tv_nsec < 0)
96 ts->tv_nsec += 1000000000;
99 while (ts->tv_nsec >= 1000000000)
102 ts->tv_nsec -= 1000000000;
104 } /* }}} void time_normalize */
106 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
107 * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
108 static void time_calc (struct timespec *ts_dest, /* {{{ */
109 const struct timespec *ts_int,
110 const struct timeval *tv_begin,
111 const struct timeval *tv_end)
113 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
114 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
115 time_normalize (ts_dest);
117 /* Assure that `(begin + interval) > end'.
118 * This may seem overly complicated, but `tv_sec' is of type `time_t'
119 * which may be `unsigned. *sigh* */
120 if ((tv_end->tv_sec > ts_dest->tv_sec)
121 || ((tv_end->tv_sec == ts_dest->tv_sec)
122 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
124 ts_dest->tv_sec = tv_end->tv_sec;
125 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
128 time_normalize (ts_dest);
129 } /* }}} void time_calc */
131 static void *ping_thread (void *arg) /* {{{ */
133 static pingobj_t *pingobj = NULL;
135 struct timeval tv_begin;
136 struct timeval tv_end;
137 struct timespec ts_wait;
138 struct timespec ts_int;
143 pthread_mutex_lock (&ping_lock);
145 pingobj = ping_construct ();
148 ERROR ("ping plugin: ping_construct failed.");
149 ping_thread_error = 1;
150 pthread_mutex_unlock (&ping_lock);
151 return ((void *) -1);
154 ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
155 ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
157 /* Add all the hosts to the ping object. */
159 for (hl = hostlist_head; hl != NULL; hl = hl->next)
162 tmp_status = ping_host_add (pingobj, hl->host);
164 WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host);
171 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
172 ping_thread_error = 1;
173 pthread_mutex_unlock (&ping_lock);
174 return ((void *) -1);
177 /* Set up `ts_int' */
182 temp_nsec = modf (ping_interval, &temp_sec);
183 ts_int.tv_sec = (time_t) temp_sec;
184 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
187 while (ping_thread_loop > 0)
189 pingobj_iter_t *iter;
192 if (gettimeofday (&tv_begin, NULL) < 0)
194 ERROR ("ping plugin: gettimeofday failed");
195 ping_thread_error = 1;
199 pthread_mutex_unlock (&ping_lock);
201 status = ping_send (pingobj);
204 ERROR ("ping plugin: ping_send failed: %s", ping_get_error (pingobj));
205 pthread_mutex_lock (&ping_lock);
206 ping_thread_error = 1;
210 pthread_mutex_lock (&ping_lock);
212 if (ping_thread_loop <= 0)
215 for (iter = ping_iterator_get (pingobj);
217 iter = ping_iterator_next (iter))
219 char userhost[NI_MAXHOST];
223 param_size = sizeof (userhost);
224 status = ping_iterator_get_info (iter,
225 #ifdef PING_INFO_USERNAME
230 userhost, ¶m_size);
233 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
234 ping_get_error (pingobj));
238 for (hl = hostlist_head; hl != NULL; hl = hl->next)
239 if (strcmp (userhost, hl->host) == 0)
244 WARNING ("ping plugin: Cannot find host %s.", userhost);
248 param_size = sizeof (latency);
249 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
250 (void *) &latency, ¶m_size);
253 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
254 ping_get_error (pingobj));
262 hl->latency_total += latency;
263 hl->latency_squared += (latency * latency);
265 } /* }}} for (iter) */
267 if (gettimeofday (&tv_end, NULL) < 0)
269 ERROR ("ping plugin: gettimeofday failed");
270 ping_thread_error = 1;
274 /* Calculate the absolute time until which to wait and store it in
276 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
278 status = pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
279 if (ping_thread_loop <= 0)
281 } /* while (ping_thread_loop > 0) */
283 pthread_mutex_unlock (&ping_lock);
284 ping_destroy (pingobj);
287 } /* }}} void *ping_thread */
289 static int start_thread (void) /* {{{ */
293 pthread_mutex_lock (&ping_lock);
295 if (ping_thread_loop != 0)
297 pthread_mutex_unlock (&ping_lock);
301 ping_thread_loop = 1;
302 ping_thread_error = 0;
303 status = pthread_create (&ping_thread_id, /* attr = */ NULL,
304 ping_thread, /* arg = */ (void *) 0);
307 ping_thread_loop = 0;
308 ERROR ("ping plugin: Starting thread failed.");
309 pthread_mutex_unlock (&ping_lock);
313 pthread_mutex_unlock (&ping_lock);
315 } /* }}} int start_thread */
317 static int stop_thread (void) /* {{{ */
321 pthread_mutex_lock (&ping_lock);
323 if (ping_thread_loop == 0)
325 pthread_mutex_unlock (&ping_lock);
329 ping_thread_loop = 0;
330 pthread_cond_broadcast (&ping_cond);
331 pthread_mutex_unlock (&ping_lock);
333 status = pthread_join (ping_thread_id, /* return = */ NULL);
336 ERROR ("ping plugin: Stopping thread failed.");
340 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
341 ping_thread_error = 0;
344 } /* }}} int stop_thread */
346 static int ping_init (void) /* {{{ */
348 if (hostlist_head == NULL)
350 NOTICE ("ping plugin: No hosts have been configured.");
354 if (ping_timeout > ping_interval)
356 ping_timeout = 0.9 * ping_interval;
357 WARNING ("ping plugin: Timeout is greater than interval. "
358 "Will use a timeout of %gs.", ping_timeout);
361 if (start_thread () != 0)
365 } /* }}} int ping_init */
367 static int ping_config (const char *key, const char *value) /* {{{ */
369 if (strcasecmp (key, "Host") == 0)
374 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
378 ERROR ("ping plugin: malloc failed: %s",
379 sstrerror (errno, errbuf, sizeof (errbuf)));
383 host = strdup (value);
388 ERROR ("ping plugin: strdup failed: %s",
389 sstrerror (errno, errbuf, sizeof (errbuf)));
396 hl->latency_total = 0.0;
397 hl->latency_squared = 0.0;
398 hl->next = hostlist_head;
401 else if (strcasecmp (key, "TTL") == 0)
403 int ttl = atoi (value);
404 if ((ttl > 0) && (ttl <= 255))
407 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
409 else if (strcasecmp (key, "Interval") == 0)
417 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
420 else if (strcasecmp (key, "Timeout") == 0)
428 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
437 } /* }}} int ping_config */
439 static void submit (const char *host, const char *type, /* {{{ */
443 value_list_t vl = VALUE_LIST_INIT;
445 values[0].gauge = value;
449 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
450 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
451 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
452 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
453 sstrncpy (vl.type, type, sizeof (vl.type));
455 plugin_dispatch_values (&vl);
456 } /* }}} void ping_submit */
458 static int ping_read (void) /* {{{ */
462 if (ping_thread_error != 0)
464 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
468 for (hl = hostlist_head; hl != NULL; hl = hl->next)
472 hl->latency_total = 0.0;
473 hl->latency_squared = 0.0;
479 } /* if (ping_thread_error != 0) */
481 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
485 double latency_total;
486 double latency_squared;
488 double latency_average;
489 double latency_stddev;
493 /* Locking here works, because the structure of the linked list is only
494 * changed during configure and shutdown. */
495 pthread_mutex_lock (&ping_lock);
497 pkg_sent = hl->pkg_sent;
498 pkg_recv = hl->pkg_recv;
499 latency_total = hl->latency_total;
500 latency_squared = hl->latency_squared;
504 hl->latency_total = 0.0;
505 hl->latency_squared = 0.0;
507 pthread_mutex_unlock (&ping_lock);
509 /* This e. g. happens when starting up. */
512 DEBUG ("ping plugin: No packages for host %s have been sent.",
517 /* Calculate average. Beware of division by zero. */
519 latency_average = NAN;
521 latency_average = latency_total / ((double) pkg_recv);
523 /* Calculate standard deviation. Beware even more of division by zero. */
525 latency_stddev = NAN;
526 else if (pkg_recv == 1)
527 latency_stddev = 0.0;
529 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
530 - (latency_total * latency_total))
531 / ((double) (pkg_recv * (pkg_recv - 1))));
533 /* Calculate drop rate. */
534 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
536 submit (hl->host, "ping", latency_average);
537 submit (hl->host, "ping_stddev", latency_stddev);
538 submit (hl->host, "ping_droprate", droprate);
539 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
542 } /* }}} int ping_read */
544 static int ping_shutdown (void) /* {{{ */
548 INFO ("ping plugin: Shutting down thread.");
549 if (stop_thread () < 0)
566 } /* }}} int ping_shutdown */
568 void module_register (void)
570 plugin_register_config ("ping", ping_config,
571 config_keys, config_keys_num);
572 plugin_register_init ("ping", ping_init);
573 plugin_register_read ("ping", ping_read);
574 plugin_register_shutdown ("ping", ping_shutdown);
575 } /* void module_register */
577 /* vim: set sw=2 sts=2 et fdm=marker : */