nut plugin: Added a plugin to query the `upsd' from the `network ups tools'.
[collectd.git] / src / ping.c
index 61bad09..6dcd9ec 100644 (file)
@@ -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"
@@ -72,20 +71,18 @@ 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",
+               DEBUG ("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 ("Successfully added host %s", hl_this->host);
                                /* Remove the host from the linked list */
                                if (hl_prev != NULL)
                                        hl_prev->next = hl_this->next;
@@ -105,7 +102,7 @@ static void add_hosts (void)
                }
                else
                {
-                       hl_this->wait_left -= step;
+                       hl_this->wait_left -= interval_g;
                }
 
                if (hl_this != NULL)
@@ -130,7 +127,7 @@ static int ping_config (const char *key, const char *value)
        {
                if ((pingobj = ping_construct ()) == NULL)
                {
-                       syslog (LOG_ERR, "ping: `ping_construct' failed: %s",
+                       ERROR ("ping: `ping_construct' failed: %s",
                                        ping_get_error (pingobj));
                        return (1);
                }
@@ -140,24 +137,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;
@@ -167,7 +167,7 @@ static int ping_config (const char *key, const char *value)
                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);
+                       WARNING ("ping: liboping did not accept the TTL value %i", ttl);
                        return (1);
                }
        }
@@ -189,7 +189,7 @@ 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.host, hostname_g);
        strcpy (vl.plugin, "ping");
        strcpy (vl.plugin_instance, "");
        strncpy (vl.type_instance, host, sizeof (vl.type_instance));
@@ -213,7 +213,7 @@ static int ping_read (void)
 
        if (ping_send (pingobj) < 0)
        {
-               syslog (LOG_ERR, "ping: `ping_send' failed: %s",
+               ERROR ("ping: `ping_send' failed: %s",
                                ping_get_error (pingobj));
                return (-1);
        }
@@ -232,17 +232,23 @@ static int ping_read (void)
                                        &latency, &buf_len))
                        continue;
 
-               DBG ("host = %s, latency = %f", host, latency);
+               DEBUG ("host = %s, latency = %f", host, latency);
                ping_submit (host, latency);
        }
 
        return (0);
 } /* int ping_read */
 
-void module_register (void)
+void module_register (modreg_e load)
 {
-       plugin_register_data_set (&ds);
-       plugin_register_init ("ping", ping_init);
-       plugin_register_read ("ping", ping_read);
-       plugin_register_config ("ping", ping_config, config_keys, config_keys_num);
+       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);
+       }
 } /* void module_register */