X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fping.c;h=61bad09fd2807385f4594e12d433ebe63feabb3c;hb=2cea8075c666a6c6c7d6e1b4f95e1bee6f3803ac;hp=69661708baeb1bd468f884643777b7c98ac3643f;hpb=4ff510ffa41e47f31c695c83f045fd79efecf5f4;p=collectd.git diff --git a/src/ping.c b/src/ping.c index 69661708..61bad09f 100644 --- a/src/ping.c +++ b/src/ping.c @@ -1,11 +1,10 @@ /** * collectd - src/ping.c - * Copyright (C) 2005 Florian octo Forster + * Copyright (C) 2005,2006 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. + * Free Software Foundation; only version 2 of the License is applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -26,23 +25,38 @@ #include "configfile.h" #include "utils_debug.h" -#define MODULE_NAME "ping" - #include -#include "liboping/liboping.h" +#include "liboping/oping.h" + +/* + * Private data types + */ +struct hostlist_s +{ + char *host; + int wait_time; + int wait_left; + struct hostlist_s *next; +}; +typedef struct hostlist_s hostlist_t; +/* + * Private variables + */ static pingobj_t *pingobj = NULL; +static hostlist_t *hosts = NULL; -static char *file_template = "ping-%s.rrd"; +static data_source_t dsrc[1] = +{ + {"ping", DS_TYPE_GAUGE, 0, 65535.0}, +}; -static char *ds_def[] = +static data_set_t ds = { - "DS:ping:GAUGE:"COLLECTD_HEARTBEAT":0:65535", - NULL + "ping", 1, dsrc }; -static int ds_num = 1; -static char *config_keys[] = +static const char *config_keys[] = { "Host", "TTL", @@ -50,29 +64,103 @@ static char *config_keys[] = }; static int config_keys_num = 2; -static void ping_init (void) +/* + * Private functions + */ +static void add_hosts (void) +{ + hostlist_t *hl_this; + hostlist_t *hl_prev; + + int step = atoi (COLLECTD_STEP); + + hl_this = hosts; + hl_prev = NULL; + while (hl_this != NULL) + { + DBG ("host = %s, wait_left = %i, wait_time = %i, next = %p", + hl_this->host, hl_this->wait_left, hl_this->wait_time, (void *) hl_this->next); + + if (hl_this->wait_left <= 0) + { + if (ping_host_add (pingobj, hl_this->host) == 0) + { + DBG ("Successfully added host %s", hl_this->host); + /* Remove the host from the linked list */ + if (hl_prev != NULL) + hl_prev->next = hl_this->next; + else + hosts = hl_this->next; + free (hl_this->host); + free (hl_this); + hl_this = (hl_prev != NULL) ? hl_prev : hosts; + } + else + { + hl_this->wait_left = hl_this->wait_time; + hl_this->wait_time *= 2; + if (hl_this->wait_time > 86400) + hl_this->wait_time = 86400; + } + } + else + { + hl_this->wait_left -= step; + } + + if (hl_this != NULL) + { + hl_prev = hl_this; + hl_this = hl_this->next; + } + } +} + +static int ping_init (void) { - return; + if (hosts != NULL) + add_hosts (); + + return (0); } -static int ping_config (char *key, char *value) +static int ping_config (const char *key, const char *value) { if (pingobj == NULL) { if ((pingobj = ping_construct ()) == NULL) { - syslog (LOG_ERR, "ping: `ping_construct' failed.\n"); + syslog (LOG_ERR, "ping: `ping_construct' failed: %s", + ping_get_error (pingobj)); return (1); } } if (strcasecmp (key, "host") == 0) { - if (ping_host_add (pingobj, value) < 0) + hostlist_t *hl; + char *host; + int step = atoi (COLLECTD_STEP); + + if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL) + { + syslog (LOG_ERR, "ping plugin: malloc failed: %s", + strerror (errno)); + return (1); + } + if ((host = strdup (value)) == NULL) { - syslog (LOG_WARNING, "ping: `ping_host_add' failed."); + free (hl); + syslog (LOG_ERR, "ping plugin: strdup failed: %s", + strerror (errno)); return (1); } + + hl->host = host; + hl->wait_time = 2 * step; + hl->wait_left = 0; + hl->next = hosts; + hosts = hl; } else if (strcasecmp (key, "ttl") == 0) { @@ -91,70 +179,70 @@ static int ping_config (char *key, char *value) return (0); } -static void ping_write (char *host, char *inst, char *val) -{ - char file[512]; - int status; - - status = snprintf (file, 512, file_template, inst); - if (status < 1) - return; - else if (status >= 512) - return; - - rrd_update_file (host, file, val, ds_def, ds_num); -} - -#define BUFSIZE 256 static void ping_submit (char *host, double latency) { - char buf[BUFSIZE]; + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].gauge = latency; - if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, latency) >= BUFSIZE) - return; + vl.values = values; + vl.values_len = 1; + vl.time = time (NULL); + strcpy (vl.host, hostname); + strcpy (vl.plugin, "ping"); + strcpy (vl.plugin_instance, ""); + strncpy (vl.type_instance, host, sizeof (vl.type_instance)); - plugin_submit (MODULE_NAME, host, buf); + plugin_dispatch_values ("ping", &vl); } -#undef BUFSIZE -static void ping_read (void) +static int ping_read (void) { pingobj_iter_t *iter; - char *host; - double latency; + char host[512]; + double latency; + size_t buf_len; if (pingobj == NULL) - return; + return (-1); + + if (hosts != NULL) + add_hosts (); if (ping_send (pingobj) < 0) { - syslog (LOG_ERR, "ping: `ping_send' failed."); - return; + syslog (LOG_ERR, "ping: `ping_send' failed: %s", + ping_get_error (pingobj)); + return (-1); } - for (iter = ping_iterator_get (pingobj); iter != NULL; iter = ping_iterator_next (iter)) + for (iter = ping_iterator_get (pingobj); + iter != NULL; + iter = ping_iterator_next (iter)) { - const char *tmp; - - if ((tmp = ping_iterator_get_host (iter)) == NULL) - continue; - if ((host = strdup (tmp)) == NULL) + buf_len = sizeof (host); + if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME, + host, &buf_len)) continue; - latency = ping_iterator_get_latency (iter); + buf_len = sizeof (latency); + if (ping_iterator_get_info (iter, PING_INFO_LATENCY, + &latency, &buf_len)) + continue; DBG ("host = %s, latency = %f", host, latency); ping_submit (host, latency); - - free (host); host = NULL; } -} + + return (0); +} /* int ping_read */ void module_register (void) { - plugin_register (MODULE_NAME, ping_init, ping_read, ping_write); - cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num); -} - -#undef MODULE_NAME + plugin_register_data_set (&ds); + plugin_register_init ("ping", ping_init); + plugin_register_read ("ping", ping_read); + plugin_register_config ("ping", ping_config, config_keys, config_keys_num); +} /* void module_register */