diff --git a/src/ping.c b/src/ping.c
index f80cab517182437ec2291885dc4c7d14458f25f1..2a2f03eb39b80f062b652d4240c8fd3cddcee849 100644 (file)
--- a/src/ping.c
+++ b/src/ping.c
/**
* collectd - src/ping.c
/**
* 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
*
* 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
#include "common.h"
#include "plugin.h"
#include "configfile.h"
#include "common.h"
#include "plugin.h"
#include "configfile.h"
+#include "utils_debug.h"
#define MODULE_NAME "ping"
#include <netinet/in.h>
#define MODULE_NAME "ping"
#include <netinet/in.h>
-#include "libping/ping.h"
+#include "liboping/oping.h"
-#define MAX_PINGHOSTS 32
-
-typedef struct
+struct hostlist_s
{
{
- char *name;
- int flags;
- int disable; /* How long (how many iterations) this host is still disabled */
- int backoff; /* How long the host will be disabled, if it failes again */
-} pinghost_t;
+ char *host;
+ int wait_time;
+ int wait_left;
+ struct hostlist_s *next;
+};
+typedef struct hostlist_s hostlist_t;
-static pinghost_t hosts[MAX_PINGHOSTS];
-static int num_pinghosts;
+static pingobj_t *pingobj = NULL;
+static hostlist_t *hosts = NULL;
static char *file_template = "ping-%s.rrd";
static char *ds_def[] =
{
static char *file_template = "ping-%s.rrd";
static char *ds_def[] =
{
- "DS:ping:GAUGE:25:0:65535",
+ "DS:ping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
NULL
};
static int ds_num = 1;
NULL
};
static int ds_num = 1;
static char *config_keys[] =
{
"Host",
static char *config_keys[] =
{
"Host",
+ "TTL",
NULL
};
NULL
};
-static int config_keys_num = 1;
+static int config_keys_num = 2;
-static void ping_init (void)
+static void add_hosts (void)
{
{
- int i;
+ hostlist_t *hl_this;
+ hostlist_t *hl_prev;
- for (i = 0; i < MAX_PINGHOSTS; i++)
+ int step = atoi (COLLECTD_STEP);
+
+ hl_this = hosts;
+ hl_prev = NULL;
+ while (hl_this != NULL)
{
{
- hosts[i].flags = 0;
- hosts[i].disable = 0;
- hosts[i].backoff = 1;
+ 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;
+ }
}
}
+}
- return;
+static void ping_init (void)
+{
+ 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"))
+ if (pingobj == NULL)
{
{
- return (-1);
+ if ((pingobj = ping_construct ()) == NULL)
+ {
+ syslog (LOG_ERR, "ping: `ping_construct' failed: %s",
+ ping_get_error (pingobj));
+ return (1);
+ }
}
}
- else if (num_pinghosts >= MAX_PINGHOSTS)
+
+ if (strcasecmp (key, "host") == 0)
{
{
- return (1);
+ 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)
+ {
+ 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 ((hosts[num_pinghosts].name = strdup (value)) == NULL)
+ else if (strcasecmp (key, "ttl") == 0)
{
{
- return (2);
+ int ttl = atoi (value);
+ if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
+ {
+ syslog (LOG_WARNING, "ping: liboping did not accept the TTL value %i", ttl);
+ return (1);
+ }
}
else
{
}
else
{
- num_pinghosts++;
- return (0);
+ return (-1);
}
}
+
+ return (0);
}
static void ping_write (char *host, char *inst, char *val)
}
static void ping_write (char *host, char *inst, char *val)
}
#define BUFSIZE 256
}
#define BUFSIZE 256
-static void ping_submit (int ping_time, char *host)
+static void ping_submit (char *host, double latency)
{
char buf[BUFSIZE];
{
char buf[BUFSIZE];
- if (snprintf (buf, BUFSIZE, "%u:%i", (unsigned int) curtime, ping_time) >= BUFSIZE)
+ if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, latency) >= BUFSIZE)
return;
plugin_submit (MODULE_NAME, host, buf);
return;
plugin_submit (MODULE_NAME, host, buf);
static void ping_read (void)
{
static void ping_read (void)
{
- int ping;
- int i;
+ pingobj_iter_t *iter;
+
+ char host[512];
+ double latency;
+ size_t buf_len;
+
+ if (pingobj == NULL)
+ return;
+
+ if (hosts != NULL)
+ add_hosts ();
- for (i = 0; i < num_pinghosts; i++)
+ if (ping_send (pingobj) < 0)
{
{
- if (hosts[i].disable > 0)
- {
- hosts[i].disable--;
+ syslog (LOG_ERR, "ping: `ping_send' failed: %s",
+ ping_get_error (pingobj));
+ return;
+ }
+
+ for (iter = ping_iterator_get (pingobj);
+ iter != NULL;
+ iter = ping_iterator_next (iter))
+ {
+ buf_len = sizeof (host);
+ if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
+ host, &buf_len))
continue;
continue;
- }
-
- ping = tpinghost (hosts[i].name);
- switch (ping)
- {
- case 0:
- if (!(hosts[i].flags & 0x01))
- syslog (LOG_WARNING, "ping %s: Connection timed out.", hosts[i].name);
- hosts[i].flags |= 0x01;
- break;
-
- case -1:
- if (!(hosts[i].flags & 0x02))
- syslog (LOG_WARNING, "ping %s: Host or service is not reachable.", hosts[i].name);
- hosts[i].flags |= 0x02;
- break;
-
- case -2:
- syslog (LOG_ERR, "ping %s: Socket error. Ping will be disabled for %i iteration(s).",
- hosts[i].name, hosts[i].backoff);
- hosts[i].disable = hosts[i].backoff;
- if (hosts[i].backoff < 8192) /* 22 3/4 hours */
- hosts[i].backoff *= 2;
- hosts[i].flags |= 0x10;
- break;
-
- case -3:
- if (!(hosts[i].flags & 0x04))
- syslog (LOG_WARNING, "ping %s: Connection refused.", hosts[i].name);
- hosts[i].flags |= 0x04;
- break;
-
- default:
- if (hosts[i].flags != 0x00)
- syslog (LOG_NOTICE, "ping %s: Back to normal: %ims.", hosts[i].name, ping);
- hosts[i].flags = 0x00;
- hosts[i].backoff = 1;
- ping_submit (ping, hosts[i].name);
- } /* switch (ping) */
- } /* for (i = 0; i < num_pinghosts; i++) */
+ 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);
+ }
}
void module_register (void)
}
void module_register (void)