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
109 typedef struct range_s range_t;
112 extern int optind, opterr, optopt;
114 static char *socket_file_g = NULL;
115 static char *value_string_g = NULL;
116 static char *hostname_g = NULL;
118 static range_t range_critical_g;
119 static range_t range_warning_g;
120 static int consolitation_g = CON_NONE;
122 static char **match_ds_g = NULL;
123 static int match_ds_num_g = 0;
125 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
126 * that, so here's an own implementation.. It's easy enough. The GCC attributes
127 * are supposed to get good performance.. -octo */
128 __attribute__((malloc, nonnull (1)))
129 static char *cn_strdup (const char *str) /* {{{ */
134 strsize = strlen (str) + 1;
135 ret = (char *) malloc (strsize);
137 memcpy (ret, str, strsize);
139 } /* }}} char *cn_strdup */
141 static int ignore_ds (const char *name)
145 if (match_ds_g == NULL)
148 for (i = 0; i < match_ds_num_g; i++)
149 if (strcasecmp (match_ds_g[i], name) == 0)
153 } /* int ignore_ds */
155 static void parse_range (char *string, range_t *range)
166 max_ptr = strchr (string, ':');
179 assert (max_ptr != NULL);
184 /* :10 == ~:10 == -inf:10 */
185 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
188 range->min = atof (min_ptr);
190 if ((*max_ptr == '\0') || (*max_ptr == '~'))
193 range->max = atof (max_ptr);
194 } /* void parse_range */
196 static int match_range (range_t *range, double value)
200 if (!isnan (range->min) && (range->min > value))
202 if (!isnan (range->max) && (range->max < value))
205 return (((ret - range->invert) == 0) ? 0 : 1);
206 } /* int match_range */
208 static void usage (const char *name)
210 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
212 "Valid options are:\n"
213 " -s <socket> Path to collectd's UNIX-socket.\n"
214 " -n <v_spec> Value specification to get from collectd.\n"
215 " Format: `plugin-instance/type-instance'\n"
216 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
217 " DSes. By default all DSes are used.\n"
218 " -g <consol> Method to use to consolidate several DSes.\n"
219 " Valid arguments are `none', `average' and `sum'\n"
220 " -H <host> Hostname to query the values for.\n"
221 " -c <range> Critical range\n"
222 " -w <range> Warning range\n"
224 "Consolidation functions:\n"
225 " none: Apply the warning- and critical-ranges to each data-source\n"
227 " average: Calculate the average of all matching DSes and apply the\n"
228 " warning- and critical-ranges to the calculated average.\n"
229 " sum: Apply the ranges to the sum of all DSes.\n"
234 static int do_check_con_none (int values_num,
235 double *values, char **values_names)
237 int num_critical = 0;
240 const char *status_str = "UNKNOWN";
241 int status_code = RET_UNKNOWN;
244 for (i = 0; i < values_num; i++)
246 if (ignore_ds (values_names[i]))
249 if (isnan (values[i]))
251 else if (match_range (&range_critical_g, values[i]) != 0)
253 else if (match_range (&range_warning_g, values[i]) != 0)
259 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
261 printf ("WARNING: No defined values found\n");
262 return (RET_WARNING);
264 else if ((num_critical == 0) && (num_warning == 0))
267 status_code = RET_OKAY;
269 else if (num_critical == 0)
271 status_str = "WARNING";
272 status_code = RET_WARNING;
276 status_str = "CRITICAL";
277 status_code = RET_CRITICAL;
280 printf ("%s: %i critical, %i warning, %i okay", status_str,
281 num_critical, num_warning, num_okay);
285 for (i = 0; i < values_num; i++)
286 printf (" %s=%g;;;;", values_names[i], values[i]);
290 return (status_code);
291 } /* int do_check_con_none */
293 static int do_check_con_average (int values_num,
294 double *values, char **values_names)
300 const char *status_str = "UNKNOWN";
301 int status_code = RET_UNKNOWN;
305 for (i = 0; i < values_num; i++)
307 if (ignore_ds (values_names[i]))
310 if (!isnan (values[i]))
319 printf ("WARNING: No defined values found\n");
320 return (RET_WARNING);
323 average = total / total_num;
325 if (match_range (&range_critical_g, average) != 0)
327 status_str = "CRITICAL";
328 status_code = RET_CRITICAL;
330 else if (match_range (&range_warning_g, average) != 0)
332 status_str = "WARNING";
333 status_code = RET_WARNING;
338 status_code = RET_OKAY;
341 printf ("%s: %g average |", status_str, average);
342 for (i = 0; i < values_num; i++)
343 printf (" %s=%g;;;;", values_names[i], values[i]);
346 return (status_code);
347 } /* int do_check_con_average */
349 static int do_check_con_sum (int values_num, 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 (ignore_ds (values_names[i]))
364 if (!isnan (values[i]))
373 printf ("WARNING: No defined values found\n");
374 return (RET_WARNING);
377 if (match_range (&range_critical_g, total) != 0)
379 status_str = "CRITICAL";
380 status_code = RET_CRITICAL;
382 else if (match_range (&range_warning_g, total) != 0)
384 status_str = "WARNING";
385 status_code = RET_WARNING;
390 status_code = RET_OKAY;
393 printf ("%s: %g sum |", status_str, total);
394 for (i = 0; i < values_num; i++)
395 printf (" %s=%g;;;;", values_names[i], values[i]);
398 return (status_code);
399 } /* int do_check_con_sum */
401 static int do_check (void)
403 lcc_connection_t *connection;
408 char ident_str[1024];
409 lcc_identifier_t ident;
413 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
414 address[sizeof (address) - 1] = 0;
416 snprintf (ident_str, sizeof (ident_str), "%s/%s",
417 hostname_g, value_string_g);
418 ident_str[sizeof (ident_str) - 1] = 0;
421 status = lcc_connect (address, &connection);
424 printf ("ERROR: Connecting to daemon at %s failed.\n",
426 return (RET_CRITICAL);
429 memset (&ident, 0, sizeof (ident));
430 status = lcc_string_to_identifier (connection, &ident, ident_str);
433 printf ("ERROR: Creating an identifier failed: %s.\n",
434 lcc_strerror (connection));
435 LCC_DESTROY (connection);
436 return (RET_CRITICAL);
439 status = lcc_getval (connection, &ident,
440 &values_num, &values, &values_names);
443 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
444 lcc_strerror (connection));
445 LCC_DESTROY (connection);
446 return (RET_CRITICAL);
449 LCC_DESTROY (connection);
451 status = RET_UNKNOWN;
452 if (consolitation_g == CON_NONE)
453 status = do_check_con_none (values_num, values, values_names);
454 else if (consolitation_g == CON_AVERAGE)
455 status = do_check_con_average (values_num, values, values_names);
456 else if (consolitation_g == CON_SUM)
457 status = do_check_con_sum (values_num, values, values_names);
460 if (values_names != NULL)
461 for (i = 0; i < values_num; i++)
462 free (values_names[i]);
468 int main (int argc, char **argv)
470 range_critical_g.min = NAN;
471 range_critical_g.max = NAN;
472 range_critical_g.invert = 0;
474 range_warning_g.min = NAN;
475 range_warning_g.max = NAN;
476 range_warning_g.invert = 0;
482 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
489 parse_range (optarg, &range_critical_g);
492 parse_range (optarg, &range_warning_g);
495 socket_file_g = optarg;
498 value_string_g = optarg;
504 if (strcasecmp (optarg, "none") == 0)
505 consolitation_g = CON_NONE;
506 else if (strcasecmp (optarg, "average") == 0)
507 consolitation_g = CON_AVERAGE;
508 else if (strcasecmp (optarg, "sum") == 0)
509 consolitation_g = CON_SUM;
516 tmp = (char **) realloc (match_ds_g,
521 fprintf (stderr, "realloc failed: %s\n",
523 return (RET_UNKNOWN);
526 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
527 if (match_ds_g[match_ds_num_g] == NULL)
529 fprintf (stderr, "cn_strdup failed: %s\n",
531 return (RET_UNKNOWN);
541 if ((socket_file_g == NULL) || (value_string_g == NULL)
542 || (hostname_g == NULL))
545 return (do_check ());