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"
55 #define RET_CRITICAL 2
61 #define CON_PERCENTAGE 3
69 typedef struct range_s range_t;
72 extern int optind, opterr, optopt;
74 static char *socket_file_g = NULL;
75 static char *value_string_g = NULL;
76 static char *hostname_g = NULL;
78 static range_t range_critical_g;
79 static range_t range_warning_g;
80 static int consolitation_g = CON_NONE;
81 static _Bool nan_is_error_g = 0;
83 static char **match_ds_g = NULL;
84 static int match_ds_num_g = 0;
86 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
87 * that, so here's an own implementation.. It's easy enough. The GCC attributes
88 * are supposed to get good performance.. -octo */
89 __attribute__((malloc, nonnull (1)))
90 static char *cn_strdup (const char *str) /* {{{ */
95 strsize = strlen (str) + 1;
96 ret = (char *) malloc (strsize);
98 memcpy (ret, str, strsize);
100 } /* }}} char *cn_strdup */
102 static int filter_ds (size_t *values_num,
103 double **values, char ***values_names)
110 if (match_ds_g == NULL)
113 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
114 if (new_values == NULL)
116 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
117 return (RET_UNKNOWN);
120 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
121 if (new_names == NULL)
123 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
125 return (RET_UNKNOWN);
128 for (i = 0; i < (size_t) match_ds_num_g; i++)
132 /* match_ds_g keeps pointers into argv but the names will be freed */
133 new_names[i] = cn_strdup (match_ds_g[i]);
134 if (new_names[i] == NULL)
136 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
138 for (j = 0; j < i; j++)
141 return (RET_UNKNOWN);
144 for (j = 0; j < *values_num; j++)
145 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
148 if (j == *values_num)
150 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
152 for (j = 0; j <= i; j++)
155 return (RET_CRITICAL);
158 new_values[i] = (*values)[j];
162 for (i = 0; i < *values_num; i++)
163 free ((*values_names)[i]);
164 free (*values_names);
166 *values = new_values;
167 *values_names = new_names;
168 *values_num = match_ds_num_g;
170 } /* int filter_ds */
172 static void parse_range (char *string, range_t *range)
183 max_ptr = strchr (string, ':');
196 assert (max_ptr != NULL);
201 /* :10 == ~:10 == -inf:10 */
202 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
205 range->min = atof (min_ptr);
207 if ((*max_ptr == '\0') || (*max_ptr == '~'))
210 range->max = atof (max_ptr);
211 } /* void parse_range */
213 static int match_range (range_t *range, double value)
217 if (!isnan (range->min) && (range->min > value))
219 if (!isnan (range->max) && (range->max < value))
222 return (((ret - range->invert) == 0) ? 0 : 1);
223 } /* int match_range */
225 static void usage (const char *name)
227 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
229 "Valid options are:\n"
230 " -s <socket> Path to collectd's UNIX-socket.\n"
231 " -n <v_spec> Value specification to get from collectd.\n"
232 " Format: `plugin-instance/type-instance'\n"
233 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
234 " DSes. By default all DSes are used.\n"
235 " -g <consol> Method to use to consolidate several DSes.\n"
236 " See below for a list of valid arguments.\n"
237 " -H <host> Hostname to query the values for.\n"
238 " -c <range> Critical range\n"
239 " -w <range> Warning range\n"
240 " -m Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
242 "Consolidation functions:\n"
243 " none: Apply the warning- and critical-ranges to each data-source\n"
245 " average: Calculate the average of all matching DSes and apply the\n"
246 " warning- and critical-ranges to the calculated average.\n"
247 " sum: Apply the ranges to the sum of all DSes.\n"
248 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
249 " and the sum of all values."
254 static int do_check_con_none (size_t values_num,
255 double *values, char **values_names)
257 int num_critical = 0;
260 const char *status_str = "UNKNOWN";
261 int status_code = RET_UNKNOWN;
264 for (i = 0; i < values_num; i++)
266 if (isnan (values[i]))
273 else if (match_range (&range_critical_g, values[i]) != 0)
275 else if (match_range (&range_warning_g, values[i]) != 0)
281 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
283 printf ("WARNING: No defined values found\n");
284 return (RET_WARNING);
286 else if ((num_critical == 0) && (num_warning == 0))
289 status_code = RET_OKAY;
291 else if (num_critical == 0)
293 status_str = "WARNING";
294 status_code = RET_WARNING;
298 status_str = "CRITICAL";
299 status_code = RET_CRITICAL;
302 printf ("%s: %i critical, %i warning, %i okay", status_str,
303 num_critical, num_warning, num_okay);
307 for (i = 0; i < values_num; i++)
308 printf (" %s=%f;;;;", values_names[i], values[i]);
312 return (status_code);
313 } /* int do_check_con_none */
315 static int do_check_con_average (size_t values_num,
316 double *values, char **values_names)
322 const char *status_str = "UNKNOWN";
323 int status_code = RET_UNKNOWN;
327 for (i = 0; i < values_num; i++)
329 if (isnan (values[i]))
334 printf ("CRITICAL: Data source \"%s\" is NaN\n",
336 return (RET_CRITICAL);
345 printf ("WARNING: No defined values found\n");
346 return (RET_WARNING);
349 average = total / total_num;
351 if (match_range (&range_critical_g, average) != 0)
353 status_str = "CRITICAL";
354 status_code = RET_CRITICAL;
356 else if (match_range (&range_warning_g, average) != 0)
358 status_str = "WARNING";
359 status_code = RET_WARNING;
364 status_code = RET_OKAY;
367 printf ("%s: %g average |", status_str, average);
368 for (i = 0; i < values_num; i++)
369 printf (" %s=%f;;;;", values_names[i], values[i]);
372 return (status_code);
373 } /* int do_check_con_average */
375 static int do_check_con_sum (size_t values_num,
376 double *values, char **values_names)
381 const char *status_str = "UNKNOWN";
382 int status_code = RET_UNKNOWN;
386 for (i = 0; i < values_num; i++)
388 if (isnan (values[i]))
393 printf ("CRITICAL: Data source \"%s\" is NaN\n",
395 return (RET_CRITICAL);
404 printf ("WARNING: No defined values found\n");
405 return (RET_WARNING);
408 if (match_range (&range_critical_g, total) != 0)
410 status_str = "CRITICAL";
411 status_code = RET_CRITICAL;
413 else if (match_range (&range_warning_g, total) != 0)
415 status_str = "WARNING";
416 status_code = RET_WARNING;
421 status_code = RET_OKAY;
424 printf ("%s: %g sum |", status_str, total);
425 for (i = 0; i < values_num; i++)
426 printf (" %s=%f;;;;", values_names[i], values[i]);
429 return (status_code);
430 } /* int do_check_con_sum */
432 static int do_check_con_percentage (size_t values_num,
433 double *values, char **values_names)
439 const char *status_str = "UNKNOWN";
440 int status_code = RET_UNKNOWN;
442 if ((values_num < 1) || (isnan (values[0])))
444 printf ("WARNING: The first value is not defined\n");
445 return (RET_WARNING);
448 for (i = 0; i < values_num; i++)
450 if (isnan (values[i]))
455 printf ("CRITICAL: Data source \"%s\" is NaN\n",
457 return (RET_CRITICAL);
465 printf ("WARNING: Values sum up to zero\n");
466 return (RET_WARNING);
469 percentage = 100.0 * values[0] / sum;
471 if (match_range (&range_critical_g, percentage) != 0)
473 status_str = "CRITICAL";
474 status_code = RET_CRITICAL;
476 else if (match_range (&range_warning_g, percentage) != 0)
478 status_str = "WARNING";
479 status_code = RET_WARNING;
484 status_code = RET_OKAY;
487 printf ("%s: %lf percent |", status_str, percentage);
488 for (i = 0; i < values_num; i++)
489 printf (" %s=%lf;;;;", values_names[i], values[i]);
490 return (status_code);
491 } /* int do_check_con_percentage */
493 static int do_check (void)
495 lcc_connection_t *connection;
500 char ident_str[1024];
501 lcc_identifier_t ident;
505 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
506 address[sizeof (address) - 1] = 0;
508 snprintf (ident_str, sizeof (ident_str), "%s/%s",
509 hostname_g, value_string_g);
510 ident_str[sizeof (ident_str) - 1] = 0;
513 status = lcc_connect (address, &connection);
516 printf ("ERROR: Connecting to daemon at %s failed.\n",
518 return (RET_CRITICAL);
521 memset (&ident, 0, sizeof (ident));
522 status = lcc_string_to_identifier (connection, &ident, ident_str);
525 printf ("ERROR: Creating an identifier failed: %s.\n",
526 lcc_strerror (connection));
527 LCC_DESTROY (connection);
528 return (RET_CRITICAL);
531 status = lcc_getval (connection, &ident,
532 &values_num, &values, &values_names);
535 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
536 lcc_strerror (connection));
537 LCC_DESTROY (connection);
538 return (RET_CRITICAL);
541 LCC_DESTROY (connection);
543 status = filter_ds (&values_num, &values, &values_names);
544 if (status != RET_OKAY)
547 status = RET_UNKNOWN;
548 if (consolitation_g == CON_NONE)
549 status = do_check_con_none (values_num, values, values_names);
550 else if (consolitation_g == CON_AVERAGE)
551 status = do_check_con_average (values_num, values, values_names);
552 else if (consolitation_g == CON_SUM)
553 status = do_check_con_sum (values_num, values, values_names);
554 else if (consolitation_g == CON_PERCENTAGE)
555 status = do_check_con_percentage (values_num, values, values_names);
558 if (values_names != NULL)
559 for (i = 0; i < values_num; i++)
560 free (values_names[i]);
566 int main (int argc, char **argv)
568 range_critical_g.min = NAN;
569 range_critical_g.max = NAN;
570 range_critical_g.invert = 0;
572 range_warning_g.min = NAN;
573 range_warning_g.max = NAN;
574 range_warning_g.invert = 0;
580 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
587 parse_range (optarg, &range_critical_g);
590 parse_range (optarg, &range_warning_g);
593 socket_file_g = optarg;
596 value_string_g = optarg;
602 if (strcasecmp (optarg, "none") == 0)
603 consolitation_g = CON_NONE;
604 else if (strcasecmp (optarg, "average") == 0)
605 consolitation_g = CON_AVERAGE;
606 else if (strcasecmp (optarg, "sum") == 0)
607 consolitation_g = CON_SUM;
608 else if (strcasecmp (optarg, "percentage") == 0)
609 consolitation_g = CON_PERCENTAGE;
612 fprintf (stderr, "Unknown consolidation function `%s'.\n",
620 tmp = (char **) realloc (match_ds_g,
625 fprintf (stderr, "realloc failed: %s\n",
627 return (RET_UNKNOWN);
630 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
631 if (match_ds_g[match_ds_num_g] == NULL)
633 fprintf (stderr, "cn_strdup failed: %s\n",
635 return (RET_UNKNOWN);
648 if ((socket_file_g == NULL) || (value_string_g == NULL)
649 || (hostname_g == NULL))
651 fprintf (stderr, "Missing required arguments.\n");
655 return (do_check ());