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) /**/
59 #include <sys/socket.h>
63 #include "libcollectdclient/client.h"
66 * This is copied directly from collectd.h. Make changes there!
68 #if NAN_STATIC_DEFAULT
70 /* #endif NAN_STATIC_DEFAULT*/
73 # define DISABLE_ISOC99 1
74 # define __USE_ISOC99 1
75 # endif /* !defined(__USE_ISOC99) */
78 # undef DISABLE_ISOC99
80 # endif /* DISABLE_ISOC99 */
81 /* #endif NAN_STATIC_ISOC */
87 # define NAN (0.0 / 0.0)
89 # define isnan(f) ((f) != (f))
90 # endif /* !defined(isnan) */
91 #endif /* NAN_ZERO_ZERO */
95 #define RET_CRITICAL 2
108 typedef struct range_s range_t;
111 extern int optind, opterr, optopt;
113 static char *socket_file_g = NULL;
114 static char *value_string_g = NULL;
115 static char *hostname_g = NULL;
117 static range_t range_critical_g;
118 static range_t range_warning_g;
119 static int consolitation_g = CON_NONE;
121 static char **match_ds_g = NULL;
122 static int match_ds_num_g = 0;
124 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
125 * that, so here's an own implementation.. It's easy enough. The GCC attributes
126 * are supposed to get good performance.. -octo */
127 __attribute__((malloc, nonnull (1)))
128 static char *cn_strdup (const char *str) /* {{{ */
133 strsize = strlen (str) + 1;
134 ret = (char *) malloc (strsize);
136 memcpy (ret, str, strsize);
138 } /* }}} char *cn_strdup */
140 static int ignore_ds (const char *name)
144 if (match_ds_g == NULL)
147 for (i = 0; i < match_ds_num_g; i++)
148 if (strcasecmp (match_ds_g[i], name) == 0)
152 } /* int ignore_ds */
154 static void parse_range (char *string, range_t *range)
165 max_ptr = strchr (string, ':');
178 assert (max_ptr != NULL);
183 /* :10 == ~:10 == -inf:10 */
184 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
187 range->min = atof (min_ptr);
189 if ((*max_ptr == '\0') || (*max_ptr == '~'))
192 range->max = atof (max_ptr);
193 } /* void parse_range */
195 static int match_range (range_t *range, double value)
199 if (!isnan (range->min) && (range->min > value))
201 if (!isnan (range->max) && (range->max < value))
204 return (((ret - range->invert) == 0) ? 0 : 1);
205 } /* int match_range */
207 static void usage (const char *name)
209 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
211 "Valid options are:\n"
212 " -s <socket> Path to collectd's UNIX-socket.\n"
213 " -n <v_spec> Value specification to get from collectd.\n"
214 " Format: `plugin-instance/type-instance'\n"
215 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
216 " DSes. By default all DSes are used.\n"
217 " -g <consol> Method to use to consolidate several DSes.\n"
218 " Valid arguments are `none', `average' and `sum'\n"
219 " -H <host> Hostname to query the values for.\n"
220 " -c <range> Critical range\n"
221 " -w <range> Warning range\n"
223 "Consolidation functions:\n"
224 " none: Apply the warning- and critical-ranges to each data-source\n"
226 " average: Calculate the average of all matching DSes and apply the\n"
227 " warning- and critical-ranges to the calculated average.\n"
228 " sum: Apply the ranges to the sum of all DSes.\n"
233 static int do_check_con_none (int values_num,
234 double *values, char **values_names)
236 int num_critical = 0;
239 const char *status_str = "UNKNOWN";
240 int status_code = RET_UNKNOWN;
243 for (i = 0; i < values_num; i++)
245 if (ignore_ds (values_names[i]))
248 if (isnan (values[i]))
250 else if (match_range (&range_critical_g, values[i]) != 0)
252 else if (match_range (&range_warning_g, values[i]) != 0)
258 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
260 printf ("WARNING: No defined values found\n");
261 return (RET_WARNING);
263 else if ((num_critical == 0) && (num_warning == 0))
266 status_code = RET_OKAY;
268 else if (num_critical == 0)
270 status_str = "WARNING";
271 status_code = RET_WARNING;
275 status_str = "CRITICAL";
276 status_code = RET_CRITICAL;
279 printf ("%s: %i critical, %i warning, %i okay", status_str,
280 num_critical, num_warning, num_okay);
284 for (i = 0; i < values_num; i++)
285 printf (" %s=%g;;;;", values_names[i], values[i]);
289 return (status_code);
290 } /* int do_check_con_none */
292 static int do_check_con_average (int values_num,
293 double *values, char **values_names)
299 const char *status_str = "UNKNOWN";
300 int status_code = RET_UNKNOWN;
304 for (i = 0; i < values_num; i++)
306 if (ignore_ds (values_names[i]))
309 if (!isnan (values[i]))
318 printf ("WARNING: No defined values found\n");
319 return (RET_WARNING);
322 average = total / total_num;
324 if (match_range (&range_critical_g, average) != 0)
326 status_str = "CRITICAL";
327 status_code = RET_CRITICAL;
329 else if (match_range (&range_warning_g, average) != 0)
331 status_str = "WARNING";
332 status_code = RET_WARNING;
337 status_code = RET_OKAY;
340 printf ("%s: %g average |", status_str, average);
341 for (i = 0; i < values_num; i++)
342 printf (" %s=%g;;;;", values_names[i], values[i]);
345 return (status_code);
346 } /* int do_check_con_average */
348 static int do_check_con_sum (int values_num, double *values, char **values_names)
353 const char *status_str = "UNKNOWN";
354 int status_code = RET_UNKNOWN;
358 for (i = 0; i < values_num; i++)
360 if (ignore_ds (values_names[i]))
363 if (!isnan (values[i]))
372 printf ("WARNING: No defined values found\n");
373 return (RET_WARNING);
376 if (match_range (&range_critical_g, total) != 0)
378 status_str = "CRITICAL";
379 status_code = RET_CRITICAL;
381 else if (match_range (&range_warning_g, total) != 0)
383 status_str = "WARNING";
384 status_code = RET_WARNING;
389 status_code = RET_OKAY;
392 printf ("%s: %g sum |", status_str, total);
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_sum */
400 static int do_check (void)
402 lcc_connection_t *connection;
407 char ident_str[1024];
408 lcc_identifier_t ident;
412 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
413 address[sizeof (address) - 1] = 0;
415 snprintf (ident_str, sizeof (ident_str), "%s/%s",
416 hostname_g, value_string_g);
417 ident_str[sizeof (ident_str) - 1] = 0;
420 status = lcc_connect (address, &connection);
423 printf ("ERROR: Connecting to daemon at %s failed.\n",
425 return (RET_CRITICAL);
428 memset (&ident, 0, sizeof (ident));
429 status = lcc_string_to_identifier (connection, &ident, ident_str);
432 printf ("ERROR: Creating an identifier failed: %s.\n",
433 lcc_strerror (connection));
434 LCC_DESTROY (connection);
435 return (RET_CRITICAL);
438 status = lcc_getval (connection, &ident,
439 &values_num, &values, &values_names);
442 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
443 lcc_strerror (connection));
444 LCC_DESTROY (connection);
445 return (RET_CRITICAL);
448 LCC_DESTROY (connection);
450 status = RET_UNKNOWN;
451 if (consolitation_g == CON_NONE)
452 status = do_check_con_none (values_num, values, values_names);
453 else if (consolitation_g == CON_AVERAGE)
454 status = do_check_con_average (values_num, values, values_names);
455 else if (consolitation_g == CON_SUM)
456 status = do_check_con_sum (values_num, values, values_names);
459 if (values_names != NULL)
460 for (i = 0; i < values_num; i++)
461 free (values_names[i]);
467 int main (int argc, char **argv)
469 range_critical_g.min = NAN;
470 range_critical_g.max = NAN;
471 range_critical_g.invert = 0;
473 range_warning_g.min = NAN;
474 range_warning_g.max = NAN;
475 range_warning_g.invert = 0;
481 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
488 parse_range (optarg, &range_critical_g);
491 parse_range (optarg, &range_warning_g);
494 socket_file_g = optarg;
497 value_string_g = optarg;
503 if (strcasecmp (optarg, "none") == 0)
504 consolitation_g = CON_NONE;
505 else if (strcasecmp (optarg, "average") == 0)
506 consolitation_g = CON_AVERAGE;
507 else if (strcasecmp (optarg, "sum") == 0)
508 consolitation_g = CON_SUM;
515 tmp = (char **) realloc (match_ds_g,
520 fprintf (stderr, "realloc failed: %s\n",
522 return (RET_UNKNOWN);
525 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
526 if (match_ds_g[match_ds_num_g] == NULL)
528 fprintf (stderr, "cn_strdup failed: %s\n",
530 return (RET_UNKNOWN);
540 if ((socket_file_g == NULL) || (value_string_g == NULL)
541 || (hostname_g == NULL))
544 return (do_check ());