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
39 #if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
40 # define HAVE_OPING_1_3
54 double latency_squared;
56 struct hostlist_s *next;
58 typedef struct hostlist_s hostlist_t;
63 static hostlist_t *hostlist_head = NULL;
65 static char *ping_source = NULL;
67 static char *ping_device = NULL;
69 static int ping_ttl = PING_DEF_TTL;
70 static double ping_interval = 1.0;
71 static double ping_timeout = 0.9;
73 static int ping_thread_loop = 0;
74 static int ping_thread_error = 0;
75 static pthread_t ping_thread_id;
76 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
79 static const char *config_keys[] =
90 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
95 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
96 static void time_normalize (struct timespec *ts) /* {{{ */
98 while (ts->tv_nsec < 0)
107 ts->tv_nsec += 1000000000;
110 while (ts->tv_nsec >= 1000000000)
113 ts->tv_nsec -= 1000000000;
115 } /* }}} void time_normalize */
117 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
118 * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
119 static void time_calc (struct timespec *ts_dest, /* {{{ */
120 const struct timespec *ts_int,
121 const struct timeval *tv_begin,
122 const struct timeval *tv_end)
124 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
125 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
126 time_normalize (ts_dest);
128 /* Assure that `(begin + interval) > end'.
129 * This may seem overly complicated, but `tv_sec' is of type `time_t'
130 * which may be `unsigned. *sigh* */
131 if ((tv_end->tv_sec > ts_dest->tv_sec)
132 || ((tv_end->tv_sec == ts_dest->tv_sec)
133 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
135 ts_dest->tv_sec = tv_end->tv_sec;
136 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
139 time_normalize (ts_dest);
140 } /* }}} void time_calc */
142 static void *ping_thread (void *arg) /* {{{ */
144 static pingobj_t *pingobj = NULL;
146 struct timeval tv_begin;
147 struct timeval tv_end;
148 struct timespec ts_wait;
149 struct timespec ts_int;
154 pthread_mutex_lock (&ping_lock);
156 pingobj = ping_construct ();
159 ERROR ("ping plugin: ping_construct failed.");
160 ping_thread_error = 1;
161 pthread_mutex_unlock (&ping_lock);
162 return ((void *) -1);
165 if (ping_source != NULL)
166 if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
167 ERROR ("ping plugin: Failed to set source address: %s",
168 ping_get_error (pingobj));
170 #ifdef HAVE_OPING_1_3
171 if (ping_device != NULL)
172 if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
173 ERROR ("ping plugin: Failed to set device: %s",
174 ping_get_error (pingobj));
177 ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
178 ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
180 /* Add all the hosts to the ping object. */
182 for (hl = hostlist_head; hl != NULL; hl = hl->next)
185 status = ping_host_add (pingobj, hl->host);
187 WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host);
194 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
195 ping_thread_error = 1;
196 pthread_mutex_unlock (&ping_lock);
197 return ((void *) -1);
200 /* Set up `ts_int' */
205 temp_nsec = modf (ping_interval, &temp_sec);
206 ts_int.tv_sec = (time_t) temp_sec;
207 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
210 while (ping_thread_loop > 0)
212 pingobj_iter_t *iter;
215 if (gettimeofday (&tv_begin, NULL) < 0)
217 ERROR ("ping plugin: gettimeofday failed");
218 ping_thread_error = 1;
222 pthread_mutex_unlock (&ping_lock);
224 status = ping_send (pingobj);
227 ERROR ("ping plugin: ping_send failed: %s", ping_get_error (pingobj));
228 pthread_mutex_lock (&ping_lock);
229 ping_thread_error = 1;
233 pthread_mutex_lock (&ping_lock);
235 if (ping_thread_loop <= 0)
238 for (iter = ping_iterator_get (pingobj);
240 iter = ping_iterator_next (iter))
242 char userhost[NI_MAXHOST];
246 param_size = sizeof (userhost);
247 status = ping_iterator_get_info (iter,
248 #ifdef PING_INFO_USERNAME
253 userhost, ¶m_size);
256 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
257 ping_get_error (pingobj));
261 for (hl = hostlist_head; hl != NULL; hl = hl->next)
262 if (strcmp (userhost, hl->host) == 0)
267 WARNING ("ping plugin: Cannot find host %s.", userhost);
271 param_size = sizeof (latency);
272 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
273 (void *) &latency, ¶m_size);
276 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
277 ping_get_error (pingobj));
285 hl->latency_total += latency;
286 hl->latency_squared += (latency * latency);
288 } /* }}} for (iter) */
290 if (gettimeofday (&tv_end, NULL) < 0)
292 ERROR ("ping plugin: gettimeofday failed");
293 ping_thread_error = 1;
297 /* Calculate the absolute time until which to wait and store it in
299 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
301 status = pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
302 if (ping_thread_loop <= 0)
304 } /* while (ping_thread_loop > 0) */
306 pthread_mutex_unlock (&ping_lock);
307 ping_destroy (pingobj);
310 } /* }}} void *ping_thread */
312 static int start_thread (void) /* {{{ */
316 pthread_mutex_lock (&ping_lock);
318 if (ping_thread_loop != 0)
320 pthread_mutex_unlock (&ping_lock);
324 ping_thread_loop = 1;
325 ping_thread_error = 0;
326 status = pthread_create (&ping_thread_id, /* attr = */ NULL,
327 ping_thread, /* arg = */ (void *) 0);
330 ping_thread_loop = 0;
331 ERROR ("ping plugin: Starting thread failed.");
332 pthread_mutex_unlock (&ping_lock);
336 pthread_mutex_unlock (&ping_lock);
338 } /* }}} int start_thread */
340 static int stop_thread (void) /* {{{ */
344 pthread_mutex_lock (&ping_lock);
346 if (ping_thread_loop == 0)
348 pthread_mutex_unlock (&ping_lock);
352 ping_thread_loop = 0;
353 pthread_cond_broadcast (&ping_cond);
354 pthread_mutex_unlock (&ping_lock);
356 status = pthread_join (ping_thread_id, /* return = */ NULL);
359 ERROR ("ping plugin: Stopping thread failed.");
363 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
364 ping_thread_error = 0;
367 } /* }}} int stop_thread */
369 static int ping_init (void) /* {{{ */
371 if (hostlist_head == NULL)
373 NOTICE ("ping plugin: No hosts have been configured.");
377 if (ping_timeout > ping_interval)
379 ping_timeout = 0.9 * ping_interval;
380 WARNING ("ping plugin: Timeout is greater than interval. "
381 "Will use a timeout of %gs.", ping_timeout);
384 if (start_thread () != 0)
388 } /* }}} int ping_init */
390 static int config_set_string (const char *name, /* {{{ */
391 char **var, const char *value)
395 tmp = strdup (value);
399 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
400 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
408 } /* }}} int config_set_string */
410 static int ping_config (const char *key, const char *value) /* {{{ */
412 if (strcasecmp (key, "Host") == 0)
417 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
421 ERROR ("ping plugin: malloc failed: %s",
422 sstrerror (errno, errbuf, sizeof (errbuf)));
426 host = strdup (value);
431 ERROR ("ping plugin: strdup failed: %s",
432 sstrerror (errno, errbuf, sizeof (errbuf)));
439 hl->latency_total = 0.0;
440 hl->latency_squared = 0.0;
441 hl->next = hostlist_head;
444 else if (strcasecmp (key, "SourceAddress") == 0)
446 int status = config_set_string (key, &ping_source, value);
450 #ifdef HAVE_OPING_1_3
451 else if (strcasecmp (key, "Device") == 0)
453 int status = config_set_string (key, &ping_device, value);
458 else if (strcasecmp (key, "TTL") == 0)
460 int ttl = atoi (value);
461 if ((ttl > 0) && (ttl <= 255))
464 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
466 else if (strcasecmp (key, "Interval") == 0)
474 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
477 else if (strcasecmp (key, "Timeout") == 0)
485 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
494 } /* }}} int ping_config */
496 static void submit (const char *host, const char *type, /* {{{ */
500 value_list_t vl = VALUE_LIST_INIT;
502 values[0].gauge = value;
506 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
507 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
508 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
509 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
510 sstrncpy (vl.type, type, sizeof (vl.type));
512 plugin_dispatch_values (&vl);
513 } /* }}} void ping_submit */
515 static int ping_read (void) /* {{{ */
519 if (ping_thread_error != 0)
521 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
525 for (hl = hostlist_head; hl != NULL; hl = hl->next)
529 hl->latency_total = 0.0;
530 hl->latency_squared = 0.0;
536 } /* if (ping_thread_error != 0) */
538 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
542 double latency_total;
543 double latency_squared;
545 double latency_average;
546 double latency_stddev;
550 /* Locking here works, because the structure of the linked list is only
551 * changed during configure and shutdown. */
552 pthread_mutex_lock (&ping_lock);
554 pkg_sent = hl->pkg_sent;
555 pkg_recv = hl->pkg_recv;
556 latency_total = hl->latency_total;
557 latency_squared = hl->latency_squared;
561 hl->latency_total = 0.0;
562 hl->latency_squared = 0.0;
564 pthread_mutex_unlock (&ping_lock);
566 /* This e. g. happens when starting up. */
569 DEBUG ("ping plugin: No packages for host %s have been sent.",
574 /* Calculate average. Beware of division by zero. */
576 latency_average = NAN;
578 latency_average = latency_total / ((double) pkg_recv);
580 /* Calculate standard deviation. Beware even more of division by zero. */
582 latency_stddev = NAN;
583 else if (pkg_recv == 1)
584 latency_stddev = 0.0;
586 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
587 - (latency_total * latency_total))
588 / ((double) (pkg_recv * (pkg_recv - 1))));
590 /* Calculate drop rate. */
591 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
593 submit (hl->host, "ping", latency_average);
594 submit (hl->host, "ping_stddev", latency_stddev);
595 submit (hl->host, "ping_droprate", droprate);
596 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
599 } /* }}} int ping_read */
601 static int ping_shutdown (void) /* {{{ */
605 INFO ("ping plugin: Shutting down thread.");
606 if (stop_thread () < 0)
623 } /* }}} int ping_shutdown */
625 void module_register (void)
627 plugin_register_config ("ping", ping_config,
628 config_keys, config_keys_num);
629 plugin_register_init ("ping", ping_init);
630 plugin_register_read ("ping", ping_read);
631 plugin_register_shutdown ("ping", ping_shutdown);
632 } /* void module_register */
634 /* vim: set sw=2 sts=2 et fdm=marker : */