1 /**
2 * collectd - src/ping.c
3 * Copyright (C) 2005-2012 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at collectd.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_complain.h"
28 #include <pthread.h>
29 #include <netinet/in.h>
30 #if HAVE_NETDB_H
31 # include <netdb.h> /* NI_MAXHOST */
32 #endif
34 #include <oping.h>
36 #ifndef NI_MAXHOST
37 # define NI_MAXHOST 1025
38 #endif
40 #if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
41 # define HAVE_OPING_1_3
42 #endif
44 /*
45 * Private data types
46 */
47 struct hostlist_s
48 {
49 char *host;
51 uint32_t pkg_sent;
52 uint32_t pkg_recv;
53 uint32_t pkg_missed;
55 double latency_total;
56 double latency_squared;
58 struct hostlist_s *next;
59 };
60 typedef struct hostlist_s hostlist_t;
62 /*
63 * Private variables
64 */
65 static hostlist_t *hostlist_head = NULL;
67 static char *ping_source = NULL;
68 #ifdef HAVE_OPING_1_3
69 static char *ping_device = NULL;
70 #endif
71 static int ping_ttl = PING_DEF_TTL;
72 static double ping_interval = 1.0;
73 static double ping_timeout = 0.9;
74 static int ping_max_missed = -1;
76 static int ping_thread_loop = 0;
77 static int ping_thread_error = 0;
78 static pthread_t ping_thread_id;
79 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
80 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
82 static const char *config_keys[] =
83 {
84 "Host",
85 "SourceAddress",
86 #ifdef HAVE_OPING_1_3
87 "Device",
88 #endif
89 "TTL",
90 "Interval",
91 "Timeout",
92 "MaxMissed"
93 };
94 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
96 /*
97 * Private functions
98 */
99 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
100 static void time_normalize (struct timespec *ts) /* {{{ */
101 {
102 while (ts->tv_nsec < 0)
103 {
104 if (ts->tv_sec == 0)
105 {
106 ts->tv_nsec = 0;
107 return;
108 }
110 ts->tv_sec -= 1;
111 ts->tv_nsec += 1000000000;
112 }
114 while (ts->tv_nsec >= 1000000000)
115 {
116 ts->tv_sec += 1;
117 ts->tv_nsec -= 1000000000;
118 }
119 } /* }}} void time_normalize */
121 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
122 * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
123 static void time_calc (struct timespec *ts_dest, /* {{{ */
124 const struct timespec *ts_int,
125 const struct timeval *tv_begin,
126 const struct timeval *tv_end)
127 {
128 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
129 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
130 time_normalize (ts_dest);
132 /* Assure that `(begin + interval) > end'.
133 * This may seem overly complicated, but `tv_sec' is of type `time_t'
134 * which may be `unsigned. *sigh* */
135 if ((tv_end->tv_sec > ts_dest->tv_sec)
136 || ((tv_end->tv_sec == ts_dest->tv_sec)
137 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
138 {
139 ts_dest->tv_sec = tv_end->tv_sec;
140 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
141 }
143 time_normalize (ts_dest);
144 } /* }}} void time_calc */
146 static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
147 {
148 pingobj_iter_t *iter;
149 hostlist_t *hl;
150 int status;
152 for (iter = ping_iterator_get (pingobj);
153 iter != NULL;
154 iter = ping_iterator_next (iter))
155 { /* {{{ */
156 char userhost[NI_MAXHOST];
157 double latency;
158 size_t param_size;
160 param_size = sizeof (userhost);
161 status = ping_iterator_get_info (iter,
162 #ifdef PING_INFO_USERNAME
163 PING_INFO_USERNAME,
164 #else
165 PING_INFO_HOSTNAME,
166 #endif
167 userhost, ¶m_size);
168 if (status != 0)
169 {
170 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
171 ping_get_error (pingobj));
172 continue;
173 }
175 for (hl = hostlist_head; hl != NULL; hl = hl->next)
176 if (strcmp (userhost, hl->host) == 0)
177 break;
179 if (hl == NULL)
180 {
181 WARNING ("ping plugin: Cannot find host %s.", userhost);
182 continue;
183 }
185 param_size = sizeof (latency);
186 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
187 (void *) &latency, ¶m_size);
188 if (status != 0)
189 {
190 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
191 ping_get_error (pingobj));
192 continue;
193 }
195 hl->pkg_sent++;
196 if (latency >= 0.0)
197 {
198 hl->pkg_recv++;
199 hl->latency_total += latency;
200 hl->latency_squared += (latency * latency);
202 /* reset missed packages counter */
203 hl->pkg_missed = 0;
204 } else
205 hl->pkg_missed++;
207 /* if the host did not answer our last N packages, trigger a resolv. */
208 if (ping_max_missed >= 0 && hl->pkg_missed >= ping_max_missed)
209 { /* {{{ */
210 /* we reset the missed package counter here, since we only want to
211 * trigger a resolv every N packages and not every package _AFTER_ N
212 * missed packages */
213 hl->pkg_missed = 0;
215 WARNING ("ping plugin: host %s has not answered %d PING requests,"
216 " triggering resolve", hl->host, ping_max_missed);
218 /* we trigger the resolv simply be removeing and adding the host to our
219 * ping object */
220 status = ping_host_remove (pingobj, hl->host);
221 if (status != 0)
222 {
223 WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
224 }
225 else
226 {
227 status = ping_host_add (pingobj, hl->host);
228 if (status != 0)
229 ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
230 }
231 } /* }}} ping_max_missed */
232 } /* }}} for (iter) */
234 return (0);
235 } /* }}} int ping_dispatch_all */
237 static void *ping_thread (void *arg) /* {{{ */
238 {
239 pingobj_t *pingobj = NULL;
241 struct timeval tv_begin;
242 struct timeval tv_end;
243 struct timespec ts_wait;
244 struct timespec ts_int;
246 hostlist_t *hl;
247 int count;
249 c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
251 pthread_mutex_lock (&ping_lock);
253 pingobj = ping_construct ();
254 if (pingobj == NULL)
255 {
256 ERROR ("ping plugin: ping_construct failed.");
257 ping_thread_error = 1;
258 pthread_mutex_unlock (&ping_lock);
259 return ((void *) -1);
260 }
262 if (ping_source != NULL)
263 if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
264 ERROR ("ping plugin: Failed to set source address: %s",
265 ping_get_error (pingobj));
267 #ifdef HAVE_OPING_1_3
268 if (ping_device != NULL)
269 if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
270 ERROR ("ping plugin: Failed to set device: %s",
271 ping_get_error (pingobj));
272 #endif
274 ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
275 ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
277 /* Add all the hosts to the ping object. */
278 count = 0;
279 for (hl = hostlist_head; hl != NULL; hl = hl->next)
280 {
281 int tmp_status;
282 tmp_status = ping_host_add (pingobj, hl->host);
283 if (tmp_status != 0)
284 WARNING ("ping plugin: ping_host_add (%s) failed: %s",
285 hl->host, ping_get_error (pingobj));
286 else
287 count++;
288 }
290 if (count == 0)
291 {
292 ERROR ("ping plugin: No host could be added to ping object. Giving up.");
293 ping_thread_error = 1;
294 pthread_mutex_unlock (&ping_lock);
295 return ((void *) -1);
296 }
298 /* Set up `ts_int' */
299 {
300 double temp_sec;
301 double temp_nsec;
303 temp_nsec = modf (ping_interval, &temp_sec);
304 ts_int.tv_sec = (time_t) temp_sec;
305 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
306 }
308 while (ping_thread_loop > 0)
309 {
310 int status;
311 _Bool send_successful = 0;
313 if (gettimeofday (&tv_begin, NULL) < 0)
314 {
315 char errbuf[1024];
316 ERROR ("ping plugin: gettimeofday failed: %s",
317 sstrerror (errno, errbuf, sizeof (errbuf)));
318 ping_thread_error = 1;
319 break;
320 }
322 pthread_mutex_unlock (&ping_lock);
324 status = ping_send (pingobj);
325 if (status < 0)
326 {
327 c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
328 ping_get_error (pingobj));
329 }
330 else
331 {
332 c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
333 send_successful = 1;
334 }
336 pthread_mutex_lock (&ping_lock);
338 if (ping_thread_loop <= 0)
339 break;
341 if (send_successful)
342 (void) ping_dispatch_all (pingobj);
344 if (gettimeofday (&tv_end, NULL) < 0)
345 {
346 char errbuf[1024];
347 ERROR ("ping plugin: gettimeofday failed: %s",
348 sstrerror (errno, errbuf, sizeof (errbuf)));
349 ping_thread_error = 1;
350 break;
351 }
353 /* Calculate the absolute time until which to wait and store it in
354 * `ts_wait'. */
355 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
357 pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
358 if (ping_thread_loop <= 0)
359 break;
360 } /* while (ping_thread_loop > 0) */
362 pthread_mutex_unlock (&ping_lock);
363 ping_destroy (pingobj);
365 return ((void *) 0);
366 } /* }}} void *ping_thread */
368 static int start_thread (void) /* {{{ */
369 {
370 int status;
372 pthread_mutex_lock (&ping_lock);
374 if (ping_thread_loop != 0)
375 {
376 pthread_mutex_unlock (&ping_lock);
377 return (0);
378 }
380 ping_thread_loop = 1;
381 ping_thread_error = 0;
382 status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL,
383 ping_thread, /* arg = */ (void *) 0);
384 if (status != 0)
385 {
386 ping_thread_loop = 0;
387 ERROR ("ping plugin: Starting thread failed.");
388 pthread_mutex_unlock (&ping_lock);
389 return (-1);
390 }
392 pthread_mutex_unlock (&ping_lock);
393 return (0);
394 } /* }}} int start_thread */
396 static int stop_thread (void) /* {{{ */
397 {
398 int status;
400 pthread_mutex_lock (&ping_lock);
402 if (ping_thread_loop == 0)
403 {
404 pthread_mutex_unlock (&ping_lock);
405 return (-1);
406 }
408 ping_thread_loop = 0;
409 pthread_cond_broadcast (&ping_cond);
410 pthread_mutex_unlock (&ping_lock);
412 status = pthread_join (ping_thread_id, /* return = */ NULL);
413 if (status != 0)
414 {
415 ERROR ("ping plugin: Stopping thread failed.");
416 status = -1;
417 }
419 pthread_mutex_lock (&ping_lock);
420 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
421 ping_thread_error = 0;
422 pthread_mutex_unlock (&ping_lock);
424 return (status);
425 } /* }}} int stop_thread */
427 static int ping_init (void) /* {{{ */
428 {
429 if (hostlist_head == NULL)
430 {
431 NOTICE ("ping plugin: No hosts have been configured.");
432 return (-1);
433 }
435 if (ping_timeout > ping_interval)
436 {
437 ping_timeout = 0.9 * ping_interval;
438 WARNING ("ping plugin: Timeout is greater than interval. "
439 "Will use a timeout of %gs.", ping_timeout);
440 }
442 return (start_thread ());
443 } /* }}} int ping_init */
445 static int config_set_string (const char *name, /* {{{ */
446 char **var, const char *value)
447 {
448 char *tmp;
450 tmp = strdup (value);
451 if (tmp == NULL)
452 {
453 char errbuf[1024];
454 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
455 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
456 return (1);
457 }
459 if (*var != NULL)
460 free (*var);
461 *var = tmp;
462 return (0);
463 } /* }}} int config_set_string */
465 static int ping_config (const char *key, const char *value) /* {{{ */
466 {
467 if (strcasecmp (key, "Host") == 0)
468 {
469 hostlist_t *hl;
470 char *host;
472 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
473 if (hl == NULL)
474 {
475 char errbuf[1024];
476 ERROR ("ping plugin: malloc failed: %s",
477 sstrerror (errno, errbuf, sizeof (errbuf)));
478 return (1);
479 }
481 host = strdup (value);
482 if (host == NULL)
483 {
484 char errbuf[1024];
485 sfree (hl);
486 ERROR ("ping plugin: strdup failed: %s",
487 sstrerror (errno, errbuf, sizeof (errbuf)));
488 return (1);
489 }
491 hl->host = host;
492 hl->pkg_sent = 0;
493 hl->pkg_recv = 0;
494 hl->pkg_missed = 0;
495 hl->latency_total = 0.0;
496 hl->latency_squared = 0.0;
497 hl->next = hostlist_head;
498 hostlist_head = hl;
499 }
500 else if (strcasecmp (key, "SourceAddress") == 0)
501 {
502 int status = config_set_string (key, &ping_source, value);
503 if (status != 0)
504 return (status);
505 }
506 #ifdef HAVE_OPING_1_3
507 else if (strcasecmp (key, "Device") == 0)
508 {
509 int status = config_set_string (key, &ping_device, value);
510 if (status != 0)
511 return (status);
512 }
513 #endif
514 else if (strcasecmp (key, "TTL") == 0)
515 {
516 int ttl = atoi (value);
517 if ((ttl > 0) && (ttl <= 255))
518 ping_ttl = ttl;
519 else
520 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
521 }
522 else if (strcasecmp (key, "Interval") == 0)
523 {
524 double tmp;
526 tmp = atof (value);
527 if (tmp > 0.0)
528 ping_interval = tmp;
529 else
530 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
531 tmp, value);
532 }
533 else if (strcasecmp (key, "Timeout") == 0)
534 {
535 double tmp;
537 tmp = atof (value);
538 if (tmp > 0.0)
539 ping_timeout = tmp;
540 else
541 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
542 tmp, value);
543 }
544 else if (strcasecmp (key, "MaxMissed") == 0)
545 {
546 ping_max_missed = atoi (value);
547 if (ping_max_missed < 0)
548 INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
549 }
550 else
551 {
552 return (-1);
553 }
555 return (0);
556 } /* }}} int ping_config */
558 static void submit (const char *host, const char *type, /* {{{ */
559 gauge_t value)
560 {
561 value_t values[1];
562 value_list_t vl = VALUE_LIST_INIT;
564 values[0].gauge = value;
566 vl.values = values;
567 vl.values_len = 1;
568 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
569 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
570 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
571 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
572 sstrncpy (vl.type, type, sizeof (vl.type));
574 plugin_dispatch_values (&vl);
575 } /* }}} void ping_submit */
577 static int ping_read (void) /* {{{ */
578 {
579 hostlist_t *hl;
581 if (ping_thread_error != 0)
582 {
583 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
585 stop_thread ();
587 for (hl = hostlist_head; hl != NULL; hl = hl->next)
588 {
589 hl->pkg_sent = 0;
590 hl->pkg_recv = 0;
591 hl->latency_total = 0.0;
592 hl->latency_squared = 0.0;
593 }
595 start_thread ();
597 return (-1);
598 } /* if (ping_thread_error != 0) */
600 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
601 {
602 uint32_t pkg_sent;
603 uint32_t pkg_recv;
604 double latency_total;
605 double latency_squared;
607 double latency_average;
608 double latency_stddev;
610 double droprate;
612 /* Locking here works, because the structure of the linked list is only
613 * changed during configure and shutdown. */
614 pthread_mutex_lock (&ping_lock);
616 pkg_sent = hl->pkg_sent;
617 pkg_recv = hl->pkg_recv;
618 latency_total = hl->latency_total;
619 latency_squared = hl->latency_squared;
621 hl->pkg_sent = 0;
622 hl->pkg_recv = 0;
623 hl->latency_total = 0.0;
624 hl->latency_squared = 0.0;
626 pthread_mutex_unlock (&ping_lock);
628 /* This e. g. happens when starting up. */
629 if (pkg_sent == 0)
630 {
631 DEBUG ("ping plugin: No packages for host %s have been sent.",
632 hl->host);
633 continue;
634 }
636 /* Calculate average. Beware of division by zero. */
637 if (pkg_recv == 0)
638 latency_average = NAN;
639 else
640 latency_average = latency_total / ((double) pkg_recv);
642 /* Calculate standard deviation. Beware even more of division by zero. */
643 if (pkg_recv == 0)
644 latency_stddev = NAN;
645 else if (pkg_recv == 1)
646 latency_stddev = 0.0;
647 else
648 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
649 - (latency_total * latency_total))
650 / ((double) (pkg_recv * (pkg_recv - 1))));
652 /* Calculate drop rate. */
653 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
655 submit (hl->host, "ping", latency_average);
656 submit (hl->host, "ping_stddev", latency_stddev);
657 submit (hl->host, "ping_droprate", droprate);
658 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
660 return (0);
661 } /* }}} int ping_read */
663 static int ping_shutdown (void) /* {{{ */
664 {
665 hostlist_t *hl;
667 INFO ("ping plugin: Shutting down thread.");
668 if (stop_thread () < 0)
669 return (-1);
671 hl = hostlist_head;
672 while (hl != NULL)
673 {
674 hostlist_t *hl_next;
676 hl_next = hl->next;
678 sfree (hl->host);
679 sfree (hl);
681 hl = hl_next;
682 }
684 return (0);
685 } /* }}} int ping_shutdown */
687 void module_register (void)
688 {
689 plugin_register_config ("ping", ping_config,
690 config_keys, config_keys_num);
691 plugin_register_init ("ping", ping_init);
692 plugin_register_read ("ping", ping_read);
693 plugin_register_shutdown ("ping", ping_shutdown);
694 } /* void module_register */
696 /* vim: set sw=2 sts=2 et fdm=marker : */