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>
22 /* Set to C99 and POSIX code */
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
27 # define _POSIX_SOURCE
29 #ifndef _POSIX_C_SOURCE
30 # define _POSIX_C_SOURCE 200112L
36 /* Disable non-standard extensions */
47 #if !defined(__GNUC__) || !__GNUC__
48 # define __attribute__(x) /**/
61 #include <sys/socket.h>
64 #include "libcollectdclient/client.h"
67 * This is copied directly from collectd.h. Make changes there!
69 #if NAN_STATIC_DEFAULT
71 /* #endif NAN_STATIC_DEFAULT*/
74 # define DISABLE_ISOC99 1
75 # define __USE_ISOC99 1
76 # endif /* !defined(__USE_ISOC99) */
79 # undef DISABLE_ISOC99
81 # endif /* DISABLE_ISOC99 */
82 /* #endif NAN_STATIC_ISOC */
88 # define NAN (0.0 / 0.0)
90 # define isnan(f) ((f) != (f))
91 # endif /* !defined(isnan) */
92 #endif /* NAN_ZERO_ZERO */
96 #define RET_CRITICAL 2
100 #define CON_AVERAGE 1
102 #define CON_PERCENTAGE 3
110 typedef struct range_s range_t;
113 extern int optind, opterr, optopt;
115 static char *socket_file_g = NULL;
116 static char *value_string_g = NULL;
117 static char *hostname_g = NULL;
119 static range_t range_critical_g;
120 static range_t range_warning_g;
121 static int consolitation_g = CON_NONE;
123 static char **match_ds_g = NULL;
124 static int match_ds_num_g = 0;
126 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
127 * that, so here's an own implementation.. It's easy enough. The GCC attributes
128 * are supposed to get good performance.. -octo */
129 __attribute__((malloc, nonnull (1)))
130 static char *cn_strdup (const char *str) /* {{{ */
135 strsize = strlen (str) + 1;
136 ret = (char *) malloc (strsize);
138 memcpy (ret, str, strsize);
140 } /* }}} char *cn_strdup */
142 static int filter_ds (size_t *values_num,
143 double **values, char ***values_names)
150 if (match_ds_g == NULL)
153 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
154 if (new_values == NULL)
156 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
157 return (RET_UNKNOWN);
160 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
161 if (new_names == NULL)
163 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
165 return (RET_UNKNOWN);
168 for (i = 0; i < match_ds_num_g; i++)
172 /* match_ds_g keeps pointers into argv but the names will be freed */
173 new_names[i] = cn_strdup (match_ds_g[i]);
174 if (new_names[i] == NULL)
176 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
178 for (j = 0; j < i; j++)
181 return (RET_UNKNOWN);
184 for (j = 0; j < *values_num; j++)
185 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
188 if (j == *values_num)
190 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
192 for (j = 0; j <= i; j++)
195 return (RET_CRITICAL);
198 new_values[i] = (*values)[j];
202 for (i = 0; i < *values_num; i++)
203 free ((*values_names)[i]);
204 free (*values_names);
206 *values = new_values;
207 *values_names = new_names;
208 *values_num = match_ds_num_g;
210 } /* int filter_ds */
212 static void parse_range (char *string, range_t *range)
223 max_ptr = strchr (string, ':');
236 assert (max_ptr != NULL);
241 /* :10 == ~:10 == -inf:10 */
242 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
245 range->min = atof (min_ptr);
247 if ((*max_ptr == '\0') || (*max_ptr == '~'))
250 range->max = atof (max_ptr);
251 } /* void parse_range */
253 static int match_range (range_t *range, double value)
257 if (!isnan (range->min) && (range->min > value))
259 if (!isnan (range->max) && (range->max < value))
262 return (((ret - range->invert) == 0) ? 0 : 1);
263 } /* int match_range */
265 static void usage (const char *name)
267 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
269 "Valid options are:\n"
270 " -s <socket> Path to collectd's UNIX-socket.\n"
271 " -n <v_spec> Value specification to get from collectd.\n"
272 " Format: `plugin-instance/type-instance'\n"
273 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
274 " DSes. By default all DSes are used.\n"
275 " -g <consol> Method to use to consolidate several DSes.\n"
276 " Valid arguments are `none', `average' and `sum'\n"
277 " -H <host> Hostname to query the values for.\n"
278 " -c <range> Critical range\n"
279 " -w <range> Warning range\n"
281 "Consolidation functions:\n"
282 " none: Apply the warning- and critical-ranges to each data-source\n"
284 " average: Calculate the average of all matching DSes and apply the\n"
285 " warning- and critical-ranges to the calculated average.\n"
286 " sum: Apply the ranges to the sum of all DSes.\n"
291 static int do_check_con_none (size_t values_num,
292 double *values, char **values_names)
294 int num_critical = 0;
297 const char *status_str = "UNKNOWN";
298 int status_code = RET_UNKNOWN;
301 for (i = 0; i < values_num; i++)
303 if (isnan (values[i]))
305 else if (match_range (&range_critical_g, values[i]) != 0)
307 else if (match_range (&range_warning_g, values[i]) != 0)
313 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
315 printf ("WARNING: No defined values found\n");
316 return (RET_WARNING);
318 else if ((num_critical == 0) && (num_warning == 0))
321 status_code = RET_OKAY;
323 else if (num_critical == 0)
325 status_str = "WARNING";
326 status_code = RET_WARNING;
330 status_str = "CRITICAL";
331 status_code = RET_CRITICAL;
334 printf ("%s: %i critical, %i warning, %i okay", status_str,
335 num_critical, num_warning, num_okay);
339 for (i = 0; i < values_num; i++)
340 printf (" %s=%g;;;;", values_names[i], values[i]);
344 return (status_code);
345 } /* int do_check_con_none */
347 static int do_check_con_average (size_t values_num,
348 double *values, char **values_names)
354 const char *status_str = "UNKNOWN";
355 int status_code = RET_UNKNOWN;
359 for (i = 0; i < values_num; i++)
361 if (!isnan (values[i]))
370 printf ("WARNING: No defined values found\n");
371 return (RET_WARNING);
374 average = total / total_num;
376 if (match_range (&range_critical_g, average) != 0)
378 status_str = "CRITICAL";
379 status_code = RET_CRITICAL;
381 else if (match_range (&range_warning_g, average) != 0)
383 status_str = "WARNING";
384 status_code = RET_WARNING;
389 status_code = RET_OKAY;
392 printf ("%s: %g average |", status_str, average);
393 for (i = 0; i < values_num; i++)
394 printf (" %s=%g;;;;", values_names[i], values[i]);
397 return (status_code);
398 } /* int do_check_con_average */
400 static int do_check_con_sum (size_t values_num,
401 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]))
422 printf ("WARNING: No defined values found\n");
423 return (RET_WARNING);
426 if (match_range (&range_critical_g, total) != 0)
428 status_str = "CRITICAL";
429 status_code = RET_CRITICAL;
431 else if (match_range (&range_warning_g, total) != 0)
433 status_str = "WARNING";
434 status_code = RET_WARNING;
439 status_code = RET_OKAY;
442 printf ("%s: %g sum |", status_str, total);
443 for (i = 0; i < values_num; i++)
444 printf (" %s=%g;;;;", values_names[i], values[i]);
447 return (status_code);
448 } /* int do_check_con_sum */
450 static int do_check_con_percentage (int values_num, double *values, char **values_names)
456 return (RET_WARNING);
457 if (isnan (values[0]) || isnan (values[1]))
458 return (RET_WARNING);
459 if ((values[0] + values[1]) == 0)
460 return (RET_WARNING);
462 percentage = 100 * values[1] / ( values[0] + values[1] );
464 printf ("%lf percentage |", percentage);
465 for (i = 0; i < values_num; i++)
466 printf (" %s=%lf;;;;", values_names[i], values[i]);
468 if (match_range (&range_critical_g, percentage) != 0)
470 printf ("CRITICAL: percentage = %lf\n", percentage);
471 return (RET_CRITICAL);
473 else if (match_range (&range_warning_g, percentage) != 0)
475 printf ("WARNING: percentage = %lf\n", percentage);
476 return (RET_WARNING);
479 printf ("OKAY: percentage = %lf\n", percentage);
481 } /* int do_check_con_percentage */
483 static int do_check (void)
485 lcc_connection_t *connection;
490 char ident_str[1024];
491 lcc_identifier_t ident;
495 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
496 address[sizeof (address) - 1] = 0;
498 snprintf (ident_str, sizeof (ident_str), "%s/%s",
499 hostname_g, value_string_g);
500 ident_str[sizeof (ident_str) - 1] = 0;
503 status = lcc_connect (address, &connection);
506 printf ("ERROR: Connecting to daemon at %s failed.\n",
508 return (RET_CRITICAL);
511 memset (&ident, 0, sizeof (ident));
512 status = lcc_string_to_identifier (connection, &ident, ident_str);
515 printf ("ERROR: Creating an identifier failed: %s.\n",
516 lcc_strerror (connection));
517 LCC_DESTROY (connection);
518 return (RET_CRITICAL);
521 status = lcc_getval (connection, &ident,
522 &values_num, &values, &values_names);
525 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
526 lcc_strerror (connection));
527 LCC_DESTROY (connection);
528 return (RET_CRITICAL);
531 LCC_DESTROY (connection);
533 status = filter_ds (&values_num, &values, &values_names);
534 if (status != RET_OKAY)
537 status = RET_UNKNOWN;
538 if (consolitation_g == CON_NONE)
539 status = do_check_con_none (values_num, values, values_names);
540 else if (consolitation_g == CON_AVERAGE)
541 status = do_check_con_average (values_num, values, values_names);
542 else if (consolitation_g == CON_SUM)
543 status = do_check_con_sum (values_num, values, values_names);
544 else if (consolitation_g == CON_PERCENTAGE)
545 status = do_check_con_percentage (values_num, values, values_names);
548 if (values_names != NULL)
549 for (i = 0; i < values_num; i++)
550 free (values_names[i]);
556 int main (int argc, char **argv)
558 range_critical_g.min = NAN;
559 range_critical_g.max = NAN;
560 range_critical_g.invert = 0;
562 range_warning_g.min = NAN;
563 range_warning_g.max = NAN;
564 range_warning_g.invert = 0;
570 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
577 parse_range (optarg, &range_critical_g);
580 parse_range (optarg, &range_warning_g);
583 socket_file_g = optarg;
586 value_string_g = optarg;
592 if (strcasecmp (optarg, "none") == 0)
593 consolitation_g = CON_NONE;
594 else if (strcasecmp (optarg, "average") == 0)
595 consolitation_g = CON_AVERAGE;
596 else if (strcasecmp (optarg, "sum") == 0)
597 consolitation_g = CON_SUM;
598 else if (strcasecmp (optarg, "percentage") == 0)
599 consolitation_g = CON_PERCENTAGE;
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))
635 return (do_check ());