Merge pull request #3339 from jkohen/patch-1
[collectd.git] / src / ping.c
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  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "utils_complain.h"
32
33 #include <pthread.h>
34 #include <netinet/in.h>
35 #if HAVE_NETDB_H
36 # include <netdb.h> /* NI_MAXHOST */
37 #endif
38
39 #include <oping.h>
40
41 #ifndef NI_MAXHOST
42 # define NI_MAXHOST 1025
43 #endif
44
45 #if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
46 # define HAVE_OPING_1_3
47 #endif
48
49 /*
50  * Private data types
51  */
52 struct hostlist_s
53 {
54   char *host;
55
56   uint32_t pkg_sent;
57   uint32_t pkg_recv;
58   uint32_t pkg_missed;
59
60   double latency_total;
61   double latency_squared;
62
63   struct hostlist_s *next;
64 };
65 typedef struct hostlist_s hostlist_t;
66
67 /*
68  * Private variables
69  */
70 static hostlist_t *hostlist_head = NULL;
71
72 static char  *ping_source = NULL;
73 #ifdef HAVE_OPING_1_3
74 static char  *ping_device = NULL;
75 #endif
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;
80
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;
86
87 static const char *config_keys[] =
88 {
89   "Host",
90   "SourceAddress",
91 #ifdef HAVE_OPING_1_3
92   "Device",
93 #endif
94   "TTL",
95   "Interval",
96   "Timeout",
97   "MaxMissed"
98 };
99 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
100
101 /*
102  * Private functions
103  */
104 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
105 static void time_normalize (struct timespec *ts) /* {{{ */
106 {
107   while (ts->tv_nsec < 0)
108   {
109     if (ts->tv_sec == 0)
110     {
111       ts->tv_nsec = 0;
112       return;
113     }
114
115     ts->tv_sec  -= 1;
116     ts->tv_nsec += 1000000000;
117   }
118
119   while (ts->tv_nsec >= 1000000000)
120   {
121     ts->tv_sec  += 1;
122     ts->tv_nsec -= 1000000000;
123   }
124 } /* }}} void time_normalize */
125
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)
132 {
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);
136
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)))
143   {
144     ts_dest->tv_sec = tv_end->tv_sec;
145     ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
146   }
147
148   time_normalize (ts_dest);
149 } /* }}} void time_calc */
150
151 static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
152 {
153   pingobj_iter_t *iter;
154   hostlist_t *hl;
155   int status;
156
157   for (iter = ping_iterator_get (pingobj);
158       iter != NULL;
159       iter = ping_iterator_next (iter))
160   { /* {{{ */
161     char userhost[NI_MAXHOST];
162     double latency;
163     size_t param_size;
164
165     param_size = sizeof (userhost);
166     status = ping_iterator_get_info (iter,
167 #ifdef PING_INFO_USERNAME
168         PING_INFO_USERNAME,
169 #else
170         PING_INFO_HOSTNAME,
171 #endif
172         userhost, &param_size);
173     if (status != 0)
174     {
175       WARNING ("ping plugin: ping_iterator_get_info failed: %s",
176           ping_get_error (pingobj));
177       continue;
178     }
179
180     for (hl = hostlist_head; hl != NULL; hl = hl->next)
181       if (strcmp (userhost, hl->host) == 0)
182         break;
183
184     if (hl == NULL)
185     {
186       WARNING ("ping plugin: Cannot find host %s.", userhost);
187       continue;
188     }
189
190     param_size = sizeof (latency);
191     status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
192         (void *) &latency, &param_size);
193     if (status != 0)
194     {
195       WARNING ("ping plugin: ping_iterator_get_info failed: %s",
196           ping_get_error (pingobj));
197       continue;
198     }
199
200     hl->pkg_sent++;
201     if (latency >= 0.0)
202     {
203       hl->pkg_recv++;
204       hl->latency_total += latency;
205       hl->latency_squared += (latency * latency);
206
207       /* reset missed packages counter */
208       hl->pkg_missed = 0;
209     } else
210       hl->pkg_missed++;
211
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)))
215     { /* {{{ */
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
218        * missed packages */
219       hl->pkg_missed = 0;
220
221       WARNING ("ping plugin: host %s has not answered %d PING requests,"
222           " triggering resolve", hl->host, ping_max_missed);
223
224       /* we trigger the resolv simply be removeing and adding the host to our
225        * ping object */
226       status = ping_host_remove (pingobj, hl->host);
227       if (status != 0)
228       {
229         WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
230       }
231       else
232       {
233         status = ping_host_add (pingobj, hl->host);
234         if (status != 0)
235           ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
236       }
237     } /* }}} ping_max_missed */
238   } /* }}} for (iter) */
239
240   return (0);
241 } /* }}} int ping_dispatch_all */
242
243 static void *ping_thread (void *arg) /* {{{ */
244 {
245   static pingobj_t *pingobj = NULL;
246
247   struct timeval  tv_begin;
248   struct timeval  tv_end;
249   struct timespec ts_wait;
250   struct timespec ts_int;
251
252   hostlist_t *hl;
253   int count;
254
255   c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
256
257   pthread_mutex_lock (&ping_lock);
258
259   pingobj = ping_construct ();
260   if (pingobj == NULL)
261   {
262     ERROR ("ping plugin: ping_construct failed.");
263     ping_thread_error = 1;
264     pthread_mutex_unlock (&ping_lock);
265     return ((void *) -1);
266   }
267
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));
272
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));
278 #endif
279
280   ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
281   ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
282
283   /* Add all the hosts to the ping object. */
284   count = 0;
285   for (hl = hostlist_head; hl != NULL; hl = hl->next)
286   {
287     int tmp_status;
288     tmp_status = ping_host_add (pingobj, hl->host);
289     if (tmp_status != 0)
290       WARNING ("ping plugin: ping_host_add (%s) failed: %s",
291           hl->host, ping_get_error (pingobj));
292     else
293       count++;
294   }
295
296   if (count == 0)
297   {
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);
302   }
303
304   /* Set up `ts_int' */
305   {
306     double temp_sec;
307     double temp_nsec;
308
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);
312   }
313
314   while (ping_thread_loop > 0)
315   {
316     int status;
317     _Bool send_successful = 0;
318
319     if (gettimeofday (&tv_begin, NULL) < 0)
320     {
321       char errbuf[1024];
322       ERROR ("ping plugin: gettimeofday failed: %s",
323           sstrerror (errno, errbuf, sizeof (errbuf)));
324       ping_thread_error = 1;
325       break;
326     }
327
328     pthread_mutex_unlock (&ping_lock);
329
330     status = ping_send (pingobj);
331     if (status < 0)
332     {
333       c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
334           ping_get_error (pingobj));
335     }
336     else
337     {
338       c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
339       send_successful = 1;
340     }
341
342     pthread_mutex_lock (&ping_lock);
343
344     if (ping_thread_loop <= 0)
345       break;
346
347     if (send_successful)
348       (void) ping_dispatch_all (pingobj);
349
350     if (gettimeofday (&tv_end, NULL) < 0)
351     {
352       char errbuf[1024];
353       ERROR ("ping plugin: gettimeofday failed: %s",
354           sstrerror (errno, errbuf, sizeof (errbuf)));
355       ping_thread_error = 1;
356       break;
357     }
358
359     /* Calculate the absolute time until which to wait and store it in
360      * `ts_wait'. */
361     time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
362
363     pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
364     if (ping_thread_loop <= 0)
365       break;
366   } /* while (ping_thread_loop > 0) */
367
368   pthread_mutex_unlock (&ping_lock);
369   ping_destroy (pingobj);
370
371   return ((void *) 0);
372 } /* }}} void *ping_thread */
373
374 static int start_thread (void) /* {{{ */
375 {
376   int status;
377
378   pthread_mutex_lock (&ping_lock);
379
380   if (ping_thread_loop != 0)
381   {
382     pthread_mutex_unlock (&ping_lock);
383     return (-1);
384   }
385
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);
390   if (status != 0)
391   {
392     ping_thread_loop = 0;
393     ERROR ("ping plugin: Starting thread failed.");
394     pthread_mutex_unlock (&ping_lock);
395     return (-1);
396   }
397     
398   pthread_mutex_unlock (&ping_lock);
399   return (0);
400 } /* }}} int start_thread */
401
402 static int stop_thread (void) /* {{{ */
403 {
404   int status;
405
406   pthread_mutex_lock (&ping_lock);
407
408   if (ping_thread_loop == 0)
409   {
410     pthread_mutex_unlock (&ping_lock);
411     return (-1);
412   }
413
414   ping_thread_loop = 0;
415   pthread_cond_broadcast (&ping_cond);
416   pthread_mutex_unlock (&ping_lock);
417
418   status = pthread_join (ping_thread_id, /* return = */ NULL);
419   if (status != 0)
420   {
421     ERROR ("ping plugin: Stopping thread failed.");
422     status = -1;
423   }
424
425   memset (&ping_thread_id, 0, sizeof (ping_thread_id));
426   ping_thread_error = 0;
427
428   return (status);
429 } /* }}} int stop_thread */
430
431 static int ping_init (void) /* {{{ */
432 {
433   if (hostlist_head == NULL)
434   {
435     NOTICE ("ping plugin: No hosts have been configured.");
436     return (-1);
437   }
438
439   if (ping_timeout > ping_interval)
440   {
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);
444   }
445
446   if (start_thread () != 0)
447     return (-1);
448
449   return (0);
450 } /* }}} int ping_init */
451
452 static int config_set_string (const char *name, /* {{{ */
453     char **var, const char *value)
454 {
455   char *tmp;
456
457   tmp = strdup (value);
458   if (tmp == NULL)
459   {
460     char errbuf[1024];
461     ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
462         name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
463     return (1);
464   }
465
466   if (*var != NULL)
467     free (*var);
468   *var = tmp;
469   return (0);
470 } /* }}} int config_set_string */
471
472 static int ping_config (const char *key, const char *value) /* {{{ */
473 {
474   if (strcasecmp (key, "Host") == 0)
475   {
476     hostlist_t *hl;
477     char *host;
478
479     hl = (hostlist_t *) malloc (sizeof (hostlist_t));
480     if (hl == NULL)
481     {
482       char errbuf[1024];
483       ERROR ("ping plugin: malloc failed: %s",
484           sstrerror (errno, errbuf, sizeof (errbuf)));
485       return (1);
486     }
487
488     host = strdup (value);
489     if (host == NULL)
490     {
491       char errbuf[1024];
492       sfree (hl);
493       ERROR ("ping plugin: strdup failed: %s",
494           sstrerror (errno, errbuf, sizeof (errbuf)));
495       return (1);
496     }
497
498     hl->host = host;
499     hl->pkg_sent = 0;
500     hl->pkg_recv = 0;
501     hl->pkg_missed = 0;
502     hl->latency_total = 0.0;
503     hl->latency_squared = 0.0;
504     hl->next = hostlist_head;
505     hostlist_head = hl;
506   }
507   else if (strcasecmp (key, "SourceAddress") == 0)
508   {
509     int status = config_set_string (key, &ping_source, value);
510     if (status != 0)
511       return (status);
512   }
513 #ifdef HAVE_OPING_1_3
514   else if (strcasecmp (key, "Device") == 0)
515   {
516     int status = config_set_string (key, &ping_device, value);
517     if (status != 0)
518       return (status);
519   }
520 #endif
521   else if (strcasecmp (key, "TTL") == 0)
522   {
523     int ttl = atoi (value);
524     if ((ttl > 0) && (ttl <= 255))
525       ping_ttl = ttl;
526     else
527       WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
528   }
529   else if (strcasecmp (key, "Interval") == 0)
530   {
531     double tmp;
532
533     tmp = atof (value);
534     if (tmp > 0.0)
535       ping_interval = tmp;
536     else
537       WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
538           tmp, value);
539   }
540   else if (strcasecmp (key, "Timeout") == 0)
541   {
542     double tmp;
543
544     tmp = atof (value);
545     if (tmp > 0.0)
546       ping_timeout = tmp;
547     else
548       WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
549           tmp, value);
550   }
551   else if (strcasecmp (key, "MaxMissed") == 0)
552   {
553     ping_max_missed = atoi (value);
554     if (ping_max_missed < 0)
555       INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
556   }
557   else
558   {
559     return (-1);
560   }
561
562   return (0);
563 } /* }}} int ping_config */
564
565 static void submit (const char *host, const char *type, /* {{{ */
566     gauge_t value)
567 {
568   value_t values[1];
569   value_list_t vl = VALUE_LIST_INIT;
570
571   values[0].gauge = value;
572
573   vl.values = values;
574   vl.values_len = 1;
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));
580
581   plugin_dispatch_values (&vl);
582 } /* }}} void ping_submit */
583
584 static int ping_read (void) /* {{{ */
585 {
586   hostlist_t *hl;
587
588   if (ping_thread_error != 0)
589   {
590     ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
591
592     stop_thread ();
593
594     for (hl = hostlist_head; hl != NULL; hl = hl->next)
595     {
596       hl->pkg_sent = 0;
597       hl->pkg_recv = 0;
598       hl->latency_total = 0.0;
599       hl->latency_squared = 0.0;
600     }
601
602     start_thread ();
603
604     return (-1);
605   } /* if (ping_thread_error != 0) */
606
607   for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
608   {
609     uint32_t pkg_sent;
610     uint32_t pkg_recv;
611     double latency_total;
612     double latency_squared;
613
614     double latency_average;
615     double latency_stddev;
616
617     double droprate;
618
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);
622
623     pkg_sent = hl->pkg_sent;
624     pkg_recv = hl->pkg_recv;
625     latency_total = hl->latency_total;
626     latency_squared = hl->latency_squared;
627
628     hl->pkg_sent = 0;
629     hl->pkg_recv = 0;
630     hl->latency_total = 0.0;
631     hl->latency_squared = 0.0;
632
633     pthread_mutex_unlock (&ping_lock);
634
635     /* This e. g. happens when starting up. */
636     if (pkg_sent == 0)
637     {
638       DEBUG ("ping plugin: No packages for host %s have been sent.",
639           hl->host);
640       continue;
641     }
642
643     /* Calculate average. Beware of division by zero. */
644     if (pkg_recv == 0)
645       latency_average = NAN;
646     else
647       latency_average = latency_total / ((double) pkg_recv);
648
649     /* Calculate standard deviation. Beware even more of division by zero. */
650     if (pkg_recv == 0)
651       latency_stddev = NAN;
652     else if (pkg_recv == 1)
653       latency_stddev = 0.0;
654     else
655       latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
656           - (latency_total * latency_total))
657           / ((double) (pkg_recv * (pkg_recv - 1))));
658
659     /* Calculate drop rate. */
660     droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
661
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) */
666
667   return (0);
668 } /* }}} int ping_read */
669
670 static int ping_shutdown (void) /* {{{ */
671 {
672   hostlist_t *hl;
673
674   INFO ("ping plugin: Shutting down thread.");
675   if (stop_thread () < 0)
676     return (-1);
677
678   hl = hostlist_head;
679   while (hl != NULL)
680   {
681     hostlist_t *hl_next;
682
683     hl_next = hl->next;
684
685     sfree (hl->host);
686     sfree (hl);
687
688     hl = hl_next;
689   }
690
691   return (0);
692 } /* }}} int ping_shutdown */
693
694 void module_register (void)
695 {
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 */
702
703 /* vim: set sw=2 sts=2 et fdm=marker : */