/**
* 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
#include "common.h"
#include "plugin.h"
#include "configfile.h"
+#include "utils_debug.h"
#define MODULE_NAME "ping"
#include <netinet/in.h>
-#include "libping/ping.h"
+#include "liboping/liboping.h"
-#define MAX_PINGHOSTS 32
-
-typedef struct
-{
- 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;
-
-static pinghost_t hosts[MAX_PINGHOSTS];
-static int num_pinghosts;
+static pingobj_t *pingobj = NULL;
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;
static char *config_keys[] =
{
"Host",
+ "TTL",
NULL
};
-static int config_keys_num = 1;
+static int config_keys_num = 2;
static void ping_init (void)
{
- int i;
-
- for (i = 0; i < MAX_PINGHOSTS; i++)
- {
- hosts[i].flags = 0;
- hosts[i].disable = 0;
- hosts[i].backoff = 1;
- }
-
return;
}
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);
+ if (ping_host_add (pingobj, value) < 0)
+ {
+ syslog (LOG_WARNING, "ping: `ping_host_add' failed: %s",
+ ping_get_error (pingobj));
+ return (1);
+ }
}
- 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
{
- num_pinghosts++;
- return (0);
+ return (-1);
}
+
+ return (0);
}
static void ping_write (char *host, char *inst, char *val)
}
#define BUFSIZE 256
-static void ping_submit (int ping_time, char *host)
+static void ping_submit (char *host, double latency)
{
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);
static void ping_read (void)
{
- int ping;
- int i;
+ pingobj_iter_t *iter;
+
+ char *host;
+ double latency;
+
+ if (pingobj == NULL)
+ return;
- 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))
+ {
+ const char *tmp;
+
+ if ((tmp = ping_iterator_get_host (iter)) == NULL)
+ continue;
+ if ((host = strdup (tmp)) == NULL)
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++) */
+ latency = ping_iterator_get_latency (iter);
+
+ DBG ("host = %s, latency = %f", host, latency);
+ ping_submit (host, latency);
+
+ free (host); host = NULL;
+ }
}
void module_register (void)