Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / ping.c
index 61bad09..084e456 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/ping.c
- * Copyright (C) 2005,2006  Florian octo Forster
+ * Copyright (C) 2005-2007  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
@@ -23,7 +23,6 @@
 #include "common.h"
 #include "plugin.h"
 #include "configfile.h"
-#include "utils_debug.h"
 
 #include <netinet/in.h>
 #include "liboping/oping.h"
@@ -46,16 +45,6 @@ typedef struct hostlist_s hostlist_t;
 static pingobj_t *pingobj = NULL;
 static hostlist_t *hosts = NULL;
 
-static data_source_t dsrc[1] =
-{
-       {"ping", DS_TYPE_GAUGE, 0, 65535.0},
-};
-
-static data_set_t ds =
-{
-       "ping", 1, dsrc
-};
-
 static const char *config_keys[] =
 {
        "Host",
@@ -72,20 +61,20 @@ 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);
+               DEBUG ("ping plugin: 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);
+                               DEBUG ("ping plugin: Successfully added host %s", hl_this->host);
                                /* Remove the host from the linked list */
                                if (hl_prev != NULL)
                                        hl_prev->next = hl_this->next;
@@ -97,6 +86,9 @@ static void add_hosts (void)
                        }
                        else
                        {
+                               WARNING ("ping plugin: Failed adding host "
+                                               "`%s': %s", hl_this->host,
+                                               ping_get_error (pingobj));
                                hl_this->wait_left = hl_this->wait_time;
                                hl_this->wait_time *= 2;
                                if (hl_this->wait_time > 86400)
@@ -105,7 +97,7 @@ static void add_hosts (void)
                }
                else
                {
-                       hl_this->wait_left -= step;
+                       hl_this->wait_left -= interval_g;
                }
 
                if (hl_this != NULL)
@@ -114,15 +106,18 @@ static void add_hosts (void)
                        hl_this = hl_this->next;
                }
        }
-}
+} /* void add_hosts */
 
 static int ping_init (void)
 {
+       if (pingobj == NULL)
+               return (-1);
+
        if (hosts != NULL)
                add_hosts ();
 
        return (0);
-}
+} /* int ping_init */
 
 static int ping_config (const char *key, const char *value)
 {
@@ -130,8 +125,7 @@ static int ping_config (const char *key, const char *value)
        {
                if ((pingobj = ping_construct ()) == NULL)
                {
-                       syslog (LOG_ERR, "ping: `ping_construct' failed: %s",
-                                       ping_get_error (pingobj));
+                       ERROR ("ping plugin: `ping_construct' failed.");
                        return (1);
                }
        }
@@ -140,24 +134,27 @@ static int ping_config (const char *key, const char *value)
        {
                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));
+                       char errbuf[1024];
+                       ERROR ("ping plugin: malloc failed: %s",
+                                       sstrerror (errno, errbuf,
+                                               sizeof (errbuf)));
                        return (1);
                }
                if ((host = strdup (value)) == NULL)
                {
+                       char errbuf[1024];
                        free (hl);
-                       syslog (LOG_ERR, "ping plugin: strdup failed: %s",
-                                       strerror (errno));
+                       ERROR ("ping plugin: strdup failed: %s",
+                                       sstrerror (errno, errbuf,
+                                               sizeof (errbuf)));
                        return (1);
                }
 
                hl->host = host;
-               hl->wait_time = 2 * step;
+               hl->wait_time = 2 * interval_g;
                hl->wait_left = 0;
                hl->next = hosts;
                hosts = hl;
@@ -165,9 +162,9 @@ static int ping_config (const char *key, const char *value)
        else if (strcasecmp (key, "ttl") == 0)
        {
                int ttl = atoi (value);
-               if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
+               if (ping_setopt (pingobj, PING_OPT_TTL, (void *) &ttl))
                {
-                       syslog (LOG_WARNING, "ping: liboping did not accept the TTL value %i", ttl);
+                       WARNING ("ping: liboping did not accept the TTL value %i", ttl);
                        return (1);
                }
        }
@@ -188,13 +185,13 @@ static void ping_submit (char *host, double latency)
 
        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));
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
+       sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+       sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
+       sstrncpy (vl.type, "ping", sizeof (vl.type));
 
-       plugin_dispatch_values ("ping", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int ping_read (void)
@@ -204,6 +201,7 @@ static int ping_read (void)
        char   host[512];
        double latency;
        size_t buf_len;
+       int    number_of_hosts;
 
        if (pingobj == NULL)
                return (-1);
@@ -213,11 +211,12 @@ static int ping_read (void)
 
        if (ping_send (pingobj) < 0)
        {
-               syslog (LOG_ERR, "ping: `ping_send' failed: %s",
+               ERROR ("ping plugin: `ping_send' failed: %s",
                                ping_get_error (pingobj));
                return (-1);
        }
 
+       number_of_hosts = 0;
        for (iter = ping_iterator_get (pingobj);
                        iter != NULL;
                        iter = ping_iterator_next (iter))
@@ -225,24 +224,38 @@ static int ping_read (void)
                buf_len = sizeof (host);
                if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
                                        host, &buf_len))
+               {
+                       WARNING ("ping plugin: ping_iterator_get_info "
+                                       "(PING_INFO_HOSTNAME) failed.");
                        continue;
+               }
 
                buf_len = sizeof (latency);
                if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
                                        &latency, &buf_len))
+               {
+                       WARNING ("ping plugin: ping_iterator_get_info (%s, "
+                                       "PING_INFO_LATENCY) failed.", host);
                        continue;
+               }
 
-               DBG ("host = %s, latency = %f", host, latency);
+               DEBUG ("ping plugin: host = %s, latency = %f", host, latency);
                ping_submit (host, latency);
+               number_of_hosts++;
        }
 
-       return (0);
+       if ((number_of_hosts == 0) && (getuid () != 0))
+       {
+               ERROR ("ping plugin: All hosts failed. Try starting collectd as root.");
+       }
+
+       return (number_of_hosts == 0 ? -1 : 0);
 } /* int ping_read */
 
 void module_register (void)
 {
-       plugin_register_data_set (&ds);
+       plugin_register_config ("ping", ping_config,
+                       config_keys, config_keys_num);
        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 */