2 * collectd-nagios - src/collectd-nagios.c
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
31 #if !defined(__GNUC__) || !__GNUC__
32 # define __attribute__(x) /**/
43 #if NAN_STATIC_DEFAULT
45 /* #endif NAN_STATIC_DEFAULT*/
48 # define DISABLE_ISOC99 1
49 # define __USE_ISOC99 1
50 # endif /* !defined(__USE_ISOC99) */
53 # undef DISABLE_ISOC99
55 # endif /* DISABLE_ISOC99 */
56 /* #endif NAN_STATIC_ISOC */
62 # define NAN (0.0 / 0.0)
64 # define isnan(f) ((f) != (f))
65 # endif /* !defined(isnan) */
67 # define isfinite(f) (((f) - (f)) == 0.0)
70 # define isinf(f) (!isfinite(f) && !isnan(f))
72 #endif /* NAN_ZERO_ZERO */
74 #include "libcollectdclient/collectd/client.h"
78 #define RET_CRITICAL 2
84 #define CON_PERCENTAGE 3
92 typedef struct range_s range_t;
95 extern int optind, opterr, optopt;
97 static char *socket_file_g = NULL;
98 static char *value_string_g = NULL;
99 static char *hostname_g = NULL;
101 static range_t range_critical_g;
102 static range_t range_warning_g;
103 static int consolitation_g = CON_NONE;
104 static _Bool nan_is_error_g = 0;
106 static char **match_ds_g = NULL;
107 static size_t match_ds_num_g = 0;
109 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
110 * that, so here's an own implementation.. It's easy enough. The GCC attributes
111 * are supposed to get good performance.. -octo */
112 __attribute__((malloc, nonnull (1)))
113 static char *cn_strdup (const char *str) /* {{{ */
118 strsize = strlen (str) + 1;
119 ret = (char *) malloc (strsize);
121 memcpy (ret, str, strsize);
123 } /* }}} char *cn_strdup */
125 static int filter_ds (size_t *values_num,
126 double **values, char ***values_names)
131 if (match_ds_g == NULL)
134 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
135 if (new_values == NULL)
137 fprintf (stderr, "calloc failed: %s\n", strerror (errno));
138 return (RET_UNKNOWN);
141 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
142 if (new_names == NULL)
144 fprintf (stderr, "calloc failed: %s\n", strerror (errno));
146 return (RET_UNKNOWN);
149 for (size_t i = 0; i < match_ds_num_g; i++)
153 /* match_ds_g keeps pointers into argv but the names will be freed */
154 new_names[i] = cn_strdup (match_ds_g[i]);
155 if (new_names[i] == NULL)
157 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
159 for (j = 0; j < i; j++)
162 return (RET_UNKNOWN);
165 for (j = 0; j < *values_num; j++)
166 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
169 if (j == *values_num)
171 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
173 for (j = 0; j <= i; j++)
176 return (RET_CRITICAL);
179 new_values[i] = (*values)[j];
183 for (size_t i = 0; i < *values_num; i++)
184 free ((*values_names)[i]);
185 free (*values_names);
187 *values = new_values;
188 *values_names = new_names;
189 *values_num = match_ds_num_g;
191 } /* int filter_ds */
193 static void parse_range (char *string, range_t *range)
204 max_ptr = strchr (string, ':');
217 assert (max_ptr != NULL);
222 /* :10 == ~:10 == -inf:10 */
223 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
226 range->min = atof (min_ptr);
228 if ((*max_ptr == '\0') || (*max_ptr == '~'))
231 range->max = atof (max_ptr);
232 } /* void parse_range */
234 static int match_range (range_t *range, double value)
238 if (!isnan (range->min) && (range->min > value))
240 if (!isnan (range->max) && (range->max < value))
243 return (((ret - range->invert) == 0) ? 0 : 1);
244 } /* int match_range */
246 __attribute__((noreturn))
247 static void usage (const char *name)
249 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
251 "Valid options are:\n"
252 " -s <socket> Path to collectd's UNIX-socket.\n"
253 " -n <v_spec> Value specification to get from collectd.\n"
254 " Format: `plugin-instance/type-instance'\n"
255 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
256 " DSes. By default all DSes are used.\n"
257 " -g <consol> Method to use to consolidate several DSes.\n"
258 " See below for a list of valid arguments.\n"
259 " -H <host> Hostname to query the values for.\n"
260 " -c <range> Critical range\n"
261 " -w <range> Warning range\n"
262 " -m Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
264 "Consolidation functions:\n"
265 " none: Apply the warning- and critical-ranges to each data-source\n"
267 " average: Calculate the average of all matching DSes and apply the\n"
268 " warning- and critical-ranges to the calculated average.\n"
269 " sum: Apply the ranges to the sum of all DSes.\n"
270 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
271 " and the sum of all values."
276 static int do_listval (lcc_connection_t *connection)
278 lcc_identifier_t *ret_ident = NULL;
279 size_t ret_ident_num = 0;
281 char *hostname = NULL;
285 status = lcc_listval (connection, &ret_ident, &ret_ident_num);
287 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
288 if (ret_ident != NULL)
290 return (RET_UNKNOWN);
293 status = lcc_sort_identifiers (connection, ret_ident, ret_ident_num);
295 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
296 if (ret_ident != NULL)
298 return (RET_UNKNOWN);
301 for (size_t i = 0; i < ret_ident_num; ++i) {
304 if ((hostname_g != NULL) && (strcasecmp (hostname_g, ret_ident[i].host)))
307 if ((hostname == NULL) || strcasecmp (hostname, ret_ident[i].host))
310 hostname = strdup (ret_ident[i].host);
311 printf ("Host: %s\n", hostname);
314 /* empty hostname; not to be printed again */
315 ret_ident[i].host[0] = '\0';
317 status = lcc_identifier_to_string (connection,
318 id, sizeof (id), ret_ident + i);
320 printf ("ERROR: listval: Failed to convert returned "
321 "identifier to a string: %s\n",
322 lcc_strerror (connection));
328 /* skip over the (empty) hostname and following '/' */
329 printf ("\t%s\n", id + 1);
335 } /* int do_listval */
337 static int do_check_con_none (size_t values_num,
338 double *values, char **values_names)
340 int num_critical = 0;
343 const char *status_str = "UNKNOWN";
344 int status_code = RET_UNKNOWN;
346 for (size_t i = 0; i < values_num; i++)
348 if (isnan (values[i]))
355 else if (match_range (&range_critical_g, values[i]) != 0)
357 else if (match_range (&range_warning_g, values[i]) != 0)
363 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
365 printf ("WARNING: No defined values found\n");
366 return (RET_WARNING);
368 else if ((num_critical == 0) && (num_warning == 0))
371 status_code = RET_OKAY;
373 else if (num_critical == 0)
375 status_str = "WARNING";
376 status_code = RET_WARNING;
380 status_str = "CRITICAL";
381 status_code = RET_CRITICAL;
384 printf ("%s: %i critical, %i warning, %i okay", status_str,
385 num_critical, num_warning, num_okay);
389 for (size_t i = 0; i < values_num; i++)
390 printf (" %s=%f;;;;", values_names[i], values[i]);
394 return (status_code);
395 } /* int do_check_con_none */
397 static int do_check_con_average (size_t values_num,
398 double *values, char **values_names)
403 const char *status_str = "UNKNOWN";
404 int status_code = RET_UNKNOWN;
408 for (size_t i = 0; i < values_num; i++)
410 if (isnan (values[i]))
415 printf ("CRITICAL: Data source \"%s\" is NaN\n",
417 return (RET_CRITICAL);
426 printf ("WARNING: No defined values found\n");
427 return (RET_WARNING);
430 average = total / total_num;
432 if (match_range (&range_critical_g, average) != 0)
434 status_str = "CRITICAL";
435 status_code = RET_CRITICAL;
437 else if (match_range (&range_warning_g, average) != 0)
439 status_str = "WARNING";
440 status_code = RET_WARNING;
445 status_code = RET_OKAY;
448 printf ("%s: %g average |", status_str, average);
449 for (size_t i = 0; i < values_num; i++)
450 printf (" %s=%f;;;;", values_names[i], values[i]);
453 return (status_code);
454 } /* int do_check_con_average */
456 static int do_check_con_sum (size_t values_num,
457 double *values, char **values_names)
461 const char *status_str = "UNKNOWN";
462 int status_code = RET_UNKNOWN;
466 for (size_t 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 (size_t 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)
518 const char *status_str = "UNKNOWN";
519 int status_code = RET_UNKNOWN;
521 if ((values_num < 1) || (isnan (values[0])))
523 printf ("WARNING: The first value is not defined\n");
524 return (RET_WARNING);
527 for (size_t i = 0; i < values_num; i++)
529 if (isnan (values[i]))
534 printf ("CRITICAL: Data source \"%s\" is NaN\n",
536 return (RET_CRITICAL);
544 printf ("WARNING: Values sum up to zero\n");
545 return (RET_WARNING);
548 percentage = 100.0 * values[0] / sum;
550 if (match_range (&range_critical_g, percentage) != 0)
552 status_str = "CRITICAL";
553 status_code = RET_CRITICAL;
555 else if (match_range (&range_warning_g, percentage) != 0)
557 status_str = "WARNING";
558 status_code = RET_WARNING;
563 status_code = RET_OKAY;
566 printf ("%s: %lf percent |", status_str, percentage);
567 for (size_t i = 0; i < values_num; i++)
568 printf (" %s=%lf;;;;", values_names[i], values[i]);
569 return (status_code);
570 } /* int do_check_con_percentage */
572 static int do_check (lcc_connection_t *connection)
577 char ident_str[1024];
578 lcc_identifier_t ident;
581 snprintf (ident_str, sizeof (ident_str), "%s/%s",
582 hostname_g, value_string_g);
583 ident_str[sizeof (ident_str) - 1] = 0;
585 status = lcc_string_to_identifier (connection, &ident, ident_str);
588 printf ("ERROR: Creating an identifier failed: %s.\n",
589 lcc_strerror (connection));
590 LCC_DESTROY (connection);
591 return (RET_CRITICAL);
594 status = lcc_getval (connection, &ident,
595 &values_num, &values, &values_names);
598 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
599 lcc_strerror (connection));
600 LCC_DESTROY (connection);
601 return (RET_CRITICAL);
604 LCC_DESTROY (connection);
606 status = filter_ds (&values_num, &values, &values_names);
607 if (status != RET_OKAY)
610 status = RET_UNKNOWN;
611 if (consolitation_g == CON_NONE)
612 status = do_check_con_none (values_num, values, values_names);
613 else if (consolitation_g == CON_AVERAGE)
614 status = do_check_con_average (values_num, values, values_names);
615 else if (consolitation_g == CON_SUM)
616 status = do_check_con_sum (values_num, values, values_names);
617 else if (consolitation_g == CON_PERCENTAGE)
618 status = do_check_con_percentage (values_num, values, values_names);
621 if (values_names != NULL)
622 for (size_t i = 0; i < values_num; i++)
623 free (values_names[i]);
629 int main (int argc, char **argv)
632 lcc_connection_t *connection;
636 range_critical_g.min = NAN;
637 range_critical_g.max = NAN;
638 range_critical_g.invert = 0;
640 range_warning_g.min = NAN;
641 range_warning_g.max = NAN;
642 range_warning_g.invert = 0;
648 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
655 parse_range (optarg, &range_critical_g);
658 parse_range (optarg, &range_warning_g);
661 socket_file_g = optarg;
664 value_string_g = optarg;
670 if (strcasecmp (optarg, "none") == 0)
671 consolitation_g = CON_NONE;
672 else if (strcasecmp (optarg, "average") == 0)
673 consolitation_g = CON_AVERAGE;
674 else if (strcasecmp (optarg, "sum") == 0)
675 consolitation_g = CON_SUM;
676 else if (strcasecmp (optarg, "percentage") == 0)
677 consolitation_g = CON_PERCENTAGE;
680 fprintf (stderr, "Unknown consolidation function `%s'.\n",
688 tmp = realloc (match_ds_g,
693 fprintf (stderr, "realloc failed: %s\n",
695 return (RET_UNKNOWN);
698 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
699 if (match_ds_g[match_ds_num_g] == NULL)
701 fprintf (stderr, "cn_strdup failed: %s\n",
703 return (RET_UNKNOWN);
716 if ((socket_file_g == NULL) || (value_string_g == NULL)
717 || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST"))))
719 fprintf (stderr, "Missing required arguments.\n");
723 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
724 address[sizeof (address) - 1] = 0;
727 status = lcc_connect (address, &connection);
730 printf ("ERROR: Connecting to daemon at %s failed.\n",
732 return (RET_CRITICAL);
735 if (0 == strcasecmp (value_string_g, "LIST"))
736 return (do_listval (connection));
738 return (do_check (connection));