2 * collectd - src/ping.c
3 * Copyright (C) 2005-2012 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_complain.h"
33 #include <netinet/in.h>
35 #include <netdb.h> /* NI_MAXHOST */
38 #ifdef HAVE_SYS_CAPABILITY_H
39 #include <sys/capability.h>
45 #define NI_MAXHOST 1025
48 #if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
49 #define HAVE_OPING_1_3
63 double latency_squared;
65 struct hostlist_s *next;
67 typedef struct hostlist_s hostlist_t;
72 static hostlist_t *hostlist_head = NULL;
74 static char *ping_source = NULL;
76 static char *ping_device = NULL;
78 static char *ping_data = NULL;
79 static int ping_ttl = PING_DEF_TTL;
80 static double ping_interval = 1.0;
81 static double ping_timeout = 0.9;
82 static int ping_max_missed = -1;
84 static pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER;
85 static pthread_cond_t ping_cond = PTHREAD_COND_INITIALIZER;
86 static int ping_thread_loop = 0;
87 static int ping_thread_error = 0;
88 static pthread_t ping_thread_id;
90 static const char *config_keys[] = {"Host", "SourceAddress",
94 "Size", "TTL", "Interval",
95 "Timeout", "MaxMissed"};
96 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
101 /* Assure that `ts->tv_nsec' is in the range 0 .. 999999999 */
102 static void time_normalize(struct timespec *ts) /* {{{ */
104 while (ts->tv_nsec < 0) {
105 if (ts->tv_sec == 0) {
111 ts->tv_nsec += 1000000000;
114 while (ts->tv_nsec >= 1000000000) {
116 ts->tv_nsec -= 1000000000;
118 } /* }}} void time_normalize */
120 /* Add `ts_int' to `tv_begin' and store the result in `ts_dest'. If the result
121 * is larger than `tv_end', copy `tv_end' to `ts_dest' instead. */
122 static void time_calc(struct timespec *ts_dest, /* {{{ */
123 const struct timespec *ts_int,
124 const struct timeval *tv_begin,
125 const struct timeval *tv_end) {
126 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
127 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
128 time_normalize(ts_dest);
130 /* Assure that `(begin + interval) > end'.
131 * This may seem overly complicated, but `tv_sec' is of type `time_t'
132 * which may be `unsigned. *sigh* */
133 if ((tv_end->tv_sec > ts_dest->tv_sec) ||
134 ((tv_end->tv_sec == ts_dest->tv_sec) &&
135 ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec))) {
136 ts_dest->tv_sec = tv_end->tv_sec;
137 ts_dest->tv_nsec = 1000 * tv_end->tv_usec;
140 time_normalize(ts_dest);
141 } /* }}} void time_calc */
143 static int ping_dispatch_all(pingobj_t *pingobj) /* {{{ */
148 for (pingobj_iter_t *iter = ping_iterator_get(pingobj); iter != NULL;
149 iter = ping_iterator_next(iter)) { /* {{{ */
150 char userhost[NI_MAXHOST];
154 param_size = sizeof(userhost);
155 status = ping_iterator_get_info(iter,
156 #ifdef PING_INFO_USERNAME
161 userhost, ¶m_size);
163 WARNING("ping plugin: ping_iterator_get_info failed: %s",
164 ping_get_error(pingobj));
168 for (hl = hostlist_head; hl != NULL; hl = hl->next)
169 if (strcmp(userhost, hl->host) == 0)
173 WARNING("ping plugin: Cannot find host %s.", userhost);
177 param_size = sizeof(latency);
178 status = ping_iterator_get_info(iter, PING_INFO_LATENCY, (void *)&latency,
181 WARNING("ping plugin: ping_iterator_get_info failed: %s",
182 ping_get_error(pingobj));
187 if (latency >= 0.0) {
189 hl->latency_total += latency;
190 hl->latency_squared += (latency * latency);
192 /* reset missed packages counter */
197 /* if the host did not answer our last N packages, trigger a resolv. */
198 if ((ping_max_missed >= 0) &&
199 (hl->pkg_missed >= ((uint32_t)ping_max_missed))) { /* {{{ */
200 /* we reset the missed package counter here, since we only want to
201 * trigger a resolv every N packages and not every package _AFTER_ N
205 WARNING("ping plugin: host %s has not answered %d PING requests,"
206 " triggering resolve",
207 hl->host, ping_max_missed);
209 /* we trigger the resolv simply be removeing and adding the host to our
211 status = ping_host_remove(pingobj, hl->host);
213 WARNING("ping plugin: ping_host_remove (%s) failed.", hl->host);
215 status = ping_host_add(pingobj, hl->host);
217 ERROR("ping plugin: ping_host_add (%s) failed.", hl->host);
219 } /* }}} ping_max_missed */
220 } /* }}} for (iter) */
223 } /* }}} int ping_dispatch_all */
225 static void *ping_thread(void *arg) /* {{{ */
227 struct timeval tv_begin;
228 struct timeval tv_end;
229 struct timespec ts_wait;
230 struct timespec ts_int;
234 c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
236 pingobj_t *pingobj = ping_construct();
237 if (pingobj == NULL) {
238 ERROR("ping plugin: ping_construct failed.");
239 pthread_mutex_lock(&ping_lock);
240 ping_thread_error = 1;
241 pthread_mutex_unlock(&ping_lock);
245 if (ping_source != NULL)
246 if (ping_setopt(pingobj, PING_OPT_SOURCE, (void *)ping_source) != 0)
247 ERROR("ping plugin: Failed to set source address: %s",
248 ping_get_error(pingobj));
250 #ifdef HAVE_OPING_1_3
251 if (ping_device != NULL)
252 if (ping_setopt(pingobj, PING_OPT_DEVICE, (void *)ping_device) != 0)
253 ERROR("ping plugin: Failed to set device: %s", ping_get_error(pingobj));
256 ping_setopt(pingobj, PING_OPT_TIMEOUT, (void *)&ping_timeout);
257 ping_setopt(pingobj, PING_OPT_TTL, (void *)&ping_ttl);
259 if (ping_data != NULL)
260 ping_setopt(pingobj, PING_OPT_DATA, (void *)ping_data);
262 /* Add all the hosts to the ping object. */
264 for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) {
266 tmp_status = ping_host_add(pingobj, hl->host);
268 WARNING("ping plugin: ping_host_add (%s) failed: %s", hl->host,
269 ping_get_error(pingobj));
275 ERROR("ping plugin: No host could be added to ping object. Giving up.");
276 pthread_mutex_lock(&ping_lock);
277 ping_thread_error = 1;
278 pthread_mutex_unlock(&ping_lock);
282 /* Set up `ts_int' */
287 temp_nsec = modf(ping_interval, &temp_sec);
288 ts_int.tv_sec = (time_t)temp_sec;
289 ts_int.tv_nsec = (long)(temp_nsec * 1000000000L);
292 pthread_mutex_lock(&ping_lock);
293 while (ping_thread_loop > 0) {
294 _Bool send_successful = 0;
296 if (gettimeofday(&tv_begin, NULL) < 0) {
298 ERROR("ping plugin: gettimeofday failed: %s",
299 sstrerror(errno, errbuf, sizeof(errbuf)));
300 ping_thread_error = 1;
304 pthread_mutex_unlock(&ping_lock);
306 int status = ping_send(pingobj);
308 c_complain(LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
309 ping_get_error(pingobj));
311 c_release(LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
315 pthread_mutex_lock(&ping_lock);
317 if (ping_thread_loop <= 0)
321 (void)ping_dispatch_all(pingobj);
323 if (gettimeofday(&tv_end, NULL) < 0) {
325 ERROR("ping plugin: gettimeofday failed: %s",
326 sstrerror(errno, errbuf, sizeof(errbuf)));
327 ping_thread_error = 1;
331 /* Calculate the absolute time until which to wait and store it in
333 time_calc(&ts_wait, &ts_int, &tv_begin, &tv_end);
335 pthread_cond_timedwait(&ping_cond, &ping_lock, &ts_wait);
336 if (ping_thread_loop <= 0)
338 } /* while (ping_thread_loop > 0) */
340 pthread_mutex_unlock(&ping_lock);
341 ping_destroy(pingobj);
344 } /* }}} void *ping_thread */
346 static int start_thread(void) /* {{{ */
350 pthread_mutex_lock(&ping_lock);
352 if (ping_thread_loop != 0) {
353 pthread_mutex_unlock(&ping_lock);
357 ping_thread_loop = 1;
358 ping_thread_error = 0;
359 status = plugin_thread_create(&ping_thread_id, /* attr = */ NULL, ping_thread,
360 /* arg = */ (void *)0, "ping");
362 ping_thread_loop = 0;
363 ERROR("ping plugin: Starting thread failed.");
364 pthread_mutex_unlock(&ping_lock);
368 pthread_mutex_unlock(&ping_lock);
370 } /* }}} int start_thread */
372 static int stop_thread(void) /* {{{ */
376 pthread_mutex_lock(&ping_lock);
378 if (ping_thread_loop == 0) {
379 pthread_mutex_unlock(&ping_lock);
383 ping_thread_loop = 0;
384 pthread_cond_broadcast(&ping_cond);
385 pthread_mutex_unlock(&ping_lock);
387 status = pthread_join(ping_thread_id, /* return = */ NULL);
389 ERROR("ping plugin: Stopping thread failed.");
393 pthread_mutex_lock(&ping_lock);
394 memset(&ping_thread_id, 0, sizeof(ping_thread_id));
395 ping_thread_error = 0;
396 pthread_mutex_unlock(&ping_lock);
399 } /* }}} int stop_thread */
401 static int ping_init(void) /* {{{ */
403 if (hostlist_head == NULL) {
404 NOTICE("ping plugin: No hosts have been configured.");
408 if (ping_timeout > ping_interval) {
409 ping_timeout = 0.9 * ping_interval;
410 WARNING("ping plugin: Timeout is greater than interval. "
411 "Will use a timeout of %gs.",
415 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW)
416 if (check_capability(CAP_NET_RAW) != 0) {
418 WARNING("ping plugin: Running collectd as root, but the CAP_NET_RAW "
419 "capability is missing. The plugin's read function will probably "
420 "fail. Is your init system dropping capabilities?");
422 WARNING("ping plugin: collectd doesn't have the CAP_NET_RAW capability. "
423 "If you don't want to run collectd as root, try running \"setcap "
424 "cap_net_raw=ep\" on the collectd binary.");
428 return start_thread();
429 } /* }}} int ping_init */
431 static int config_set_string(const char *name, /* {{{ */
432 char **var, const char *value) {
438 ERROR("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s", name,
439 value, sstrerror(errno, errbuf, sizeof(errbuf)));
447 } /* }}} int config_set_string */
449 static int ping_config(const char *key, const char *value) /* {{{ */
451 if (strcasecmp(key, "Host") == 0) {
455 hl = malloc(sizeof(*hl));
458 ERROR("ping plugin: malloc failed: %s",
459 sstrerror(errno, errbuf, sizeof(errbuf)));
463 host = strdup(value);
467 ERROR("ping plugin: strdup failed: %s",
468 sstrerror(errno, errbuf, sizeof(errbuf)));
476 hl->latency_total = 0.0;
477 hl->latency_squared = 0.0;
478 hl->next = hostlist_head;
480 } else if (strcasecmp(key, "SourceAddress") == 0) {
481 int status = config_set_string(key, &ping_source, value);
485 #ifdef HAVE_OPING_1_3
486 else if (strcasecmp(key, "Device") == 0) {
487 int status = config_set_string(key, &ping_device, value);
492 else if (strcasecmp(key, "TTL") == 0) {
493 int ttl = atoi(value);
494 if ((ttl > 0) && (ttl <= 255))
497 WARNING("ping plugin: Ignoring invalid TTL %i.", ttl);
498 } else if (strcasecmp(key, "Interval") == 0) {
505 WARNING("ping plugin: Ignoring invalid interval %g (%s)", tmp, value);
506 } else if (strcasecmp(key, "Size") == 0) {
507 size_t size = (size_t)atoi(value);
509 /* Max IP packet size - (IPv6 + ICMP) = 65535 - (40 + 8) = 65487 */
512 ping_data = malloc(size + 1);
513 if (ping_data == NULL) {
514 ERROR("ping plugin: malloc failed.");
518 /* Note: By default oping is using constant string
519 * "liboping -- ICMP ping library <http://octo.it/liboping/>"
520 * which is exactly 56 bytes.
522 * Optimally we would follow the ping(1) behaviour, but we
523 * cannot use byte 00 or start data payload at exactly same
524 * location, due to oping library limitations. */
525 for (size_t i = 0; i < size; i++) /* {{{ */
527 /* This restricts data pattern to be only composed of easily
528 * printable characters, and not NUL character. */
529 ping_data[i] = ('0' + i % 64);
530 } /* }}} for (i = 0; i < size; i++) */
533 WARNING("ping plugin: Ignoring invalid Size %zu.", size);
534 } else if (strcasecmp(key, "Timeout") == 0) {
541 WARNING("ping plugin: Ignoring invalid timeout %g (%s)", tmp, value);
542 } else if (strcasecmp(key, "MaxMissed") == 0) {
543 ping_max_missed = atoi(value);
544 if (ping_max_missed < 0)
545 INFO("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
551 } /* }}} int ping_config */
553 static void submit(const char *host, const char *type, /* {{{ */
555 value_list_t vl = VALUE_LIST_INIT;
557 vl.values = &(value_t){.gauge = value};
559 sstrncpy(vl.plugin, "ping", sizeof(vl.plugin));
560 sstrncpy(vl.type_instance, host, sizeof(vl.type_instance));
561 sstrncpy(vl.type, type, sizeof(vl.type));
563 plugin_dispatch_values(&vl);
564 } /* }}} void ping_submit */
566 static int ping_read(void) /* {{{ */
568 if (ping_thread_error != 0) {
569 ERROR("ping plugin: The ping thread had a problem. Restarting it.");
573 for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) {
576 hl->latency_total = 0.0;
577 hl->latency_squared = 0.0;
583 } /* if (ping_thread_error != 0) */
585 for (hostlist_t *hl = hostlist_head; hl != NULL; hl = hl->next) /* {{{ */
589 double latency_total;
590 double latency_squared;
592 double latency_average;
593 double latency_stddev;
597 /* Locking here works, because the structure of the linked list is only
598 * changed during configure and shutdown. */
599 pthread_mutex_lock(&ping_lock);
601 pkg_sent = hl->pkg_sent;
602 pkg_recv = hl->pkg_recv;
603 latency_total = hl->latency_total;
604 latency_squared = hl->latency_squared;
608 hl->latency_total = 0.0;
609 hl->latency_squared = 0.0;
611 pthread_mutex_unlock(&ping_lock);
613 /* This e. g. happens when starting up. */
615 DEBUG("ping plugin: No packages for host %s have been sent.", hl->host);
619 /* Calculate average. Beware of division by zero. */
621 latency_average = NAN;
623 latency_average = latency_total / ((double)pkg_recv);
625 /* Calculate standard deviation. Beware even more of division by zero. */
627 latency_stddev = NAN;
628 else if (pkg_recv == 1)
629 latency_stddev = 0.0;
631 latency_stddev = sqrt(((((double)pkg_recv) * latency_squared) -
632 (latency_total * latency_total)) /
633 ((double)(pkg_recv * (pkg_recv - 1))));
635 /* Calculate drop rate. */
636 droprate = ((double)(pkg_sent - pkg_recv)) / ((double)pkg_sent);
638 submit(hl->host, "ping", latency_average);
639 submit(hl->host, "ping_stddev", latency_stddev);
640 submit(hl->host, "ping_droprate", droprate);
641 } /* }}} for (hl = hostlist_head; hl != NULL; hl = hl->next) */
644 } /* }}} int ping_read */
646 static int ping_shutdown(void) /* {{{ */
650 INFO("ping plugin: Shutting down thread.");
651 if (stop_thread() < 0)
666 if (ping_data != NULL) {
672 } /* }}} int ping_shutdown */
674 void module_register(void) {
675 plugin_register_config("ping", ping_config, config_keys, config_keys_num);
676 plugin_register_init("ping", ping_init);
677 plugin_register_read("ping", ping_read);
678 plugin_register_shutdown("ping", ping_shutdown);
679 } /* void module_register */