1 /**
2 * collectd-nagios - src/collectd-nagios.c
3 * Copyright (C) 2008-2010 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #if !defined(__GNUC__) || !__GNUC__
32 # define __attribute__(x) /**/
33 #endif
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <assert.h>
43 #if NAN_STATIC_DEFAULT
44 # include <math.h>
45 /* #endif NAN_STATIC_DEFAULT*/
46 #elif NAN_STATIC_ISOC
47 # ifndef __USE_ISOC99
48 # define DISABLE_ISOC99 1
49 # define __USE_ISOC99 1
50 # endif /* !defined(__USE_ISOC99) */
51 # include <math.h>
52 # if DISABLE_ISOC99
53 # undef DISABLE_ISOC99
54 # undef __USE_ISOC99
55 # endif /* DISABLE_ISOC99 */
56 /* #endif NAN_STATIC_ISOC */
57 #elif NAN_ZERO_ZERO
58 # include <math.h>
59 # ifdef NAN
60 # undef NAN
61 # endif
62 # define NAN (0.0 / 0.0)
63 # ifndef isnan
64 # define isnan(f) ((f) != (f))
65 # endif /* !defined(isnan) */
66 # ifndef isfinite
67 # define isfinite(f) (((f) - (f)) == 0.0)
68 # endif
69 # ifndef isinf
70 # define isinf(f) (!isfinite(f) && !isnan(f))
71 # endif
72 #endif /* NAN_ZERO_ZERO */
74 #include "libcollectdclient/collectd/client.h"
76 #define RET_OKAY 0
77 #define RET_WARNING 1
78 #define RET_CRITICAL 2
79 #define RET_UNKNOWN 3
81 #define CON_NONE 0
82 #define CON_AVERAGE 1
83 #define CON_SUM 2
84 #define CON_PERCENTAGE 3
86 struct range_s
87 {
88 double min;
89 double max;
90 int invert;
91 };
92 typedef struct range_s range_t;
94 extern char *optarg;
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;
104 static _Bool nan_is_error_g = 0;
106 static char **match_ds_g = NULL;
107 static size_t match_ds_num_g = 0;
109 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
110 * that, so here's an own implementation.. It's easy enough. The GCC attributes
111 * are supposed to get good performance.. -octo */
112 __attribute__((malloc, nonnull (1)))
113 static char *cn_strdup (const char *str) /* {{{ */
114 {
115 size_t strsize;
116 char *ret;
118 strsize = strlen (str) + 1;
119 ret = (char *) malloc (strsize);
120 if (ret != NULL)
121 memcpy (ret, str, strsize);
122 return (ret);
123 } /* }}} char *cn_strdup */
125 static int filter_ds (size_t *values_num,
126 double **values, char ***values_names)
127 {
128 gauge_t *new_values;
129 char **new_names;
131 if (match_ds_g == NULL)
132 return (RET_OKAY);
134 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
135 if (new_values == NULL)
136 {
137 fprintf (stderr, "calloc failed: %s\n", strerror (errno));
138 return (RET_UNKNOWN);
139 }
141 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
142 if (new_names == NULL)
143 {
144 fprintf (stderr, "calloc failed: %s\n", strerror (errno));
145 free (new_values);
146 return (RET_UNKNOWN);
147 }
149 for (size_t i = 0; i < match_ds_num_g; i++)
150 {
151 size_t j;
153 /* match_ds_g keeps pointers into argv but the names will be freed */
154 new_names[i] = cn_strdup (match_ds_g[i]);
155 if (new_names[i] == NULL)
156 {
157 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
158 free (new_values);
159 for (j = 0; j < i; j++)
160 free (new_names[j]);
161 free (new_names);
162 return (RET_UNKNOWN);
163 }
165 for (j = 0; j < *values_num; j++)
166 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
167 break;
169 if (j == *values_num)
170 {
171 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
172 free (new_values);
173 for (j = 0; j <= i; j++)
174 free (new_names[j]);
175 free (new_names);
176 return (RET_CRITICAL);
177 }
179 new_values[i] = (*values)[j];
180 }
182 free (*values);
183 for (size_t i = 0; i < *values_num; i++)
184 free ((*values_names)[i]);
185 free (*values_names);
187 *values = new_values;
188 *values_names = new_names;
189 *values_num = match_ds_num_g;
190 return (RET_OKAY);
191 } /* int filter_ds */
193 static void parse_range (char *string, range_t *range)
194 {
195 char *min_ptr;
196 char *max_ptr;
198 if (*string == '@')
199 {
200 range->invert = 1;
201 string++;
202 }
204 max_ptr = strchr (string, ':');
205 if (max_ptr == NULL)
206 {
207 min_ptr = NULL;
208 max_ptr = string;
209 }
210 else
211 {
212 min_ptr = string;
213 *max_ptr = '\0';
214 max_ptr++;
215 }
217 assert (max_ptr != NULL);
219 /* `10' == `0:10' */
220 if (min_ptr == NULL)
221 range->min = 0.0;
222 /* :10 == ~:10 == -inf:10 */
223 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
224 range->min = NAN;
225 else
226 range->min = atof (min_ptr);
228 if ((*max_ptr == '\0') || (*max_ptr == '~'))
229 range->max = NAN;
230 else
231 range->max = atof (max_ptr);
232 } /* void parse_range */
234 static int match_range (range_t *range, double value)
235 {
236 int ret = 0;
238 if (!isnan (range->min) && (range->min > value))
239 ret = 1;
240 if (!isnan (range->max) && (range->max < value))
241 ret = 1;
243 return (((ret - range->invert) == 0) ? 0 : 1);
244 } /* int match_range */
246 __attribute__((noreturn))
247 static void usage (const char *name)
248 {
249 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
250 "\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"
262 " -m Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
263 "\n"
264 "Consolidation functions:\n"
265 " none: Apply the warning- and critical-ranges to each data-source\n"
266 " individually.\n"
267 " average: Calculate the average of all matching DSes and apply the\n"
268 " warning- and critical-ranges to the calculated average.\n"
269 " sum: Apply the ranges to the sum of all DSes.\n"
270 " percentage: Apply the ranges to the ratio (in percent) of the first value\n"
271 " and the sum of all values."
272 "\n", name);
273 exit (1);
274 } /* void usage */
276 static int do_listval (lcc_connection_t *connection)
277 {
278 lcc_identifier_t *ret_ident = NULL;
279 size_t ret_ident_num = 0;
281 char *hostname = NULL;
283 int status;
285 status = lcc_listval (connection, &ret_ident, &ret_ident_num);
286 if (status != 0) {
287 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
288 if (ret_ident != NULL)
289 free (ret_ident);
290 return (RET_UNKNOWN);
291 }
293 status = lcc_sort_identifiers (connection, ret_ident, ret_ident_num);
294 if (status != 0) {
295 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
296 if (ret_ident != NULL)
297 free (ret_ident);
298 return (RET_UNKNOWN);
299 }
301 for (size_t i = 0; i < ret_ident_num; ++i) {
302 char id[1024];
304 if ((hostname_g != NULL) && (strcasecmp (hostname_g, ret_ident[i].host)))
305 continue;
307 if ((hostname == NULL) || strcasecmp (hostname, ret_ident[i].host))
308 {
309 free (hostname);
310 hostname = strdup (ret_ident[i].host);
311 printf ("Host: %s\n", hostname);
312 }
314 /* empty hostname; not to be printed again */
315 ret_ident[i].host[0] = '\0';
317 status = lcc_identifier_to_string (connection,
318 id, sizeof (id), ret_ident + i);
319 if (status != 0) {
320 printf ("ERROR: listval: Failed to convert returned "
321 "identifier to a string: %s\n",
322 lcc_strerror (connection));
323 free (hostname);
324 hostname = NULL;
325 continue;
326 }
328 /* skip over the (empty) hostname and following '/' */
329 printf ("\t%s\n", id + 1);
330 }
332 free (ret_ident);
333 free (hostname);
334 return (RET_OKAY);
335 } /* int do_listval */
337 static int do_check_con_none (size_t values_num,
338 double *values, char **values_names)
339 {
340 int num_critical = 0;
341 int num_warning = 0;
342 int num_okay = 0;
343 const char *status_str = "UNKNOWN";
344 int status_code = RET_UNKNOWN;
346 for (size_t i = 0; i < values_num; i++)
347 {
348 if (isnan (values[i]))
349 {
350 if (nan_is_error_g)
351 num_critical++;
352 else
353 num_warning++;
354 }
355 else if (match_range (&range_critical_g, values[i]) != 0)
356 num_critical++;
357 else if (match_range (&range_warning_g, values[i]) != 0)
358 num_warning++;
359 else
360 num_okay++;
361 }
363 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
364 {
365 printf ("WARNING: No defined values found\n");
366 return (RET_WARNING);
367 }
368 else if ((num_critical == 0) && (num_warning == 0))
369 {
370 status_str = "OKAY";
371 status_code = RET_OKAY;
372 }
373 else if (num_critical == 0)
374 {
375 status_str = "WARNING";
376 status_code = RET_WARNING;
377 }
378 else
379 {
380 status_str = "CRITICAL";
381 status_code = RET_CRITICAL;
382 }
384 printf ("%s: %i critical, %i warning, %i okay", status_str,
385 num_critical, num_warning, num_okay);
386 if (values_num > 0)
387 {
388 printf (" |");
389 for (size_t i = 0; i < values_num; i++)
390 printf (" %s=%f;;;;", values_names[i], values[i]);
391 }
392 printf ("\n");
394 return (status_code);
395 } /* int do_check_con_none */
397 static int do_check_con_average (size_t values_num,
398 double *values, char **values_names)
399 {
400 double total;
401 int total_num;
402 double average;
403 const char *status_str = "UNKNOWN";
404 int status_code = RET_UNKNOWN;
406 total = 0.0;
407 total_num = 0;
408 for (size_t i = 0; i < values_num; i++)
409 {
410 if (isnan (values[i]))
411 {
412 if (!nan_is_error_g)
413 continue;
415 printf ("CRITICAL: Data source \"%s\" is NaN\n",
416 values_names[i]);
417 return (RET_CRITICAL);
418 }
420 total += values[i];
421 total_num++;
422 }
424 if (total_num == 0)
425 {
426 printf ("WARNING: No defined values found\n");
427 return (RET_WARNING);
428 }
430 average = total / total_num;
432 if (match_range (&range_critical_g, average) != 0)
433 {
434 status_str = "CRITICAL";
435 status_code = RET_CRITICAL;
436 }
437 else if (match_range (&range_warning_g, average) != 0)
438 {
439 status_str = "WARNING";
440 status_code = RET_WARNING;
441 }
442 else
443 {
444 status_str = "OKAY";
445 status_code = RET_OKAY;
446 }
448 printf ("%s: %g average |", status_str, average);
449 for (size_t i = 0; i < values_num; i++)
450 printf (" %s=%f;;;;", values_names[i], values[i]);
451 printf ("\n");
453 return (status_code);
454 } /* int do_check_con_average */
456 static int do_check_con_sum (size_t values_num,
457 double *values, char **values_names)
458 {
459 double total;
460 int total_num;
461 const char *status_str = "UNKNOWN";
462 int status_code = RET_UNKNOWN;
464 total = 0.0;
465 total_num = 0;
466 for (size_t i = 0; i < values_num; i++)
467 {
468 if (isnan (values[i]))
469 {
470 if (!nan_is_error_g)
471 continue;
473 printf ("CRITICAL: Data source \"%s\" is NaN\n",
474 values_names[i]);
475 return (RET_CRITICAL);
476 }
478 total += values[i];
479 total_num++;
480 }
482 if (total_num == 0)
483 {
484 printf ("WARNING: No defined values found\n");
485 return (RET_WARNING);
486 }
488 if (match_range (&range_critical_g, total) != 0)
489 {
490 status_str = "CRITICAL";
491 status_code = RET_CRITICAL;
492 }
493 else if (match_range (&range_warning_g, total) != 0)
494 {
495 status_str = "WARNING";
496 status_code = RET_WARNING;
497 }
498 else
499 {
500 status_str = "OKAY";
501 status_code = RET_OKAY;
502 }
504 printf ("%s: %g sum |", status_str, total);
505 for (size_t i = 0; i < values_num; i++)
506 printf (" %s=%f;;;;", values_names[i], values[i]);
507 printf ("\n");
509 return (status_code);
510 } /* int do_check_con_sum */
512 static int do_check_con_percentage (size_t values_num,
513 double *values, char **values_names)
514 {
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 (size_t 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 (size_t 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 int status;
581 snprintf (ident_str, sizeof (ident_str), "%s/%s",
582 hostname_g, value_string_g);
583 ident_str[sizeof (ident_str) - 1] = 0;
585 status = lcc_string_to_identifier (connection, &ident, ident_str);
586 if (status != 0)
587 {
588 printf ("ERROR: Creating an identifier failed: %s.\n",
589 lcc_strerror (connection));
590 LCC_DESTROY (connection);
591 return (RET_CRITICAL);
592 }
594 status = lcc_getval (connection, &ident,
595 &values_num, &values, &values_names);
596 if (status != 0)
597 {
598 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
599 lcc_strerror (connection));
600 LCC_DESTROY (connection);
601 return (RET_CRITICAL);
602 }
604 LCC_DESTROY (connection);
606 status = filter_ds (&values_num, &values, &values_names);
607 if (status != RET_OKAY)
608 return (status);
610 status = RET_UNKNOWN;
611 if (consolitation_g == CON_NONE)
612 status = do_check_con_none (values_num, values, values_names);
613 else if (consolitation_g == CON_AVERAGE)
614 status = do_check_con_average (values_num, values, values_names);
615 else if (consolitation_g == CON_SUM)
616 status = do_check_con_sum (values_num, values, values_names);
617 else if (consolitation_g == CON_PERCENTAGE)
618 status = do_check_con_percentage (values_num, values, values_names);
620 free (values);
621 if (values_names != NULL)
622 for (size_t i = 0; i < values_num; i++)
623 free (values_names[i]);
624 free (values_names);
626 return (status);
627 } /* int do_check */
629 int main (int argc, char **argv)
630 {
631 char address[1024];
632 lcc_connection_t *connection;
634 int status;
636 range_critical_g.min = NAN;
637 range_critical_g.max = NAN;
638 range_critical_g.invert = 0;
640 range_warning_g.min = NAN;
641 range_warning_g.max = NAN;
642 range_warning_g.invert = 0;
644 while (42)
645 {
646 int c;
648 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
649 if (c < 0)
650 break;
652 switch (c)
653 {
654 case 'c':
655 parse_range (optarg, &range_critical_g);
656 break;
657 case 'w':
658 parse_range (optarg, &range_warning_g);
659 break;
660 case 's':
661 socket_file_g = optarg;
662 break;
663 case 'n':
664 value_string_g = optarg;
665 break;
666 case 'H':
667 hostname_g = optarg;
668 break;
669 case 'g':
670 if (strcasecmp (optarg, "none") == 0)
671 consolitation_g = CON_NONE;
672 else if (strcasecmp (optarg, "average") == 0)
673 consolitation_g = CON_AVERAGE;
674 else if (strcasecmp (optarg, "sum") == 0)
675 consolitation_g = CON_SUM;
676 else if (strcasecmp (optarg, "percentage") == 0)
677 consolitation_g = CON_PERCENTAGE;
678 else
679 {
680 fprintf (stderr, "Unknown consolidation function `%s'.\n",
681 optarg);
682 usage (argv[0]);
683 }
684 break;
685 case 'd':
686 {
687 char **tmp;
688 tmp = realloc (match_ds_g,
689 (match_ds_num_g + 1)
690 * sizeof (char *));
691 if (tmp == NULL)
692 {
693 fprintf (stderr, "realloc failed: %s\n",
694 strerror (errno));
695 return (RET_UNKNOWN);
696 }
697 match_ds_g = tmp;
698 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
699 if (match_ds_g[match_ds_num_g] == NULL)
700 {
701 fprintf (stderr, "cn_strdup failed: %s\n",
702 strerror (errno));
703 return (RET_UNKNOWN);
704 }
705 match_ds_num_g++;
706 break;
707 }
708 case 'm':
709 nan_is_error_g = 1;
710 break;
711 default:
712 usage (argv[0]);
713 } /* switch (c) */
714 }
716 if ((socket_file_g == NULL) || (value_string_g == NULL)
717 || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST"))))
718 {
719 fprintf (stderr, "Missing required arguments.\n");
720 usage (argv[0]);
721 }
723 snprintf (address, sizeof (address), "unix:%s", socket_file_g);
724 address[sizeof (address) - 1] = 0;
726 connection = NULL;
727 status = lcc_connect (address, &connection);
728 if (status != 0)
729 {
730 printf ("ERROR: Connecting to daemon at %s failed.\n",
731 socket_file_g);
732 return (RET_CRITICAL);
733 }
735 if (0 == strcasecmp (value_string_g, "LIST"))
736 return (do_listval (connection));
738 return (do_check (connection));
739 } /* int main */