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 #ifndef _ISOC99_SOURCE
27 # define _ISOC99_SOURCE
30 #ifndef _POSIX_C_SOURCE
31 # define _POSIX_C_SOURCE 200112L
35 # define _XOPEN_SOURCE 600
38 #if !defined(__GNUC__) || !__GNUC__
39 # define __attribute__(x) /**/
51 #include "libcollectdclient/client.h"
54 * This is copied directly from collectd.h. Make changes there!
58 #define RET_CRITICAL 2
64 #define CON_PERCENTAGE 3
72 typedef struct range_s range_t;
75 extern int optind, opterr, optopt;
77 static char *socket_file_g = NULL;
78 static char *value_string_g = NULL;
79 static char *hostname_g = NULL;
81 static range_t range_critical_g;
82 static range_t range_warning_g;
83 static int consolitation_g = CON_NONE;
84 static _Bool nan_is_error_g = 0;
86 static char **match_ds_g = NULL;
87 static int match_ds_num_g = 0;
89 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
90 * that, so here's an own implementation.. It's easy enough. The GCC attributes
91 * are supposed to get good performance.. -octo */
92 __attribute__((malloc, nonnull (1)))
93 static char *cn_strdup (const char *str) /* {{{ */
98 strsize = strlen (str) + 1;
99 ret = (char *) malloc (strsize);
101 memcpy (ret, str, strsize);
103 } /* }}} char *cn_strdup */
105 static int filter_ds (size_t *values_num,
106 double **values, char ***values_names)
113 if (match_ds_g == NULL)
116 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
117 if (new_values == NULL)
119 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
120 return (RET_UNKNOWN);
123 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
124 if (new_names == NULL)
126 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
128 return (RET_UNKNOWN);
131 for (i = 0; i < (size_t) match_ds_num_g; i++)
135 /* match_ds_g keeps pointers into argv but the names will be freed */
136 new_names[i] = cn_strdup (match_ds_g[i]);
137 if (new_names[i] == NULL)
139 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
141 for (j = 0; j < i; j++)
144 return (RET_UNKNOWN);
147 for (j = 0; j < *values_num; j++)
148 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
151 if (j == *values_num)
153 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
155 for (j = 0; j <= i; j++)
158 return (RET_CRITICAL);
161 new_values[i] = (*values)[j];
165 for (i = 0; i < *values_num; i++)
166 free ((*values_names)[i]);
167 free (*values_names);
169 *values = new_values;
170 *values_names = new_names;
171 *values_num = match_ds_num_g;
173 } /* int filter_ds */
175 static void parse_range (char *string, range_t *range)
186 max_ptr = strchr (string, ':');
199 assert (max_ptr != NULL);
204 /* :10 == ~:10 == -inf:10 */
205 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
208 range->min = atof (min_ptr);
210 if ((*max_ptr == '\0') || (*max_ptr == '~'))
213 range->max = atof (max_ptr);
214 } /* void parse_range */
216 static int match_range (range_t *range, double value)
220 if (!isnan (range->min) && (range->min > value))
222 if (!isnan (range->max) && (range->max < value))
225 return (((ret - range->invert) == 0) ? 0 : 1);
226 } /* int match_range */
228 static void usage (const char *name)
230 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
232 "Valid options are:\n"
233 " -s <socket> Path to collectd's UNIX-socket.\n"
234 " -n <v_spec> Value specification to get from collectd.\n"
235 " Format: `plugin-instance/type-instance'\n"
236 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
237 " DSes. By default all DSes are used.\n"
238 " -g <consol> Method to use to consolidate several DSes.\n"
239 " See below for a list of valid arguments.\n"
240 " -H <host> Hostname to query the values for.\n"
241 " -c <range> Critical range\n"
242 " -w <range> Warning range\n"
243 " -m Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
245 "Consolidation functions:\n"
246 " none: Apply the warning- and critical-ranges to each data-source\n"
248 " average: Calculate the average of all matching DSes and apply the\n"
249 " warning- and critical-ranges to the calculated average.\n"
250 " sum: Apply the ranges to the sum of all DSes.\n"
251 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
252 " and the sum of all values."
257 static int do_check_con_none (size_t values_num,
258 double *values, char **values_names)
260 int num_critical = 0;
263 const char *status_str = "UNKNOWN";
264 int status_code = RET_UNKNOWN;
267 for (i = 0; i < values_num; i++)
269 if (isnan (values[i]))
276 else if (match_range (&range_critical_g, values[i]) != 0)
278 else if (match_range (&range_warning_g, values[i]) != 0)
284 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
286 printf ("WARNING: No defined values found\n");
287 return (RET_WARNING);
289 else if ((num_critical == 0) && (num_warning == 0))
292 status_code = RET_OKAY;
294 else if (num_critical == 0)
296 status_str = "WARNING";
297 status_code = RET_WARNING;
301 status_str = "CRITICAL";
302 status_code = RET_CRITICAL;
305 printf ("%s: %i critical, %i warning, %i okay", status_str,
306 num_critical, num_warning, num_okay);
310 for (i = 0; i < values_num; i++)
311 printf (" %s=%f;;;;", values_names[i], values[i]);
315 return (status_code);
316 } /* int do_check_con_none */
318 static int do_check_con_average (size_t values_num,
319 double *values, char **values_names)
325 const char *status_str = "UNKNOWN";
326 int status_code = RET_UNKNOWN;
330 for (i = 0; i < values_num; i++)
332 if (isnan (values[i]))
337 printf ("CRITICAL: Data source \"%s\" is NaN\n",
339 return (RET_CRITICAL);
348 printf ("WARNING: No defined values found\n");
349 return (RET_WARNING);
352 average = total / total_num;
354 if (match_range (&range_critical_g, average) != 0)
356 status_str = "CRITICAL";
357 status_code = RET_CRITICAL;
359 else if (match_range (&range_warning_g, average) != 0)
361 status_str = "WARNING";
362 status_code = RET_WARNING;
367 status_code = RET_OKAY;
370 printf ("%s: %g average |", status_str, average);
371 for (i = 0; i < values_num; i++)
372 printf (" %s=%f;;;;", values_names[i], values[i]);
375 return (status_code);
376 } /* int do_check_con_average */
378 static int do_check_con_sum (size_t values_num,
379 double *values, char **values_names)
384 const char *status_str = "UNKNOWN";
385 int status_code = RET_UNKNOWN;
389 for (i = 0; i < values_num; i++)
391 if (isnan (values[i]))
396 printf ("CRITICAL: Data source \"%s\" is NaN\n",
398 return (RET_CRITICAL);
407 printf ("WARNING: No defined values found\n");
408 return (RET_WARNING);
411 if (match_range (&range_critical_g, total) != 0)
413 status_str = "CRITICAL";
414 status_code = RET_CRITICAL;
416 else if (match_range (&range_warning_g, total) != 0)
418 status_str = "WARNING";
419 status_code = RET_WARNING;
424 status_code = RET_OKAY;
427 printf ("%s: %g sum |", status_str, total);
428 for (i = 0; i < values_num; i++)
429 printf (" %s=%f;;;;", values_names[i], values[i]);
432 return (status_code);
433 } /* int do_check_con_sum */
435 static int do_check_con_percentage (size_t values_num,
436 double *values, char **values_names)
442 const char *status_str = "UNKNOWN";
443 int status_code = RET_UNKNOWN;
445 if ((values_num < 1) || (isnan (values[0])))
447 printf ("WARNING: The first value is not defined\n");
448 return (RET_WARNING);
451 for (i = 0; i < values_num; i++)
453 if (isnan (values[i]))
458 printf ("CRITICAL: Data source \"%s\" is NaN\n",
460 return (RET_CRITICAL);
468 printf ("WARNING: Values sum up to zero\n");
469 return (RET_WARNING);
472 percentage = 100.0 * values[0] / sum;
474 if (match_range (&range_critical_g, percentage) != 0)
476 status_str = "CRITICAL";
477 status_code = RET_CRITICAL;
479 else if (match_range (&range_warning_g, percentage) != 0)
481 status_str = "WARNING";
482 status_code = RET_WARNING;
487 status_code = RET_OKAY;
490 printf ("%s: %lf percent |", status_str, percentage);
491 for (i = 0; i < values_num; i++)
492 printf (" %s=%lf;;;;", values_names[i], values[i]);
493 return (status_code);
494 } /* int do_check_con_percentage */
496 static int do_check (void)
498 lcc_connection_t *connection;
503 char ident_str[1024];
504 lcc_identifier_t ident;
508 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
509 address[sizeof (address) - 1] = 0;
511 snprintf (ident_str, sizeof (ident_str), "%s/%s",
512 hostname_g, value_string_g);
513 ident_str[sizeof (ident_str) - 1] = 0;
516 status = lcc_connect (address, &connection);
519 printf ("ERROR: Connecting to daemon at %s failed.\n",
521 return (RET_CRITICAL);
524 memset (&ident, 0, sizeof (ident));
525 status = lcc_string_to_identifier (connection, &ident, ident_str);
528 printf ("ERROR: Creating an identifier failed: %s.\n",
529 lcc_strerror (connection));
530 LCC_DESTROY (connection);
531 return (RET_CRITICAL);
534 status = lcc_getval (connection, &ident,
535 &values_num, &values, &values_names);
538 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
539 lcc_strerror (connection));
540 LCC_DESTROY (connection);
541 return (RET_CRITICAL);
544 LCC_DESTROY (connection);
546 status = filter_ds (&values_num, &values, &values_names);
547 if (status != RET_OKAY)
550 status = RET_UNKNOWN;
551 if (consolitation_g == CON_NONE)
552 status = do_check_con_none (values_num, values, values_names);
553 else if (consolitation_g == CON_AVERAGE)
554 status = do_check_con_average (values_num, values, values_names);
555 else if (consolitation_g == CON_SUM)
556 status = do_check_con_sum (values_num, values, values_names);
557 else if (consolitation_g == CON_PERCENTAGE)
558 status = do_check_con_percentage (values_num, values, values_names);
561 if (values_names != NULL)
562 for (i = 0; i < values_num; i++)
563 free (values_names[i]);
569 int main (int argc, char **argv)
571 range_critical_g.min = NAN;
572 range_critical_g.max = NAN;
573 range_critical_g.invert = 0;
575 range_warning_g.min = NAN;
576 range_warning_g.max = NAN;
577 range_warning_g.invert = 0;
583 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
590 parse_range (optarg, &range_critical_g);
593 parse_range (optarg, &range_warning_g);
596 socket_file_g = optarg;
599 value_string_g = optarg;
605 if (strcasecmp (optarg, "none") == 0)
606 consolitation_g = CON_NONE;
607 else if (strcasecmp (optarg, "average") == 0)
608 consolitation_g = CON_AVERAGE;
609 else if (strcasecmp (optarg, "sum") == 0)
610 consolitation_g = CON_SUM;
611 else if (strcasecmp (optarg, "percentage") == 0)
612 consolitation_g = CON_PERCENTAGE;
615 fprintf (stderr, "Unknown consolidation function `%s'.\n",
623 tmp = (char **) realloc (match_ds_g,
628 fprintf (stderr, "realloc failed: %s\n",
630 return (RET_UNKNOWN);
633 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
634 if (match_ds_g[match_ds_num_g] == NULL)
636 fprintf (stderr, "cn_strdup failed: %s\n",
638 return (RET_UNKNOWN);
651 if ((socket_file_g == NULL) || (value_string_g == NULL)
652 || (hostname_g == NULL))
654 fprintf (stderr, "Missing required arguments.\n");
658 return (do_check ());