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)
133 if (match_ds_g == NULL)
136 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
137 if (new_values == NULL)
139 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
140 return (RET_UNKNOWN);
143 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
144 if (new_names == NULL)
146 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
148 return (RET_UNKNOWN);
151 for (i = 0; i < match_ds_num_g; i++)
155 /* match_ds_g keeps pointers into argv but the names will be freed */
156 new_names[i] = cn_strdup (match_ds_g[i]);
157 if (new_names[i] == NULL)
159 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
161 for (j = 0; j < i; j++)
164 return (RET_UNKNOWN);
167 for (j = 0; j < *values_num; j++)
168 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
171 if (j == *values_num)
173 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
175 for (j = 0; j <= i; j++)
178 return (RET_CRITICAL);
181 new_values[i] = (*values)[j];
185 for (i = 0; i < *values_num; i++)
186 free ((*values_names)[i]);
187 free (*values_names);
189 *values = new_values;
190 *values_names = new_names;
191 *values_num = match_ds_num_g;
193 } /* int filter_ds */
195 static void parse_range (char *string, range_t *range)
206 max_ptr = strchr (string, ':');
219 assert (max_ptr != NULL);
224 /* :10 == ~:10 == -inf:10 */
225 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
228 range->min = atof (min_ptr);
230 if ((*max_ptr == '\0') || (*max_ptr == '~'))
233 range->max = atof (max_ptr);
234 } /* void parse_range */
236 static int match_range (range_t *range, double value)
240 if (!isnan (range->min) && (range->min > value))
242 if (!isnan (range->max) && (range->max < value))
245 return (((ret - range->invert) == 0) ? 0 : 1);
246 } /* int match_range */
248 static void usage (const char *name)
250 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
252 "Valid options are:\n"
253 " -s <socket> Path to collectd's UNIX-socket.\n"
254 " -n <v_spec> Value specification to get from collectd.\n"
255 " Format: `plugin-instance/type-instance'\n"
256 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
257 " DSes. By default all DSes are used.\n"
258 " -g <consol> Method to use to consolidate several DSes.\n"
259 " See below for a list of valid arguments.\n"
260 " -H <host> Hostname to query the values for.\n"
261 " -c <range> Critical range\n"
262 " -w <range> Warning range\n"
263 " -m Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
265 "Consolidation functions:\n"
266 " none: Apply the warning- and critical-ranges to each data-source\n"
268 " average: Calculate the average of all matching DSes and apply the\n"
269 " warning- and critical-ranges to the calculated average.\n"
270 " sum: Apply the ranges to the sum of all DSes.\n"
271 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
272 " and the sum of all values."
277 static int do_listval (lcc_connection_t *connection)
279 lcc_identifier_t *ret_ident = NULL;
280 size_t ret_ident_num = 0;
282 char *hostname = NULL;
287 status = lcc_listval (connection, &ret_ident, &ret_ident_num);
289 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
290 if (ret_ident != NULL)
292 return (RET_UNKNOWN);
295 status = lcc_sort_identifiers (connection, ret_ident, ret_ident_num);
297 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
298 if (ret_ident != NULL)
300 return (RET_UNKNOWN);
303 for (i = 0; i < ret_ident_num; ++i) {
306 if ((hostname_g != NULL) && (strcasecmp (hostname_g, ret_ident[i].host)))
309 if ((hostname == NULL) || strcasecmp (hostname, ret_ident[i].host))
311 if (hostname != NULL)
313 hostname = strdup (ret_ident[i].host);
314 printf ("Host: %s\n", hostname);
317 /* empty hostname; not to be printed again */
318 ret_ident[i].host[0] = '\0';
320 status = lcc_identifier_to_string (connection,
321 id, sizeof (id), ret_ident + i);
323 printf ("ERROR: listval: Failed to convert returned "
324 "identifier to a string: %s\n",
325 lcc_strerror (connection));
329 /* skip over the (empty) hostname and following '/' */
330 printf ("\t%s\n", id + 1);
333 if (ret_ident != NULL)
336 } /* int do_listval */
338 static int do_check_con_none (size_t values_num,
339 double *values, char **values_names)
341 int num_critical = 0;
344 const char *status_str = "UNKNOWN";
345 int status_code = RET_UNKNOWN;
348 for (i = 0; i < values_num; i++)
350 if (isnan (values[i]))
357 else if (match_range (&range_critical_g, values[i]) != 0)
359 else if (match_range (&range_warning_g, values[i]) != 0)
365 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
367 printf ("WARNING: No defined values found\n");
368 return (RET_WARNING);
370 else if ((num_critical == 0) && (num_warning == 0))
373 status_code = RET_OKAY;
375 else if (num_critical == 0)
377 status_str = "WARNING";
378 status_code = RET_WARNING;
382 status_str = "CRITICAL";
383 status_code = RET_CRITICAL;
386 printf ("%s: %i critical, %i warning, %i okay", status_str,
387 num_critical, num_warning, num_okay);
391 for (i = 0; i < values_num; i++)
392 printf (" %s=%f;;;;", values_names[i], values[i]);
396 return (status_code);
397 } /* int do_check_con_none */
399 static int do_check_con_average (size_t values_num,
400 double *values, char **values_names)
406 const char *status_str = "UNKNOWN";
407 int status_code = RET_UNKNOWN;
411 for (i = 0; i < values_num; i++)
413 if (isnan (values[i]))
418 printf ("CRITICAL: Data source \"%s\" is NaN\n",
420 return (RET_CRITICAL);
429 printf ("WARNING: No defined values found\n");
430 return (RET_WARNING);
433 average = total / total_num;
435 if (match_range (&range_critical_g, average) != 0)
437 status_str = "CRITICAL";
438 status_code = RET_CRITICAL;
440 else if (match_range (&range_warning_g, average) != 0)
442 status_str = "WARNING";
443 status_code = RET_WARNING;
448 status_code = RET_OKAY;
451 printf ("%s: %g average |", status_str, average);
452 for (i = 0; i < values_num; i++)
453 printf (" %s=%f;;;;", values_names[i], values[i]);
456 return (status_code);
457 } /* int do_check_con_average */
459 static int do_check_con_sum (size_t values_num,
460 double *values, char **values_names)
465 const char *status_str = "UNKNOWN";
466 int status_code = RET_UNKNOWN;
470 for (i = 0; i < values_num; i++)
472 if (isnan (values[i]))
477 printf ("CRITICAL: Data source \"%s\" is NaN\n",
479 return (RET_CRITICAL);
488 printf ("WARNING: No defined values found\n");
489 return (RET_WARNING);
492 if (match_range (&range_critical_g, total) != 0)
494 status_str = "CRITICAL";
495 status_code = RET_CRITICAL;
497 else if (match_range (&range_warning_g, total) != 0)
499 status_str = "WARNING";
500 status_code = RET_WARNING;
505 status_code = RET_OKAY;
508 printf ("%s: %g sum |", status_str, total);
509 for (i = 0; i < values_num; i++)
510 printf (" %s=%f;;;;", values_names[i], values[i]);
513 return (status_code);
514 } /* int do_check_con_sum */
516 static int do_check_con_percentage (size_t values_num,
517 double *values, char **values_names)
523 const char *status_str = "UNKNOWN";
524 int status_code = RET_UNKNOWN;
526 if ((values_num < 1) || (isnan (values[0])))
528 printf ("WARNING: The first value is not defined\n");
529 return (RET_WARNING);
532 for (i = 0; i < values_num; i++)
534 if (isnan (values[i]))
539 printf ("CRITICAL: Data source \"%s\" is NaN\n",
541 return (RET_CRITICAL);
549 printf ("WARNING: Values sum up to zero\n");
550 return (RET_WARNING);
553 percentage = 100.0 * values[0] / sum;
555 if (match_range (&range_critical_g, percentage) != 0)
557 status_str = "CRITICAL";
558 status_code = RET_CRITICAL;
560 else if (match_range (&range_warning_g, percentage) != 0)
562 status_str = "WARNING";
563 status_code = RET_WARNING;
568 status_code = RET_OKAY;
571 printf ("%s: %lf percent |", status_str, percentage);
572 for (i = 0; i < values_num; i++)
573 printf (" %s=%lf;;;;", values_names[i], values[i]);
574 return (status_code);
575 } /* int do_check_con_percentage */
577 static int do_check (lcc_connection_t *connection)
582 char ident_str[1024];
583 lcc_identifier_t ident;
587 snprintf (ident_str, sizeof (ident_str), "%s/%s",
588 hostname_g, value_string_g);
589 ident_str[sizeof (ident_str) - 1] = 0;
591 memset (&ident, 0, sizeof (ident));
592 status = lcc_string_to_identifier (connection, &ident, ident_str);
595 printf ("ERROR: Creating an identifier failed: %s.\n",
596 lcc_strerror (connection));
597 LCC_DESTROY (connection);
598 return (RET_CRITICAL);
601 status = lcc_getval (connection, &ident,
602 &values_num, &values, &values_names);
605 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
606 lcc_strerror (connection));
607 LCC_DESTROY (connection);
608 return (RET_CRITICAL);
611 LCC_DESTROY (connection);
613 status = filter_ds (&values_num, &values, &values_names);
614 if (status != RET_OKAY)
617 status = RET_UNKNOWN;
618 if (consolitation_g == CON_NONE)
619 status = do_check_con_none (values_num, values, values_names);
620 else if (consolitation_g == CON_AVERAGE)
621 status = do_check_con_average (values_num, values, values_names);
622 else if (consolitation_g == CON_SUM)
623 status = do_check_con_sum (values_num, values, values_names);
624 else if (consolitation_g == CON_PERCENTAGE)
625 status = do_check_con_percentage (values_num, values, values_names);
628 if (values_names != NULL)
629 for (i = 0; i < values_num; i++)
630 free (values_names[i]);
636 int main (int argc, char **argv)
639 lcc_connection_t *connection;
643 range_critical_g.min = NAN;
644 range_critical_g.max = NAN;
645 range_critical_g.invert = 0;
647 range_warning_g.min = NAN;
648 range_warning_g.max = NAN;
649 range_warning_g.invert = 0;
655 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
662 parse_range (optarg, &range_critical_g);
665 parse_range (optarg, &range_warning_g);
668 socket_file_g = optarg;
671 value_string_g = optarg;
677 if (strcasecmp (optarg, "none") == 0)
678 consolitation_g = CON_NONE;
679 else if (strcasecmp (optarg, "average") == 0)
680 consolitation_g = CON_AVERAGE;
681 else if (strcasecmp (optarg, "sum") == 0)
682 consolitation_g = CON_SUM;
683 else if (strcasecmp (optarg, "percentage") == 0)
684 consolitation_g = CON_PERCENTAGE;
687 fprintf (stderr, "Unknown consolidation function `%s'.\n",
695 tmp = (char **) realloc (match_ds_g,
700 fprintf (stderr, "realloc failed: %s\n",
702 return (RET_UNKNOWN);
705 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
706 if (match_ds_g[match_ds_num_g] == NULL)
708 fprintf (stderr, "cn_strdup failed: %s\n",
710 return (RET_UNKNOWN);
723 if ((socket_file_g == NULL) || (value_string_g == NULL)
724 || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST"))))
726 fprintf (stderr, "Missing required arguments.\n");
730 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
731 address[sizeof (address) - 1] = 0;
734 status = lcc_connect (address, &connection);
737 printf ("ERROR: Connecting to daemon at %s failed.\n",
739 return (RET_CRITICAL);
742 if (0 == strcasecmp (value_string_g, "LIST"))
743 return (do_listval (connection));
745 return (do_check (connection));