2 * collectd - src/ping.c
3 * Copyright (C) 2005-2012 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
30 #include "configfile.h"
31 #include "utils_complain.h"
34 #include <netinet/in.h>
36 # include <netdb.h> /* NI_MAXHOST */
42 # define NI_MAXHOST 1025
45 #if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
46 # define HAVE_OPING_1_3
61 double latency_squared;
63 struct hostlist_s *next;
65 typedef struct hostlist_s hostlist_t;
70 static hostlist_t *hostlist_head = NULL;
72 static char *ping_source = NULL;
74 static char *ping_device = NULL;
76 static char *ping_data = NULL;
77 static int ping_ttl = PING_DEF_TTL;
78 static double ping_interval = 1.0;
79 static double ping_timeout = 0.9;
80 static int ping_max_missed = -1;
82 static int ping_thread_loop = 0;
83 static int ping_thread_error = 0;
84 static pthread_t ping_thread_id;
85 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
86 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
88 static const char *config_keys[] =
101 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
106 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
107 static void time_normalize (struct timespec *ts) /* {{{ */
109 while (ts->tv_nsec < 0)
118 ts->tv_nsec += 1000000000;
121 while (ts->tv_nsec >= 1000000000)
124 ts->tv_nsec -= 1000000000;
126 } /* }}} void time_normalize */
128 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
129 * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
130 static void time_calc (struct timespec *ts_dest, /* {{{ */
131 const struct timespec *ts_int,
132 const struct timeval *tv_begin,
133 const struct timeval *tv_end)
135 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
136 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
137 time_normalize (ts_dest);
139 /* Assure that `(begin + interval) > end'.
140 * This may seem overly complicated, but `tv_sec' is of type `time_t'
141 * which may be `unsigned. *sigh* */
142 if ((tv_end->tv_sec > ts_dest->tv_sec)
143 || ((tv_end->tv_sec == ts_dest->tv_sec)
144 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
146 ts_dest->tv_sec = tv_end->tv_sec;
147 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
150 time_normalize (ts_dest);
151 } /* }}} void time_calc */
153 static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
155 pingobj_iter_t *iter;
159 for (iter = ping_iterator_get (pingobj);
161 iter = ping_iterator_next (iter))
163 char userhost[NI_MAXHOST];
167 param_size = sizeof (userhost);
168 status = ping_iterator_get_info (iter,
169 #ifdef PING_INFO_USERNAME
174 userhost, ¶m_size);
177 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
178 ping_get_error (pingobj));
182 for (hl = hostlist_head; hl != NULL; hl = hl->next)
183 if (strcmp (userhost, hl->host) == 0)
188 WARNING ("ping plugin: Cannot find host %s.", userhost);
192 param_size = sizeof (latency);
193 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
194 (void *) &latency, ¶m_size);
197 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
198 ping_get_error (pingobj));
206 hl->latency_total += latency;
207 hl->latency_squared += (latency * latency);
209 /* reset missed packages counter */
214 /* if the host did not answer our last N packages, trigger a resolv. */
215 if ((ping_max_missed >= 0)
216 && (hl->pkg_missed >= ((uint32_t) ping_max_missed)))
218 /* we reset the missed package counter here, since we only want to
219 * trigger a resolv every N packages and not every package _AFTER_ N
223 WARNING ("ping plugin: host %s has not answered %d PING requests,"
224 " triggering resolve", hl->host, ping_max_missed);
226 /* we trigger the resolv simply be removeing and adding the host to our
228 status = ping_host_remove (pingobj, hl->host);
231 WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
235 status = ping_host_add (pingobj, hl->host);
237 ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
239 } /* }}} ping_max_missed */
240 } /* }}} for (iter) */
243 } /* }}} int ping_dispatch_all */
245 static void *ping_thread (void *arg) /* {{{ */
247 pingobj_t *pingobj = NULL;
249 struct timeval tv_begin;
250 struct timeval tv_end;
251 struct timespec ts_wait;
252 struct timespec ts_int;
257 c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
259 pthread_mutex_lock (&ping_lock);
261 pingobj = ping_construct ();
264 ERROR ("ping plugin: ping_construct failed.");
265 ping_thread_error = 1;
266 pthread_mutex_unlock (&ping_lock);
267 return ((void *) -1);
270 if (ping_source != NULL)
271 if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
272 ERROR ("ping plugin: Failed to set source address: %s",
273 ping_get_error (pingobj));
275 #ifdef HAVE_OPING_1_3
276 if (ping_device != NULL)
277 if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
278 ERROR ("ping plugin: Failed to set device: %s",
279 ping_get_error (pingobj));
282 ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
283 ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
285 if (ping_data != NULL)
286 ping_setopt (pingobj, PING_OPT_DATA, (void *) ping_data);
288 /* Add all the hosts to the ping object. */
290 for (hl = hostlist_head; hl != NULL; hl = hl->next)
293 tmp_status = ping_host_add (pingobj, hl->host);
295 WARNING ("ping plugin: ping_host_add (%s) failed: %s",
296 hl->host, ping_get_error (pingobj));
303 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
304 ping_thread_error = 1;
305 pthread_mutex_unlock (&ping_lock);
306 return ((void *) -1);
309 /* Set up `ts_int' */
314 temp_nsec = modf (ping_interval, &temp_sec);
315 ts_int.tv_sec = (time_t) temp_sec;
316 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
319 while (ping_thread_loop > 0)
322 _Bool send_successful = 0;
324 if (gettimeofday (&tv_begin, NULL) < 0)
327 ERROR ("ping plugin: gettimeofday failed: %s",
328 sstrerror (errno, errbuf, sizeof (errbuf)));
329 ping_thread_error = 1;
333 pthread_mutex_unlock (&ping_lock);
335 status = ping_send (pingobj);
338 c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
339 ping_get_error (pingobj));
343 c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
347 pthread_mutex_lock (&ping_lock);
349 if (ping_thread_loop <= 0)
353 (void) ping_dispatch_all (pingobj);
355 if (gettimeofday (&tv_end, NULL) < 0)
358 ERROR ("ping plugin: gettimeofday failed: %s",
359 sstrerror (errno, errbuf, sizeof (errbuf)));
360 ping_thread_error = 1;
364 /* Calculate the absolute time until which to wait and store it in
366 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
368 pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
369 if (ping_thread_loop <= 0)
371 } /* while (ping_thread_loop > 0) */
373 pthread_mutex_unlock (&ping_lock);
374 ping_destroy (pingobj);
377 } /* }}} void *ping_thread */
379 static int start_thread (void) /* {{{ */
383 pthread_mutex_lock (&ping_lock);
385 if (ping_thread_loop != 0)
387 pthread_mutex_unlock (&ping_lock);
391 ping_thread_loop = 1;
392 ping_thread_error = 0;
393 status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL,
394 ping_thread, /* arg = */ (void *) 0);
397 ping_thread_loop = 0;
398 ERROR ("ping plugin: Starting thread failed.");
399 pthread_mutex_unlock (&ping_lock);
403 pthread_mutex_unlock (&ping_lock);
405 } /* }}} int start_thread */
407 static int stop_thread (void) /* {{{ */
411 pthread_mutex_lock (&ping_lock);
413 if (ping_thread_loop == 0)
415 pthread_mutex_unlock (&ping_lock);
419 ping_thread_loop = 0;
420 pthread_cond_broadcast (&ping_cond);
421 pthread_mutex_unlock (&ping_lock);
423 status = pthread_join (ping_thread_id, /* return = */ NULL);
426 ERROR ("ping plugin: Stopping thread failed.");
430 pthread_mutex_lock (&ping_lock);
431 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
432 ping_thread_error = 0;
433 pthread_mutex_unlock (&ping_lock);
436 } /* }}} int stop_thread */
438 static int ping_init (void) /* {{{ */
440 if (hostlist_head == NULL)
442 NOTICE ("ping plugin: No hosts have been configured.");
446 if (ping_timeout > ping_interval)
448 ping_timeout = 0.9 * ping_interval;
449 WARNING ("ping plugin: Timeout is greater than interval. "
450 "Will use a timeout of %gs.", ping_timeout);
453 if (start_thread () != 0)
457 } /* }}} int ping_init */
459 static int config_set_string (const char *name, /* {{{ */
460 char **var, const char *value)
464 tmp = strdup (value);
468 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
469 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
477 } /* }}} int config_set_string */
479 static int ping_config (const char *key, const char *value) /* {{{ */
481 if (strcasecmp (key, "Host") == 0)
486 hl = malloc (sizeof (*hl));
490 ERROR ("ping plugin: malloc failed: %s",
491 sstrerror (errno, errbuf, sizeof (errbuf)));
495 host = strdup (value);
500 ERROR ("ping plugin: strdup failed: %s",
501 sstrerror (errno, errbuf, sizeof (errbuf)));
509 hl->latency_total = 0.0;
510 hl->latency_squared = 0.0;
511 hl->next = hostlist_head;
514 else if (strcasecmp (key, "SourceAddress") == 0)
516 int status = config_set_string (key, &ping_source, value);
520 #ifdef HAVE_OPING_1_3
521 else if (strcasecmp (key, "Device") == 0)
523 int status = config_set_string (key, &ping_device, value);
528 else if (strcasecmp (key, "TTL") == 0)
530 int ttl = atoi (value);
531 if ((ttl > 0) && (ttl <= 255))
534 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
536 else if (strcasecmp (key, "Interval") == 0)
544 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
547 else if (strcasecmp (key, "Size") == 0) {
548 size_t size = (size_t) atoi (value);
550 /* Max IP packet size - (IPv6 + ICMP) = 65535 - (40 + 8) = 65487 */
556 ping_data = malloc (size + 1);
557 if (ping_data == NULL)
559 ERROR ("ping plugin: malloc failed.");
563 /* Note: By default oping is using constant string
564 * "liboping -- ICMP ping library <http://octo.it/liboping/>"
565 * which is exactly 56 bytes.
567 * Optimally we would follow the ping(1) behaviour, but we
568 * cannot use byte 00 or start data payload at exactly same
569 * location, due to oping library limitations. */
570 for (i = 0; i < size; i++) /* {{{ */
572 /* This restricts data pattern to be only composed of easily
573 * printable characters, and not NUL character. */
574 ping_data[i] = ('0' + i % 64);
575 } /* }}} for (i = 0; i < size; i++) */
578 WARNING ("ping plugin: Ignoring invalid Size %zu.", size);
580 else if (strcasecmp (key, "Timeout") == 0)
588 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
591 else if (strcasecmp (key, "MaxMissed") == 0)
593 ping_max_missed = atoi (value);
594 if (ping_max_missed < 0)
595 INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
603 } /* }}} int ping_config */
605 static void submit (const char *host, const char *type, /* {{{ */
609 value_list_t vl = VALUE_LIST_INIT;
611 values[0].gauge = value;
615 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
616 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
617 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
618 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
619 sstrncpy (vl.type, type, sizeof (vl.type));
621 plugin_dispatch_values (&vl);
622 } /* }}} void ping_submit */
624 static int ping_read (void) /* {{{ */
628 if (ping_thread_error != 0)
630 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
634 for (hl = hostlist_head; hl != NULL; hl = hl->next)
638 hl->latency_total = 0.0;
639 hl->latency_squared = 0.0;
645 } /* if (ping_thread_error != 0) */
647 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
651 double latency_total;
652 double latency_squared;
654 double latency_average;
655 double latency_stddev;
659 /* Locking here works, because the structure of the linked list is only
660 * changed during configure and shutdown. */
661 pthread_mutex_lock (&ping_lock);
663 pkg_sent = hl->pkg_sent;
664 pkg_recv = hl->pkg_recv;
665 latency_total = hl->latency_total;
666 latency_squared = hl->latency_squared;
670 hl->latency_total = 0.0;
671 hl->latency_squared = 0.0;
673 pthread_mutex_unlock (&ping_lock);
675 /* This e. g. happens when starting up. */
678 DEBUG ("ping plugin: No packages for host %s have been sent.",
683 /* Calculate average. Beware of division by zero. */
685 latency_average = NAN;
687 latency_average = latency_total / ((double) pkg_recv);
689 /* Calculate standard deviation. Beware even more of division by zero. */
691 latency_stddev = NAN;
692 else if (pkg_recv == 1)
693 latency_stddev = 0.0;
695 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
696 - (latency_total * latency_total))
697 / ((double) (pkg_recv * (pkg_recv - 1))));
699 /* Calculate drop rate. */
700 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
702 submit (hl->host, "ping", latency_average);
703 submit (hl->host, "ping_stddev", latency_stddev);
704 submit (hl->host, "ping_droprate", droprate);
705 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
708 } /* }}} int ping_read */
710 static int ping_shutdown (void) /* {{{ */
714 INFO ("ping plugin: Shutting down thread.");
715 if (stop_thread () < 0)
731 if (ping_data != NULL) {
737 } /* }}} int ping_shutdown */
739 void module_register (void)
741 plugin_register_config ("ping", ping_config,
742 config_keys, config_keys_num);
743 plugin_register_init ("ping", ping_init);
744 plugin_register_read ("ping", ping_read);
745 plugin_register_shutdown ("ping", ping_shutdown);
746 } /* void module_register */
748 /* vim: set sw=2 sts=2 et fdm=marker : */