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 <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 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;
81
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;
87
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);
102
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     }
116
117     ts->tv_sec  -= 1;
118     ts->tv_nsec += 1000000000;
119   }
120
121   while (ts->tv_nsec >= 1000000000)
122   {
123     ts->tv_sec  += 1;
124     ts->tv_nsec -= 1000000000;
125   }
126 } /* }}} void time_normalize */
127
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);
138
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   }
149
150   time_normalize (ts_dest);
151 } /* }}} void time_calc */
152
153 static int ping_dispatch_all (pingobj_t *pingobj) /* {{{ */
154 {
155   pingobj_iter_t *iter;
156   hostlist_t *hl;
157   int status;
158
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;
166
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, &param_size);
175     if (status != 0)
176     {
177       WARNING ("ping plugin: ping_iterator_get_info failed: %s",
178           ping_get_error (pingobj));
179       continue;
180     }
181
182     for (hl = hostlist_head; hl != NULL; hl = hl->next)
183       if (strcmp (userhost, hl->host) == 0)
184         break;
185
186     if (hl == NULL)
187     {
188       WARNING ("ping plugin: Cannot find host %s.", userhost);
189       continue;
190     }
191
192     param_size = sizeof (latency);
193     status = ping_iterator_get_info (iter, PING_INFO_LATENCY,
194         (void *) &latency, &param_size);
195     if (status != 0)
196     {
197       WARNING ("ping plugin: ping_iterator_get_info failed: %s",
198           ping_get_error (pingobj));
199       continue;
200     }
201
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);
208
209       /* reset missed packages counter */
210       hl->pkg_missed = 0;
211     } else
212       hl->pkg_missed++;
213
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;
222
223       WARNING ("ping plugin: host %s has not answered %d PING requests,"
224           " triggering resolve", hl->host, ping_max_missed);
225
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) */
241
242   return (0);
243 } /* }}} int ping_dispatch_all */
244
245 static void *ping_thread (void *arg) /* {{{ */
246 {
247   pingobj_t *pingobj = NULL;
248
249   struct timeval  tv_begin;
250   struct timeval  tv_end;
251   struct timespec ts_wait;
252   struct timespec ts_int;
253
254   hostlist_t *hl;
255   int count;
256
257   c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
258
259   pthread_mutex_lock (&ping_lock);
260
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   }
269
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));
274
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
281
282   ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
283   ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
284
285   if (ping_data != NULL)
286     ping_setopt (pingobj, PING_OPT_DATA, (void *) ping_data);
287
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   }
300
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   }
308
309   /* Set up `ts_int' */
310   {
311     double temp_sec;
312     double temp_nsec;
313
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   }
318
319   while (ping_thread_loop > 0)
320   {
321     int status;
322     _Bool send_successful = 0;
323
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     }
332
333     pthread_mutex_unlock (&ping_lock);
334
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     }
346
347     pthread_mutex_lock (&ping_lock);
348
349     if (ping_thread_loop <= 0)
350       break;
351
352     if (send_successful)
353       (void) ping_dispatch_all (pingobj);
354
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     }
363
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);
367
368     pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
369     if (ping_thread_loop <= 0)
370       break;
371   } /* while (ping_thread_loop > 0) */
372
373   pthread_mutex_unlock (&ping_lock);
374   ping_destroy (pingobj);
375
376   return ((void *) 0);
377 } /* }}} void *ping_thread */
378
379 static int start_thread (void) /* {{{ */
380 {
381   int status;
382
383   pthread_mutex_lock (&ping_lock);
384
385   if (ping_thread_loop != 0)
386   {
387     pthread_mutex_unlock (&ping_lock);
388     return (-1);
389   }
390
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   }
402
403   pthread_mutex_unlock (&ping_lock);
404   return (0);
405 } /* }}} int start_thread */
406
407 static int stop_thread (void) /* {{{ */
408 {
409   int status;
410
411   pthread_mutex_lock (&ping_lock);
412
413   if (ping_thread_loop == 0)
414   {
415     pthread_mutex_unlock (&ping_lock);
416     return (-1);
417   }
418
419   ping_thread_loop = 0;
420   pthread_cond_broadcast (&ping_cond);
421   pthread_mutex_unlock (&ping_lock);
422
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   }
429
430   pthread_mutex_lock (&ping_lock);
431   memset (&ping_thread_id, 0, sizeof (ping_thread_id));
432   ping_thread_error = 0;
433   pthread_mutex_unlock (&ping_lock);
434
435   return (status);
436 } /* }}} int stop_thread */
437
438 static int ping_init (void) /* {{{ */
439 {
440   if (hostlist_head == NULL)
441   {
442     NOTICE ("ping plugin: No hosts have been configured.");
443     return (-1);
444   }
445
446   if (ping_timeout > ping_interval)
447   {
448     ping_timeout = 0.9 * ping_interval;
449     WARNING ("ping plugin: Timeout is greater than interval. "
450         "Will use a timeout of %gs.", ping_timeout);
451   }
452
453   if (start_thread () != 0)
454     return (-1);
455
456   return (0);
457 } /* }}} int ping_init */
458
459 static int config_set_string (const char *name, /* {{{ */
460     char **var, const char *value)
461 {
462   char *tmp;
463
464   tmp = strdup (value);
465   if (tmp == NULL)
466   {
467     char errbuf[1024];
468     ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
469         name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
470     return (1);
471   }
472
473   if (*var != NULL)
474     free (*var);
475   *var = tmp;
476   return (0);
477 } /* }}} int config_set_string */
478
479 static int ping_config (const char *key, const char *value) /* {{{ */
480 {
481   if (strcasecmp (key, "Host") == 0)
482   {
483     hostlist_t *hl;
484     char *host;
485
486     hl = malloc (sizeof (*hl));
487     if (hl == NULL)
488     {
489       char errbuf[1024];
490       ERROR ("ping plugin: malloc failed: %s",
491           sstrerror (errno, errbuf, sizeof (errbuf)));
492       return (1);
493     }
494
495     host = strdup (value);
496     if (host == NULL)
497     {
498       char errbuf[1024];
499       sfree (hl);
500       ERROR ("ping plugin: strdup failed: %s",
501           sstrerror (errno, errbuf, sizeof (errbuf)));
502       return (1);
503     }
504
505     hl->host = host;
506     hl->pkg_sent = 0;
507     hl->pkg_recv = 0;
508     hl->pkg_missed = 0;
509     hl->latency_total = 0.0;
510     hl->latency_squared = 0.0;
511     hl->next = hostlist_head;
512     hostlist_head = hl;
513   }
514   else if (strcasecmp (key, "SourceAddress") == 0)
515   {
516     int status = config_set_string (key, &ping_source, value);
517     if (status != 0)
518       return (status);
519   }
520 #ifdef HAVE_OPING_1_3
521   else if (strcasecmp (key, "Device") == 0)
522   {
523     int status = config_set_string (key, &ping_device, value);
524     if (status != 0)
525       return (status);
526   }
527 #endif
528   else if (strcasecmp (key, "TTL") == 0)
529   {
530     int ttl = atoi (value);
531     if ((ttl > 0) && (ttl <= 255))
532       ping_ttl = ttl;
533     else
534       WARNING ("ping plugin: Ignoring invalid TTL %i.", ttl);
535   }
536   else if (strcasecmp (key, "Interval") == 0)
537   {
538     double tmp;
539
540     tmp = atof (value);
541     if (tmp > 0.0)
542       ping_interval = tmp;
543     else
544       WARNING ("ping plugin: Ignoring invalid interval %g (%s)",
545           tmp, value);
546   }
547   else if (strcasecmp (key, "Size") == 0) {
548     size_t size = (size_t) atoi (value);
549
550     /* Max IP packet size - (IPv6 + ICMP) = 65535 - (40 + 8) = 65487 */
551     if (size <= 65487)
552     {
553       size_t i;
554
555       sfree (ping_data);
556       ping_data = malloc (size + 1);
557       if (ping_data == NULL)
558       {
559         ERROR ("ping plugin: malloc failed.");
560         return (1);
561       }
562
563       /* Note: By default oping is using constant string
564        * "liboping -- ICMP ping library <http://octo.it/liboping/>"
565        * which is exactly 56 bytes.
566        *
567        * Optimally we would follow the ping(1) behaviour, but we
568        * cannot use byte 00 or start data payload at exactly same
569        * location, due to oping library limitations. */
570       for (i = 0; i < size; i++) /* {{{ */
571       {
572         /* This restricts data pattern to be only composed of easily
573          * printable characters, and not NUL character. */
574         ping_data[i] = ('0' + i % 64);
575       }  /* }}} for (i = 0; i < size; i++) */
576       ping_data[size] = 0;
577     } else
578       WARNING ("ping plugin: Ignoring invalid Size %zu.", size);
579   }
580   else if (strcasecmp (key, "Timeout") == 0)
581   {
582     double tmp;
583
584     tmp = atof (value);
585     if (tmp > 0.0)
586       ping_timeout = tmp;
587     else
588       WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
589           tmp, value);
590   }
591   else if (strcasecmp (key, "MaxMissed") == 0)
592   {
593     ping_max_missed = atoi (value);
594     if (ping_max_missed < 0)
595       INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
596   }
597   else
598   {
599     return (-1);
600   }
601
602   return (0);
603 } /* }}} int ping_config */
604
605 static void submit (const char *host, const char *type, /* {{{ */
606     gauge_t value)
607 {
608   value_t values[1];
609   value_list_t vl = VALUE_LIST_INIT;
610
611   values[0].gauge = value;
612
613   vl.values = values;
614   vl.values_len = 1;
615   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
616   sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
617   sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
618   sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
619   sstrncpy (vl.type, type, sizeof (vl.type));
620
621   plugin_dispatch_values (&vl);
622 } /* }}} void ping_submit */
623
624 static int ping_read (void) /* {{{ */
625 {
626   hostlist_t *hl;
627
628   if (ping_thread_error != 0)
629   {
630     ERROR ("ping plugin: The ping thread had a problem. Restarting it.");
631
632     stop_thread ();
633
634     for (hl = hostlist_head; hl != NULL; hl = hl->next)
635     {
636       hl->pkg_sent = 0;
637       hl->pkg_recv = 0;
638       hl->latency_total = 0.0;
639       hl->latency_squared = 0.0;
640     }
641
642     start_thread ();
643
644     return (-1);
645   } /* if (ping_thread_error != 0) */
646
647   for (hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
648   {
649     uint32_t pkg_sent;
650     uint32_t pkg_recv;
651     double latency_total;
652     double latency_squared;
653
654     double latency_average;
655     double latency_stddev;
656
657     double droprate;
658
659     /* Locking here works, because the structure of the linked list is only
660      * changed during configure and shutdown. */
661     pthread_mutex_lock (&ping_lock);
662
663     pkg_sent = hl->pkg_sent;
664     pkg_recv = hl->pkg_recv;
665     latency_total = hl->latency_total;
666     latency_squared = hl->latency_squared;
667
668     hl->pkg_sent = 0;
669     hl->pkg_recv = 0;
670     hl->latency_total = 0.0;
671     hl->latency_squared = 0.0;
672
673     pthread_mutex_unlock (&ping_lock);
674
675     /* This e. g. happens when starting up. */
676     if (pkg_sent == 0)
677     {
678       DEBUG ("ping plugin: No packages for host %s have been sent.",
679           hl->host);
680       continue;
681     }
682
683     /* Calculate average. Beware of division by zero. */
684     if (pkg_recv == 0)
685       latency_average = NAN;
686     else
687       latency_average = latency_total / ((double) pkg_recv);
688
689     /* Calculate standard deviation. Beware even more of division by zero. */
690     if (pkg_recv == 0)
691       latency_stddev = NAN;
692     else if (pkg_recv == 1)
693       latency_stddev = 0.0;
694     else
695       latency_stddev = sqrt (((((double) pkg_recv) * latency_squared)
696           - (latency_total * latency_total))
697           / ((double) (pkg_recv * (pkg_recv - 1))));
698
699     /* Calculate drop rate. */
700     droprate = ((double) (pkg_sent - pkg_recv)) / ((double) pkg_sent);
701
702     submit (hl->host, "ping", latency_average);
703     submit (hl->host, "ping_stddev", latency_stddev);
704     submit (hl->host, "ping_droprate", droprate);
705   } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
706
707   return (0);
708 } /* }}} int ping_read */
709
710 static int ping_shutdown (void) /* {{{ */
711 {
712   hostlist_t *hl;
713
714   INFO ("ping plugin: Shutting down thread.");
715   if (stop_thread () < 0)
716     return (-1);
717
718   hl = hostlist_head;
719   while (hl != NULL)
720   {
721     hostlist_t *hl_next;
722
723     hl_next = hl->next;
724
725     sfree (hl->host);
726     sfree (hl);
727
728     hl = hl_next;
729   }
730
731   if (ping_data != NULL) {
732     free (ping_data);
733     ping_data = NULL;
734   }
735
736   return (0);
737 } /* }}} int ping_shutdown */
738
739 void module_register (void)
740 {
741   plugin_register_config ("ping", ping_config,
742       config_keys, config_keys_num);
743   plugin_register_init ("ping", ping_init);
744   plugin_register_read ("ping", ping_read);
745   plugin_register_shutdown ("ping", ping_shutdown);
746 } /* void module_register */
747
748 /* vim: set sw=2 sts=2 et fdm=marker : */