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>
31 #include "configfile.h"
32 #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) /* {{{ */
158 for (pingobj_iter_t *iter = ping_iterator_get (pingobj);
160 iter = ping_iterator_next (iter))
162 char userhost[NI_MAXHOST];
166 param_size = sizeof (userhost);
167 status = ping_iterator_get_info (iter,
168 #ifdef PING_INFO_USERNAME
173 userhost, ¶m_size);
176 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
177 ping_get_error (pingobj));
181 for (hl = hostlist_head; hl != NULL; hl = hl->next)
182 if (strcmp (userhost, hl->host) == 0)
187 WARNING ("ping plugin: Cannot find host %s.", userhost);
191 param_size = sizeof (latency);
192 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
193 (void *) &latency, ¶m_size);
196 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
197 ping_get_error (pingobj));
205 hl->latency_total += latency;
206 hl->latency_squared += (latency * latency);
208 /* reset missed packages counter */
213 /* if the host did not answer our last N packages, trigger a resolv. */
214 if ((ping_max_missed >= 0)
215 && (hl->pkg_missed >= ((uint32_t) ping_max_missed)))
217 /* we reset the missed package counter here, since we only want to
218 * trigger a resolv every N packages and not every package _AFTER_ N
222 WARNING ("ping plugin: host %s has not answered %d PING requests,"
223 " triggering resolve", hl->host, ping_max_missed);
225 /* we trigger the resolv simply be removeing and adding the host to our
227 status = ping_host_remove (pingobj, hl->host);
230 WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
234 status = ping_host_add (pingobj, hl->host);
236 ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
238 } /* }}} ping_max_missed */
239 } /* }}} for (iter) */
242 } /* }}} int ping_dispatch_all */
244 static void *ping_thread (void *arg) /* {{{ */
246 pingobj_t *pingobj = NULL;
248 struct timeval tv_begin;
249 struct timeval tv_end;
250 struct timespec ts_wait;
251 struct timespec ts_int;
255 c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
257 pthread_mutex_lock (&ping_lock);
259 pingobj = ping_construct ();
262 ERROR ("ping plugin: ping_construct failed.");
263 ping_thread_error = 1;
264 pthread_mutex_unlock (&ping_lock);
265 return ((void *) -1);
268 if (ping_source != NULL)
269 if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
270 ERROR ("ping plugin: Failed to set source address: %s",
271 ping_get_error (pingobj));
273 #ifdef HAVE_OPING_1_3
274 if (ping_device != NULL)
275 if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
276 ERROR ("ping plugin: Failed to set device: %s",
277 ping_get_error (pingobj));
280 ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
281 ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
283 if (ping_data != NULL)
284 ping_setopt (pingobj, PING_OPT_DATA, (void *) ping_data);
286 /* Add all the hosts to the ping object. */
288 for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next)
291 tmp_status = ping_host_add (pingobj, hl->host);
293 WARNING ("ping plugin: ping_host_add (%s) failed: %s",
294 hl->host, ping_get_error (pingobj));
301 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
302 ping_thread_error = 1;
303 pthread_mutex_unlock (&ping_lock);
304 return ((void *) -1);
307 /* Set up `ts_int' */
312 temp_nsec = modf (ping_interval, &temp_sec);
313 ts_int.tv_sec = (time_t) temp_sec;
314 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
317 while (ping_thread_loop > 0)
320 _Bool send_successful = 0;
322 if (gettimeofday (&tv_begin, NULL) < 0)
325 ERROR ("ping plugin: gettimeofday failed: %s",
326 sstrerror (errno, errbuf, sizeof (errbuf)));
327 ping_thread_error = 1;
331 pthread_mutex_unlock (&ping_lock);
333 status = ping_send (pingobj);
336 c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
337 ping_get_error (pingobj));
341 c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
345 pthread_mutex_lock (&ping_lock);
347 if (ping_thread_loop <= 0)
351 (void) ping_dispatch_all (pingobj);
353 if (gettimeofday (&tv_end, NULL) < 0)
356 ERROR ("ping plugin: gettimeofday failed: %s",
357 sstrerror (errno, errbuf, sizeof (errbuf)));
358 ping_thread_error = 1;
362 /* Calculate the absolute time until which to wait and store it in
364 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
366 pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
367 if (ping_thread_loop <= 0)
369 } /* while (ping_thread_loop > 0) */
371 pthread_mutex_unlock (&ping_lock);
372 ping_destroy (pingobj);
375 } /* }}} void *ping_thread */
377 static int start_thread (void) /* {{{ */
381 pthread_mutex_lock (&ping_lock);
383 if (ping_thread_loop != 0)
385 pthread_mutex_unlock (&ping_lock);
389 ping_thread_loop = 1;
390 ping_thread_error = 0;
391 status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL,
392 ping_thread, /* arg = */ (void *) 0);
395 ping_thread_loop = 0;
396 ERROR ("ping plugin: Starting thread failed.");
397 pthread_mutex_unlock (&ping_lock);
401 pthread_mutex_unlock (&ping_lock);
403 } /* }}} int start_thread */
405 static int stop_thread (void) /* {{{ */
409 pthread_mutex_lock (&ping_lock);
411 if (ping_thread_loop == 0)
413 pthread_mutex_unlock (&ping_lock);
417 ping_thread_loop = 0;
418 pthread_cond_broadcast (&ping_cond);
419 pthread_mutex_unlock (&ping_lock);
421 status = pthread_join (ping_thread_id, /* return = */ NULL);
424 ERROR ("ping plugin: Stopping thread failed.");
428 pthread_mutex_lock (&ping_lock);
429 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
430 ping_thread_error = 0;
431 pthread_mutex_unlock (&ping_lock);
434 } /* }}} int stop_thread */
436 static int ping_init (void) /* {{{ */
438 if (hostlist_head == NULL)
440 NOTICE ("ping plugin: No hosts have been configured.");
444 if (ping_timeout > ping_interval)
446 ping_timeout = 0.9 * ping_interval;
447 WARNING ("ping plugin: Timeout is greater than interval. "
448 "Will use a timeout of %gs.", ping_timeout);
451 return (start_thread ());
452 } /* }}} int ping_init */
454 static int config_set_string (const char *name, /* {{{ */
455 char **var, const char *value)
459 tmp = strdup (value);
463 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
464 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
472 } /* }}} int config_set_string */
474 static int ping_config (const char *key, const char *value) /* {{{ */
476 if (strcasecmp (key, "Host") == 0)
481 hl = malloc (sizeof (*hl));
485 ERROR ("ping plugin: malloc failed: %s",
486 sstrerror (errno, errbuf, sizeof (errbuf)));
490 host = strdup (value);
495 ERROR ("ping plugin: strdup failed: %s",
496 sstrerror (errno, errbuf, sizeof (errbuf)));
504 hl->latency_total = 0.0;
505 hl->latency_squared = 0.0;
506 hl->next = hostlist_head;
509 else if (strcasecmp (key, "SourceAddress") == 0)
511 int status = config_set_string (key, &ping_source, value);
515 #ifdef HAVE_OPING_1_3
516 else if (strcasecmp (key, "Device") == 0)
518 int status = config_set_string (key, &ping_device, value);
523 else if (strcasecmp (key, "TTL") == 0)
525 int ttl = atoi (value);
526 if ((ttl > 0) && (ttl <= 255))
529 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
531 else if (strcasecmp (key, "Interval") == 0)
539 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
542 else if (strcasecmp (key, "Size") == 0) {
543 size_t size = (size_t) atoi (value);
545 /* Max IP packet size - (IPv6 + ICMP) = 65535 - (40 + 8) = 65487 */
549 ping_data = malloc (size + 1);
550 if (ping_data == NULL)
552 ERROR ("ping plugin: malloc failed.");
556 /* Note: By default oping is using constant string
557 * "liboping -- ICMP ping library <http://octo.it/liboping/>"
558 * which is exactly 56 bytes.
560 * Optimally we would follow the ping(1) behaviour, but we
561 * cannot use byte 00 or start data payload at exactly same
562 * location, due to oping library limitations. */
563 for (size_t i = 0; i < size; i++) /* {{{ */
565 /* This restricts data pattern to be only composed of easily
566 * printable characters, and not NUL character. */
567 ping_data[i] = ('0' + i % 64);
568 } /* }}} for (i = 0; i < size; i++) */
571 WARNING ("ping plugin: Ignoring invalid Size %zu.", size);
573 else if (strcasecmp (key, "Timeout") == 0)
581 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
584 else if (strcasecmp (key, "MaxMissed") == 0)
586 ping_max_missed = atoi (value);
587 if (ping_max_missed < 0)
588 INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
596 } /* }}} int ping_config */
598 static void submit (const char *host, const char *type, /* {{{ */
602 value_list_t vl = VALUE_LIST_INIT;
604 values[0].gauge = value;
608 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
609 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
610 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
611 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
612 sstrncpy (vl.type, type, sizeof (vl.type));
614 plugin_dispatch_values (&vl);
615 } /* }}} void ping_submit */
617 static int ping_read (void) /* {{{ */
619 if (ping_thread_error != 0)
621 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
625 for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next)
629 hl->latency_total = 0.0;
630 hl->latency_squared = 0.0;
636 } /* if (ping_thread_error != 0) */
638 for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
642 double latency_total;
643 double latency_squared;
645 double latency_average;
646 double latency_stddev;
650 /* Locking here works, because the structure of the linked list is only
651 * changed during configure and shutdown. */
652 pthread_mutex_lock (&ping_lock);
654 pkg_sent = hl->pkg_sent;
655 pkg_recv = hl->pkg_recv;
656 latency_total = hl->latency_total;
657 latency_squared = hl->latency_squared;
661 hl->latency_total = 0.0;
662 hl->latency_squared = 0.0;
664 pthread_mutex_unlock (&ping_lock);
666 /* This e. g. happens when starting up. */
669 DEBUG ("ping plugin: No packages for host %s have been sent.",
674 /* Calculate average. Beware of division by zero. */
676 latency_average = NAN;
678 latency_average = latency_total / ((double) pkg_recv);
680 /* Calculate standard deviation. Beware even more of division by zero. */
682 latency_stddev = NAN;
683 else if (pkg_recv == 1)
684 latency_stddev = 0.0;
686 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
687 - (latency_total * latency_total))
688 / ((double) (pkg_recv * (pkg_recv - 1))));
690 /* Calculate drop rate. */
691 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
693 submit (hl->host, "ping", latency_average);
694 submit (hl->host, "ping_stddev", latency_stddev);
695 submit (hl->host, "ping_droprate", droprate);
696 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
699 } /* }}} int ping_read */
701 static int ping_shutdown (void) /* {{{ */
705 INFO ("ping plugin: Shutting down thread.");
706 if (stop_thread () < 0)
722 if (ping_data != NULL) {
728 } /* }}} int ping_shutdown */
730 void module_register (void)
732 plugin_register_config ("ping", ping_config,
733 config_keys, config_keys_num);
734 plugin_register_init ("ping", ping_init);
735 plugin_register_read ("ping", ping_read);
736 plugin_register_shutdown ("ping", ping_shutdown);
737 } /* void module_register */
739 /* vim: set sw=2 sts=2 et fdm=marker : */