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 /* Set to C99 and POSIX code */
28 # include "standards.h"
29 #endif /* COLLECT_STANDARDS */
31 #if !defined(__GNUC__) || !__GNUC__
32 # define __attribute__(x) /**/
43 #include <sys/socket.h>
46 #include "libcollectdclient/client.h"
49 * This is copied directly from collectd.h. Make changes there!
51 #if NAN_STATIC_DEFAULT
53 /* #endif NAN_STATIC_DEFAULT*/
56 # define DISABLE_ISOC99 1
57 # define __USE_ISOC99 1
58 # endif /* !defined(__USE_ISOC99) */
61 # undef DISABLE_ISOC99
63 # endif /* DISABLE_ISOC99 */
64 /* #endif NAN_STATIC_ISOC */
70 # define NAN (0.0 / 0.0)
72 # define isnan(f) ((f) != (f))
73 # endif /* !defined(isnan) */
74 #endif /* NAN_ZERO_ZERO */
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;
105 static char **match_ds_g = NULL;
106 static int match_ds_num_g = 0;
108 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
109 * that, so here's an own implementation.. It's easy enough. The GCC attributes
110 * are supposed to get good performance.. -octo */
111 __attribute__((malloc, nonnull (1)))
112 static char *cn_strdup (const char *str) /* {{{ */
117 strsize = strlen (str) + 1;
118 ret = (char *) malloc (strsize);
120 memcpy (ret, str, strsize);
122 } /* }}} char *cn_strdup */
124 static int filter_ds (size_t *values_num,
125 double **values, char ***values_names)
132 if (match_ds_g == NULL)
135 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
136 if (new_values == NULL)
138 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
139 return (RET_UNKNOWN);
142 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
143 if (new_names == NULL)
145 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
147 return (RET_UNKNOWN);
150 for (i = 0; i < (size_t) match_ds_num_g; i++)
154 /* match_ds_g keeps pointers into argv but the names will be freed */
155 new_names[i] = cn_strdup (match_ds_g[i]);
156 if (new_names[i] == NULL)
158 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
160 for (j = 0; j < i; j++)
163 return (RET_UNKNOWN);
166 for (j = 0; j < *values_num; j++)
167 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
170 if (j == *values_num)
172 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
174 for (j = 0; j <= i; j++)
177 return (RET_CRITICAL);
180 new_values[i] = (*values)[j];
184 for (i = 0; i < *values_num; i++)
185 free ((*values_names)[i]);
186 free (*values_names);
188 *values = new_values;
189 *values_names = new_names;
190 *values_num = match_ds_num_g;
192 } /* int filter_ds */
194 static void parse_range (char *string, range_t *range)
205 max_ptr = strchr (string, ':');
218 assert (max_ptr != NULL);
223 /* :10 == ~:10 == -inf:10 */
224 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
227 range->min = atof (min_ptr);
229 if ((*max_ptr == '\0') || (*max_ptr == '~'))
232 range->max = atof (max_ptr);
233 } /* void parse_range */
235 static int match_range (range_t *range, double value)
239 if (!isnan (range->min) && (range->min > value))
241 if (!isnan (range->max) && (range->max < value))
244 return (((ret - range->invert) == 0) ? 0 : 1);
245 } /* int match_range */
247 static void usage (const char *name)
249 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
251 "Valid options are:\n"
252 " -s <socket> Path to collectd's UNIX-socket.\n"
253 " -n <v_spec> Value specification to get from collectd.\n"
254 " Format: `plugin-instance/type-instance'\n"
255 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
256 " DSes. By default all DSes are used.\n"
257 " -g <consol> Method to use to consolidate several DSes.\n"
258 " See below for a list of valid arguments.\n"
259 " -H <host> Hostname to query the values for.\n"
260 " -c <range> Critical range\n"
261 " -w <range> Warning range\n"
263 "Consolidation functions:\n"
264 " none: Apply the warning- and critical-ranges to each data-source\n"
266 " average: Calculate the average of all matching DSes and apply the\n"
267 " warning- and critical-ranges to the calculated average.\n"
268 " sum: Apply the ranges to the sum of all DSes.\n"
269 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
270 " and the sum of all values."
275 static int do_check_con_none (size_t values_num,
276 double *values, char **values_names)
278 int num_critical = 0;
281 const char *status_str = "UNKNOWN";
282 int status_code = RET_UNKNOWN;
285 for (i = 0; i < values_num; i++)
287 if (isnan (values[i]))
289 else if (match_range (&range_critical_g, values[i]) != 0)
291 else if (match_range (&range_warning_g, values[i]) != 0)
297 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
299 printf ("WARNING: No defined values found\n");
300 return (RET_WARNING);
302 else if ((num_critical == 0) && (num_warning == 0))
305 status_code = RET_OKAY;
307 else if (num_critical == 0)
309 status_str = "WARNING";
310 status_code = RET_WARNING;
314 status_str = "CRITICAL";
315 status_code = RET_CRITICAL;
318 printf ("%s: %i critical, %i warning, %i okay", status_str,
319 num_critical, num_warning, num_okay);
323 for (i = 0; i < values_num; i++)
324 printf (" %s=%g;;;;", values_names[i], values[i]);
328 return (status_code);
329 } /* int do_check_con_none */
331 static int do_check_con_average (size_t values_num,
332 double *values, char **values_names)
338 const char *status_str = "UNKNOWN";
339 int status_code = RET_UNKNOWN;
343 for (i = 0; i < values_num; i++)
345 if (!isnan (values[i]))
354 printf ("WARNING: No defined values found\n");
355 return (RET_WARNING);
358 average = total / total_num;
360 if (match_range (&range_critical_g, average) != 0)
362 status_str = "CRITICAL";
363 status_code = RET_CRITICAL;
365 else if (match_range (&range_warning_g, average) != 0)
367 status_str = "WARNING";
368 status_code = RET_WARNING;
373 status_code = RET_OKAY;
376 printf ("%s: %g average |", status_str, average);
377 for (i = 0; i < values_num; i++)
378 printf (" %s=%g;;;;", values_names[i], values[i]);
381 return (status_code);
382 } /* int do_check_con_average */
384 static int do_check_con_sum (size_t values_num,
385 double *values, char **values_names)
390 const char *status_str = "UNKNOWN";
391 int status_code = RET_UNKNOWN;
395 for (i = 0; i < values_num; i++)
397 if (!isnan (values[i]))
406 printf ("WARNING: No defined values found\n");
407 return (RET_WARNING);
410 if (match_range (&range_critical_g, total) != 0)
412 status_str = "CRITICAL";
413 status_code = RET_CRITICAL;
415 else if (match_range (&range_warning_g, total) != 0)
417 status_str = "WARNING";
418 status_code = RET_WARNING;
423 status_code = RET_OKAY;
426 printf ("%s: %g sum |", status_str, total);
427 for (i = 0; i < values_num; i++)
428 printf (" %s=%g;;;;", values_names[i], values[i]);
431 return (status_code);
432 } /* int do_check_con_sum */
434 static int do_check_con_percentage (size_t values_num,
435 double *values, char **values_names)
441 const char *status_str = "UNKNOWN";
442 int status_code = RET_UNKNOWN;
444 if ((values_num < 1) || (isnan (values[0])))
446 printf ("WARNING: The first value is not defined\n");
447 return (RET_WARNING);
450 for (i = 0; i < values_num; i++)
451 if (!isnan (values[i]))
456 printf ("WARNING: Values sum up to zero\n");
457 return (RET_WARNING);
460 percentage = 100.0 * values[0] / sum;
462 if (match_range (&range_critical_g, percentage) != 0)
464 status_str = "CRITICAL";
465 status_code = RET_CRITICAL;
467 else if (match_range (&range_warning_g, percentage) != 0)
469 status_str = "WARNING";
470 status_code = RET_WARNING;
475 status_code = RET_OKAY;
478 printf ("%s: %lf percent |", status_str, percentage);
479 for (i = 0; i < values_num; i++)
480 printf (" %s=%lf;;;;", values_names[i], values[i]);
481 return (status_code);
482 } /* int do_check_con_percentage */
484 static int do_check (void)
486 lcc_connection_t *connection;
491 char ident_str[1024];
492 lcc_identifier_t ident;
496 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
497 address[sizeof (address) - 1] = 0;
499 snprintf (ident_str, sizeof (ident_str), "%s/%s",
500 hostname_g, value_string_g);
501 ident_str[sizeof (ident_str) - 1] = 0;
504 status = lcc_connect (address, &connection);
507 printf ("ERROR: Connecting to daemon at %s failed.\n",
509 return (RET_CRITICAL);
512 memset (&ident, 0, sizeof (ident));
513 status = lcc_string_to_identifier (connection, &ident, ident_str);
516 printf ("ERROR: Creating an identifier failed: %s.\n",
517 lcc_strerror (connection));
518 LCC_DESTROY (connection);
519 return (RET_CRITICAL);
522 status = lcc_getval (connection, &ident,
523 &values_num, &values, &values_names);
526 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
527 lcc_strerror (connection));
528 LCC_DESTROY (connection);
529 return (RET_CRITICAL);
532 LCC_DESTROY (connection);
534 status = filter_ds (&values_num, &values, &values_names);
535 if (status != RET_OKAY)
538 status = RET_UNKNOWN;
539 if (consolitation_g == CON_NONE)
540 status = do_check_con_none (values_num, values, values_names);
541 else if (consolitation_g == CON_AVERAGE)
542 status = do_check_con_average (values_num, values, values_names);
543 else if (consolitation_g == CON_SUM)
544 status = do_check_con_sum (values_num, values, values_names);
545 else if (consolitation_g == CON_PERCENTAGE)
546 status = do_check_con_percentage (values_num, values, values_names);
549 if (values_names != NULL)
550 for (i = 0; i < values_num; i++)
551 free (values_names[i]);
557 int main (int argc, char **argv)
559 range_critical_g.min = NAN;
560 range_critical_g.max = NAN;
561 range_critical_g.invert = 0;
563 range_warning_g.min = NAN;
564 range_warning_g.max = NAN;
565 range_warning_g.invert = 0;
571 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
578 parse_range (optarg, &range_critical_g);
581 parse_range (optarg, &range_warning_g);
584 socket_file_g = optarg;
587 value_string_g = optarg;
593 if (strcasecmp (optarg, "none") == 0)
594 consolitation_g = CON_NONE;
595 else if (strcasecmp (optarg, "average") == 0)
596 consolitation_g = CON_AVERAGE;
597 else if (strcasecmp (optarg, "sum") == 0)
598 consolitation_g = CON_SUM;
599 else if (strcasecmp (optarg, "percentage") == 0)
600 consolitation_g = CON_PERCENTAGE;
603 fprintf (stderr, "Unknown consolidation function `%s'.\n",
611 tmp = (char **) realloc (match_ds_g,
616 fprintf (stderr, "realloc failed: %s\n",
618 return (RET_UNKNOWN);
621 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
622 if (match_ds_g[match_ds_num_g] == NULL)
624 fprintf (stderr, "cn_strdup failed: %s\n",
626 return (RET_UNKNOWN);
636 if ((socket_file_g == NULL) || (value_string_g == NULL)
637 || (hostname_g == NULL))
639 fprintf (stderr, "Missing required arguments.\n");
643 return (do_check ());