X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcollectd-nagios.c;h=63effd5528bd706a30426d0b22cb557fd91faee9;hb=759f6d627526e42a02c901d2d2d554b597432605;hp=21e877e181b6ae16e0b860246206e0bbf28844d4;hpb=76c0a54209a25f62b1fe16d9b00b41dde5725ae0;p=collectd.git diff --git a/src/collectd-nagios.c b/src/collectd-nagios.c index 21e877e1..63effd55 100644 --- a/src/collectd-nagios.c +++ b/src/collectd-nagios.c @@ -1,3 +1,5 @@ +#include "config.h" + #include #include #include @@ -9,18 +11,32 @@ #include /* - * This weird macro cascade forces the glibc to define `NAN'. I don't know - * another way to solve this, so more intelligent solutions are welcome. -octo + * This is copied directly from collectd.h. Make changes there! */ -#ifndef __USE_ISOC99 -# define DISABLE__USE_ISOC99 1 -# define __USE_ISOC99 1 -#endif -#include -#ifdef DISABLE__USE_ISOC99 -# undef DISABLE__USE_ISOC99 -# undef __USE_ISOC99 -#endif +#if NAN_STATIC_DEFAULT +# include +/* #endif NAN_STATIC_DEFAULT*/ +#elif NAN_STATIC_ISOC +# ifndef __USE_ISOC99 +# define DISABLE_ISOC99 1 +# define __USE_ISOC99 1 +# endif /* !defined(__USE_ISOC99) */ +# include +# if DISABLE_ISOC99 +# undef DISABLE_ISOC99 +# undef __USE_ISOC99 +# endif /* DISABLE_ISOC99 */ +/* #endif NAN_STATIC_ISOC */ +#elif NAN_ZERO_ZERO +# include +# ifdef NAN +# undef NAN +# endif +# define NAN (0.0 / 0.0) +# ifndef isnan +# define isnan(f) ((f) != (f)) +# endif /* !defined(isnan) */ +#endif /* NAN_ZERO_ZERO */ #define RET_OKAY 0 #define RET_WARNING 1 @@ -44,10 +60,29 @@ extern int optind, opterr, optopt; static char *socket_file_g = NULL; static char *value_string_g = NULL; +static char *hostname_g = NULL; + static range_t range_critical_g; static range_t range_warning_g; static int consolitation_g = CON_NONE; +static char **match_ds_g = NULL; +static int match_ds_num_g = 0; + +static int ignore_ds (const char *name) +{ + int i; + + if (match_ds_g == NULL) + return (0); + + for (i = 0; i < match_ds_num_g; i++) + if (strcasecmp (match_ds_g[i], name) == 0) + return (0); + + return (1); +} /* int ignore_ds */ + static void parse_range (char *string, range_t *range) { char *min_ptr; @@ -93,9 +128,9 @@ int match_range (range_t *range, double value) { int ret = 0; - if ((range->min != NAN) && (range->min > value)) + if (!isnan (range->min) && (range->min > value)) ret = 1; - if ((range->max != NAN) && (range->max < value)) + if (!isnan (range->max) && (range->max < value)) ret = 1; return (((ret - range->invert) == 0) ? 0 : 1); @@ -146,7 +181,7 @@ static int get_values (int *ret_values_num, double **ret_values, return (-1); } - fprintf (fh, "GETVAL %s\n", value_string_g); + fprintf (fh, "GETVAL %s/%s\n", hostname_g, value_string_g); fflush (fh); if (fgets (buffer, sizeof (buffer), fh) == NULL) @@ -193,6 +228,9 @@ static int get_values (int *ret_values_num, double **ret_values, continue; *value = '\0'; value++; + if (ignore_ds (key) != 0) + continue; + values_names[i] = strdup (key); values[i] = atof (value); @@ -212,14 +250,27 @@ static int get_values (int *ret_values_num, double **ret_values, static void usage (const char *name) { - fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> [options]\n" + fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n" "\n" "Valid options are:\n" - " -s Path to collectd's UNIX-socket\n" - " -n Value specification to get from collectd\n" - " -c Critical range\n" - " -w Range for critical values\n", - name); + " -s Path to collectd's UNIX-socket.\n" + " -n Value specification to get from collectd.\n" + " Format: `plugin-instance/type-instance'\n" + " -d Select the DS to examine. May be repeated to examine multiple\n" + " DSes. By default all DSes are used.\n" + " -g Method to use to consolidate several DSes.\n" + " Valid arguments are `none', `average' and `sum'\n" + " -H Hostname to query the values for.\n" + " -c Critical range\n" + " -w Warning range\n" + "\n" + "Consolidation functions:\n" + " none: Apply the warning- and critical-ranges to each data-source\n" + " individually.\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" + "\n", name); exit (1); } /* void usage */ @@ -233,7 +284,7 @@ int do_check_con_none (int values_num, double *values, char **values_names) for (i = 0; i < values_num; i++) { - if (values[i] == NAN) + if (isnan (values[i])) num_warning++; else if (match_range (&range_critical_g, values[i]) != 0) num_critical++; @@ -243,25 +294,22 @@ int do_check_con_none (int values_num, double *values, char **values_names) num_okay++; } - if ((num_critical != 0) || (values_num == 0)) + printf ("%i critical, %i warning, %i okay", + num_critical, num_warning, num_okay); + if (values_num > 0) { - printf ("CRITICAL: %i critical, %i warning, %i okay\n", - num_critical, num_warning, num_okay); - return (RET_CRITICAL); + printf (" |"); + for (i = 0; i < values_num; i++) + printf (" %s=%lf;;;;", values_names[i], values[i]); } + printf ("\n"); + + if ((num_critical != 0) || (values_num == 0)) + return (RET_CRITICAL); else if (num_warning != 0) - { - printf ("WARNING: %i warning, %i okay\n", - num_warning, num_okay); return (RET_WARNING); - } - else - { - printf ("OKAY: %i okay\n", num_okay); - return (RET_OKAY); - } - return (RET_UNKNOWN); + return (RET_OKAY); } /* int do_check_con_none */ int do_check_con_average (int values_num, double *values, char **values_names) @@ -269,12 +317,13 @@ int do_check_con_average (int values_num, double *values, char **values_names) int i; double total; int total_num; + double average; total = 0.0; total_num = 0; for (i = 0; i < values_num; i++) { - if (values[i] != NAN) + if (!isnan (values[i])) { total += values[i]; total_num++; @@ -282,31 +331,23 @@ int do_check_con_average (int values_num, double *values, char **values_names) } if (total_num == 0) - { - printf ("WARNING: No defined values found\n"); + 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]); + + if (total_num == 0) return (RET_WARNING); - } - if (match_range (&range_critical_g, total / total_num) != 0) - { - printf ("CRITICAL: Average = %lf\n", - (double) (total / total_num)); + if (isnan (average) + || match_range (&range_critical_g, average)) return (RET_CRITICAL); - } - else if (match_range (&range_warning_g, total / total_num) != 0) - { - printf ("WARNING: Average = %lf\n", - (double) (total / total_num)); + else if (match_range (&range_warning_g, average) != 0) return (RET_WARNING); - } - else - { - printf ("OKAY: Average = %lf\n", - (double) (total / total_num)); - return (RET_OKAY); - } - return (RET_UNKNOWN); + return (RET_OKAY); } /* int do_check_con_average */ int do_check_con_sum (int values_num, double *values, char **values_names) @@ -319,7 +360,7 @@ int do_check_con_sum (int values_num, double *values, char **values_names) total_num = 0; for (i = 0; i < values_num; i++) { - if (values[i] != NAN) + if (!isnan (values[i])) { total += values[i]; total_num++; @@ -356,7 +397,6 @@ int do_check (void) double *values; char **values_names; int values_num; - int i; if (get_values (&values_num, &values, &values_names) != 0) { @@ -364,9 +404,6 @@ int do_check (void) return (RET_CRITICAL); } - for (i = 0; i < values_num; i++) - printf ("%s=%lf\n", values_names[i], values[i]); - if (consolitation_g == CON_NONE) return (do_check_con_none (values_num, values, values_names)); else if (consolitation_g == CON_AVERAGE) @@ -374,6 +411,9 @@ int do_check (void) else if (consolitation_g == CON_SUM) return (do_check_con_sum (values_num, values, values_names)); + free (values); + free (values_names); /* FIXME? */ + return (RET_UNKNOWN); } @@ -391,7 +431,7 @@ int main (int argc, char **argv) { int c; - c = getopt (argc, argv, "w:c:s:n:g:h"); + c = getopt (argc, argv, "w:c:s:n:H:g:d:h"); if (c < 0) break; @@ -409,6 +449,9 @@ int main (int argc, char **argv) case 'n': value_string_g = optarg; break; + case 'H': + hostname_g = optarg; + break; case 'g': if (strcasecmp (optarg, "none") == 0) consolitation_g = CON_NONE; @@ -419,12 +462,36 @@ int main (int argc, char **argv) else usage (argv[0]); break; + case 'd': + { + char **tmp; + tmp = (char **) realloc (match_ds_g, + (match_ds_num_g + 1) + * sizeof (char *)); + if (tmp == NULL) + { + fprintf (stderr, "realloc failed: %s\n", + strerror (errno)); + return (RET_UNKNOWN); + } + match_ds_g = tmp; + match_ds_g[match_ds_num_g] = strdup (optarg); + if (match_ds_g[match_ds_num_g] == NULL) + { + fprintf (stderr, "strdup failed: %s\n", + strerror (errno)); + return (RET_UNKNOWN); + } + match_ds_num_g++; + break; + } default: usage (argv[0]); } /* switch (c) */ } - if ((socket_file_g == NULL) || (value_string_g == NULL)) + if ((socket_file_g == NULL) || (value_string_g == NULL) + || (hostname_g == NULL)) usage (argv[0]); return (do_check ());