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 int ping_ttl = PING_DEF_TTL;
77 static double ping_interval = 1.0;
78 static double ping_timeout = 0.9;
79 static int ping_max_missed = -1;
81 static int ping_thread_loop = 0;
82 static int ping_thread_error = 0;
83 static pthread_t ping_thread_id;
84 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
85 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
87 static const char *config_keys[] =
99 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
104 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
105 static void time_normalize (struct timespec *ts) /* {{{ */
107 while (ts->tv_nsec < 0)
116 ts->tv_nsec += 1000000000;
119 while (ts->tv_nsec >= 1000000000)
122 ts->tv_nsec -= 1000000000;
124 } /* }}} void time_normalize */
126 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
127 * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
128 static void time_calc (struct timespec *ts_dest, /* {{{ */
129 const struct timespec *ts_int,
130 const struct timeval *tv_begin,
131 const struct timeval *tv_end)
133 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
134 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
135 time_normalize (ts_dest);
137 /* Assure that `(begin + interval) > end'.
138 * This may seem overly complicated, but `tv_sec' is of type `time_t'
139 * which may be `unsigned. *sigh* */
140 if ((tv_end->tv_sec > ts_dest->tv_sec)
141 || ((tv_end->tv_sec == ts_dest->tv_sec)
142 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
144 ts_dest->tv_sec = tv_end->tv_sec;
145 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
148 time_normalize (ts_dest);
149 } /* }}} void time_calc */
151 static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
153 pingobj_iter_t *iter;
157 for (iter = ping_iterator_get (pingobj);
159 iter = ping_iterator_next (iter))
161 char userhost[NI_MAXHOST];
165 param_size = sizeof (userhost);
166 status = ping_iterator_get_info (iter,
167 #ifdef PING_INFO_USERNAME
172 userhost, ¶m_size);
175 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
176 ping_get_error (pingobj));
180 for (hl = hostlist_head; hl != NULL; hl = hl->next)
181 if (strcmp (userhost, hl->host) == 0)
186 WARNING ("ping plugin: Cannot find host %s.", userhost);
190 param_size = sizeof (latency);
191 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
192 (void *) &latency, ¶m_size);
195 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
196 ping_get_error (pingobj));
204 hl->latency_total += latency;
205 hl->latency_squared += (latency * latency);
207 /* reset missed packages counter */
212 /* if the host did not answer our last N packages, trigger a resolv. */
213 if (ping_max_missed >= 0 && hl->pkg_missed >= ping_max_missed)
215 /* we reset the missed package counter here, since we only want to
216 * trigger a resolv every N packages and not every package _AFTER_ N
220 WARNING ("ping plugin: host %s has not answered %d PING requests,"
221 " triggering resolve", hl->host, ping_max_missed);
223 /* we trigger the resolv simply be removeing and adding the host to our
225 status = ping_host_remove (pingobj, hl->host);
228 WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
232 status = ping_host_add (pingobj, hl->host);
234 ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
236 } /* }}} ping_max_missed */
237 } /* }}} for (iter) */
240 } /* }}} int ping_dispatch_all */
242 static void *ping_thread (void *arg) /* {{{ */
244 static pingobj_t *pingobj = NULL;
246 struct timeval tv_begin;
247 struct timeval tv_end;
248 struct timespec ts_wait;
249 struct timespec ts_int;
254 c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
256 pthread_mutex_lock (&ping_lock);
258 pingobj = ping_construct ();
261 ERROR ("ping plugin: ping_construct failed.");
262 ping_thread_error = 1;
263 pthread_mutex_unlock (&ping_lock);
264 return ((void *) -1);
267 if (ping_source != NULL)
268 if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
269 ERROR ("ping plugin: Failed to set source address: %s",
270 ping_get_error (pingobj));
272 #ifdef HAVE_OPING_1_3
273 if (ping_device != NULL)
274 if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
275 ERROR ("ping plugin: Failed to set device: %s",
276 ping_get_error (pingobj));
279 ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
280 ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
282 /* Add all the hosts to the ping object. */
284 for (hl = hostlist_head; hl != NULL; hl = hl->next)
287 tmp_status = ping_host_add (pingobj, hl->host);
289 WARNING ("ping plugin: ping_host_add (%s) failed: %s",
290 hl->host, ping_get_error (pingobj));
297 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
298 ping_thread_error = 1;
299 pthread_mutex_unlock (&ping_lock);
300 return ((void *) -1);
303 /* Set up `ts_int' */
308 temp_nsec = modf (ping_interval, &temp_sec);
309 ts_int.tv_sec = (time_t) temp_sec;
310 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
313 while (ping_thread_loop > 0)
316 _Bool send_successful = 0;
318 if (gettimeofday (&tv_begin, NULL) < 0)
321 ERROR ("ping plugin: gettimeofday failed: %s",
322 sstrerror (errno, errbuf, sizeof (errbuf)));
323 ping_thread_error = 1;
327 pthread_mutex_unlock (&ping_lock);
329 status = ping_send (pingobj);
332 c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
333 ping_get_error (pingobj));
337 c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
341 pthread_mutex_lock (&ping_lock);
343 if (ping_thread_loop <= 0)
347 (void) ping_dispatch_all (pingobj);
349 if (gettimeofday (&tv_end, NULL) < 0)
352 ERROR ("ping plugin: gettimeofday failed: %s",
353 sstrerror (errno, errbuf, sizeof (errbuf)));
354 ping_thread_error = 1;
358 /* Calculate the absolute time until which to wait and store it in
360 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
362 status = pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
363 if (ping_thread_loop <= 0)
365 } /* while (ping_thread_loop > 0) */
367 pthread_mutex_unlock (&ping_lock);
368 ping_destroy (pingobj);
371 } /* }}} void *ping_thread */
373 static int start_thread (void) /* {{{ */
377 pthread_mutex_lock (&ping_lock);
379 if (ping_thread_loop != 0)
381 pthread_mutex_unlock (&ping_lock);
385 ping_thread_loop = 1;
386 ping_thread_error = 0;
387 status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL,
388 ping_thread, /* arg = */ (void *) 0);
391 ping_thread_loop = 0;
392 ERROR ("ping plugin: Starting thread failed.");
393 pthread_mutex_unlock (&ping_lock);
397 pthread_mutex_unlock (&ping_lock);
399 } /* }}} int start_thread */
401 static int stop_thread (void) /* {{{ */
405 pthread_mutex_lock (&ping_lock);
407 if (ping_thread_loop == 0)
409 pthread_mutex_unlock (&ping_lock);
413 ping_thread_loop = 0;
414 pthread_cond_broadcast (&ping_cond);
415 pthread_mutex_unlock (&ping_lock);
417 status = pthread_join (ping_thread_id, /* return = */ NULL);
420 ERROR ("ping plugin: Stopping thread failed.");
424 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
425 ping_thread_error = 0;
428 } /* }}} int stop_thread */
430 static int ping_init (void) /* {{{ */
432 if (hostlist_head == NULL)
434 NOTICE ("ping plugin: No hosts have been configured.");
438 if (ping_timeout > ping_interval)
440 ping_timeout = 0.9 * ping_interval;
441 WARNING ("ping plugin: Timeout is greater than interval. "
442 "Will use a timeout of %gs.", ping_timeout);
445 if (start_thread () != 0)
449 } /* }}} int ping_init */
451 static int config_set_string (const char *name, /* {{{ */
452 char **var, const char *value)
456 tmp = strdup (value);
460 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
461 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
469 } /* }}} int config_set_string */
471 static int ping_config (const char *key, const char *value) /* {{{ */
473 if (strcasecmp (key, "Host") == 0)
478 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
482 ERROR ("ping plugin: malloc failed: %s",
483 sstrerror (errno, errbuf, sizeof (errbuf)));
487 host = strdup (value);
492 ERROR ("ping plugin: strdup failed: %s",
493 sstrerror (errno, errbuf, sizeof (errbuf)));
501 hl->latency_total = 0.0;
502 hl->latency_squared = 0.0;
503 hl->next = hostlist_head;
506 else if (strcasecmp (key, "SourceAddress") == 0)
508 int status = config_set_string (key, &ping_source, value);
512 #ifdef HAVE_OPING_1_3
513 else if (strcasecmp (key, "Device") == 0)
515 int status = config_set_string (key, &ping_device, value);
520 else if (strcasecmp (key, "TTL") == 0)
522 int ttl = atoi (value);
523 if ((ttl > 0) && (ttl <= 255))
526 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
528 else if (strcasecmp (key, "Interval") == 0)
536 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
539 else if (strcasecmp (key, "Timeout") == 0)
547 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
550 else if (strcasecmp (key, "MaxMissed") == 0)
552 ping_max_missed = atoi (value);
553 if (ping_max_missed < 0)
554 INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
562 } /* }}} int ping_config */
564 static void submit (const char *host, const char *type, /* {{{ */
568 value_list_t vl = VALUE_LIST_INIT;
570 values[0].gauge = value;
574 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
575 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
576 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
577 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
578 sstrncpy (vl.type, type, sizeof (vl.type));
580 plugin_dispatch_values (&vl);
581 } /* }}} void ping_submit */
583 static int ping_read (void) /* {{{ */
587 if (ping_thread_error != 0)
589 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
593 for (hl = hostlist_head; hl != NULL; hl = hl->next)
597 hl->latency_total = 0.0;
598 hl->latency_squared = 0.0;
604 } /* if (ping_thread_error != 0) */
606 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
610 double latency_total;
611 double latency_squared;
613 double latency_average;
614 double latency_stddev;
618 /* Locking here works, because the structure of the linked list is only
619 * changed during configure and shutdown. */
620 pthread_mutex_lock (&ping_lock);
622 pkg_sent = hl->pkg_sent;
623 pkg_recv = hl->pkg_recv;
624 latency_total = hl->latency_total;
625 latency_squared = hl->latency_squared;
629 hl->latency_total = 0.0;
630 hl->latency_squared = 0.0;
632 pthread_mutex_unlock (&ping_lock);
634 /* This e. g. happens when starting up. */
637 DEBUG ("ping plugin: No packages for host %s have been sent.",
642 /* Calculate average. Beware of division by zero. */
644 latency_average = NAN;
646 latency_average = latency_total / ((double) pkg_recv);
648 /* Calculate standard deviation. Beware even more of division by zero. */
650 latency_stddev = NAN;
651 else if (pkg_recv == 1)
652 latency_stddev = 0.0;
654 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
655 - (latency_total * latency_total))
656 / ((double) (pkg_recv * (pkg_recv - 1))));
658 /* Calculate drop rate. */
659 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
661 submit (hl->host, "ping", latency_average);
662 submit (hl->host, "ping_stddev", latency_stddev);
663 submit (hl->host, "ping_droprate", droprate);
664 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
667 } /* }}} int ping_read */
669 static int ping_shutdown (void) /* {{{ */
673 INFO ("ping plugin: Shutting down thread.");
674 if (stop_thread () < 0)
691 } /* }}} int ping_shutdown */
693 void module_register (void)
695 plugin_register_config ("ping", ping_config,
696 config_keys, config_keys_num);
697 plugin_register_init ("ping", ping_init);
698 plugin_register_read ("ping", ping_read);
699 plugin_register_shutdown ("ping", ping_shutdown);
700 } /* void module_register */
702 /* vim: set sw=2 sts=2 et fdm=marker : */