diff --git a/src/ping.c b/src/ping.c
index 91e0dbcc01072fd7601695349f08684adcd53523..2a2f03eb39b80f062b652d4240c8fd3cddcee849 100644 (file)
--- a/src/ping.c
+++ b/src/ping.c
#define MODULE_NAME "ping"
#include <netinet/in.h>
#define MODULE_NAME "ping"
#include <netinet/in.h>
-#include "liboping/liboping.h"
+#include "liboping/oping.h"
+
+struct hostlist_s
+{
+ char *host;
+ int wait_time;
+ int wait_left;
+ struct hostlist_s *next;
+};
+typedef struct hostlist_s hostlist_t;
static pingobj_t *pingobj = NULL;
static pingobj_t *pingobj = NULL;
+static hostlist_t *hosts = NULL;
static char *file_template = "ping-%s.rrd";
static char *file_template = "ping-%s.rrd";
};
static int config_keys_num = 2;
};
static int config_keys_num = 2;
+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 void ping_init (void)
{
static void ping_init (void)
{
- return;
+ if (hosts != NULL)
+ add_hosts ();
}
static int ping_config (char *key, char *value)
}
static int ping_config (char *key, char *value)
if (strcasecmp (key, "host") == 0)
{
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_WARNING, "ping: `ping_host_add' failed: %s",
- ping_get_error (pingobj));
+ syslog (LOG_ERR, "ping plugin: malloc failed: %s",
+ strerror (errno));
+ return (1);
+ }
+ if ((host = strdup (value)) == NULL)
+ {
+ free (hl);
+ syslog (LOG_ERR, "ping plugin: strdup failed: %s",
+ strerror (errno));
return (1);
}
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)
{
}
else if (strcasecmp (key, "ttl") == 0)
{
{
pingobj_iter_t *iter;
{
pingobj_iter_t *iter;
- char *host;
- double latency;
+ char host[512];
+ double latency;
+ size_t buf_len;
if (pingobj == NULL)
return;
if (pingobj == NULL)
return;
+ if (hosts != NULL)
+ add_hosts ();
+
if (ping_send (pingobj) < 0)
{
syslog (LOG_ERR, "ping: `ping_send' failed: %s",
if (ping_send (pingobj) < 0)
{
syslog (LOG_ERR, "ping: `ping_send' failed: %s",
return;
}
return;
}
- 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;
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);
DBG ("host = %s, latency = %f", host, latency);
ping_submit (host, latency);
-
- free (host); host = NULL;
}
}
}
}