Merge branch 'collectd-5.5'
[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 <netinet/in.h>
34 #if HAVE_NETDB_H
35 # include <netdb.h> /* NI_MAXHOST */
36 #endif
37
38 #include <oping.h>
39
40 #ifndef NI_MAXHOST
41 # define NI_MAXHOST 1025
42 #endif
43
44 #if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
45 # define HAVE_OPING_1_3
46 #endif
47
48 /*
49  * Private data types
50  */
51 struct hostlist_s
52 {
53   char *host;
54
55   uint32_t pkg_sent;
56   uint32_t pkg_recv;
57   uint32_t pkg_missed;
58
59   double latency_total;
60   double latency_squared;
61
62   struct hostlist_s *next;
63 };
64 typedef struct hostlist_s hostlist_t;
65
66 /*
67  * Private variables
68  */
69 static hostlist_t *hostlist_head = NULL;
70
71 static char  *ping_source = NULL;
72 #ifdef HAVE_OPING_1_3
73 static char  *ping_device = NULL;
74 #endif
75 static char  *ping_data = 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;
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   "Size",
95   "TTL",
96   "Interval",
97   "Timeout",
98   "MaxMissed"
99 };
100 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
101
102 /*
103  * Private functions
104  */
105 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
106 static void time_normalize (struct timespec *ts) /* {{{ */
107 {
108   while (ts->tv_nsec < 0)
109   {
110     if (ts->tv_sec == 0)
111     {
112       ts->tv_nsec = 0;
113       return;
114     }
115
116     ts->tv_sec  -= 1;
117     ts->tv_nsec += 1000000000;
118   }
119
120   while (ts->tv_nsec >= 1000000000)
121   {
122     ts->tv_sec  += 1;
123     ts->tv_nsec -= 1000000000;
124   }
125 } /* }}} void time_normalize */
126
127 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
128  * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
129 static void time_calc (struct timespec *ts_dest, /* {{{ */
130     const struct timespec *ts_int,
131     const struct timeval  *tv_begin,
132     const struct timeval  *tv_end)
133 {
134   ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
135   ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
136   time_normalize (ts_dest);
137
138   /* Assure that `(begin + interval) > end'.
139    * This may seem overly complicated, but `tv_sec' is of type `time_t'
140    * which may be `unsigned. *sigh* */
141   if ((tv_end->tv_sec > ts_dest->tv_sec)
142       || ((tv_end->tv_sec == ts_dest->tv_sec)
143         && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
144   {
145     ts_dest->tv_sec = tv_end->tv_sec;
146     ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
147   }
148
149   time_normalize (ts_dest);
150 } /* }}} void time_calc */
151
152 static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
153 {
154   pingobj_iter_t *iter;
155   hostlist_t *hl;
156   int status;
157
158   for (iter = ping_iterator_get (pingobj);
159       iter != NULL;
160       iter = ping_iterator_next (iter))
161   { /* {{{ */
162     char userhost[NI_MAXHOST];
163     double latency;
164     size_t param_size;
165
166     param_size = sizeof (userhost);
167     status = ping_iterator_get_info (iter,
168 #ifdef PING_INFO_USERNAME
169         PING_INFO_USERNAME,
170 #else
171         PING_INFO_HOSTNAME,
172 #endif
173         userhost, &param_size);
174     if (status != 0)
175     {
176       WARNING ("ping plugin: ping_iterator_get_info failed: %s",
177           ping_get_error (pingobj));
178       continue;
179     }
180
181     for (hl = hostlist_head; hl != NULL; hl = hl->next)
182       if (strcmp (userhost, hl->host) == 0)
183         break;
184
185     if (hl == NULL)
186     {
187       WARNING ("ping plugin: Cannot find host %s.", userhost);
188       continue;
189     }
190
191     param_size = sizeof (latency);
192     status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
193         (void *) &latency, &param_size);
194     if (status != 0)
195     {
196       WARNING ("ping plugin: ping_iterator_get_info failed: %s",
197           ping_get_error (pingobj));
198       continue;
199     }
200
201     hl->pkg_sent++;
202     if (latency >= 0.0)
203     {
204       hl->pkg_recv++;
205       hl->latency_total += latency;
206       hl->latency_squared += (latency * latency);
207
208       /* reset missed packages counter */
209       hl->pkg_missed = 0;
210     } else
211       hl->pkg_missed++;
212
213     /* if the host did not answer our last N packages, trigger a resolv. */
214     if ((ping_max_missed >= 0)
215         && (hl->pkg_missed >= ((uint32_t) ping_max_missed)))
216     { /* {{{ */
217       /* we reset the missed package counter here, since we only want to
218        * trigger a resolv every N packages and not every package _AFTER_ N
219        * missed packages */
220       hl->pkg_missed = 0;
221
222       WARNING ("ping plugin: host %s has not answered %d PING requests,"
223           " triggering resolve", hl->host, ping_max_missed);
224
225       /* we trigger the resolv simply be removeing and adding the host to our
226        * ping object */
227       status = ping_host_remove (pingobj, hl->host);
228       if (status != 0)
229       {
230         WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
231       }
232       else
233       {
234         status = ping_host_add (pingobj, hl->host);
235         if (status != 0)
236           ERROR ("ping plugin: ping_host_add (%s) failed.", hl->host);
237       }
238     } /* }}} ping_max_missed */
239   } /* }}} for (iter) */
240
241   return (0);
242 } /* }}} int ping_dispatch_all */
243
244 static void *ping_thread (void *arg) /* {{{ */
245 {
246   pingobj_t *pingobj = NULL;
247
248   struct timeval  tv_begin;
249   struct timeval  tv_end;
250   struct timespec ts_wait;
251   struct timespec ts_int;
252
253   hostlist_t *hl;
254   int count;
255
256   c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
257
258   pthread_mutex_lock (&ping_lock);
259
260   pingobj = ping_construct ();
261   if (pingobj == NULL)
262   {
263     ERROR ("ping plugin: ping_construct failed.");
264     ping_thread_error = 1;
265     pthread_mutex_unlock (&ping_lock);
266     return ((void *) -1);
267   }
268
269   if (ping_source != NULL)
270     if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
271       ERROR ("ping plugin: Failed to set source address: %s",
272           ping_get_error (pingobj));
273
274 #ifdef HAVE_OPING_1_3
275   if (ping_device != NULL)
276     if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
277       ERROR ("ping plugin: Failed to set device: %s",
278           ping_get_error (pingobj));
279 #endif
280
281   ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
282   ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
283
284   if (ping_data != NULL)
285     ping_setopt (pingobj, PING_OPT_DATA, (void *) ping_data);
286
287   /* Add all the hosts to the ping object. */
288   count = 0;
289   for (hl = hostlist_head; hl != NULL; hl = hl->next)
290   {
291     int tmp_status;
292     tmp_status = ping_host_add (pingobj, hl->host);
293     if (tmp_status != 0)
294       WARNING ("ping plugin: ping_host_add (%s) failed: %s",
295           hl->host, ping_get_error (pingobj));
296     else
297       count++;
298   }
299
300   if (count == 0)
301   {
302     ERROR ("ping plugin: No host could be added to ping object. Giving up.");
303     ping_thread_error = 1;
304     pthread_mutex_unlock (&ping_lock);
305     return ((void *) -1);
306   }
307
308   /* Set up `ts_int' */
309   {
310     double temp_sec;
311     double temp_nsec;
312
313     temp_nsec = modf (ping_interval, &temp_sec);
314     ts_int.tv_sec  = (time_t) temp_sec;
315     ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
316   }
317
318   while (ping_thread_loop > 0)
319   {
320     int status;
321     _Bool send_successful = 0;
322
323     if (gettimeofday (&tv_begin, NULL) < 0)
324     {
325       char errbuf[1024];
326       ERROR ("ping plugin: gettimeofday failed: %s",
327           sstrerror (errno, errbuf, sizeof (errbuf)));
328       ping_thread_error = 1;
329       break;
330     }
331
332     pthread_mutex_unlock (&ping_lock);
333
334     status = ping_send (pingobj);
335     if (status < 0)
336     {
337       c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
338           ping_get_error (pingobj));
339     }
340     else
341     {
342       c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
343       send_successful = 1;
344     }
345
346     pthread_mutex_lock (&ping_lock);
347
348     if (ping_thread_loop <= 0)
349       break;
350
351     if (send_successful)
352       (void) ping_dispatch_all (pingobj);
353
354     if (gettimeofday (&tv_end, NULL) < 0)
355     {
356       char errbuf[1024];
357       ERROR ("ping plugin: gettimeofday failed: %s",
358           sstrerror (errno, errbuf, sizeof (errbuf)));
359       ping_thread_error = 1;
360       break;
361     }
362
363     /* Calculate the absolute time until which to wait and store it in
364      * `ts_wait'. */
365     time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
366
367     pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
368     if (ping_thread_loop <= 0)
369       break;
370   } /* while (ping_thread_loop > 0) */
371
372   pthread_mutex_unlock (&ping_lock);
373   ping_destroy (pingobj);
374
375   return ((void *) 0);
376 } /* }}} void *ping_thread */
377
378 static int start_thread (void) /* {{{ */
379 {
380   int status;
381
382   pthread_mutex_lock (&ping_lock);
383
384   if (ping_thread_loop != 0)
385   {
386     pthread_mutex_unlock (&ping_lock);
387     return (-1);
388   }
389
390   ping_thread_loop = 1;
391   ping_thread_error = 0;
392   status = plugin_thread_create (&ping_thread_id, /* attr = */ NULL,
393       ping_thread, /* arg = */ (void *) 0);
394   if (status != 0)
395   {
396     ping_thread_loop = 0;
397     ERROR ("ping plugin: Starting thread failed.");
398     pthread_mutex_unlock (&ping_lock);
399     return (-1);
400   }
401
402   pthread_mutex_unlock (&ping_lock);
403   return (0);
404 } /* }}} int start_thread */
405
406 static int stop_thread (void) /* {{{ */
407 {
408   int status;
409
410   pthread_mutex_lock (&ping_lock);
411
412   if (ping_thread_loop == 0)
413   {
414     pthread_mutex_unlock (&ping_lock);
415     return (-1);
416   }
417
418   ping_thread_loop = 0;
419   pthread_cond_broadcast (&ping_cond);
420   pthread_mutex_unlock (&ping_lock);
421
422   status = pthread_join (ping_thread_id, /* return = */ NULL);
423   if (status != 0)
424   {
425     ERROR ("ping plugin: Stopping thread failed.");
426     status = -1;
427   }
428
429   pthread_mutex_lock (&ping_lock);
430   memset (&ping_thread_id, 0, sizeof (ping_thread_id));
431   ping_thread_error = 0;
432   pthread_mutex_unlock (&ping_lock);
433
434   return (status);
435 } /* }}} int stop_thread */
436
437 static int ping_init (void) /* {{{ */
438 {
439   if (hostlist_head == NULL)
440   {
441     NOTICE ("ping plugin: No hosts have been configured.");
442     return (-1);
443   }
444
445   if (ping_timeout > ping_interval)
446   {
447     ping_timeout = 0.9 * ping_interval;
448     WARNING ("ping plugin: Timeout is greater than interval. "
449         "Will use a timeout of %gs.", ping_timeout);
450   }
451
452   if (start_thread () != 0)
453     return (-1);
454
455   return (0);
456 } /* }}} int ping_init */
457
458 static int config_set_string (const char *name, /* {{{ */
459     char **var, const char *value)
460 {
461   char *tmp;
462
463   tmp = strdup (value);
464   if (tmp == NULL)
465   {
466     char errbuf[1024];
467     ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
468         name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
469     return (1);
470   }
471
472   if (*var != NULL)
473     free (*var);
474   *var = tmp;
475   return (0);
476 } /* }}} int config_set_string */
477
478 static int ping_config (const char *key, const char *value) /* {{{ */
479 {
480   if (strcasecmp (key, "Host") == 0)
481   {
482     hostlist_t *hl;
483     char *host;
484
485     hl = malloc (sizeof (*hl));
486     if (hl == NULL)
487     {
488       char errbuf[1024];
489       ERROR ("ping plugin: malloc failed: %s",
490           sstrerror (errno, errbuf, sizeof (errbuf)));
491       return (1);
492     }
493
494     host = strdup (value);
495     if (host == NULL)
496     {
497       char errbuf[1024];
498       sfree (hl);
499       ERROR ("ping plugin: strdup failed: %s",
500           sstrerror (errno, errbuf, sizeof (errbuf)));
501       return (1);
502     }
503
504     hl->host = host;
505     hl->pkg_sent = 0;
506     hl->pkg_recv = 0;
507     hl->pkg_missed = 0;
508     hl->latency_total = 0.0;
509     hl->latency_squared = 0.0;
510     hl->next = hostlist_head;
511     hostlist_head = hl;
512   }
513   else if (strcasecmp (key, "SourceAddress") == 0)
514   {
515     int status = config_set_string (key, &ping_source, value);
516     if (status != 0)
517       return (status);
518   }
519 #ifdef HAVE_OPING_1_3
520   else if (strcasecmp (key, "Device") == 0)
521   {
522     int status = config_set_string (key, &ping_device, value);
523     if (status != 0)
524       return (status);
525   }
526 #endif
527   else if (strcasecmp (key, "TTL") == 0)
528   {
529     int ttl = atoi (value);
530     if ((ttl > 0) && (ttl <= 255))
531       ping_ttl = ttl;
532     else
533       WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
534   }
535   else if (strcasecmp (key, "Interval") == 0)
536   {
537     double tmp;
538
539     tmp = atof (value);
540     if (tmp > 0.0)
541       ping_interval = tmp;
542     else
543       WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
544           tmp, value);
545   }
546   else if (strcasecmp (key, "Size") == 0) {
547     size_t size = (size_t) atoi (value);
548
549     /* Max IP packet size - (IPv6 + ICMP) = 65535 - (40 + 8) = 65487 */
550     if (size <= 65487)
551     {
552       size_t i;
553
554       sfree (ping_data);
555       ping_data = malloc (size + 1);
556       if (ping_data == NULL)
557       {
558         ERROR ("ping plugin: malloc failed.");
559         return (1);
560       }
561
562       /* Note: By default oping is using constant string
563        * "liboping -- ICMP ping library <http://octo.it/liboping/>"
564        * which is exactly 56 bytes.
565        *
566        * Optimally we would follow the ping(1) behaviour, but we
567        * cannot use byte 00 or start data payload at exactly same
568        * location, due to oping library limitations. */
569       for (i = 0; i < size; i++) /* {{{ */
570       {
571         /* This restricts data pattern to be only composed of easily
572          * printable characters, and not NUL character. */
573         ping_data[i] = ('0' + i % 64);
574       }  /* }}} for (i = 0; i < size; i++) */
575       ping_data[size] = 0;
576     } else
577       WARNING ("ping plugin: Ignoring invalid Size %zu.", size);
578   }
579   else if (strcasecmp (key, "Timeout") == 0)
580   {
581     double tmp;
582
583     tmp = atof (value);
584     if (tmp > 0.0)
585       ping_timeout = tmp;
586     else
587       WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
588           tmp, value);
589   }
590   else if (strcasecmp (key, "MaxMissed") == 0)
591   {
592     ping_max_missed = atoi (value);
593     if (ping_max_missed < 0)
594       INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
595   }
596   else
597   {
598     return (-1);
599   }
600
601   return (0);
602 } /* }}} int ping_config */
603
604 static void submit (const char *host, const char *type, /* {{{ */
605     gauge_t value)
606 {
607   value_t values[1];
608   value_list_t vl = VALUE_LIST_INIT;
609
610   values[0].gauge = value;
611
612   vl.values = values;
613   vl.values_len = 1;
614   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
615   sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
616   sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
617   sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
618   sstrncpy (vl.type, type, sizeof (vl.type));
619
620   plugin_dispatch_values (&vl);
621 } /* }}} void ping_submit */
622
623 static int ping_read (void) /* {{{ */
624 {
625   hostlist_t *hl;
626
627   if (ping_thread_error != 0)
628   {
629     ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
630
631     stop_thread ();
632
633     for (hl = hostlist_head; hl != NULL; hl = hl->next)
634     {
635       hl->pkg_sent = 0;
636       hl->pkg_recv = 0;
637       hl->latency_total = 0.0;
638       hl->latency_squared = 0.0;
639     }
640
641     start_thread ();
642
643     return (-1);
644   } /* if (ping_thread_error != 0) */
645
646   for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
647   {
648     uint32_t pkg_sent;
649     uint32_t pkg_recv;
650     double latency_total;
651     double latency_squared;
652
653     double latency_average;
654     double latency_stddev;
655
656     double droprate;
657
658     /* Locking here works, because the structure of the linked list is only
659      * changed during configure and shutdown. */
660     pthread_mutex_lock (&ping_lock);
661
662     pkg_sent = hl->pkg_sent;
663     pkg_recv = hl->pkg_recv;
664     latency_total = hl->latency_total;
665     latency_squared = hl->latency_squared;
666
667     hl->pkg_sent = 0;
668     hl->pkg_recv = 0;
669     hl->latency_total = 0.0;
670     hl->latency_squared = 0.0;
671
672     pthread_mutex_unlock (&ping_lock);
673
674     /* This e. g. happens when starting up. */
675     if (pkg_sent == 0)
676     {
677       DEBUG ("ping plugin: No packages for host %s have been sent.",
678           hl->host);
679       continue;
680     }
681
682     /* Calculate average. Beware of division by zero. */
683     if (pkg_recv == 0)
684       latency_average = NAN;
685     else
686       latency_average = latency_total / ((double) pkg_recv);
687
688     /* Calculate standard deviation. Beware even more of division by zero. */
689     if (pkg_recv == 0)
690       latency_stddev = NAN;
691     else if (pkg_recv == 1)
692       latency_stddev = 0.0;
693     else
694       latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
695           - (latency_total * latency_total))
696           / ((double) (pkg_recv * (pkg_recv - 1))));
697
698     /* Calculate drop rate. */
699     droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
700
701     submit (hl->host, "ping", latency_average);
702     submit (hl->host, "ping_stddev", latency_stddev);
703     submit (hl->host, "ping_droprate", droprate);
704   } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
705
706   return (0);
707 } /* }}} int ping_read */
708
709 static int ping_shutdown (void) /* {{{ */
710 {
711   hostlist_t *hl;
712
713   INFO ("ping plugin: Shutting down thread.");
714   if (stop_thread () < 0)
715     return (-1);
716
717   hl = hostlist_head;
718   while (hl != NULL)
719   {
720     hostlist_t *hl_next;
721
722     hl_next = hl->next;
723
724     sfree (hl->host);
725     sfree (hl);
726
727     hl = hl_next;
728   }
729
730   if (ping_data != NULL) {
731     free (ping_data);
732     ping_data = NULL;
733   }
734
735   return (0);
736 } /* }}} int ping_shutdown */
737
738 void module_register (void)
739 {
740   plugin_register_config ("ping", ping_config,
741       config_keys, config_keys_num);
742   plugin_register_init ("ping", ping_init);
743   plugin_register_read ("ping", ping_read);
744   plugin_register_shutdown ("ping", ping_shutdown);
745 } /* void module_register */
746
747 /* vim: set sw=2 sts=2 et fdm=marker : */