2 * collectd-nagios - src/collectd-nagios.c
3 * Copyright (C) 2008-2010 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
31 #if !defined(__GNUC__) || !__GNUC__
32 #define __attribute__(x) /**/
44 #if NAN_STATIC_DEFAULT
46 /* #endif NAN_STATIC_DEFAULT*/
49 #define DISABLE_ISOC99 1
50 #define __USE_ISOC99 1
51 #endif /* !defined(__USE_ISOC99) */
56 #endif /* DISABLE_ISOC99 */
57 /* #endif NAN_STATIC_ISOC */
63 #define NAN (0.0 / 0.0)
65 #define isnan(f) ((f) != (f))
66 #endif /* !defined(isnan) */
68 #define isfinite(f) (((f) - (f)) == 0.0)
71 #define isinf(f) (!isfinite(f) && !isnan(f))
73 #endif /* NAN_ZERO_ZERO */
75 #include "collectd/client.h"
79 #define RET_CRITICAL 2
85 #define CON_PERCENTAGE 3
92 typedef struct range_s range_t;
95 extern int optind, opterr, optopt;
97 static char *socket_file_g;
98 static char *value_string_g;
99 static char *hostname_g;
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;
106 static char **match_ds_g;
107 static size_t match_ds_num_g;
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))) static char *
113 cn_strdup(const char *str) /* {{{ */
118 strsize = strlen(str) + 1;
119 ret = (char *)malloc(strsize);
121 memcpy(ret, str, strsize);
123 } /* }}} char *cn_strdup */
125 static int filter_ds(size_t *values_num, double **values,
126 char ***values_names) {
130 if (match_ds_g == NULL)
133 new_values = (gauge_t *)calloc(match_ds_num_g, sizeof(*new_values));
134 if (new_values == NULL) {
135 fprintf(stderr, "calloc failed: %s\n", strerror(errno));
139 new_names = (char **)calloc(match_ds_num_g, sizeof(*new_names));
140 if (new_names == NULL) {
141 fprintf(stderr, "calloc failed: %s\n", strerror(errno));
146 for (size_t i = 0; i < match_ds_num_g; i++) {
149 /* match_ds_g keeps pointers into argv but the names will be freed */
150 new_names[i] = cn_strdup(match_ds_g[i]);
151 if (new_names[i] == NULL) {
152 fprintf(stderr, "cn_strdup failed: %s\n", strerror(errno));
154 for (j = 0; j < i; j++)
160 for (j = 0; j < *values_num; j++)
161 if (strcasecmp(new_names[i], (*values_names)[j]) == 0)
164 if (j == *values_num) {
165 printf("ERROR: DS `%s' is not available.\n", new_names[i]);
167 for (j = 0; j <= i; j++)
173 new_values[i] = (*values)[j];
177 for (size_t i = 0; i < *values_num; i++)
178 free((*values_names)[i]);
181 *values = new_values;
182 *values_names = new_names;
183 *values_num = match_ds_num_g;
185 } /* int filter_ds */
187 static void parse_range(char *string, range_t *range) {
191 if (*string == '@') {
196 max_ptr = strchr(string, ':');
197 if (max_ptr == NULL) {
206 assert(max_ptr != NULL);
211 /* :10 == ~:10 == -inf:10 */
212 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
215 range->min = atof(min_ptr);
217 if ((*max_ptr == '\0') || (*max_ptr == '~'))
220 range->max = atof(max_ptr);
221 } /* void parse_range */
223 static int match_range(range_t *range, double value) {
226 if (!isnan(range->min) && (range->min > value))
228 if (!isnan(range->max) && (range->max < value))
231 return ((ret - range->invert) == 0) ? 0 : 1;
232 } /* int match_range */
234 __attribute__((noreturn)) static void usage(const char *name) {
236 "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
238 "Valid options are:\n"
239 " -s <socket> Path to collectd's UNIX-socket.\n"
240 " -n <v_spec> Value specification to get from collectd.\n"
241 " Format: `plugin-instance/type-instance'\n"
242 " -d <ds> Select the DS to examine. May be repeated to "
244 " DSes. By default all DSes are used.\n"
245 " -g <consol> Method to use to consolidate several DSes.\n"
246 " See below for a list of valid arguments.\n"
247 " -H <host> Hostname to query the values for.\n"
248 " -c <range> Critical range\n"
249 " -w <range> Warning range\n"
250 " -m Treat \"Not a Number\" (NaN) as critical (default: "
253 "Consolidation functions:\n"
254 " none: Apply the warning- and critical-ranges to each "
257 " average: Calculate the average of all matching DSes and "
259 " warning- and critical-ranges to the calculated "
261 " sum: Apply the ranges to the sum of all DSes.\n"
262 " percentage: Apply the ranges to the ratio (in percent) of the "
264 " and the sum of all values."
270 static int do_listval(lcc_connection_t *connection) {
271 lcc_identifier_t *ret_ident = NULL;
272 size_t ret_ident_num = 0;
274 char *hostname = NULL;
278 status = lcc_listval(connection, &ret_ident, &ret_ident_num);
280 printf("UNKNOWN: %s\n", lcc_strerror(connection));
281 if (ret_ident != NULL)
286 status = lcc_sort_identifiers(connection, ret_ident, ret_ident_num);
288 printf("UNKNOWN: %s\n", lcc_strerror(connection));
289 if (ret_ident != NULL)
294 for (size_t i = 0; i < ret_ident_num; ++i) {
297 if ((hostname_g != NULL) && (strcasecmp(hostname_g, ret_ident[i].host)))
300 if ((hostname == NULL) || strcasecmp(hostname, ret_ident[i].host)) {
302 hostname = strdup(ret_ident[i].host);
303 printf("Host: %s\n", hostname);
306 /* empty hostname; not to be printed again */
307 ret_ident[i].host[0] = '\0';
310 lcc_identifier_to_string(connection, id, sizeof(id), ret_ident + i);
312 printf("ERROR: listval: Failed to convert returned "
313 "identifier to a string: %s\n",
314 lcc_strerror(connection));
320 /* skip over the (empty) hostname and following '/' */
321 printf("\t%s\n", id + 1);
327 } /* int do_listval */
329 static int do_check_con_none(size_t values_num, double *values,
330 char **values_names) {
331 int num_critical = 0;
334 const char *status_str = "UNKNOWN";
335 int status_code = RET_UNKNOWN;
337 for (size_t i = 0; i < values_num; i++) {
338 if (isnan(values[i])) {
343 } else if (match_range(&range_critical_g, values[i]) != 0)
345 else if (match_range(&range_warning_g, values[i]) != 0)
351 if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0)) {
352 printf("WARNING: No defined values found\n");
354 } else if ((num_critical == 0) && (num_warning == 0)) {
356 status_code = RET_OKAY;
357 } else if (num_critical == 0) {
358 status_str = "WARNING";
359 status_code = RET_WARNING;
361 status_str = "CRITICAL";
362 status_code = RET_CRITICAL;
365 printf("%s: %i critical, %i warning, %i okay", status_str, num_critical,
366 num_warning, num_okay);
367 if (values_num > 0) {
369 for (size_t i = 0; i < values_num; i++)
370 printf(" %s=%f;;;;", values_names[i], values[i]);
375 } /* int do_check_con_none */
377 static int do_check_con_average(size_t values_num, double *values,
378 char **values_names) {
382 const char *status_str = "UNKNOWN";
383 int status_code = RET_UNKNOWN;
387 for (size_t i = 0; i < values_num; i++) {
388 if (isnan(values[i])) {
392 printf("CRITICAL: Data source \"%s\" is NaN\n", values_names[i]);
400 if (total_num == 0) {
401 printf("WARNING: No defined values found\n");
405 average = total / total_num;
407 if (match_range(&range_critical_g, average) != 0) {
408 status_str = "CRITICAL";
409 status_code = RET_CRITICAL;
410 } else if (match_range(&range_warning_g, average) != 0) {
411 status_str = "WARNING";
412 status_code = RET_WARNING;
415 status_code = RET_OKAY;
418 printf("%s: %g average |", status_str, average);
419 for (size_t i = 0; i < values_num; i++)
420 printf(" %s=%f;;;;", values_names[i], values[i]);
424 } /* int do_check_con_average */
426 static int do_check_con_sum(size_t values_num, double *values,
427 char **values_names) {
430 const char *status_str = "UNKNOWN";
431 int status_code = RET_UNKNOWN;
435 for (size_t i = 0; i < values_num; i++) {
436 if (isnan(values[i])) {
440 printf("CRITICAL: Data source \"%s\" is NaN\n", values_names[i]);
448 if (total_num == 0) {
449 printf("WARNING: No defined values found\n");
453 if (match_range(&range_critical_g, total) != 0) {
454 status_str = "CRITICAL";
455 status_code = RET_CRITICAL;
456 } else if (match_range(&range_warning_g, total) != 0) {
457 status_str = "WARNING";
458 status_code = RET_WARNING;
461 status_code = RET_OKAY;
464 printf("%s: %g sum |", status_str, total);
465 for (size_t i = 0; i < values_num; i++)
466 printf(" %s=%f;;;;", values_names[i], values[i]);
470 } /* int do_check_con_sum */
472 static int do_check_con_percentage(size_t values_num, double *values,
473 char **values_names) {
477 const char *status_str = "UNKNOWN";
478 int status_code = RET_UNKNOWN;
480 if ((values_num < 1) || (isnan(values[0]))) {
481 printf("WARNING: The first value is not defined\n");
485 for (size_t i = 0; i < values_num; i++) {
486 if (isnan(values[i])) {
490 printf("CRITICAL: Data source \"%s\" is NaN\n", values_names[i]);
498 printf("WARNING: Values sum up to zero\n");
502 percentage = 100.0 * values[0] / sum;
504 if (match_range(&range_critical_g, percentage) != 0) {
505 status_str = "CRITICAL";
506 status_code = RET_CRITICAL;
507 } else if (match_range(&range_warning_g, percentage) != 0) {
508 status_str = "WARNING";
509 status_code = RET_WARNING;
512 status_code = RET_OKAY;
515 printf("%s: %lf percent |", status_str, percentage);
516 for (size_t i = 0; i < values_num; i++)
517 printf(" %s=%lf;;;;", values_names[i], values[i]);
519 } /* int do_check_con_percentage */
521 static int do_check(lcc_connection_t *connection) {
525 char ident_str[1024];
526 lcc_identifier_t ident;
529 snprintf(ident_str, sizeof(ident_str), "%s/%s", hostname_g, value_string_g);
530 ident_str[sizeof(ident_str) - 1] = 0;
532 status = lcc_string_to_identifier(connection, &ident, ident_str);
534 printf("ERROR: Creating an identifier failed: %s.\n",
535 lcc_strerror(connection));
536 LCC_DESTROY(connection);
540 status = lcc_getval(connection, &ident, &values_num, &values, &values_names);
542 printf("ERROR: Retrieving values from the daemon failed: %s.\n",
543 lcc_strerror(connection));
544 LCC_DESTROY(connection);
548 LCC_DESTROY(connection);
550 status = filter_ds(&values_num, &values, &values_names);
551 if (status != RET_OKAY)
554 status = RET_UNKNOWN;
555 if (consolitation_g == CON_NONE)
556 status = do_check_con_none(values_num, values, values_names);
557 else if (consolitation_g == CON_AVERAGE)
558 status = do_check_con_average(values_num, values, values_names);
559 else if (consolitation_g == CON_SUM)
560 status = do_check_con_sum(values_num, values, values_names);
561 else if (consolitation_g == CON_PERCENTAGE)
562 status = do_check_con_percentage(values_num, values, values_names);
565 if (values_names != NULL)
566 for (size_t i = 0; i < values_num; i++)
567 free(values_names[i]);
573 int main(int argc, char **argv) {
575 lcc_connection_t *connection;
579 range_critical_g.min = NAN;
580 range_critical_g.max = NAN;
581 range_critical_g.invert = 0;
583 range_warning_g.min = NAN;
584 range_warning_g.max = NAN;
585 range_warning_g.invert = 0;
590 c = getopt(argc, argv, "w:c:s:n:H:g:d:hm");
596 parse_range(optarg, &range_critical_g);
599 parse_range(optarg, &range_warning_g);
602 socket_file_g = optarg;
605 value_string_g = optarg;
611 if (strcasecmp(optarg, "none") == 0)
612 consolitation_g = CON_NONE;
613 else if (strcasecmp(optarg, "average") == 0)
614 consolitation_g = CON_AVERAGE;
615 else if (strcasecmp(optarg, "sum") == 0)
616 consolitation_g = CON_SUM;
617 else if (strcasecmp(optarg, "percentage") == 0)
618 consolitation_g = CON_PERCENTAGE;
620 fprintf(stderr, "Unknown consolidation function `%s'.\n", optarg);
626 tmp = realloc(match_ds_g, (match_ds_num_g + 1) * sizeof(char *));
628 fprintf(stderr, "realloc failed: %s\n", strerror(errno));
632 match_ds_g[match_ds_num_g] = cn_strdup(optarg);
633 if (match_ds_g[match_ds_num_g] == NULL) {
634 fprintf(stderr, "cn_strdup failed: %s\n", strerror(errno));
641 nan_is_error_g = true;
648 if ((socket_file_g == NULL) || (value_string_g == NULL) ||
649 ((hostname_g == NULL) && (strcasecmp(value_string_g, "LIST")))) {
650 fprintf(stderr, "Missing required arguments.\n");
654 snprintf(address, sizeof(address), "unix:%s", socket_file_g);
655 address[sizeof(address) - 1] = 0;
658 status = lcc_connect(address, &connection);
660 printf("ERROR: Connecting to daemon at %s failed.\n", socket_file_g);
664 if (0 == strcasecmp(value_string_g, "LIST"))
665 return do_listval(connection);
667 return do_check(connection);