Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / ping.c
index 89fecc8..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
@@ -45,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",
@@ -96,7 +86,9 @@ static void add_hosts (void)
                        }
                        else
                        {
-                               DEBUG ("ping plugin: Failed adding host `%s'", hl_this->host);
+                               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)
@@ -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)
                {
-                       ERROR ("ping: `ping_construct' failed: %s",
-                                       ping_get_error (pingobj));
+                       ERROR ("ping plugin: `ping_construct' failed.");
                        return (1);
                }
        }
@@ -168,7 +162,7 @@ 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))
                {
                        WARNING ("ping: liboping did not accept the TTL value %i", ttl);
                        return (1);
@@ -191,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_g);
-       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)
@@ -217,7 +211,7 @@ static int ping_read (void)
 
        if (ping_send (pingobj) < 0)
        {
-               ERROR ("ping: `ping_send' failed: %s",
+               ERROR ("ping plugin: `ping_send' failed: %s",
                                ping_get_error (pingobj));
                return (-1);
        }
@@ -230,31 +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;
+               }
 
                DEBUG ("ping plugin: host = %s, latency = %f", host, latency);
                ping_submit (host, latency);
                number_of_hosts++;
        }
 
+       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 (modreg_e load)
+void module_register (void)
 {
-       if (load & MR_DATASETS)
-               plugin_register_data_set (&ds);
-
-       if (load & MR_READ)
-       {
-               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);
+       plugin_register_init ("ping", ping_init);
+       plugin_register_read ("ping", ping_read);
 } /* void module_register */