1 /**
2 * collectd-nagios - src/collectd-nagios.c
3 * Copyright (C) 2008 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 /* Set to C99 and POSIX code */
27 #if COLLECT_STANDARDS
28 # include "standards.h"
29 #endif /* COLLECT_STANDARDS */
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 #include <sys/socket.h>
44 #include <sys/un.h>
46 #include "libcollectdclient/client.h"
48 /*
49 * This is copied directly from collectd.h. Make changes there!
50 */
51 #if NAN_STATIC_DEFAULT
52 # include <math.h>
53 /* #endif NAN_STATIC_DEFAULT*/
54 #elif NAN_STATIC_ISOC
55 # ifndef __USE_ISOC99
56 # define DISABLE_ISOC99 1
57 # define __USE_ISOC99 1
58 # endif /* !defined(__USE_ISOC99) */
59 # include <math.h>
60 # if DISABLE_ISOC99
61 # undef DISABLE_ISOC99
62 # undef __USE_ISOC99
63 # endif /* DISABLE_ISOC99 */
64 /* #endif NAN_STATIC_ISOC */
65 #elif NAN_ZERO_ZERO
66 # include <math.h>
67 # ifdef NAN
68 # undef NAN
69 # endif
70 # define NAN (0.0 / 0.0)
71 # ifndef isnan
72 # define isnan(f) ((f) != (f))
73 # endif /* !defined(isnan) */
74 #endif /* NAN_ZERO_ZERO */
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;
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) /* {{{ */
113 {
114 size_t strsize;
115 char *ret;
117 strsize = strlen (str) + 1;
118 ret = (char *) malloc (strsize);
119 if (ret != NULL)
120 memcpy (ret, str, strsize);
121 return (ret);
122 } /* }}} char *cn_strdup */
124 static int filter_ds (size_t *values_num,
125 double **values, char ***values_names)
126 {
127 gauge_t *new_values;
128 char **new_names;
130 size_t i;
132 if (match_ds_g == NULL)
133 return (RET_OKAY);
135 new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
136 if (new_values == NULL)
137 {
138 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
139 return (RET_UNKNOWN);
140 }
142 new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
143 if (new_names == NULL)
144 {
145 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
146 free (new_values);
147 return (RET_UNKNOWN);
148 }
150 for (i = 0; i < (size_t) match_ds_num_g; i++)
151 {
152 size_t j;
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)
157 {
158 fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
159 free (new_values);
160 for (j = 0; j < i; j++)
161 free (new_names[j]);
162 free (new_names);
163 return (RET_UNKNOWN);
164 }
166 for (j = 0; j < *values_num; j++)
167 if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
168 break;
170 if (j == *values_num)
171 {
172 printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
173 free (new_values);
174 for (j = 0; j <= i; j++)
175 free (new_names[j]);
176 free (new_names);
177 return (RET_CRITICAL);
178 }
180 new_values[i] = (*values)[j];
181 }
183 free (*values);
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;
191 return (RET_OKAY);
192 } /* int filter_ds */
194 static void parse_range (char *string, range_t *range)
195 {
196 char *min_ptr;
197 char *max_ptr;
199 if (*string == '@')
200 {
201 range->invert = 1;
202 string++;
203 }
205 max_ptr = strchr (string, ':');
206 if (max_ptr == NULL)
207 {
208 min_ptr = NULL;
209 max_ptr = string;
210 }
211 else
212 {
213 min_ptr = string;
214 *max_ptr = '\0';
215 max_ptr++;
216 }
218 assert (max_ptr != NULL);
220 /* `10' == `0:10' */
221 if (min_ptr == NULL)
222 range->min = 0.0;
223 /* :10 == ~:10 == -inf:10 */
224 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
225 range->min = NAN;
226 else
227 range->min = atof (min_ptr);
229 if ((*max_ptr == '\0') || (*max_ptr == '~'))
230 range->max = NAN;
231 else
232 range->max = atof (max_ptr);
233 } /* void parse_range */
235 static int match_range (range_t *range, double value)
236 {
237 int ret = 0;
239 if (!isnan (range->min) && (range->min > value))
240 ret = 1;
241 if (!isnan (range->max) && (range->max < value))
242 ret = 1;
244 return (((ret - range->invert) == 0) ? 0 : 1);
245 } /* int match_range */
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 "\n"
263 "Consolidation functions:\n"
264 " none: Apply the warning- and critical-ranges to each data-source\n"
265 " individually.\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."
271 "\n", name);
272 exit (1);
273 } /* void usage */
275 static int do_check_con_none (size_t values_num,
276 double *values, char **values_names)
277 {
278 int num_critical = 0;
279 int num_warning = 0;
280 int num_okay = 0;
281 const char *status_str = "UNKNOWN";
282 int status_code = RET_UNKNOWN;
283 size_t i;
285 for (i = 0; i < values_num; i++)
286 {
287 if (isnan (values[i]))
288 num_warning++;
289 else if (match_range (&range_critical_g, values[i]) != 0)
290 num_critical++;
291 else if (match_range (&range_warning_g, values[i]) != 0)
292 num_warning++;
293 else
294 num_okay++;
295 }
297 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
298 {
299 printf ("WARNING: No defined values found\n");
300 return (RET_WARNING);
301 }
302 else if ((num_critical == 0) && (num_warning == 0))
303 {
304 status_str = "OKAY";
305 status_code = RET_OKAY;
306 }
307 else if (num_critical == 0)
308 {
309 status_str = "WARNING";
310 status_code = RET_WARNING;
311 }
312 else
313 {
314 status_str = "CRITICAL";
315 status_code = RET_CRITICAL;
316 }
318 printf ("%s: %i critical, %i warning, %i okay", status_str,
319 num_critical, num_warning, num_okay);
320 if (values_num > 0)
321 {
322 printf (" |");
323 for (i = 0; i < values_num; i++)
324 printf (" %s=%g;;;;", values_names[i], values[i]);
325 }
326 printf ("\n");
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)
333 {
334 size_t i;
335 double total;
336 int total_num;
337 double average;
338 const char *status_str = "UNKNOWN";
339 int status_code = RET_UNKNOWN;
341 total = 0.0;
342 total_num = 0;
343 for (i = 0; i < values_num; i++)
344 {
345 if (!isnan (values[i]))
346 {
347 total += values[i];
348 total_num++;
349 }
350 }
352 if (total_num == 0)
353 {
354 printf ("WARNING: No defined values found\n");
355 return (RET_WARNING);
356 }
358 average = total / total_num;
360 if (match_range (&range_critical_g, average) != 0)
361 {
362 status_str = "CRITICAL";
363 status_code = RET_CRITICAL;
364 }
365 else if (match_range (&range_warning_g, average) != 0)
366 {
367 status_str = "WARNING";
368 status_code = RET_WARNING;
369 }
370 else
371 {
372 status_str = "OKAY";
373 status_code = RET_OKAY;
374 }
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]);
379 printf ("\n");
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)
386 {
387 size_t i;
388 double total;
389 int total_num;
390 const char *status_str = "UNKNOWN";
391 int status_code = RET_UNKNOWN;
393 total = 0.0;
394 total_num = 0;
395 for (i = 0; i < values_num; i++)
396 {
397 if (!isnan (values[i]))
398 {
399 total += values[i];
400 total_num++;
401 }
402 }
404 if (total_num == 0)
405 {
406 printf ("WARNING: No defined values found\n");
407 return (RET_WARNING);
408 }
410 if (match_range (&range_critical_g, total) != 0)
411 {
412 status_str = "CRITICAL";
413 status_code = RET_CRITICAL;
414 }
415 else if (match_range (&range_warning_g, total) != 0)
416 {
417 status_str = "WARNING";
418 status_code = RET_WARNING;
419 }
420 else
421 {
422 status_str = "OKAY";
423 status_code = RET_OKAY;
424 }
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]);
429 printf ("\n");
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)
436 {
437 size_t i;
438 double sum = 0.0;
439 double percentage;
441 const char *status_str = "UNKNOWN";
442 int status_code = RET_UNKNOWN;
444 if ((values_num < 1) || (isnan (values[0])))
445 {
446 printf ("WARNING: The first value is not defined\n");
447 return (RET_WARNING);
448 }
450 for (i = 0; i < values_num; i++)
451 if (!isnan (values[i]))
452 sum += values[i];
454 if (sum == 0.0)
455 {
456 printf ("WARNING: Values sum up to zero\n");
457 return (RET_WARNING);
458 }
460 percentage = 100.0 * values[0] / sum;
462 if (match_range (&range_critical_g, percentage) != 0)
463 {
464 status_str = "CRITICAL";
465 status_code = RET_CRITICAL;
466 }
467 else if (match_range (&range_warning_g, percentage) != 0)
468 {
469 status_str = "WARNING";
470 status_code = RET_WARNING;
471 }
472 else
473 {
474 status_str = "OKAY";
475 status_code = RET_OKAY;
476 }
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)
485 {
486 lcc_connection_t *connection;
487 gauge_t *values;
488 char **values_names;
489 size_t values_num;
490 char address[1024];
491 char ident_str[1024];
492 lcc_identifier_t ident;
493 size_t i;
494 int status;
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;
503 connection = NULL;
504 status = lcc_connect (address, &connection);
505 if (status != 0)
506 {
507 printf ("ERROR: Connecting to daemon at %s failed.\n",
508 socket_file_g);
509 return (RET_CRITICAL);
510 }
512 memset (&ident, 0, sizeof (ident));
513 status = lcc_string_to_identifier (connection, &ident, ident_str);
514 if (status != 0)
515 {
516 printf ("ERROR: Creating an identifier failed: %s.\n",
517 lcc_strerror (connection));
518 LCC_DESTROY (connection);
519 return (RET_CRITICAL);
520 }
522 status = lcc_getval (connection, &ident,
523 &values_num, &values, &values_names);
524 if (status != 0)
525 {
526 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
527 lcc_strerror (connection));
528 LCC_DESTROY (connection);
529 return (RET_CRITICAL);
530 }
532 LCC_DESTROY (connection);
534 status = filter_ds (&values_num, &values, &values_names);
535 if (status != RET_OKAY)
536 return (status);
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);
548 free (values);
549 if (values_names != NULL)
550 for (i = 0; i < values_num; i++)
551 free (values_names[i]);
552 free (values_names);
554 return (status);
555 } /* int do_check */
557 int main (int argc, char **argv)
558 {
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;
567 while (42)
568 {
569 int c;
571 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
572 if (c < 0)
573 break;
575 switch (c)
576 {
577 case 'c':
578 parse_range (optarg, &range_critical_g);
579 break;
580 case 'w':
581 parse_range (optarg, &range_warning_g);
582 break;
583 case 's':
584 socket_file_g = optarg;
585 break;
586 case 'n':
587 value_string_g = optarg;
588 break;
589 case 'H':
590 hostname_g = optarg;
591 break;
592 case 'g':
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;
601 else
602 {
603 fprintf (stderr, "Unknown consolidation function `%s'.\n",
604 optarg);
605 usage (argv[0]);
606 }
607 break;
608 case 'd':
609 {
610 char **tmp;
611 tmp = (char **) realloc (match_ds_g,
612 (match_ds_num_g + 1)
613 * sizeof (char *));
614 if (tmp == NULL)
615 {
616 fprintf (stderr, "realloc failed: %s\n",
617 strerror (errno));
618 return (RET_UNKNOWN);
619 }
620 match_ds_g = tmp;
621 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
622 if (match_ds_g[match_ds_num_g] == NULL)
623 {
624 fprintf (stderr, "cn_strdup failed: %s\n",
625 strerror (errno));
626 return (RET_UNKNOWN);
627 }
628 match_ds_num_g++;
629 break;
630 }
631 default:
632 usage (argv[0]);
633 } /* switch (c) */
634 }
636 if ((socket_file_g == NULL) || (value_string_g == NULL)
637 || (hostname_g == NULL))
638 {
639 fprintf (stderr, "Missing required arguments.\n");
640 usage (argv[0]);
641 }
643 return (do_check ());
644 } /* int main */