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 /* Set to C99 and POSIX code */
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
25 #endif
26 #ifndef _POSIX_SOURCE
27 # define _POSIX_SOURCE
28 #endif
29 #ifndef _POSIX_C_SOURCE
30 # define _POSIX_C_SOURCE 200112L
31 #endif
32 #ifndef _REENTRANT
33 # define _REENTRANT
34 #endif
36 /* Disable non-standard extensions */
37 #ifdef _BSD_SOURCE
38 # undef _BSD_SOURCE
39 #endif
40 #ifdef _SVID_SOURCE
41 # undef _SVID_SOURCE
42 #endif
43 #ifdef _GNU_SOURCE
44 # undef _GNU_SOURCE
45 #endif
47 #if !defined(__GNUC__) || !__GNUC__
48 # define __attribute__(x) /**/
49 #endif
51 #include "config.h"
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <errno.h>
57 #include <string.h>
58 #include <strings.h>
59 #include <assert.h>
61 #include <sys/socket.h>
62 #include <sys/un.h>
64 #include "libcollectdclient/client.h"
66 /*
67 * This is copied directly from collectd.h. Make changes there!
68 */
69 #if NAN_STATIC_DEFAULT
70 # include <math.h>
71 /* #endif NAN_STATIC_DEFAULT*/
72 #elif NAN_STATIC_ISOC
73 # ifndef __USE_ISOC99
74 # define DISABLE_ISOC99 1
75 # define __USE_ISOC99 1
76 # endif /* !defined(__USE_ISOC99) */
77 # include <math.h>
78 # if DISABLE_ISOC99
79 # undef DISABLE_ISOC99
80 # undef __USE_ISOC99
81 # endif /* DISABLE_ISOC99 */
82 /* #endif NAN_STATIC_ISOC */
83 #elif NAN_ZERO_ZERO
84 # include <math.h>
85 # ifdef NAN
86 # undef NAN
87 # endif
88 # define NAN (0.0 / 0.0)
89 # ifndef isnan
90 # define isnan(f) ((f) != (f))
91 # endif /* !defined(isnan) */
92 #endif /* NAN_ZERO_ZERO */
94 #define RET_OKAY 0
95 #define RET_WARNING 1
96 #define RET_CRITICAL 2
97 #define RET_UNKNOWN 3
99 #define CON_NONE 0
100 #define CON_AVERAGE 1
101 #define CON_SUM 2
103 struct range_s
104 {
105 double min;
106 double max;
107 int invert;
108 };
109 typedef struct range_s range_t;
111 extern char *optarg;
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) /* {{{ */
130 {
131 size_t strsize;
132 char *ret;
134 strsize = strlen (str) + 1;
135 ret = (char *) malloc (strsize);
136 if (ret != NULL)
137 memcpy (ret, str, strsize);
138 return (ret);
139 } /* }}} char *cn_strdup */
141 static int ignore_ds (const char *name)
142 {
143 int i;
145 if (match_ds_g == NULL)
146 return (0);
148 for (i = 0; i < match_ds_num_g; i++)
149 if (strcasecmp (match_ds_g[i], name) == 0)
150 return (0);
152 return (1);
153 } /* int ignore_ds */
155 static void parse_range (char *string, range_t *range)
156 {
157 char *min_ptr;
158 char *max_ptr;
160 if (*string == '@')
161 {
162 range->invert = 1;
163 string++;
164 }
166 max_ptr = strchr (string, ':');
167 if (max_ptr == NULL)
168 {
169 min_ptr = NULL;
170 max_ptr = string;
171 }
172 else
173 {
174 min_ptr = string;
175 *max_ptr = '\0';
176 max_ptr++;
177 }
179 assert (max_ptr != NULL);
181 /* `10' == `0:10' */
182 if (min_ptr == NULL)
183 range->min = 0.0;
184 /* :10 == ~:10 == -inf:10 */
185 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
186 range->min = NAN;
187 else
188 range->min = atof (min_ptr);
190 if ((*max_ptr == '\0') || (*max_ptr == '~'))
191 range->max = NAN;
192 else
193 range->max = atof (max_ptr);
194 } /* void parse_range */
196 static int match_range (range_t *range, double value)
197 {
198 int ret = 0;
200 if (!isnan (range->min) && (range->min > value))
201 ret = 1;
202 if (!isnan (range->max) && (range->max < value))
203 ret = 1;
205 return (((ret - range->invert) == 0) ? 0 : 1);
206 } /* int match_range */
208 static void usage (const char *name)
209 {
210 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
211 "\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"
223 "\n"
224 "Consolidation functions:\n"
225 " none: Apply the warning- and critical-ranges to each data-source\n"
226 " individually.\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"
230 "\n", name);
231 exit (1);
232 } /* void usage */
234 static int do_check_con_none (int values_num,
235 double *values, char **values_names)
236 {
237 int num_critical = 0;
238 int num_warning = 0;
239 int num_okay = 0;
240 const char *status_str = "UNKNOWN";
241 int status_code = RET_UNKNOWN;
242 int i;
244 for (i = 0; i < values_num; i++)
245 {
246 if (ignore_ds (values_names[i]))
247 continue;
249 if (isnan (values[i]))
250 num_warning++;
251 else if (match_range (&range_critical_g, values[i]) != 0)
252 num_critical++;
253 else if (match_range (&range_warning_g, values[i]) != 0)
254 num_warning++;
255 else
256 num_okay++;
257 }
259 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
260 {
261 printf ("WARNING: No defined values found\n");
262 return (RET_WARNING);
263 }
264 else if ((num_critical == 0) && (num_warning == 0))
265 {
266 status_str = "OKAY";
267 status_code = RET_OKAY;
268 }
269 else if (num_critical == 0)
270 {
271 status_str = "WARNING";
272 status_code = RET_WARNING;
273 }
274 else
275 {
276 status_str = "CRITICAL";
277 status_code = RET_CRITICAL;
278 }
280 printf ("%s: %i critical, %i warning, %i okay", status_str,
281 num_critical, num_warning, num_okay);
282 if (values_num > 0)
283 {
284 printf (" |");
285 for (i = 0; i < values_num; i++)
286 printf (" %s=%g;;;;", values_names[i], values[i]);
287 }
288 printf ("\n");
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)
295 {
296 int i;
297 double total;
298 int total_num;
299 double average;
300 const char *status_str = "UNKNOWN";
301 int status_code = RET_UNKNOWN;
303 total = 0.0;
304 total_num = 0;
305 for (i = 0; i < values_num; i++)
306 {
307 if (ignore_ds (values_names[i]))
308 continue;
310 if (!isnan (values[i]))
311 {
312 total += values[i];
313 total_num++;
314 }
315 }
317 if (total_num == 0)
318 {
319 printf ("WARNING: No defined values found\n");
320 return (RET_WARNING);
321 }
323 average = total / total_num;
325 if (match_range (&range_critical_g, average) != 0)
326 {
327 status_str = "CRITICAL";
328 status_code = RET_CRITICAL;
329 }
330 else if (match_range (&range_warning_g, average) != 0)
331 {
332 status_str = "WARNING";
333 status_code = RET_WARNING;
334 }
335 else
336 {
337 status_str = "OKAY";
338 status_code = RET_OKAY;
339 }
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]);
344 printf ("\n");
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)
350 {
351 int i;
352 double total;
353 int total_num;
354 const char *status_str = "UNKNOWN";
355 int status_code = RET_UNKNOWN;
357 total = 0.0;
358 total_num = 0;
359 for (i = 0; i < values_num; i++)
360 {
361 if (ignore_ds (values_names[i]))
362 continue;
364 if (!isnan (values[i]))
365 {
366 total += values[i];
367 total_num++;
368 }
369 }
371 if (total_num == 0)
372 {
373 printf ("WARNING: No defined values found\n");
374 return (RET_WARNING);
375 }
377 if (match_range (&range_critical_g, total) != 0)
378 {
379 status_str = "CRITICAL";
380 status_code = RET_CRITICAL;
381 }
382 else if (match_range (&range_warning_g, total) != 0)
383 {
384 status_str = "WARNING";
385 status_code = RET_WARNING;
386 }
387 else
388 {
389 status_str = "OKAY";
390 status_code = RET_OKAY;
391 }
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]);
396 printf ("\n");
398 return (status_code);
399 } /* int do_check_con_sum */
401 static int do_check (void)
402 {
403 lcc_connection_t *connection;
404 gauge_t *values;
405 char **values_names;
406 size_t values_num;
407 char address[1024];
408 char ident_str[1024];
409 lcc_identifier_t ident;
410 size_t i;
411 int status;
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;
420 connection = NULL;
421 status = lcc_connect (address, &connection);
422 if (status != 0)
423 {
424 printf ("ERROR: Connecting to daemon at %s failed.\n",
425 socket_file_g);
426 return (RET_CRITICAL);
427 }
429 memset (&ident, 0, sizeof (ident));
430 status = lcc_string_to_identifier (connection, &ident, ident_str);
431 if (status != 0)
432 {
433 printf ("ERROR: Creating an identifier failed: %s.\n",
434 lcc_strerror (connection));
435 LCC_DESTROY (connection);
436 return (RET_CRITICAL);
437 }
439 status = lcc_getval (connection, &ident,
440 &values_num, &values, &values_names);
441 if (status != 0)
442 {
443 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
444 lcc_strerror (connection));
445 LCC_DESTROY (connection);
446 return (RET_CRITICAL);
447 }
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);
459 free (values);
460 if (values_names != NULL)
461 for (i = 0; i < values_num; i++)
462 free (values_names[i]);
463 free (values_names);
465 return (status);
466 } /* int do_check */
468 int main (int argc, char **argv)
469 {
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;
478 while (42)
479 {
480 int c;
482 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
483 if (c < 0)
484 break;
486 switch (c)
487 {
488 case 'c':
489 parse_range (optarg, &range_critical_g);
490 break;
491 case 'w':
492 parse_range (optarg, &range_warning_g);
493 break;
494 case 's':
495 socket_file_g = optarg;
496 break;
497 case 'n':
498 value_string_g = optarg;
499 break;
500 case 'H':
501 hostname_g = optarg;
502 break;
503 case 'g':
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;
510 else
511 usage (argv[0]);
512 break;
513 case 'd':
514 {
515 char **tmp;
516 tmp = (char **) realloc (match_ds_g,
517 (match_ds_num_g + 1)
518 * sizeof (char *));
519 if (tmp == NULL)
520 {
521 fprintf (stderr, "realloc failed: %s\n",
522 strerror (errno));
523 return (RET_UNKNOWN);
524 }
525 match_ds_g = tmp;
526 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
527 if (match_ds_g[match_ds_num_g] == NULL)
528 {
529 fprintf (stderr, "cn_strdup failed: %s\n",
530 strerror (errno));
531 return (RET_UNKNOWN);
532 }
533 match_ds_num_g++;
534 break;
535 }
536 default:
537 usage (argv[0]);
538 } /* switch (c) */
539 }
541 if ((socket_file_g == NULL) || (value_string_g == NULL)
542 || (hostname_g == NULL))
543 usage (argv[0]);
545 return (do_check ());
546 } /* int main */