1 /**
2 * collectd-nagios - src/collectd-nagios.c
3 * Copyright (C) 2008-2010 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #if !defined(__GNUC__) || !__GNUC__
27 # define __attribute__(x) /**/
28 #endif
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <assert.h>
38 #if NAN_STATIC_DEFAULT
39 # include <math.h>
40 /* #endif NAN_STATIC_DEFAULT*/
41 #elif NAN_STATIC_ISOC
42 # ifndef __USE_ISOC99
43 # define DISABLE_ISOC99 1
44 # define __USE_ISOC99 1
45 # endif /* !defined(__USE_ISOC99) */
46 # include <math.h>
47 # if DISABLE_ISOC99
48 # undef DISABLE_ISOC99
49 # undef __USE_ISOC99
50 # endif /* DISABLE_ISOC99 */
51 /* #endif NAN_STATIC_ISOC */
52 #elif NAN_ZERO_ZERO
53 # include <math.h>
54 # ifdef NAN
55 # undef NAN
56 # endif
57 # define NAN (0.0 / 0.0)
58 # ifndef isnan
59 # define isnan(f) ((f) != (f))
60 # endif /* !defined(isnan) */
61 # ifndef isfinite
62 # define isfinite(f) (((f) - (f)) == 0.0)
63 # endif
64 # ifndef isinf
65 # define isinf(f) (!isfinite(f) && !isnan(f))
66 # endif
67 #endif /* NAN_ZERO_ZERO */
69 #include "libcollectdclient/collectd/client.h"
71 #define RET_OKAY 0
72 #define RET_WARNING 1
73 #define RET_CRITICAL 2
74 #define RET_UNKNOWN 3
76 #define CON_NONE 0
77 #define CON_AVERAGE 1
78 #define CON_SUM 2
79 #define CON_PERCENTAGE 3
81 struct range_s
82 {
83 double min;
84 double max;
85 int invert;
86 };
87 typedef struct range_s range_t;
89 extern char *optarg;
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;
99 static _Bool nan_is_error_g = 0;
101 static char **match_ds_g = NULL;
102 static int match_ds_num_g = 0;
104 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
105 * that, so here's an own implementation.. It's easy enough. The GCC attributes
106 * are supposed to get good performance.. -octo */
107 __attribute__((malloc, nonnull (1)))
108 static char *cn_strdup (const char *str) /* {{{ */
109 {
110 size_t strsize;
111 char *ret;
113 strsize = strlen (str) + 1;
114 ret = (char *) malloc (strsize);
115 if (ret != NULL)
116 memcpy (ret, str, strsize);
117 return (ret);
118 } /* }}} char *cn_strdup */
120 static int filter_ds (size_t *values_num,
121 double **values, char ***values_names)
122 {
123 gauge_t *new_values;
124 char **new_names;
126 size_t i;
128 if (match_ds_g == NULL)
129 return (RET_OKAY);
131 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
132 if (new_values == NULL)
133 {
134 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
135 return (RET_UNKNOWN);
136 }
138 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
139 if (new_names == NULL)
140 {
141 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
142 free (new_values);
143 return (RET_UNKNOWN);
144 }
146 for (i = 0; i < (size_t) match_ds_num_g; i++)
147 {
148 size_t j;
150 /* match_ds_g keeps pointers into argv but the names will be freed */
151 new_names[i] = cn_strdup (match_ds_g[i]);
152 if (new_names[i] == NULL)
153 {
154 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
155 free (new_values);
156 for (j = 0; j < i; j++)
157 free (new_names[j]);
158 free (new_names);
159 return (RET_UNKNOWN);
160 }
162 for (j = 0; j < *values_num; j++)
163 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
164 break;
166 if (j == *values_num)
167 {
168 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
169 free (new_values);
170 for (j = 0; j <= i; j++)
171 free (new_names[j]);
172 free (new_names);
173 return (RET_CRITICAL);
174 }
176 new_values[i] = (*values)[j];
177 }
179 free (*values);
180 for (i = 0; i < *values_num; i++)
181 free ((*values_names)[i]);
182 free (*values_names);
184 *values = new_values;
185 *values_names = new_names;
186 *values_num = match_ds_num_g;
187 return (RET_OKAY);
188 } /* int filter_ds */
190 static void parse_range (char *string, range_t *range)
191 {
192 char *min_ptr;
193 char *max_ptr;
195 if (*string == '@')
196 {
197 range->invert = 1;
198 string++;
199 }
201 max_ptr = strchr (string, ':');
202 if (max_ptr == NULL)
203 {
204 min_ptr = NULL;
205 max_ptr = string;
206 }
207 else
208 {
209 min_ptr = string;
210 *max_ptr = '\0';
211 max_ptr++;
212 }
214 assert (max_ptr != NULL);
216 /* `10' == `0:10' */
217 if (min_ptr == NULL)
218 range->min = 0.0;
219 /* :10 == ~:10 == -inf:10 */
220 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
221 range->min = NAN;
222 else
223 range->min = atof (min_ptr);
225 if ((*max_ptr == '\0') || (*max_ptr == '~'))
226 range->max = NAN;
227 else
228 range->max = atof (max_ptr);
229 } /* void parse_range */
231 static int match_range (range_t *range, double value)
232 {
233 int ret = 0;
235 if (!isnan (range->min) && (range->min > value))
236 ret = 1;
237 if (!isnan (range->max) && (range->max < value))
238 ret = 1;
240 return (((ret - range->invert) == 0) ? 0 : 1);
241 } /* int match_range */
243 static void usage (const char *name)
244 {
245 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
246 "\n"
247 "Valid options are:\n"
248 " -s <socket> Path to collectd's UNIX-socket.\n"
249 " -n <v_spec> Value specification to get from collectd.\n"
250 " Format: `plugin-instance/type-instance'\n"
251 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
252 " DSes. By default all DSes are used.\n"
253 " -g <consol> Method to use to consolidate several DSes.\n"
254 " See below for a list of valid arguments.\n"
255 " -H <host> Hostname to query the values for.\n"
256 " -c <range> Critical range\n"
257 " -w <range> Warning range\n"
258 " -m Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
259 "\n"
260 "Consolidation functions:\n"
261 " none: Apply the warning- and critical-ranges to each data-source\n"
262 " individually.\n"
263 " average: Calculate the average of all matching DSes and apply the\n"
264 " warning- and critical-ranges to the calculated average.\n"
265 " sum: Apply the ranges to the sum of all DSes.\n"
266 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
267 " and the sum of all values."
268 "\n", name);
269 exit (1);
270 } /* void usage */
272 static int do_listval (lcc_connection_t *connection)
273 {
274 lcc_identifier_t *ret_ident = NULL;
275 size_t ret_ident_num = 0;
277 char *hostname = NULL;
279 int status;
280 size_t i;
282 status = lcc_listval (connection, &ret_ident, &ret_ident_num);
283 if (status != 0) {
284 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
285 if (ret_ident != NULL)
286 free (ret_ident);
287 return (RET_UNKNOWN);
288 }
290 status = lcc_sort_identifiers (connection, ret_ident, ret_ident_num);
291 if (status != 0) {
292 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
293 if (ret_ident != NULL)
294 free (ret_ident);
295 return (RET_UNKNOWN);
296 }
298 for (i = 0; i < ret_ident_num; ++i) {
299 char id[1024];
301 if ((hostname_g != NULL) && (strcasecmp (hostname_g, ret_ident[i].host)))
302 continue;
304 if ((hostname == NULL) || strcasecmp (hostname, ret_ident[i].host))
305 {
306 if (hostname != NULL)
307 free (hostname);
308 hostname = strdup (ret_ident[i].host);
309 printf ("Host: %s\n", hostname);
310 }
312 /* empty hostname; not to be printed again */
313 ret_ident[i].host[0] = '\0';
315 status = lcc_identifier_to_string (connection,
316 id, sizeof (id), ret_ident + i);
317 if (status != 0) {
318 printf ("ERROR: listval: Failed to convert returned "
319 "identifier to a string: %s\n",
320 lcc_strerror (connection));
321 continue;
322 }
324 /* skip over the (empty) hostname and following '/' */
325 printf ("\t%s\n", id + 1);
326 }
328 if (ret_ident != NULL)
329 free (ret_ident);
330 return (RET_OKAY);
331 } /* int do_listval */
333 static int do_check_con_none (size_t values_num,
334 double *values, char **values_names)
335 {
336 int num_critical = 0;
337 int num_warning = 0;
338 int num_okay = 0;
339 const char *status_str = "UNKNOWN";
340 int status_code = RET_UNKNOWN;
341 size_t i;
343 for (i = 0; i < values_num; i++)
344 {
345 if (isnan (values[i]))
346 {
347 if (nan_is_error_g)
348 num_critical++;
349 else
350 num_warning++;
351 }
352 else if (match_range (&range_critical_g, values[i]) != 0)
353 num_critical++;
354 else if (match_range (&range_warning_g, values[i]) != 0)
355 num_warning++;
356 else
357 num_okay++;
358 }
360 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
361 {
362 printf ("WARNING: No defined values found\n");
363 return (RET_WARNING);
364 }
365 else if ((num_critical == 0) && (num_warning == 0))
366 {
367 status_str = "OKAY";
368 status_code = RET_OKAY;
369 }
370 else if (num_critical == 0)
371 {
372 status_str = "WARNING";
373 status_code = RET_WARNING;
374 }
375 else
376 {
377 status_str = "CRITICAL";
378 status_code = RET_CRITICAL;
379 }
381 printf ("%s: %i critical, %i warning, %i okay", status_str,
382 num_critical, num_warning, num_okay);
383 if (values_num > 0)
384 {
385 printf (" |");
386 for (i = 0; i < values_num; i++)
387 printf (" %s=%f;;;;", values_names[i], values[i]);
388 }
389 printf ("\n");
391 return (status_code);
392 } /* int do_check_con_none */
394 static int do_check_con_average (size_t values_num,
395 double *values, char **values_names)
396 {
397 size_t i;
398 double total;
399 int total_num;
400 double average;
401 const char *status_str = "UNKNOWN";
402 int status_code = RET_UNKNOWN;
404 total = 0.0;
405 total_num = 0;
406 for (i = 0; i < values_num; i++)
407 {
408 if (isnan (values[i]))
409 {
410 if (!nan_is_error_g)
411 continue;
413 printf ("CRITICAL: Data source \"%s\" is NaN\n",
414 values_names[i]);
415 return (RET_CRITICAL);
416 }
418 total += values[i];
419 total_num++;
420 }
422 if (total_num == 0)
423 {
424 printf ("WARNING: No defined values found\n");
425 return (RET_WARNING);
426 }
428 average = total / total_num;
430 if (match_range (&range_critical_g, average) != 0)
431 {
432 status_str = "CRITICAL";
433 status_code = RET_CRITICAL;
434 }
435 else if (match_range (&range_warning_g, average) != 0)
436 {
437 status_str = "WARNING";
438 status_code = RET_WARNING;
439 }
440 else
441 {
442 status_str = "OKAY";
443 status_code = RET_OKAY;
444 }
446 printf ("%s: %g average |", status_str, average);
447 for (i = 0; i < values_num; i++)
448 printf (" %s=%f;;;;", values_names[i], values[i]);
449 printf ("\n");
451 return (status_code);
452 } /* int do_check_con_average */
454 static int do_check_con_sum (size_t values_num,
455 double *values, char **values_names)
456 {
457 size_t i;
458 double total;
459 int total_num;
460 const char *status_str = "UNKNOWN";
461 int status_code = RET_UNKNOWN;
463 total = 0.0;
464 total_num = 0;
465 for (i = 0; i < values_num; i++)
466 {
467 if (isnan (values[i]))
468 {
469 if (!nan_is_error_g)
470 continue;
472 printf ("CRITICAL: Data source \"%s\" is NaN\n",
473 values_names[i]);
474 return (RET_CRITICAL);
475 }
477 total += values[i];
478 total_num++;
479 }
481 if (total_num == 0)
482 {
483 printf ("WARNING: No defined values found\n");
484 return (RET_WARNING);
485 }
487 if (match_range (&range_critical_g, total) != 0)
488 {
489 status_str = "CRITICAL";
490 status_code = RET_CRITICAL;
491 }
492 else if (match_range (&range_warning_g, total) != 0)
493 {
494 status_str = "WARNING";
495 status_code = RET_WARNING;
496 }
497 else
498 {
499 status_str = "OKAY";
500 status_code = RET_OKAY;
501 }
503 printf ("%s: %g sum |", status_str, total);
504 for (i = 0; i < values_num; i++)
505 printf (" %s=%f;;;;", values_names[i], values[i]);
506 printf ("\n");
508 return (status_code);
509 } /* int do_check_con_sum */
511 static int do_check_con_percentage (size_t values_num,
512 double *values, char **values_names)
513 {
514 size_t i;
515 double sum = 0.0;
516 double percentage;
518 const char *status_str = "UNKNOWN";
519 int status_code = RET_UNKNOWN;
521 if ((values_num < 1) || (isnan (values[0])))
522 {
523 printf ("WARNING: The first value is not defined\n");
524 return (RET_WARNING);
525 }
527 for (i = 0; i < values_num; i++)
528 {
529 if (isnan (values[i]))
530 {
531 if (!nan_is_error_g)
532 continue;
534 printf ("CRITICAL: Data source \"%s\" is NaN\n",
535 values_names[i]);
536 return (RET_CRITICAL);
537 }
539 sum += values[i];
540 }
542 if (sum == 0.0)
543 {
544 printf ("WARNING: Values sum up to zero\n");
545 return (RET_WARNING);
546 }
548 percentage = 100.0 * values[0] / sum;
550 if (match_range (&range_critical_g, percentage) != 0)
551 {
552 status_str = "CRITICAL";
553 status_code = RET_CRITICAL;
554 }
555 else if (match_range (&range_warning_g, percentage) != 0)
556 {
557 status_str = "WARNING";
558 status_code = RET_WARNING;
559 }
560 else
561 {
562 status_str = "OKAY";
563 status_code = RET_OKAY;
564 }
566 printf ("%s: %lf percent |", status_str, percentage);
567 for (i = 0; i < values_num; i++)
568 printf (" %s=%lf;;;;", values_names[i], values[i]);
569 return (status_code);
570 } /* int do_check_con_percentage */
572 static int do_check (lcc_connection_t *connection)
573 {
574 gauge_t *values;
575 char **values_names;
576 size_t values_num;
577 char ident_str[1024];
578 lcc_identifier_t ident;
579 size_t i;
580 int status;
582 snprintf (ident_str, sizeof (ident_str), "%s/%s",
583 hostname_g, value_string_g);
584 ident_str[sizeof (ident_str) - 1] = 0;
586 memset (&ident, 0, sizeof (ident));
587 status = lcc_string_to_identifier (connection, &ident, ident_str);
588 if (status != 0)
589 {
590 printf ("ERROR: Creating an identifier failed: %s.\n",
591 lcc_strerror (connection));
592 LCC_DESTROY (connection);
593 return (RET_CRITICAL);
594 }
596 status = lcc_getval (connection, &ident,
597 &values_num, &values, &values_names);
598 if (status != 0)
599 {
600 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
601 lcc_strerror (connection));
602 LCC_DESTROY (connection);
603 return (RET_CRITICAL);
604 }
606 LCC_DESTROY (connection);
608 status = filter_ds (&values_num, &values, &values_names);
609 if (status != RET_OKAY)
610 return (status);
612 status = RET_UNKNOWN;
613 if (consolitation_g == CON_NONE)
614 status = do_check_con_none (values_num, values, values_names);
615 else if (consolitation_g == CON_AVERAGE)
616 status = do_check_con_average (values_num, values, values_names);
617 else if (consolitation_g == CON_SUM)
618 status = do_check_con_sum (values_num, values, values_names);
619 else if (consolitation_g == CON_PERCENTAGE)
620 status = do_check_con_percentage (values_num, values, values_names);
622 free (values);
623 if (values_names != NULL)
624 for (i = 0; i < values_num; i++)
625 free (values_names[i]);
626 free (values_names);
628 return (status);
629 } /* int do_check */
631 int main (int argc, char **argv)
632 {
633 char address[1024];
634 lcc_connection_t *connection;
636 int status;
638 range_critical_g.min = NAN;
639 range_critical_g.max = NAN;
640 range_critical_g.invert = 0;
642 range_warning_g.min = NAN;
643 range_warning_g.max = NAN;
644 range_warning_g.invert = 0;
646 while (42)
647 {
648 int c;
650 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
651 if (c < 0)
652 break;
654 switch (c)
655 {
656 case 'c':
657 parse_range (optarg, &range_critical_g);
658 break;
659 case 'w':
660 parse_range (optarg, &range_warning_g);
661 break;
662 case 's':
663 socket_file_g = optarg;
664 break;
665 case 'n':
666 value_string_g = optarg;
667 break;
668 case 'H':
669 hostname_g = optarg;
670 break;
671 case 'g':
672 if (strcasecmp (optarg, "none") == 0)
673 consolitation_g = CON_NONE;
674 else if (strcasecmp (optarg, "average") == 0)
675 consolitation_g = CON_AVERAGE;
676 else if (strcasecmp (optarg, "sum") == 0)
677 consolitation_g = CON_SUM;
678 else if (strcasecmp (optarg, "percentage") == 0)
679 consolitation_g = CON_PERCENTAGE;
680 else
681 {
682 fprintf (stderr, "Unknown consolidation function `%s'.\n",
683 optarg);
684 usage (argv[0]);
685 }
686 break;
687 case 'd':
688 {
689 char **tmp;
690 tmp = (char **) realloc (match_ds_g,
691 (match_ds_num_g + 1)
692 * sizeof (char *));
693 if (tmp == NULL)
694 {
695 fprintf (stderr, "realloc failed: %s\n",
696 strerror (errno));
697 return (RET_UNKNOWN);
698 }
699 match_ds_g = tmp;
700 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
701 if (match_ds_g[match_ds_num_g] == NULL)
702 {
703 fprintf (stderr, "cn_strdup failed: %s\n",
704 strerror (errno));
705 return (RET_UNKNOWN);
706 }
707 match_ds_num_g++;
708 break;
709 }
710 case 'm':
711 nan_is_error_g = 1;
712 break;
713 default:
714 usage (argv[0]);
715 } /* switch (c) */
716 }
718 if ((socket_file_g == NULL) || (value_string_g == NULL)
719 || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST"))))
720 {
721 fprintf (stderr, "Missing required arguments.\n");
722 usage (argv[0]);
723 }
725 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
726 address[sizeof (address) - 1] = 0;
728 connection = NULL;
729 status = lcc_connect (address, &connection);
730 if (status != 0)
731 {
732 printf ("ERROR: Connecting to daemon at %s failed.\n",
733 socket_file_g);
734 return (RET_CRITICAL);
735 }
737 if (0 == strcasecmp (value_string_g, "LIST"))
738 return (do_listval (connection));
740 return (do_check (connection));
741 } /* int main */