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)
214 && (hl->pkg_missed >= ((uint32_t) ping_max_missed)))
216 /* we reset the missed package counter here, since we only want to
217 * trigger a resolv every N packages and not every package _AFTER_ N
221 WARNING ("ping plugin: host %s has not answered %d PING requests,"
222 " triggering resolve", hl->host, ping_max_missed);
224 /* we trigger the resolv simply be removeing and adding the host to our
226 status = ping_host_remove (pingobj, hl->host);
229 WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
233 status = ping_host_add (pingobj, hl->host);
235 ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
237 } /* }}} ping_max_missed */
238 } /* }}} for (iter) */
241 } /* }}} int ping_dispatch_all */
243 static void *ping_thread (void *arg) /* {{{ */
245 static pingobj_t *pingobj = NULL;
247 struct timeval tv_begin;
248 struct timeval tv_end;
249 struct timespec ts_wait;
250 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 /* Add all the hosts to the ping object. */
285 for (hl = hostlist_head; hl != NULL; hl = hl->next)
288 tmp_status = ping_host_add (pingobj, hl->host);
290 WARNING ("ping plugin: ping_host_add (%s) failed: %s",
291 hl->host, ping_get_error (pingobj));
298 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
299 ping_thread_error = 1;
300 pthread_mutex_unlock (&ping_lock);
301 return ((void *) -1);
304 /* Set up `ts_int' */
309 temp_nsec = modf (ping_interval, &temp_sec);
310 ts_int.tv_sec = (time_t) temp_sec;
311 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
314 while (ping_thread_loop > 0)
317 _Bool send_successful = 0;
319 if (gettimeofday (&tv_begin, NULL) < 0)
322 ERROR ("ping plugin: gettimeofday failed: %s",
323 sstrerror (errno, errbuf, sizeof (errbuf)));
324 ping_thread_error = 1;
328 pthread_mutex_unlock (&ping_lock);
330 status = ping_send (pingobj);
333 c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
334 ping_get_error (pingobj));
338 c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
342 pthread_mutex_lock (&ping_lock);
344 if (ping_thread_loop <= 0)
348 (void) ping_dispatch_all (pingobj);
350 if (gettimeofday (&tv_end, NULL) < 0)
353 ERROR ("ping plugin: gettimeofday failed: %s",
354 sstrerror (errno, errbuf, sizeof (errbuf)));
355 ping_thread_error = 1;
359 /* Calculate the absolute time until which to wait and store it in
361 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
363 pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
364 if (ping_thread_loop <= 0)
366 } /* while (ping_thread_loop > 0) */
368 pthread_mutex_unlock (&ping_lock);
369 ping_destroy (pingobj);
372 } /* }}} void *ping_thread */
374 static int start_thread (void) /* {{{ */
378 pthread_mutex_lock (&ping_lock);
380 if (ping_thread_loop != 0)
382 pthread_mutex_unlock (&ping_lock);
386 ping_thread_loop = 1;
387 ping_thread_error = 0;
388 status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL,
389 ping_thread, /* arg = */ (void *) 0);
392 ping_thread_loop = 0;
393 ERROR ("ping plugin: Starting thread failed.");
394 pthread_mutex_unlock (&ping_lock);
398 pthread_mutex_unlock (&ping_lock);
400 } /* }}} int start_thread */
402 static int stop_thread (void) /* {{{ */
406 pthread_mutex_lock (&ping_lock);
408 if (ping_thread_loop == 0)
410 pthread_mutex_unlock (&ping_lock);
414 ping_thread_loop = 0;
415 pthread_cond_broadcast (&ping_cond);
416 pthread_mutex_unlock (&ping_lock);
418 status = pthread_join (ping_thread_id, /* return = */ NULL);
421 ERROR ("ping plugin: Stopping thread failed.");
425 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
426 ping_thread_error = 0;
429 } /* }}} int stop_thread */
431 static int ping_init (void) /* {{{ */
433 if (hostlist_head == NULL)
435 NOTICE ("ping plugin: No hosts have been configured.");
439 if (ping_timeout > ping_interval)
441 ping_timeout = 0.9 * ping_interval;
442 WARNING ("ping plugin: Timeout is greater than interval. "
443 "Will use a timeout of %gs.", ping_timeout);
446 if (start_thread () != 0)
450 } /* }}} int ping_init */
452 static int config_set_string (const char *name, /* {{{ */
453 char **var, const char *value)
457 tmp = strdup (value);
461 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
462 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
470 } /* }}} int config_set_string */
472 static int ping_config (const char *key, const char *value) /* {{{ */
474 if (strcasecmp (key, "Host") == 0)
479 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
483 ERROR ("ping plugin: malloc failed: %s",
484 sstrerror (errno, errbuf, sizeof (errbuf)));
488 host = strdup (value);
493 ERROR ("ping plugin: strdup failed: %s",
494 sstrerror (errno, errbuf, sizeof (errbuf)));
502 hl->latency_total = 0.0;
503 hl->latency_squared = 0.0;
504 hl->next = hostlist_head;
507 else if (strcasecmp (key, "SourceAddress") == 0)
509 int status = config_set_string (key, &ping_source, value);
513 #ifdef HAVE_OPING_1_3
514 else if (strcasecmp (key, "Device") == 0)
516 int status = config_set_string (key, &ping_device, value);
521 else if (strcasecmp (key, "TTL") == 0)
523 int ttl = atoi (value);
524 if ((ttl > 0) && (ttl <= 255))
527 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
529 else if (strcasecmp (key, "Interval") == 0)
537 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
540 else if (strcasecmp (key, "Timeout") == 0)
548 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
551 else if (strcasecmp (key, "MaxMissed") == 0)
553 ping_max_missed = atoi (value);
554 if (ping_max_missed < 0)
555 INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
563 } /* }}} int ping_config */
565 static void submit (const char *host, const char *type, /* {{{ */
569 value_list_t vl = VALUE_LIST_INIT;
571 values[0].gauge = value;
575 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
576 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
577 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
578 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
579 sstrncpy (vl.type, type, sizeof (vl.type));
581 plugin_dispatch_values (&vl);
582 } /* }}} void ping_submit */
584 static int ping_read (void) /* {{{ */
588 if (ping_thread_error != 0)
590 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
594 for (hl = hostlist_head; hl != NULL; hl = hl->next)
598 hl->latency_total = 0.0;
599 hl->latency_squared = 0.0;
605 } /* if (ping_thread_error != 0) */
607 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
611 double latency_total;
612 double latency_squared;
614 double latency_average;
615 double latency_stddev;
619 /* Locking here works, because the structure of the linked list is only
620 * changed during configure and shutdown. */
621 pthread_mutex_lock (&ping_lock);
623 pkg_sent = hl->pkg_sent;
624 pkg_recv = hl->pkg_recv;
625 latency_total = hl->latency_total;
626 latency_squared = hl->latency_squared;
630 hl->latency_total = 0.0;
631 hl->latency_squared = 0.0;
633 pthread_mutex_unlock (&ping_lock);
635 /* This e. g. happens when starting up. */
638 DEBUG ("ping plugin: No packages for host %s have been sent.",
643 /* Calculate average. Beware of division by zero. */
645 latency_average = NAN;
647 latency_average = latency_total / ((double) pkg_recv);
649 /* Calculate standard deviation. Beware even more of division by zero. */
651 latency_stddev = NAN;
652 else if (pkg_recv == 1)
653 latency_stddev = 0.0;
655 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
656 - (latency_total * latency_total))
657 / ((double) (pkg_recv * (pkg_recv - 1))));
659 /* Calculate drop rate. */
660 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
662 submit (hl->host, "ping", latency_average);
663 submit (hl->host, "ping_stddev", latency_stddev);
664 submit (hl->host, "ping_droprate", droprate);
665 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
668 } /* }}} int ping_read */
670 static int ping_shutdown (void) /* {{{ */
674 INFO ("ping plugin: Shutting down thread.");
675 if (stop_thread () < 0)
692 } /* }}} int ping_shutdown */
694 void module_register (void)
696 plugin_register_config ("ping", ping_config,
697 config_keys, config_keys_num);
698 plugin_register_init ("ping", ping_init);
699 plugin_register_read ("ping", ping_read);
700 plugin_register_shutdown ("ping", ping_shutdown);
701 } /* void module_register */
703 /* vim: set sw=2 sts=2 et fdm=marker : */