Merge branch 'ff/netlib'
[collectd.git] / src / collectd-nagios.c
index 2d59ea7..e31d95c 100644 (file)
 # include "config.h"
 #endif
 
-#ifndef _ISOC99_SOURCE
-# define _ISOC99_SOURCE
-#endif
-
-#ifndef _POSIX_C_SOURCE
-# define _POSIX_C_SOURCE 200112L
-#endif
-
-#ifndef _XOPEN_SOURCE
-# define _XOPEN_SOURCE 600
-#endif
-
 #if !defined(__GNUC__) || !__GNUC__
 # define __attribute__(x) /**/
 #endif
 #include <string.h>
 #include <strings.h>
 #include <assert.h>
-#include <math.h>
+
+#if NAN_STATIC_DEFAULT
+# include <math.h>
+/* #endif NAN_STATIC_DEFAULT*/
+#elif NAN_STATIC_ISOC
+# ifndef __USE_ISOC99
+#  define DISABLE_ISOC99 1
+#  define __USE_ISOC99 1
+# endif /* !defined(__USE_ISOC99) */
+# include <math.h>
+# if DISABLE_ISOC99
+#  undef DISABLE_ISOC99
+#  undef __USE_ISOC99
+# endif /* DISABLE_ISOC99 */
+/* #endif NAN_STATIC_ISOC */
+#elif NAN_ZERO_ZERO
+# include <math.h>
+# ifdef NAN
+#  undef NAN
+# endif
+# define NAN (0.0 / 0.0)
+# ifndef isnan
+#  define isnan(f) ((f) != (f))
+# endif /* !defined(isnan) */
+# ifndef isfinite
+#  define isfinite(f) (((f) - (f)) == 0.0)
+# endif
+# ifndef isinf
+#  define isinf(f) (!isfinite(f) && !isnan(f))
+# endif
+#endif /* NAN_ZERO_ZERO */
 
 #include "libcollectdclient/collectd/client.h"
 
@@ -251,6 +269,67 @@ static void usage (const char *name)
        exit (1);
 } /* void usage */
 
+static int do_listval (lcc_connection_t *connection)
+{
+       lcc_identifier_t *ret_ident = NULL;
+       size_t ret_ident_num = 0;
+
+       char *hostname = NULL;
+
+       int status;
+       size_t i;
+
+       status = lcc_listval (connection, &ret_ident, &ret_ident_num);
+       if (status != 0) {
+               printf ("UNKNOWN: %s\n", lcc_strerror (connection));
+               if (ret_ident != NULL)
+                       free (ret_ident);
+               return (RET_UNKNOWN);
+       }
+
+       status = lcc_sort_identifiers (connection, ret_ident, ret_ident_num);
+       if (status != 0) {
+               printf ("UNKNOWN: %s\n", lcc_strerror (connection));
+               if (ret_ident != NULL)
+                       free (ret_ident);
+               return (RET_UNKNOWN);
+       }
+
+       for (i = 0; i < ret_ident_num; ++i) {
+               char id[1024];
+
+               if ((hostname_g != NULL) && (strcasecmp (hostname_g, ret_ident[i].host)))
+                       continue;
+
+               if ((hostname == NULL) || strcasecmp (hostname, ret_ident[i].host))
+               {
+                       if (hostname != NULL)
+                               free (hostname);
+                       hostname = strdup (ret_ident[i].host);
+                       printf ("Host: %s\n", hostname);
+               }
+
+               /* empty hostname; not to be printed again */
+               ret_ident[i].host[0] = '\0';
+
+               status = lcc_identifier_to_string (connection,
+                               id, sizeof (id), ret_ident + i);
+               if (status != 0) {
+                       printf ("ERROR: listval: Failed to convert returned "
+                                       "identifier to a string: %s\n",
+                                       lcc_strerror (connection));
+                       continue;
+               }
+
+               /* skip over the (empty) hostname and following '/' */
+               printf ("\t%s\n", id + 1);
+       }
+
+       if (ret_ident != NULL)
+               free (ret_ident);
+       return (RET_OKAY);
+} /* int do_listval */
+
 static int do_check_con_none (size_t values_num,
                double *values, char **values_names)
 {
@@ -490,34 +569,20 @@ static int do_check_con_percentage (size_t values_num,
        return (status_code);
 } /* int do_check_con_percentage */
 
-static int do_check (void)
+static int do_check (lcc_connection_t *connection)
 {
-       lcc_connection_t *connection;
        gauge_t *values;
        char   **values_names;
        size_t   values_num;
-       char address[1024];
        char ident_str[1024];
        lcc_identifier_t ident;
        size_t i;
        int status;
 
-       snprintf (address, sizeof (address), "unix:%s", socket_file_g);
-       address[sizeof (address) - 1] = 0;
-
        snprintf (ident_str, sizeof (ident_str), "%s/%s",
                        hostname_g, value_string_g);
        ident_str[sizeof (ident_str) - 1] = 0;
 
-       connection = NULL;
-       status = lcc_connect (address, &connection);
-       if (status != 0)
-       {
-               printf ("ERROR: Connecting to daemon at %s failed.\n",
-                               socket_file_g);
-               return (RET_CRITICAL);
-       }
-
        memset (&ident, 0, sizeof (ident));
        status = lcc_string_to_identifier (connection, &ident, ident_str);
        if (status != 0)
@@ -565,6 +630,11 @@ static int do_check (void)
 
 int main (int argc, char **argv)
 {
+       char address[1024];
+       lcc_connection_t *connection;
+
+       int status;
+
        range_critical_g.min = NAN;
        range_critical_g.max = NAN;
        range_critical_g.invert = 0;
@@ -646,11 +716,26 @@ int main (int argc, char **argv)
        }
 
        if ((socket_file_g == NULL) || (value_string_g == NULL)
-                       || (hostname_g == NULL))
+                       || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST"))))
        {
                fprintf (stderr, "Missing required arguments.\n");
                usage (argv[0]);
        }
 
-       return (do_check ());
+       snprintf (address, sizeof (address), "unix:%s", socket_file_g);
+       address[sizeof (address) - 1] = 0;
+
+       connection = NULL;
+       status = lcc_connect (address, &connection);
+       if (status != 0)
+       {
+               printf ("ERROR: Connecting to daemon at %s failed.\n",
+                               socket_file_g);
+               return (RET_CRITICAL);
+       }
+
+       if (0 == strcasecmp (value_string_g, "LIST"))
+               return (do_listval (connection));
+
+       return (do_check (connection));
 } /* int main */