contrib/exec-nagios.px: Added a Perl script which handles Nagios plugins.
[collectd.git] / src / ping.c
index 9c318ce..27a65fa 100644 (file)
@@ -1,11 +1,10 @@
 /**
  * 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
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
+ * Free Software Foundation; only version 2 of the License is applicable.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  *   Florian octo Forster <octo at verplant.org>
  **/
 
-#include "ping.h"
-
-#if COLLECT_PING
-#define MODULE_NAME "ping"
-
-#include "plugin.h"
+#include "collectd.h"
 #include "common.h"
+#include "plugin.h"
 #include "configfile.h"
 
 #include <netinet/in.h>
-#include "libping/ping.h"
-
-extern char *pinghosts[MAX_PINGHOSTS];
-extern int   num_pinghosts;
-static int   pingerrors[MAX_PINGHOSTS];
-
-static char *file_template = "ping-%s.rrd";
+#include "liboping/oping.h"
 
-static char *ds_def[] = 
+/*
+ * Private data types
+ */
+struct hostlist_s
 {
-       "DS:ping:GAUGE:25:0:65535",
-       NULL
+       char *host;
+       int   wait_time;
+       int   wait_left;
+       struct hostlist_s *next;
 };
-static int ds_num = 1;
+typedef struct hostlist_s hostlist_t;
+
+/*
+ * Private variables
+ */
+static pingobj_t *pingobj = NULL;
+static hostlist_t *hosts = NULL;
 
-static char *config_keys[] =
+static const char *config_keys[] =
 {
        "Host",
+       "TTL",
        NULL
 };
-static int config_keys_num = 1;
-
-extern time_t curtime;
+static int config_keys_num = 2;
 
-void ping_init (void)
+/*
+ * Private functions
+ */
+static void add_hosts (void)
 {
-       int i;
+       hostlist_t *hl_this;
+       hostlist_t *hl_prev;
+
+       hl_this = hosts;
+       hl_prev = NULL;
+       while (hl_this != NULL)
+       {
+               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)
+                       {
+                               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;
+                               else
+                                       hosts = hl_this->next;
+                               free (hl_this->host);
+                               free (hl_this);
+                               hl_this = (hl_prev != NULL) ? hl_prev : hosts;
+                       }
+                       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)
+                                       hl_this->wait_time = 86400;
+                       }
+               }
+               else
+               {
+                       hl_this->wait_left -= interval_g;
+               }
+
+               if (hl_this != NULL)
+               {
+                       hl_prev = hl_this;
+                       hl_this = hl_this->next;
+               }
+       }
+}
 
-       for (i = 0; i < num_pinghosts; i++)
-               pingerrors[i] = 0;
+static int ping_init (void)
+{
+       if (hosts != NULL)
+               add_hosts ();
 
-       return;
+       return (0);
 }
 
-int ping_config (char *key, char *value)
+static int ping_config (const char *key, const char *value)
 {
-       if (strcasecmp (key, "host"))
+       if (pingobj == NULL)
        {
-               return (-1);
+               if ((pingobj = ping_construct ()) == NULL)
+               {
+                       ERROR ("ping plugin: `ping_construct' failed.");
+                       return (1);
+               }
        }
-       else if (num_pinghosts >= MAX_PINGHOSTS)
+
+       if (strcasecmp (key, "host") == 0)
        {
-               return (1);
+               hostlist_t *hl;
+               char *host;
+
+               if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
+               {
+                       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);
+                       ERROR ("ping plugin: strdup failed: %s",
+                                       sstrerror (errno, errbuf,
+                                               sizeof (errbuf)));
+                       return (1);
+               }
+
+               hl->host = host;
+               hl->wait_time = 2 * interval_g;
+               hl->wait_left = 0;
+               hl->next = hosts;
+               hosts = hl;
        }
-       else if ((pinghosts[num_pinghosts] = strdup (value)) == NULL)
+       else if (strcasecmp (key, "ttl") == 0)
        {
-               return (2);
+               int ttl = atoi (value);
+               if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
+               {
+                       WARNING ("ping: liboping did not accept the TTL value %i", ttl);
+                       return (1);
+               }
        }
        else
        {
-               pingerrors[num_pinghosts] = 0;
-               num_pinghosts++;
-               return (0);
+               return (-1);
        }
+
+       return (0);
 }
 
-void ping_write (char *host, char *inst, char *val)
+static void ping_submit (char *host, double latency)
 {
-       char file[512];
-       int status;
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].gauge = latency;
 
-       status = snprintf (file, 512, file_template, inst);
-       if (status < 1)
-               return;
-       else if (status >= 512)
-               return;
+       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));
 
-       rrd_update_file (host, file, val, ds_def, ds_num);
+       plugin_dispatch_values ("ping", &vl);
 }
 
-#define BUFSIZE 256
-void ping_submit (int ping_time, char *host)
+static int ping_read (void)
 {
-       char buf[BUFSIZE];
+       pingobj_iter_t *iter;
 
-       if (snprintf (buf, BUFSIZE, "%u:%u", (unsigned int) curtime, ping_time) >= BUFSIZE)
-               return;
+       char   host[512];
+       double latency;
+       size_t buf_len;
+       int    number_of_hosts;
 
-       plugin_submit (MODULE_NAME, host, buf);
-}
-#undef BUFSIZE
+       if (pingobj == NULL)
+               return (-1);
 
-void ping_read (void)
-{
-       int ping;
-       int i;
+       if (hosts != NULL)
+               add_hosts ();
+
+       if (ping_send (pingobj) < 0)
+       {
+               ERROR ("ping plugin: `ping_send' failed: %s",
+                               ping_get_error (pingobj));
+               return (-1);
+       }
 
-       for (i = 0; i < num_pinghosts; i++)
+       number_of_hosts = 0;
+       for (iter = ping_iterator_get (pingobj);
+                       iter != NULL;
+                       iter = ping_iterator_next (iter))
        {
-               if (pingerrors[i] & 0x30)
+               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;
-               
-               ping = tpinghost (pinghosts[i]);
+               }
 
-               switch (ping)
+               buf_len = sizeof (latency);
+               if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
+                                       &latency, &buf_len))
                {
-                       case 0:
-                               if (!(pingerrors[i] & 0x01))
-                                       syslog (LOG_WARNING, "ping %s: Connection timed out.", pinghosts[i]);
-                               pingerrors[i] |= 0x01;
-                               break;
-
-                       case -1:
-                               if (!(pingerrors[i] & 0x02))
-                                       syslog (LOG_WARNING, "ping %s: Host or service is not reachable.", pinghosts[i]);
-                               pingerrors[i] |= 0x02;
-                               break;
-
-                       case -2:
-                               syslog (LOG_ERR, "ping %s: Socket error. Ping will be disabled.", pinghosts[i]);
-                               pingerrors[i] |= 0x10;
-                               break;
-
-                       case -3:
-                               if (!(pingerrors[i] & 0x04))
-                                       syslog (LOG_WARNING, "ping %s: Connection refused.", pinghosts[i]);
-                               pingerrors[i] |= 0x04;
-                               break;
-
-                       default:
-                               if (pingerrors[i] != 0x00)
-                                       syslog (LOG_NOTICE, "ping %s: Back to normal: %ims.", pinghosts[i], ping);
-                               pingerrors[i] = 0x00;
-                               ping_submit (ping, pinghosts[i]);
-               } /* switch (ping) */
-       } /* for (i = 0; i < num_pinghosts; i++) */
-}
+                       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 (void)
 {
-       plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
-       cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);
-}
-
-#undef MODULE_NAME
-#endif /* COLLECT_PING */
+       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 */