2 * collectd-nagios - src/collectd-nagios.c
3 * Copyright (C) 2008 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 #include <sys/socket.h>
41 #include "libcollectdclient/client.h"
44 * This is copied directly from collectd.h. Make changes there!
46 #if NAN_STATIC_DEFAULT
48 /* #endif NAN_STATIC_DEFAULT*/
51 # define DISABLE_ISOC99 1
52 # define __USE_ISOC99 1
53 # endif /* !defined(__USE_ISOC99) */
56 # undef DISABLE_ISOC99
58 # endif /* DISABLE_ISOC99 */
59 /* #endif NAN_STATIC_ISOC */
65 # define NAN (0.0 / 0.0)
67 # define isnan(f) ((f) != (f))
68 # endif /* !defined(isnan) */
69 #endif /* NAN_ZERO_ZERO */
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;
100 static char **match_ds_g = NULL;
101 static int match_ds_num_g = 0;
103 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
104 * that, so here's an own implementation.. It's easy enough. The GCC attributes
105 * are supposed to get good performance.. -octo */
106 __attribute__((malloc, nonnull (1)))
107 static char *cn_strdup (const char *str) /* {{{ */
112 strsize = strlen (str) + 1;
113 ret = (char *) malloc (strsize);
115 memcpy (ret, str, strsize);
117 } /* }}} char *cn_strdup */
119 static int filter_ds (size_t *values_num,
120 double **values, char ***values_names)
127 if (match_ds_g == NULL)
130 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
131 if (new_values == NULL)
133 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
134 return (RET_UNKNOWN);
137 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
138 if (new_names == NULL)
140 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
142 return (RET_UNKNOWN);
145 for (i = 0; i < (size_t) match_ds_num_g; i++)
149 /* match_ds_g keeps pointers into argv but the names will be freed */
150 new_names[i] = cn_strdup (match_ds_g[i]);
151 if (new_names[i] == NULL)
153 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
155 for (j = 0; j < i; j++)
158 return (RET_UNKNOWN);
161 for (j = 0; j < *values_num; j++)
162 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
165 if (j == *values_num)
167 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
169 for (j = 0; j <= i; j++)
172 return (RET_CRITICAL);
175 new_values[i] = (*values)[j];
179 for (i = 0; i < *values_num; i++)
180 free ((*values_names)[i]);
181 free (*values_names);
183 *values = new_values;
184 *values_names = new_names;
185 *values_num = match_ds_num_g;
187 } /* int filter_ds */
189 static void parse_range (char *string, range_t *range)
200 max_ptr = strchr (string, ':');
213 assert (max_ptr != NULL);
218 /* :10 == ~:10 == -inf:10 */
219 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
222 range->min = atof (min_ptr);
224 if ((*max_ptr == '\0') || (*max_ptr == '~'))
227 range->max = atof (max_ptr);
228 } /* void parse_range */
230 static int match_range (range_t *range, double value)
234 if (!isnan (range->min) && (range->min > value))
236 if (!isnan (range->max) && (range->max < value))
239 return (((ret - range->invert) == 0) ? 0 : 1);
240 } /* int match_range */
242 static void usage (const char *name)
244 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
246 "Valid options are:\n"
247 " -s <socket> Path to collectd's UNIX-socket.\n"
248 " -n <v_spec> Value specification to get from collectd.\n"
249 " Format: `plugin-instance/type-instance'\n"
250 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
251 " DSes. By default all DSes are used.\n"
252 " -g <consol> Method to use to consolidate several DSes.\n"
253 " See below for a list of valid arguments.\n"
254 " -H <host> Hostname to query the values for.\n"
255 " -c <range> Critical range\n"
256 " -w <range> Warning range\n"
258 "Consolidation functions:\n"
259 " none: Apply the warning- and critical-ranges to each data-source\n"
261 " average: Calculate the average of all matching DSes and apply the\n"
262 " warning- and critical-ranges to the calculated average.\n"
263 " sum: Apply the ranges to the sum of all DSes.\n"
264 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
265 " and the sum of all values."
270 static int do_check_con_none (size_t values_num,
271 double *values, char **values_names)
273 int num_critical = 0;
276 const char *status_str = "UNKNOWN";
277 int status_code = RET_UNKNOWN;
280 for (i = 0; i < values_num; i++)
282 if (isnan (values[i]))
284 else if (match_range (&range_critical_g, values[i]) != 0)
286 else if (match_range (&range_warning_g, values[i]) != 0)
292 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
294 printf ("WARNING: No defined values found\n");
295 return (RET_WARNING);
297 else if ((num_critical == 0) && (num_warning == 0))
300 status_code = RET_OKAY;
302 else if (num_critical == 0)
304 status_str = "WARNING";
305 status_code = RET_WARNING;
309 status_str = "CRITICAL";
310 status_code = RET_CRITICAL;
313 printf ("%s: %i critical, %i warning, %i okay", status_str,
314 num_critical, num_warning, num_okay);
318 for (i = 0; i < values_num; i++)
319 printf (" %s=%g;;;;", values_names[i], values[i]);
323 return (status_code);
324 } /* int do_check_con_none */
326 static int do_check_con_average (size_t values_num,
327 double *values, char **values_names)
333 const char *status_str = "UNKNOWN";
334 int status_code = RET_UNKNOWN;
338 for (i = 0; i < values_num; i++)
340 if (!isnan (values[i]))
349 printf ("WARNING: No defined values found\n");
350 return (RET_WARNING);
353 average = total / total_num;
355 if (match_range (&range_critical_g, average) != 0)
357 status_str = "CRITICAL";
358 status_code = RET_CRITICAL;
360 else if (match_range (&range_warning_g, average) != 0)
362 status_str = "WARNING";
363 status_code = RET_WARNING;
368 status_code = RET_OKAY;
371 printf ("%s: %g average |", status_str, average);
372 for (i = 0; i < values_num; i++)
373 printf (" %s=%g;;;;", values_names[i], values[i]);
376 return (status_code);
377 } /* int do_check_con_average */
379 static int do_check_con_sum (size_t values_num,
380 double *values, char **values_names)
385 const char *status_str = "UNKNOWN";
386 int status_code = RET_UNKNOWN;
390 for (i = 0; i < values_num; i++)
392 if (!isnan (values[i]))
401 printf ("WARNING: No defined values found\n");
402 return (RET_WARNING);
405 if (match_range (&range_critical_g, total) != 0)
407 status_str = "CRITICAL";
408 status_code = RET_CRITICAL;
410 else if (match_range (&range_warning_g, total) != 0)
412 status_str = "WARNING";
413 status_code = RET_WARNING;
418 status_code = RET_OKAY;
421 printf ("%s: %g sum |", status_str, total);
422 for (i = 0; i < values_num; i++)
423 printf (" %s=%g;;;;", values_names[i], values[i]);
426 return (status_code);
427 } /* int do_check_con_sum */
429 static int do_check_con_percentage (size_t values_num,
430 double *values, char **values_names)
436 const char *status_str = "UNKNOWN";
437 int status_code = RET_UNKNOWN;
439 if ((values_num < 1) || (isnan (values[0])))
441 printf ("WARNING: The first value is not defined\n");
442 return (RET_WARNING);
445 for (i = 0; i < values_num; i++)
446 if (!isnan (values[i]))
451 printf ("WARNING: Values sum up to zero\n");
452 return (RET_WARNING);
455 percentage = 100.0 * values[0] / sum;
457 if (match_range (&range_critical_g, percentage) != 0)
459 status_str = "CRITICAL";
460 status_code = RET_CRITICAL;
462 else if (match_range (&range_warning_g, percentage) != 0)
464 status_str = "WARNING";
465 status_code = RET_WARNING;
470 status_code = RET_OKAY;
473 printf ("%s: %lf percent |", status_str, percentage);
474 for (i = 0; i < values_num; i++)
475 printf (" %s=%lf;;;;", values_names[i], values[i]);
476 return (status_code);
477 } /* int do_check_con_percentage */
479 static int do_check (void)
481 lcc_connection_t *connection;
486 char ident_str[1024];
487 lcc_identifier_t ident;
491 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
492 address[sizeof (address) - 1] = 0;
494 snprintf (ident_str, sizeof (ident_str), "%s/%s",
495 hostname_g, value_string_g);
496 ident_str[sizeof (ident_str) - 1] = 0;
499 status = lcc_connect (address, &connection);
502 printf ("ERROR: Connecting to daemon at %s failed.\n",
504 return (RET_CRITICAL);
507 memset (&ident, 0, sizeof (ident));
508 status = lcc_string_to_identifier (connection, &ident, ident_str);
511 printf ("ERROR: Creating an identifier failed: %s.\n",
512 lcc_strerror (connection));
513 LCC_DESTROY (connection);
514 return (RET_CRITICAL);
517 status = lcc_getval (connection, &ident,
518 &values_num, &values, &values_names);
521 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
522 lcc_strerror (connection));
523 LCC_DESTROY (connection);
524 return (RET_CRITICAL);
527 LCC_DESTROY (connection);
529 status = filter_ds (&values_num, &values, &values_names);
530 if (status != RET_OKAY)
533 status = RET_UNKNOWN;
534 if (consolitation_g == CON_NONE)
535 status = do_check_con_none (values_num, values, values_names);
536 else if (consolitation_g == CON_AVERAGE)
537 status = do_check_con_average (values_num, values, values_names);
538 else if (consolitation_g == CON_SUM)
539 status = do_check_con_sum (values_num, values, values_names);
540 else if (consolitation_g == CON_PERCENTAGE)
541 status = do_check_con_percentage (values_num, values, values_names);
544 if (values_names != NULL)
545 for (i = 0; i < values_num; i++)
546 free (values_names[i]);
552 int main (int argc, char **argv)
554 range_critical_g.min = NAN;
555 range_critical_g.max = NAN;
556 range_critical_g.invert = 0;
558 range_warning_g.min = NAN;
559 range_warning_g.max = NAN;
560 range_warning_g.invert = 0;
566 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
573 parse_range (optarg, &range_critical_g);
576 parse_range (optarg, &range_warning_g);
579 socket_file_g = optarg;
582 value_string_g = optarg;
588 if (strcasecmp (optarg, "none") == 0)
589 consolitation_g = CON_NONE;
590 else if (strcasecmp (optarg, "average") == 0)
591 consolitation_g = CON_AVERAGE;
592 else if (strcasecmp (optarg, "sum") == 0)
593 consolitation_g = CON_SUM;
594 else if (strcasecmp (optarg, "percentage") == 0)
595 consolitation_g = CON_PERCENTAGE;
598 fprintf (stderr, "Unknown consolidation function `%s'.\n",
606 tmp = (char **) realloc (match_ds_g,
611 fprintf (stderr, "realloc failed: %s\n",
613 return (RET_UNKNOWN);
616 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
617 if (match_ds_g[match_ds_num_g] == NULL)
619 fprintf (stderr, "cn_strdup failed: %s\n",
621 return (RET_UNKNOWN);
631 if ((socket_file_g == NULL) || (value_string_g == NULL)
632 || (hostname_g == NULL))
634 fprintf (stderr, "Missing required arguments.\n");
638 return (do_check ());