X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fping.c;h=69661708baeb1bd468f884643777b7c98ac3643f;hb=ed3b4a8fe6377817e1dadfbc713c4cc5c0bc4444;hp=f80cab517182437ec2291885dc4c7d14458f25f1;hpb=fdf2cf57cd814f5d454f124b23a54a50b48c34d7;p=collectd.git diff --git a/src/ping.c b/src/ping.c index f80cab51..69661708 100644 --- a/src/ping.c +++ b/src/ping.c @@ -24,30 +24,20 @@ #include "common.h" #include "plugin.h" #include "configfile.h" +#include "utils_debug.h" #define MODULE_NAME "ping" #include -#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; @@ -55,43 +45,50 @@ 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.\n"); + 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."); + 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) @@ -109,11 +106,11 @@ 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); @@ -122,56 +119,36 @@ static void ping_submit (int ping_time, char *host) 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."); + 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)