7eb229323627a1c4aab86b0017796bebb3b3c422
1 /**
2 * collectd - src/ping.c
3 * Copyright (C) 2005-2012 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "utils_complain.h"
33 #include <pthread.h>
34 #include <netinet/in.h>
35 #if HAVE_NETDB_H
36 # include <netdb.h> /* NI_MAXHOST */
37 #endif
39 #include <oping.h>
41 #ifndef NI_MAXHOST
42 # define NI_MAXHOST 1025
43 #endif
45 #if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
46 # define HAVE_OPING_1_3
47 #endif
49 /*
50 * Private data types
51 */
52 struct hostlist_s
53 {
54 char *host;
56 uint32_t pkg_sent;
57 uint32_t pkg_recv;
58 uint32_t pkg_missed;
60 double latency_total;
61 double latency_squared;
63 struct hostlist_s *next;
64 };
65 typedef struct hostlist_s hostlist_t;
67 /*
68 * Private variables
69 */
70 static hostlist_t *hostlist_head = NULL;
72 static char *ping_source = NULL;
73 #ifdef HAVE_OPING_1_3
74 static char *ping_device = NULL;
75 #endif
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[] =
89 {
90 "Host",
91 "SourceAddress",
92 #ifdef HAVE_OPING_1_3
93 "Device",
94 #endif
95 "Size",
96 "TTL",
97 "Interval",
98 "Timeout",
99 "MaxMissed"
100 };
101 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
103 /*
104 * Private functions
105 */
106 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
107 static void time_normalize (struct timespec *ts) /* {{{ */
108 {
109 while (ts->tv_nsec < 0)
110 {
111 if (ts->tv_sec == 0)
112 {
113 ts->tv_nsec = 0;
114 return;
115 }
117 ts->tv_sec -= 1;
118 ts->tv_nsec += 1000000000;
119 }
121 while (ts->tv_nsec >= 1000000000)
122 {
123 ts->tv_sec += 1;
124 ts->tv_nsec -= 1000000000;
125 }
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)
134 {
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)))
145 {
146 ts_dest->tv_sec = tv_end->tv_sec;
147 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
148 }
150 time_normalize (ts_dest);
151 } /* }}} void time_calc */
153 static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
154 {
155 pingobj_iter_t *iter;
156 hostlist_t *hl;
157 int status;
159 for (iter = ping_iterator_get (pingobj);
160 iter != NULL;
161 iter = ping_iterator_next (iter))
162 { /* {{{ */
163 char userhost[NI_MAXHOST];
164 double latency;
165 size_t param_size;
167 param_size = sizeof (userhost);
168 status = ping_iterator_get_info (iter,
169 #ifdef PING_INFO_USERNAME
170 PING_INFO_USERNAME,
171 #else
172 PING_INFO_HOSTNAME,
173 #endif
174 userhost, ¶m_size);
175 if (status != 0)
176 {
177 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
178 ping_get_error (pingobj));
179 continue;
180 }
182 for (hl = hostlist_head; hl != NULL; hl = hl->next)
183 if (strcmp (userhost, hl->host) == 0)
184 break;
186 if (hl == NULL)
187 {
188 WARNING ("ping plugin: Cannot find host %s.", userhost);
189 continue;
190 }
192 param_size = sizeof (latency);
193 status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
194 (void *) &latency, ¶m_size);
195 if (status != 0)
196 {
197 WARNING ("ping plugin: ping_iterator_get_info failed: %s",
198 ping_get_error (pingobj));
199 continue;
200 }
202 hl->pkg_sent++;
203 if (latency >= 0.0)
204 {
205 hl->pkg_recv++;
206 hl->latency_total += latency;
207 hl->latency_squared += (latency * latency);
209 /* reset missed packages counter */
210 hl->pkg_missed = 0;
211 } else
212 hl->pkg_missed++;
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)))
217 { /* {{{ */
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
220 * missed packages */
221 hl->pkg_missed = 0;
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
227 * ping object */
228 status = ping_host_remove (pingobj, hl->host);
229 if (status != 0)
230 {
231 WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
232 }
233 else
234 {
235 status = ping_host_add (pingobj, hl->host);
236 if (status != 0)
237 ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
238 }
239 } /* }}} ping_max_missed */
240 } /* }}} for (iter) */
242 return (0);
243 } /* }}} int ping_dispatch_all */
245 static void *ping_thread (void *arg) /* {{{ */
246 {
247 static pingobj_t *pingobj = NULL;
249 struct timeval tv_begin;
250 struct timeval tv_end;
251 struct timespec ts_wait;
252 struct timespec ts_int;
254 hostlist_t *hl;
255 int count;
257 c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
259 pthread_mutex_lock (&ping_lock);
261 pingobj = ping_construct ();
262 if (pingobj == NULL)
263 {
264 ERROR ("ping plugin: ping_construct failed.");
265 ping_thread_error = 1;
266 pthread_mutex_unlock (&ping_lock);
267 return ((void *) -1);
268 }
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));
280 #endif
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. */
289 count = 0;
290 for (hl = hostlist_head; hl != NULL; hl = hl->next)
291 {
292 int tmp_status;
293 tmp_status = ping_host_add (pingobj, hl->host);
294 if (tmp_status != 0)
295 WARNING ("ping plugin: ping_host_add (%s) failed: %s",
296 hl->host, ping_get_error (pingobj));
297 else
298 count++;
299 }
301 if (count == 0)
302 {
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);
307 }
309 /* Set up `ts_int' */
310 {
311 double temp_sec;
312 double temp_nsec;
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);
317 }
319 while (ping_thread_loop > 0)
320 {
321 int status;
322 _Bool send_successful = 0;
324 if (gettimeofday (&tv_begin, NULL) < 0)
325 {
326 char errbuf[1024];
327 ERROR ("ping plugin: gettimeofday failed: %s",
328 sstrerror (errno, errbuf, sizeof (errbuf)));
329 ping_thread_error = 1;
330 break;
331 }
333 pthread_mutex_unlock (&ping_lock);
335 status = ping_send (pingobj);
336 if (status < 0)
337 {
338 c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
339 ping_get_error (pingobj));
340 }
341 else
342 {
343 c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
344 send_successful = 1;
345 }
347 pthread_mutex_lock (&ping_lock);
349 if (ping_thread_loop <= 0)
350 break;
352 if (send_successful)
353 (void) ping_dispatch_all (pingobj);
355 if (gettimeofday (&tv_end, NULL) < 0)
356 {
357 char errbuf[1024];
358 ERROR ("ping plugin: gettimeofday failed: %s",
359 sstrerror (errno, errbuf, sizeof (errbuf)));
360 ping_thread_error = 1;
361 break;
362 }
364 /* Calculate the absolute time until which to wait and store it in
365 * `ts_wait'. */
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)
370 break;
371 } /* while (ping_thread_loop > 0) */
373 pthread_mutex_unlock (&ping_lock);
374 ping_destroy (pingobj);
376 return ((void *) 0);
377 } /* }}} void *ping_thread */
379 static int start_thread (void) /* {{{ */
380 {
381 int status;
383 pthread_mutex_lock (&ping_lock);
385 if (ping_thread_loop != 0)
386 {
387 pthread_mutex_unlock (&ping_lock);
388 return (-1);
389 }
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);
395 if (status != 0)
396 {
397 ping_thread_loop = 0;
398 ERROR ("ping plugin: Starting thread failed.");
399 pthread_mutex_unlock (&ping_lock);
400 return (-1);
401 }
403 pthread_mutex_unlock (&ping_lock);
404 return (0);
405 } /* }}} int start_thread */
407 static int stop_thread (void) /* {{{ */
408 {
409 int status;
411 pthread_mutex_lock (&ping_lock);
413 if (ping_thread_loop == 0)
414 {
415 pthread_mutex_unlock (&ping_lock);
416 return (-1);
417 }
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);
424 if (status != 0)
425 {
426 ERROR ("ping plugin: Stopping thread failed.");
427 status = -1;
428 }
430 memset (&ping_thread_id, 0, sizeof (ping_thread_id));
431 ping_thread_error = 0;
433 return (status);
434 } /* }}} int stop_thread */
436 static int ping_init (void) /* {{{ */
437 {
438 if (hostlist_head == NULL)
439 {
440 NOTICE ("ping plugin: No hosts have been configured.");
441 return (-1);
442 }
444 if (ping_timeout > ping_interval)
445 {
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);
449 }
451 if (start_thread () != 0)
452 return (-1);
454 return (0);
455 } /* }}} int ping_init */
457 static int config_set_string (const char *name, /* {{{ */
458 char **var, const char *value)
459 {
460 char *tmp;
462 tmp = strdup (value);
463 if (tmp == NULL)
464 {
465 char errbuf[1024];
466 ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
467 name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
468 return (1);
469 }
471 if (*var != NULL)
472 free (*var);
473 *var = tmp;
474 return (0);
475 } /* }}} int config_set_string */
477 static int ping_config (const char *key, const char *value) /* {{{ */
478 {
479 if (strcasecmp (key, "Host") == 0)
480 {
481 hostlist_t *hl;
482 char *host;
484 hl = (hostlist_t *) malloc (sizeof (hostlist_t));
485 if (hl == NULL)
486 {
487 char errbuf[1024];
488 ERROR ("ping plugin: malloc failed: %s",
489 sstrerror (errno, errbuf, sizeof (errbuf)));
490 return (1);
491 }
493 host = strdup (value);
494 if (host == NULL)
495 {
496 char errbuf[1024];
497 sfree (hl);
498 ERROR ("ping plugin: strdup failed: %s",
499 sstrerror (errno, errbuf, sizeof (errbuf)));
500 return (1);
501 }
503 hl->host = host;
504 hl->pkg_sent = 0;
505 hl->pkg_recv = 0;
506 hl->pkg_missed = 0;
507 hl->latency_total = 0.0;
508 hl->latency_squared = 0.0;
509 hl->next = hostlist_head;
510 hostlist_head = hl;
511 }
512 else if (strcasecmp (key, "SourceAddress") == 0)
513 {
514 int status = config_set_string (key, &ping_source, value);
515 if (status != 0)
516 return (status);
517 }
518 #ifdef HAVE_OPING_1_3
519 else if (strcasecmp (key, "Device") == 0)
520 {
521 int status = config_set_string (key, &ping_device, value);
522 if (status != 0)
523 return (status);
524 }
525 #endif
526 else if (strcasecmp (key, "TTL") == 0)
527 {
528 int ttl = atoi (value);
529 if ((ttl > 0) && (ttl <= 255))
530 ping_ttl = ttl;
531 else
532 WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
533 }
534 else if (strcasecmp (key, "Interval") == 0)
535 {
536 double tmp;
538 tmp = atof (value);
539 if (tmp > 0.0)
540 ping_interval = tmp;
541 else
542 WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
543 tmp, value);
544 }
545 else if (strcasecmp (key, "Size") == 0) {
546 int size;
548 if (ping_data != NULL)
549 {
550 free(ping_data);
551 ping_data = NULL;
552 }
554 size = atoi (value);
555 if ((size >= 1) && (size <= 65536))
556 {
557 int i;
558 ping_data = (char*) malloc(size + 1);
559 if (ping_data == NULL)
560 {
561 char errbuf[1024];
562 ERROR ("ping plugin: malloc failed: %s",
563 sstrerror (errno, errbuf, sizeof (errbuf)));
564 return (1);
565 }
566 /* Note: By default oping is using constant string
567 * "liboping -- ICMP ping library <http://octo.it/liboping/>"
568 * which is exactly 56 bytes.
569 *
570 * Optimally we would follow the ping(1) behaviour, but we
571 * cannot use byte 00 or start data payload at exactly same
572 * location, due to oping library limitations. */
573 for (i = 0; i < size; i++) /* {{{ */
574 {
575 /* This restricts data pattern to be only composed of easily
576 * printable characters, and not NUL character. */
577 ping_data[i] = ('0' + i % 64);
578 } /* }}} for (i = 0; i < size; i++) */
579 ping_data[size] = '\0';
580 } else
581 WARNING ("ping plugin: Ignoring invalid Size %i.", size);
582 }
583 else if (strcasecmp (key, "Timeout") == 0)
584 {
585 double tmp;
587 tmp = atof (value);
588 if (tmp > 0.0)
589 ping_timeout = tmp;
590 else
591 WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
592 tmp, value);
593 }
594 else if (strcasecmp (key, "MaxMissed") == 0)
595 {
596 ping_max_missed = atoi (value);
597 if (ping_max_missed < 0)
598 INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
599 }
600 else
601 {
602 return (-1);
603 }
605 return (0);
606 } /* }}} int ping_config */
608 static void submit (const char *host, const char *type, /* {{{ */
609 gauge_t value)
610 {
611 value_t values[1];
612 value_list_t vl = VALUE_LIST_INIT;
614 values[0].gauge = value;
616 vl.values = values;
617 vl.values_len = 1;
618 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
619 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
620 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
621 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
622 sstrncpy (vl.type, type, sizeof (vl.type));
624 plugin_dispatch_values (&vl);
625 } /* }}} void ping_submit */
627 static int ping_read (void) /* {{{ */
628 {
629 hostlist_t *hl;
631 if (ping_thread_error != 0)
632 {
633 ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
635 stop_thread ();
637 for (hl = hostlist_head; hl != NULL; hl = hl->next)
638 {
639 hl->pkg_sent = 0;
640 hl->pkg_recv = 0;
641 hl->latency_total = 0.0;
642 hl->latency_squared = 0.0;
643 }
645 start_thread ();
647 return (-1);
648 } /* if (ping_thread_error != 0) */
650 for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
651 {
652 uint32_t pkg_sent;
653 uint32_t pkg_recv;
654 double latency_total;
655 double latency_squared;
657 double latency_average;
658 double latency_stddev;
660 double droprate;
662 /* Locking here works, because the structure of the linked list is only
663 * changed during configure and shutdown. */
664 pthread_mutex_lock (&ping_lock);
666 pkg_sent = hl->pkg_sent;
667 pkg_recv = hl->pkg_recv;
668 latency_total = hl->latency_total;
669 latency_squared = hl->latency_squared;
671 hl->pkg_sent = 0;
672 hl->pkg_recv = 0;
673 hl->latency_total = 0.0;
674 hl->latency_squared = 0.0;
676 pthread_mutex_unlock (&ping_lock);
678 /* This e. g. happens when starting up. */
679 if (pkg_sent == 0)
680 {
681 DEBUG ("ping plugin: No packages for host %s have been sent.",
682 hl->host);
683 continue;
684 }
686 /* Calculate average. Beware of division by zero. */
687 if (pkg_recv == 0)
688 latency_average = NAN;
689 else
690 latency_average = latency_total / ((double) pkg_recv);
692 /* Calculate standard deviation. Beware even more of division by zero. */
693 if (pkg_recv == 0)
694 latency_stddev = NAN;
695 else if (pkg_recv == 1)
696 latency_stddev = 0.0;
697 else
698 latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
699 - (latency_total * latency_total))
700 / ((double) (pkg_recv * (pkg_recv - 1))));
702 /* Calculate drop rate. */
703 droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
705 submit (hl->host, "ping", latency_average);
706 submit (hl->host, "ping_stddev", latency_stddev);
707 submit (hl->host, "ping_droprate", droprate);
708 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
710 return (0);
711 } /* }}} int ping_read */
713 static int ping_shutdown (void) /* {{{ */
714 {
715 hostlist_t *hl;
717 INFO ("ping plugin: Shutting down thread.");
718 if (stop_thread () < 0)
719 return (-1);
721 hl = hostlist_head;
722 while (hl != NULL)
723 {
724 hostlist_t *hl_next;
726 hl_next = hl->next;
728 sfree (hl->host);
729 sfree (hl);
731 hl = hl_next;
732 }
734 if (ping_data != NULL) {
735 free (ping_data);
736 ping_data = NULL;
737 }
739 return (0);
740 } /* }}} int ping_shutdown */
742 void module_register (void)
743 {
744 plugin_register_config ("ping", ping_config,
745 config_keys, config_keys_num);
746 plugin_register_init ("ping", ping_init);
747 plugin_register_read ("ping", ping_read);
748 plugin_register_shutdown ("ping", ping_shutdown);
749 } /* void module_register */
751 /* vim: set sw=2 sts=2 et fdm=marker : */