2 * collectd-nagios - src/collectd-nagios.c
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #if !defined(__GNUC__) || !__GNUC__
27 # define __attribute__(x) /**/
38 #if NAN_STATIC_DEFAULT
40 /* #endif NAN_STATIC_DEFAULT*/
43 # define DISABLE_ISOC99 1
44 # define __USE_ISOC99 1
45 # endif /* !defined(__USE_ISOC99) */
48 # undef DISABLE_ISOC99
50 # endif /* DISABLE_ISOC99 */
51 /* #endif NAN_STATIC_ISOC */
57 # define NAN (0.0 / 0.0)
59 # define isnan(f) ((f) != (f))
60 # endif /* !defined(isnan) */
62 # define isfinite(f) (((f) - (f)) == 0.0)
65 # define isinf(f) (!isfinite(f) && !isnan(f))
67 #endif /* NAN_ZERO_ZERO */
69 #include "libcollectdclient/collectd/client.h"
73 #define RET_CRITICAL 2
79 #define CON_PERCENTAGE 3
87 typedef struct range_s range_t;
90 extern int optind, opterr, optopt;
92 static char *socket_file_g = NULL;
93 static char *value_string_g = NULL;
94 static char *hostname_g = NULL;
96 static range_t range_critical_g;
97 static range_t range_warning_g;
98 static int consolitation_g = CON_NONE;
99 static _Bool nan_is_error_g = 0;
101 static char **match_ds_g = NULL;
102 static int match_ds_num_g = 0;
104 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
105 * that, so here's an own implementation.. It's easy enough. The GCC attributes
106 * are supposed to get good performance.. -octo */
107 __attribute__((malloc, nonnull (1)))
108 static char *cn_strdup (const char *str) /* {{{ */
113 strsize = strlen (str) + 1;
114 ret = (char *) malloc (strsize);
116 memcpy (ret, str, strsize);
118 } /* }}} char *cn_strdup */
120 static int filter_ds (size_t *values_num,
121 double **values, char ***values_names)
128 if (match_ds_g == NULL)
131 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
132 if (new_values == NULL)
134 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
135 return (RET_UNKNOWN);
138 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
139 if (new_names == NULL)
141 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
143 return (RET_UNKNOWN);
146 for (i = 0; i < (size_t) match_ds_num_g; i++)
150 /* match_ds_g keeps pointers into argv but the names will be freed */
151 new_names[i] = cn_strdup (match_ds_g[i]);
152 if (new_names[i] == NULL)
154 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
156 for (j = 0; j < i; j++)
159 return (RET_UNKNOWN);
162 for (j = 0; j < *values_num; j++)
163 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
166 if (j == *values_num)
168 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
170 for (j = 0; j <= i; j++)
173 return (RET_CRITICAL);
176 new_values[i] = (*values)[j];
180 for (i = 0; i < *values_num; i++)
181 free ((*values_names)[i]);
182 free (*values_names);
184 *values = new_values;
185 *values_names = new_names;
186 *values_num = match_ds_num_g;
188 } /* int filter_ds */
190 static void parse_range (char *string, range_t *range)
201 max_ptr = strchr (string, ':');
214 assert (max_ptr != NULL);
219 /* :10 == ~:10 == -inf:10 */
220 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
223 range->min = atof (min_ptr);
225 if ((*max_ptr == '\0') || (*max_ptr == '~'))
228 range->max = atof (max_ptr);
229 } /* void parse_range */
231 static int match_range (range_t *range, double value)
235 if (!isnan (range->min) && (range->min > value))
237 if (!isnan (range->max) && (range->max < value))
240 return (((ret - range->invert) == 0) ? 0 : 1);
241 } /* int match_range */
243 static void usage (const char *name)
245 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
247 "Valid options are:\n"
248 " -s <socket> Path to collectd's UNIX-socket.\n"
249 " -n <v_spec> Value specification to get from collectd.\n"
250 " Format: `plugin-instance/type-instance'\n"
251 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
252 " DSes. By default all DSes are used.\n"
253 " -g <consol> Method to use to consolidate several DSes.\n"
254 " See below for a list of valid arguments.\n"
255 " -H <host> Hostname to query the values for.\n"
256 " -c <range> Critical range\n"
257 " -w <range> Warning range\n"
258 " -m Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
260 "Consolidation functions:\n"
261 " none: Apply the warning- and critical-ranges to each data-source\n"
263 " average: Calculate the average of all matching DSes and apply the\n"
264 " warning- and critical-ranges to the calculated average.\n"
265 " sum: Apply the ranges to the sum of all DSes.\n"
266 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
267 " and the sum of all values."
272 static int do_listval (lcc_connection_t *connection)
274 lcc_identifier_t *ret_ident = NULL;
275 size_t ret_ident_num = 0;
277 char *hostname = NULL;
282 status = lcc_listval (connection, &ret_ident, &ret_ident_num);
284 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
285 if (ret_ident != NULL)
287 return (RET_UNKNOWN);
290 status = lcc_sort_identifiers (connection, ret_ident, ret_ident_num);
292 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
293 if (ret_ident != NULL)
295 return (RET_UNKNOWN);
298 for (i = 0; i < ret_ident_num; ++i) {
301 if ((hostname_g != NULL) && (strcasecmp (hostname_g, ret_ident[i].host)))
304 if ((hostname == NULL) || strcasecmp (hostname, ret_ident[i].host))
307 hostname = strdup (ret_ident[i].host);
308 printf ("Host: %s\n", hostname);
311 /* empty hostname; not to be printed again */
312 ret_ident[i].host[0] = '\0';
314 status = lcc_identifier_to_string (connection,
315 id, sizeof (id), ret_ident + i);
317 printf ("ERROR: listval: Failed to convert returned "
318 "identifier to a string: %s\n",
319 lcc_strerror (connection));
325 /* skip over the (empty) hostname and following '/' */
326 printf ("\t%s\n", id + 1);
332 } /* int do_listval */
334 static int do_check_con_none (size_t values_num,
335 double *values, char **values_names)
337 int num_critical = 0;
340 const char *status_str = "UNKNOWN";
341 int status_code = RET_UNKNOWN;
344 for (i = 0; i < values_num; i++)
346 if (isnan (values[i]))
353 else if (match_range (&range_critical_g, values[i]) != 0)
355 else if (match_range (&range_warning_g, values[i]) != 0)
361 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
363 printf ("WARNING: No defined values found\n");
364 return (RET_WARNING);
366 else if ((num_critical == 0) && (num_warning == 0))
369 status_code = RET_OKAY;
371 else if (num_critical == 0)
373 status_str = "WARNING";
374 status_code = RET_WARNING;
378 status_str = "CRITICAL";
379 status_code = RET_CRITICAL;
382 printf ("%s: %i critical, %i warning, %i okay", status_str,
383 num_critical, num_warning, num_okay);
387 for (i = 0; i < values_num; i++)
388 printf (" %s=%f;;;;", values_names[i], values[i]);
392 return (status_code);
393 } /* int do_check_con_none */
395 static int do_check_con_average (size_t values_num,
396 double *values, char **values_names)
402 const char *status_str = "UNKNOWN";
403 int status_code = RET_UNKNOWN;
407 for (i = 0; i < values_num; i++)
409 if (isnan (values[i]))
414 printf ("CRITICAL: Data source \"%s\" is NaN\n",
416 return (RET_CRITICAL);
425 printf ("WARNING: No defined values found\n");
426 return (RET_WARNING);
429 average = total / total_num;
431 if (match_range (&range_critical_g, average) != 0)
433 status_str = "CRITICAL";
434 status_code = RET_CRITICAL;
436 else if (match_range (&range_warning_g, average) != 0)
438 status_str = "WARNING";
439 status_code = RET_WARNING;
444 status_code = RET_OKAY;
447 printf ("%s: %g average |", status_str, average);
448 for (i = 0; i < values_num; i++)
449 printf (" %s=%f;;;;", values_names[i], values[i]);
452 return (status_code);
453 } /* int do_check_con_average */
455 static int do_check_con_sum (size_t values_num,
456 double *values, char **values_names)
461 const char *status_str = "UNKNOWN";
462 int status_code = RET_UNKNOWN;
466 for (i = 0; i < values_num; i++)
468 if (isnan (values[i]))
473 printf ("CRITICAL: Data source \"%s\" is NaN\n",
475 return (RET_CRITICAL);
484 printf ("WARNING: No defined values found\n");
485 return (RET_WARNING);
488 if (match_range (&range_critical_g, total) != 0)
490 status_str = "CRITICAL";
491 status_code = RET_CRITICAL;
493 else if (match_range (&range_warning_g, total) != 0)
495 status_str = "WARNING";
496 status_code = RET_WARNING;
501 status_code = RET_OKAY;
504 printf ("%s: %g sum |", status_str, total);
505 for (i = 0; i < values_num; i++)
506 printf (" %s=%f;;;;", values_names[i], values[i]);
509 return (status_code);
510 } /* int do_check_con_sum */
512 static int do_check_con_percentage (size_t values_num,
513 double *values, char **values_names)
519 const char *status_str = "UNKNOWN";
520 int status_code = RET_UNKNOWN;
522 if ((values_num < 1) || (isnan (values[0])))
524 printf ("WARNING: The first value is not defined\n");
525 return (RET_WARNING);
528 for (i = 0; i < values_num; i++)
530 if (isnan (values[i]))
535 printf ("CRITICAL: Data source \"%s\" is NaN\n",
537 return (RET_CRITICAL);
545 printf ("WARNING: Values sum up to zero\n");
546 return (RET_WARNING);
549 percentage = 100.0 * values[0] / sum;
551 if (match_range (&range_critical_g, percentage) != 0)
553 status_str = "CRITICAL";
554 status_code = RET_CRITICAL;
556 else if (match_range (&range_warning_g, percentage) != 0)
558 status_str = "WARNING";
559 status_code = RET_WARNING;
564 status_code = RET_OKAY;
567 printf ("%s: %lf percent |", status_str, percentage);
568 for (i = 0; i < values_num; i++)
569 printf (" %s=%lf;;;;", values_names[i], values[i]);
570 return (status_code);
571 } /* int do_check_con_percentage */
573 static int do_check (lcc_connection_t *connection)
578 char ident_str[1024];
579 lcc_identifier_t ident;
583 snprintf (ident_str, sizeof (ident_str), "%s/%s",
584 hostname_g, value_string_g);
585 ident_str[sizeof (ident_str) - 1] = 0;
587 memset (&ident, 0, sizeof (ident));
588 status = lcc_string_to_identifier (connection, &ident, ident_str);
591 printf ("ERROR: Creating an identifier failed: %s.\n",
592 lcc_strerror (connection));
593 LCC_DESTROY (connection);
594 return (RET_CRITICAL);
597 status = lcc_getval (connection, &ident,
598 &values_num, &values, &values_names);
601 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
602 lcc_strerror (connection));
603 LCC_DESTROY (connection);
604 return (RET_CRITICAL);
607 LCC_DESTROY (connection);
609 status = filter_ds (&values_num, &values, &values_names);
610 if (status != RET_OKAY)
613 status = RET_UNKNOWN;
614 if (consolitation_g == CON_NONE)
615 status = do_check_con_none (values_num, values, values_names);
616 else if (consolitation_g == CON_AVERAGE)
617 status = do_check_con_average (values_num, values, values_names);
618 else if (consolitation_g == CON_SUM)
619 status = do_check_con_sum (values_num, values, values_names);
620 else if (consolitation_g == CON_PERCENTAGE)
621 status = do_check_con_percentage (values_num, values, values_names);
624 if (values_names != NULL)
625 for (i = 0; i < values_num; i++)
626 free (values_names[i]);
632 int main (int argc, char **argv)
635 lcc_connection_t *connection;
639 range_critical_g.min = NAN;
640 range_critical_g.max = NAN;
641 range_critical_g.invert = 0;
643 range_warning_g.min = NAN;
644 range_warning_g.max = NAN;
645 range_warning_g.invert = 0;
651 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
658 parse_range (optarg, &range_critical_g);
661 parse_range (optarg, &range_warning_g);
664 socket_file_g = optarg;
667 value_string_g = optarg;
673 if (strcasecmp (optarg, "none") == 0)
674 consolitation_g = CON_NONE;
675 else if (strcasecmp (optarg, "average") == 0)
676 consolitation_g = CON_AVERAGE;
677 else if (strcasecmp (optarg, "sum") == 0)
678 consolitation_g = CON_SUM;
679 else if (strcasecmp (optarg, "percentage") == 0)
680 consolitation_g = CON_PERCENTAGE;
683 fprintf (stderr, "Unknown consolidation function `%s'.\n",
691 tmp = (char **) realloc (match_ds_g,
696 fprintf (stderr, "realloc failed: %s\n",
698 return (RET_UNKNOWN);
701 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
702 if (match_ds_g[match_ds_num_g] == NULL)
704 fprintf (stderr, "cn_strdup failed: %s\n",
706 return (RET_UNKNOWN);
719 if ((socket_file_g == NULL) || (value_string_g == NULL)
720 || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST"))))
722 fprintf (stderr, "Missing required arguments.\n");
726 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
727 address[sizeof (address) - 1] = 0;
730 status = lcc_connect (address, &connection);
733 printf ("ERROR: Connecting to daemon at %s failed.\n",
735 return (RET_CRITICAL);
738 if (0 == strcasecmp (value_string_g, "LIST"))
739 return (do_listval (connection));
741 return (do_check (connection));