-#include "config.h"
+/**
+ * collectd-nagios - src/collectd-nagios.c
+ * Copyright (C) 2008-2010 Florian octo Forster
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ * Florian octo Forster <octo at collectd.org>
+ **/
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#if !defined(__GNUC__) || !__GNUC__
+# define __attribute__(x) /**/
+#endif
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
+#include <strings.h>
#include <assert.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-
-/*
- * This is copied directly from collectd.h. Make changes there!
- */
#if NAN_STATIC_DEFAULT
# include <math.h>
/* #endif NAN_STATIC_DEFAULT*/
# 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"
+
#define RET_OKAY 0
#define RET_WARNING 1
#define RET_CRITICAL 2
#define CON_NONE 0
#define CON_AVERAGE 1
#define CON_SUM 2
+#define CON_PERCENTAGE 3
struct range_s
{
static range_t range_critical_g;
static range_t range_warning_g;
static int consolitation_g = CON_NONE;
+static _Bool nan_is_error_g = 0;
static char **match_ds_g = NULL;
-static int match_ds_num_g = 0;
+static size_t match_ds_num_g = 0;
-static int ignore_ds (const char *name)
+/* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
+ * that, so here's an own implementation.. It's easy enough. The GCC attributes
+ * are supposed to get good performance.. -octo */
+__attribute__((malloc, nonnull (1)))
+static char *cn_strdup (const char *str) /* {{{ */
{
- int i;
+ size_t strsize;
+ char *ret;
+
+ strsize = strlen (str) + 1;
+ ret = (char *) malloc (strsize);
+ if (ret != NULL)
+ memcpy (ret, str, strsize);
+ return (ret);
+} /* }}} char *cn_strdup */
+
+static int filter_ds (size_t *values_num,
+ double **values, char ***values_names)
+{
+ gauge_t *new_values;
+ char **new_names;
+
+ size_t i;
if (match_ds_g == NULL)
- return (0);
+ return (RET_OKAY);
+
+ new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
+ if (new_values == NULL)
+ {
+ fprintf (stderr, "calloc failed: %s\n", strerror (errno));
+ return (RET_UNKNOWN);
+ }
+
+ new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
+ if (new_names == NULL)
+ {
+ fprintf (stderr, "calloc failed: %s\n", strerror (errno));
+ free (new_values);
+ return (RET_UNKNOWN);
+ }
for (i = 0; i < match_ds_num_g; i++)
- if (strcasecmp (match_ds_g[i], name) == 0)
- return (0);
+ {
+ size_t j;
- return (1);
-} /* int ignore_ds */
+ /* match_ds_g keeps pointers into argv but the names will be freed */
+ new_names[i] = cn_strdup (match_ds_g[i]);
+ if (new_names[i] == NULL)
+ {
+ fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
+ free (new_values);
+ for (j = 0; j < i; j++)
+ free (new_names[j]);
+ free (new_names);
+ return (RET_UNKNOWN);
+ }
+
+ for (j = 0; j < *values_num; j++)
+ if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
+ break;
+
+ if (j == *values_num)
+ {
+ printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
+ free (new_values);
+ for (j = 0; j <= i; j++)
+ free (new_names[j]);
+ free (new_names);
+ return (RET_CRITICAL);
+ }
+
+ new_values[i] = (*values)[j];
+ }
+
+ free (*values);
+ for (i = 0; i < *values_num; i++)
+ free ((*values_names)[i]);
+ free (*values_names);
+
+ *values = new_values;
+ *values_names = new_names;
+ *values_num = match_ds_num_g;
+ return (RET_OKAY);
+} /* int filter_ds */
static void parse_range (char *string, range_t *range)
{
range->max = atof (max_ptr);
} /* void parse_range */
-int match_range (range_t *range, double value)
+static int match_range (range_t *range, double value)
{
int ret = 0;
ret = 1;
return (((ret - range->invert) == 0) ? 0 : 1);
-}
-
-static int get_values (int *ret_values_num, double **ret_values,
- char ***ret_values_names)
-{
- struct sockaddr_un sa;
- int status;
- int fd;
- FILE *fh_in, *fh_out;
- char buffer[4096];
-
- int values_num;
- double *values;
- char **values_names;
-
- int i;
- int j;
-
- fd = socket (PF_UNIX, SOCK_STREAM, 0);
- if (fd < 0)
- {
- fprintf (stderr, "socket failed: %s\n",
- strerror (errno));
- return (-1);
- }
-
- memset (&sa, '\0', sizeof (sa));
- sa.sun_family = AF_UNIX;
- strncpy (sa.sun_path, socket_file_g,
- sizeof (sa.sun_path) - 1);
-
- status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
- if (status != 0)
- {
- fprintf (stderr, "connect failed: %s\n",
- strerror (errno));
- return (-1);
- }
-
- fh_in = fdopen (fd, "r");
- if (fh_in == NULL)
- {
- fprintf (stderr, "fdopen failed: %s\n",
- strerror (errno));
- close (fd);
- return (-1);
- }
-
- fh_out = fdopen (fd, "w");
- if (fh_out == NULL)
- {
- fprintf (stderr, "fdopen failed: %s\n",
- strerror (errno));
- fclose (fh_in);
- return (-1);
- }
-
- fprintf (fh_out, "GETVAL %s/%s\n", hostname_g, value_string_g);
- fflush (fh_out);
-
- if (fgets (buffer, sizeof (buffer), fh_in) == NULL)
- {
- fprintf (stderr, "fgets failed: %s\n",
- strerror (errno));
- fclose (fh_in);
- fclose (fh_out);
- return (-1);
- }
-
- {
- char *ptr = strchr (buffer, ' ');
-
- if (ptr != NULL)
- *ptr = '\0';
-
- values_num = atoi (buffer);
- if (values_num < 1)
- return (-1);
- }
-
- values = (double *) malloc (values_num * sizeof (double));
- if (values == NULL)
- {
- fprintf (stderr, "malloc failed: %s\n",
- strerror (errno));
- return (-1);
- }
-
- values_names = (char **) malloc (values_num * sizeof (char *));
- if (values_names == NULL)
- {
- fprintf (stderr, "malloc failed: %s\n",
- strerror (errno));
- free (values);
- return (-1);
- }
- memset (values_names, 0, values_num * sizeof (char *));
-
- i = 0; /* index of the values returned by the server */
- j = 0; /* number of values in `values_names' and `values' */
- while (fgets (buffer, sizeof (buffer), fh_in) != NULL)
- {
- do /* while (0) */
- {
- char *key;
- char *value;
- char *endptr;
-
- key = buffer;
-
- value = strchr (key, '=');
- if (value == NULL)
- {
- fprintf (stderr, "Cannot parse line: %s\n", buffer);
- break;
- }
- *value = 0;
- value++;
-
- if (ignore_ds (key) != 0)
- break;
-
- endptr = NULL;
- errno = 0;
- values[j] = strtod (value, &endptr);
- if ((endptr == value) || (errno != 0))
- {
- fprintf (stderr, "Could not parse buffer "
- "as number: %s\n", value);
- break;
- }
-
- values_names[j] = strdup (key);
- if (values_names[j] == NULL)
- {
- fprintf (stderr, "strdup failed.\n");
- break;
- }
- j++;
- } while (0);
-
- i++;
- if (i >= values_num)
- break;
- }
- /* Set `values_num' to the number of values actually stored in the
- * array. */
- values_num = j;
-
- fclose (fh_in); fh_in = NULL; fd = -1;
- fclose (fh_out); fh_out = NULL;
-
- *ret_values_num = values_num;
- *ret_values = values;
- *ret_values_names = values_names;
-
- return (0);
-} /* int get_values */
+} /* int match_range */
+__attribute__((noreturn))
static void usage (const char *name)
{
fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
" -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
" DSes. By default all DSes are used.\n"
" -g <consol> Method to use to consolidate several DSes.\n"
- " Valid arguments are `none', `average' and `sum'\n"
+ " See below for a list of valid arguments.\n"
" -H <host> Hostname to query the values for.\n"
" -c <range> Critical range\n"
" -w <range> Warning range\n"
+ " -m Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
"\n"
"Consolidation functions:\n"
" none: Apply the warning- and critical-ranges to each data-source\n"
" average: Calculate the average of all matching DSes and apply the\n"
" warning- and critical-ranges to the calculated average.\n"
" sum: Apply the ranges to the sum of all DSes.\n"
+ " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
+ " and the sum of all values."
"\n", name);
exit (1);
} /* void usage */
-int do_check_con_none (int values_num, double *values, char **values_names)
+static int do_listval (lcc_connection_t *connection)
{
- int i;
+ 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))
+ {
+ 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));
+ free (hostname);
+ hostname = NULL;
+ continue;
+ }
+
+ /* skip over the (empty) hostname and following '/' */
+ printf ("\t%s\n", id + 1);
+ }
+
+ free (ret_ident);
+ free (hostname);
+ return (RET_OKAY);
+} /* int do_listval */
+static int do_check_con_none (size_t values_num,
+ double *values, char **values_names)
+{
int num_critical = 0;
int num_warning = 0;
int num_okay = 0;
+ const char *status_str = "UNKNOWN";
+ int status_code = RET_UNKNOWN;
+ size_t i;
for (i = 0; i < values_num; i++)
{
if (isnan (values[i]))
- num_warning++;
+ {
+ if (nan_is_error_g)
+ num_critical++;
+ else
+ num_warning++;
+ }
else if (match_range (&range_critical_g, values[i]) != 0)
num_critical++;
else if (match_range (&range_warning_g, values[i]) != 0)
num_okay++;
}
- printf ("%i critical, %i warning, %i okay",
+ if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
+ {
+ printf ("WARNING: No defined values found\n");
+ return (RET_WARNING);
+ }
+ else if ((num_critical == 0) && (num_warning == 0))
+ {
+ status_str = "OKAY";
+ status_code = RET_OKAY;
+ }
+ else if (num_critical == 0)
+ {
+ status_str = "WARNING";
+ status_code = RET_WARNING;
+ }
+ else
+ {
+ status_str = "CRITICAL";
+ status_code = RET_CRITICAL;
+ }
+
+ printf ("%s: %i critical, %i warning, %i okay", status_str,
num_critical, num_warning, num_okay);
if (values_num > 0)
{
printf (" |");
for (i = 0; i < values_num; i++)
- printf (" %s=%lf;;;;", values_names[i], values[i]);
+ printf (" %s=%f;;;;", values_names[i], values[i]);
}
printf ("\n");
- if ((num_critical != 0) || (values_num == 0))
- return (RET_CRITICAL);
- else if (num_warning != 0)
- return (RET_WARNING);
-
- return (RET_OKAY);
+ return (status_code);
} /* int do_check_con_none */
-int do_check_con_average (int values_num, double *values, char **values_names)
+static int do_check_con_average (size_t values_num,
+ double *values, char **values_names)
{
- int i;
+ size_t i;
double total;
int total_num;
double average;
+ const char *status_str = "UNKNOWN";
+ int status_code = RET_UNKNOWN;
total = 0.0;
total_num = 0;
for (i = 0; i < values_num; i++)
{
- if (!isnan (values[i]))
+ if (isnan (values[i]))
{
- total += values[i];
- total_num++;
+ if (!nan_is_error_g)
+ continue;
+
+ printf ("CRITICAL: Data source \"%s\" is NaN\n",
+ values_names[i]);
+ return (RET_CRITICAL);
}
- }
- if (total_num == 0)
- average = NAN;
- else
- average = total / total_num;
- printf ("%lf average |", average);
- for (i = 0; i < values_num; i++)
- printf (" %s=%lf;;;;", values_names[i], values[i]);
+ total += values[i];
+ total_num++;
+ }
if (total_num == 0)
+ {
+ printf ("WARNING: No defined values found\n");
return (RET_WARNING);
+ }
- if (isnan (average)
- || match_range (&range_critical_g, average))
- return (RET_CRITICAL);
+ average = total / total_num;
+
+ if (match_range (&range_critical_g, average) != 0)
+ {
+ status_str = "CRITICAL";
+ status_code = RET_CRITICAL;
+ }
else if (match_range (&range_warning_g, average) != 0)
- return (RET_WARNING);
+ {
+ status_str = "WARNING";
+ status_code = RET_WARNING;
+ }
+ else
+ {
+ status_str = "OKAY";
+ status_code = RET_OKAY;
+ }
- return (RET_OKAY);
+ printf ("%s: %g average |", status_str, average);
+ for (i = 0; i < values_num; i++)
+ printf (" %s=%f;;;;", values_names[i], values[i]);
+ printf ("\n");
+
+ return (status_code);
} /* int do_check_con_average */
-int do_check_con_sum (int values_num, double *values, char **values_names)
+static int do_check_con_sum (size_t values_num,
+ double *values, char **values_names)
{
- int i;
+ size_t i;
double total;
int total_num;
+ const char *status_str = "UNKNOWN";
+ int status_code = RET_UNKNOWN;
total = 0.0;
total_num = 0;
for (i = 0; i < values_num; i++)
{
- if (!isnan (values[i]))
+ if (isnan (values[i]))
{
- total += values[i];
- total_num++;
+ if (!nan_is_error_g)
+ continue;
+
+ printf ("CRITICAL: Data source \"%s\" is NaN\n",
+ values_names[i]);
+ return (RET_CRITICAL);
}
+
+ total += values[i];
+ total_num++;
}
if (total_num == 0)
if (match_range (&range_critical_g, total) != 0)
{
- printf ("CRITICAL: Sum = %lf\n", total);
- return (RET_CRITICAL);
+ status_str = "CRITICAL";
+ status_code = RET_CRITICAL;
}
else if (match_range (&range_warning_g, total) != 0)
{
- printf ("WARNING: Sum = %lf\n", total);
- return (RET_WARNING);
+ status_str = "WARNING";
+ status_code = RET_WARNING;
}
else
{
- printf ("OKAY: Sum = %lf\n", total);
- return (RET_OKAY);
+ status_str = "OKAY";
+ status_code = RET_OKAY;
}
- return (RET_UNKNOWN);
+ printf ("%s: %g sum |", status_str, total);
+ for (i = 0; i < values_num; i++)
+ printf (" %s=%f;;;;", values_names[i], values[i]);
+ printf ("\n");
+
+ return (status_code);
} /* int do_check_con_sum */
-int do_check (void)
+static int do_check_con_percentage (size_t values_num,
+ double *values, char **values_names)
+{
+ size_t i;
+ double sum = 0.0;
+ double percentage;
+
+ const char *status_str = "UNKNOWN";
+ int status_code = RET_UNKNOWN;
+
+ if ((values_num < 1) || (isnan (values[0])))
+ {
+ printf ("WARNING: The first value is not defined\n");
+ return (RET_WARNING);
+ }
+
+ for (i = 0; i < values_num; i++)
+ {
+ if (isnan (values[i]))
+ {
+ if (!nan_is_error_g)
+ continue;
+
+ printf ("CRITICAL: Data source \"%s\" is NaN\n",
+ values_names[i]);
+ return (RET_CRITICAL);
+ }
+
+ sum += values[i];
+ }
+
+ if (sum == 0.0)
+ {
+ printf ("WARNING: Values sum up to zero\n");
+ return (RET_WARNING);
+ }
+
+ percentage = 100.0 * values[0] / sum;
+
+ if (match_range (&range_critical_g, percentage) != 0)
+ {
+ status_str = "CRITICAL";
+ status_code = RET_CRITICAL;
+ }
+ else if (match_range (&range_warning_g, percentage) != 0)
+ {
+ status_str = "WARNING";
+ status_code = RET_WARNING;
+ }
+ else
+ {
+ status_str = "OKAY";
+ status_code = RET_OKAY;
+ }
+
+ printf ("%s: %lf percent |", status_str, percentage);
+ for (i = 0; i < values_num; i++)
+ printf (" %s=%lf;;;;", values_names[i], values[i]);
+ return (status_code);
+} /* int do_check_con_percentage */
+
+static int do_check (lcc_connection_t *connection)
{
- double *values;
+ gauge_t *values;
char **values_names;
- int values_num;
+ size_t values_num;
+ char ident_str[1024];
+ lcc_identifier_t ident;
+ size_t i;
+ int status;
+
+ snprintf (ident_str, sizeof (ident_str), "%s/%s",
+ hostname_g, value_string_g);
+ ident_str[sizeof (ident_str) - 1] = 0;
- if (get_values (&values_num, &values, &values_names) != 0)
+ memset (&ident, 0, sizeof (ident));
+ status = lcc_string_to_identifier (connection, &ident, ident_str);
+ if (status != 0)
{
- fputs ("ERROR: Cannot get values from daemon\n", stdout);
+ printf ("ERROR: Creating an identifier failed: %s.\n",
+ lcc_strerror (connection));
+ LCC_DESTROY (connection);
return (RET_CRITICAL);
}
+ status = lcc_getval (connection, &ident,
+ &values_num, &values, &values_names);
+ if (status != 0)
+ {
+ printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
+ lcc_strerror (connection));
+ LCC_DESTROY (connection);
+ return (RET_CRITICAL);
+ }
+
+ LCC_DESTROY (connection);
+
+ status = filter_ds (&values_num, &values, &values_names);
+ if (status != RET_OKAY)
+ return (status);
+
+ status = RET_UNKNOWN;
if (consolitation_g == CON_NONE)
- return (do_check_con_none (values_num, values, values_names));
+ status = do_check_con_none (values_num, values, values_names);
else if (consolitation_g == CON_AVERAGE)
- return (do_check_con_average (values_num, values, values_names));
+ status = do_check_con_average (values_num, values, values_names);
else if (consolitation_g == CON_SUM)
- return (do_check_con_sum (values_num, values, values_names));
+ status = do_check_con_sum (values_num, values, values_names);
+ else if (consolitation_g == CON_PERCENTAGE)
+ status = do_check_con_percentage (values_num, values, values_names);
free (values);
- free (values_names); /* FIXME? */
+ if (values_names != NULL)
+ for (i = 0; i < values_num; i++)
+ free (values_names[i]);
+ free (values_names);
- return (RET_UNKNOWN);
-}
+ return (status);
+} /* int do_check */
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;
{
int c;
- c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
+ c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
if (c < 0)
break;
consolitation_g = CON_AVERAGE;
else if (strcasecmp (optarg, "sum") == 0)
consolitation_g = CON_SUM;
+ else if (strcasecmp (optarg, "percentage") == 0)
+ consolitation_g = CON_PERCENTAGE;
else
+ {
+ fprintf (stderr, "Unknown consolidation function `%s'.\n",
+ optarg);
usage (argv[0]);
+ }
break;
case 'd':
{
return (RET_UNKNOWN);
}
match_ds_g = tmp;
- match_ds_g[match_ds_num_g] = strdup (optarg);
+ match_ds_g[match_ds_num_g] = cn_strdup (optarg);
if (match_ds_g[match_ds_num_g] == NULL)
{
- fprintf (stderr, "strdup failed: %s\n",
+ fprintf (stderr, "cn_strdup failed: %s\n",
strerror (errno));
return (RET_UNKNOWN);
}
match_ds_num_g++;
break;
}
+ case 'm':
+ nan_is_error_g = 1;
+ break;
default:
usage (argv[0]);
} /* switch (c) */
}
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]);
+ }
+
+ 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 ());
+ return (do_check (connection));
} /* int main */