collectd-nagios: Let '-n LIST' list all available datasets.
[collectd.git] / src / collectd-nagios.c
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  **/
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #if !defined(__GNUC__) || !__GNUC__
27 # define __attribute__(x) /**/
28 #endif
29
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>
37
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 */
68
69 #include "libcollectdclient/client.h"
70
71 #define RET_OKAY     0
72 #define RET_WARNING  1
73 #define RET_CRITICAL 2
74 #define RET_UNKNOWN  3
75
76 #define CON_NONE     0
77 #define CON_AVERAGE  1
78 #define CON_SUM      2
79 #define CON_PERCENTAGE  3
80
81 struct range_s
82 {
83         double min;
84         double max;
85         int    invert;
86 };
87 typedef struct range_s range_t;
88
89 extern char *optarg;
90 extern int optind, opterr, optopt;
91
92 static char *socket_file_g = NULL;
93 static char *value_string_g = NULL;
94 static char *hostname_g = NULL;
95
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;
100
101 static char **match_ds_g = NULL;
102 static int    match_ds_num_g = 0;
103
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;
112
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 */
119
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;
125
126         size_t i;
127
128         if (match_ds_g == NULL)
129                 return (RET_OKAY);
130
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         }
137
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         }
145
146         for (i = 0; i < (size_t) match_ds_num_g; i++)
147         {
148                 size_t j;
149
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                 }
161
162                 for (j = 0; j < *values_num; j++)
163                         if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
164                                 break;
165
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                 }
175
176                 new_values[i] = (*values)[j];
177         }
178
179         free (*values);
180         for (i = 0; i < *values_num; i++)
181                 free ((*values_names)[i]);
182         free (*values_names);
183
184         *values       = new_values;
185         *values_names = new_names;
186         *values_num   = match_ds_num_g;
187         return (RET_OKAY);
188 } /* int filter_ds */
189
190 static void parse_range (char *string, range_t *range)
191 {
192         char *min_ptr;
193         char *max_ptr;
194
195         if (*string == '@')
196         {
197                 range->invert = 1;
198                 string++;
199         }
200
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         }
213
214         assert (max_ptr != NULL);
215
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);
224
225         if ((*max_ptr == '\0') || (*max_ptr == '~'))
226                 range->max = NAN;
227         else
228                 range->max = atof (max_ptr);
229 } /* void parse_range */
230
231 static int match_range (range_t *range, double value)
232 {
233         int ret = 0;
234
235         if (!isnan (range->min) && (range->min > value))
236                 ret = 1;
237         if (!isnan (range->max) && (range->max < value))
238                 ret = 1;
239
240         return (((ret - range->invert) == 0) ? 0 : 1);
241 } /* int match_range */
242
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 */
271
272 static int do_listval (lcc_connection_t *connection)
273 {
274         lcc_identifier_t *ret_ident = NULL;
275         size_t ret_ident_num = 0;
276
277         int status;
278         size_t i;
279
280         status = lcc_listval (connection, &ret_ident, &ret_ident_num);
281         if (status != 0) {
282                 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
283                 if (ret_ident != NULL)
284                         free (ret_ident);
285                 return (RET_UNKNOWN);
286         }
287
288         for (i = 0; i < ret_ident_num; ++i) {
289                 char id[1024];
290
291                 status = lcc_identifier_to_string (connection,
292                                 id, sizeof (id), ret_ident + i);
293                 if (status != 0) {
294                         printf ("ERROR: listval: Failed to convert returned "
295                                         "identifier to a string: %s\n",
296                                         lcc_strerror (connection));
297                         continue;
298                 }
299
300                 printf ("%s\n", id);
301         }
302
303         if (ret_ident != NULL)
304                 free (ret_ident);
305         return (RET_OKAY);
306 } /* int do_listval */
307
308 static int do_check_con_none (size_t values_num,
309                 double *values, char **values_names)
310 {
311         int num_critical = 0;
312         int num_warning  = 0;
313         int num_okay = 0;
314         const char *status_str = "UNKNOWN";
315         int status_code = RET_UNKNOWN;
316         size_t i;
317
318         for (i = 0; i < values_num; i++)
319         {
320                 if (isnan (values[i]))
321                 {
322                         if (nan_is_error_g)
323                                 num_critical++;
324                         else
325                                 num_warning++;
326                 }
327                 else if (match_range (&range_critical_g, values[i]) != 0)
328                         num_critical++;
329                 else if (match_range (&range_warning_g, values[i]) != 0)
330                         num_warning++;
331                 else
332                         num_okay++;
333         }
334
335         if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
336         {
337                 printf ("WARNING: No defined values found\n");
338                 return (RET_WARNING);
339         }
340         else if ((num_critical == 0) && (num_warning == 0))
341         {
342                 status_str = "OKAY";
343                 status_code = RET_OKAY;
344         }
345         else if (num_critical == 0)
346         {
347                 status_str = "WARNING";
348                 status_code = RET_WARNING;
349         }
350         else
351         {
352                 status_str = "CRITICAL";
353                 status_code = RET_CRITICAL;
354         }
355
356         printf ("%s: %i critical, %i warning, %i okay", status_str,
357                         num_critical, num_warning, num_okay);
358         if (values_num > 0)
359         {
360                 printf (" |");
361                 for (i = 0; i < values_num; i++)
362                         printf (" %s=%f;;;;", values_names[i], values[i]);
363         }
364         printf ("\n");
365
366         return (status_code);
367 } /* int do_check_con_none */
368
369 static int do_check_con_average (size_t values_num,
370                 double *values, char **values_names)
371 {
372         size_t i;
373         double total;
374         int total_num;
375         double average;
376         const char *status_str = "UNKNOWN";
377         int status_code = RET_UNKNOWN;
378
379         total = 0.0;
380         total_num = 0;
381         for (i = 0; i < values_num; i++)
382         {
383                 if (isnan (values[i]))
384                 {
385                         if (!nan_is_error_g)
386                                 continue;
387
388                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
389                                         values_names[i]);
390                         return (RET_CRITICAL);
391                 }
392
393                 total += values[i];
394                 total_num++;
395         }
396
397         if (total_num == 0)
398         {
399                 printf ("WARNING: No defined values found\n");
400                 return (RET_WARNING);
401         }
402
403         average = total / total_num;
404
405         if (match_range (&range_critical_g, average) != 0)
406         {
407                 status_str = "CRITICAL";
408                 status_code = RET_CRITICAL;
409         }
410         else if (match_range (&range_warning_g, average) != 0)
411         {
412                 status_str = "WARNING";
413                 status_code = RET_WARNING;
414         }
415         else
416         {
417                 status_str = "OKAY";
418                 status_code = RET_OKAY;
419         }
420
421         printf ("%s: %g average |", status_str, average);
422         for (i = 0; i < values_num; i++)
423                 printf (" %s=%f;;;;", values_names[i], values[i]);
424         printf ("\n");
425
426         return (status_code);
427 } /* int do_check_con_average */
428
429 static int do_check_con_sum (size_t values_num,
430                 double *values, char **values_names)
431 {
432         size_t i;
433         double total;
434         int total_num;
435         const char *status_str = "UNKNOWN";
436         int status_code = RET_UNKNOWN;
437
438         total = 0.0;
439         total_num = 0;
440         for (i = 0; i < values_num; i++)
441         {
442                 if (isnan (values[i]))
443                 {
444                         if (!nan_is_error_g)
445                                 continue;
446
447                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
448                                         values_names[i]);
449                         return (RET_CRITICAL);
450                 }
451
452                 total += values[i];
453                 total_num++;
454         }
455
456         if (total_num == 0)
457         {
458                 printf ("WARNING: No defined values found\n");
459                 return (RET_WARNING);
460         }
461
462         if (match_range (&range_critical_g, total) != 0)
463         {
464                 status_str = "CRITICAL";
465                 status_code = RET_CRITICAL;
466         }
467         else if (match_range (&range_warning_g, total) != 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         }
477
478         printf ("%s: %g sum |", status_str, total);
479         for (i = 0; i < values_num; i++)
480                 printf (" %s=%f;;;;", values_names[i], values[i]);
481         printf ("\n");
482
483         return (status_code);
484 } /* int do_check_con_sum */
485
486 static int do_check_con_percentage (size_t values_num,
487                 double *values, char **values_names)
488 {
489         size_t i;
490         double sum = 0.0;
491         double percentage;
492
493         const char *status_str  = "UNKNOWN";
494         int         status_code = RET_UNKNOWN;
495
496         if ((values_num < 1) || (isnan (values[0])))
497         {
498                 printf ("WARNING: The first value is not defined\n");
499                 return (RET_WARNING);
500         }
501
502         for (i = 0; i < values_num; i++)
503         {
504                 if (isnan (values[i]))
505                 {
506                         if (!nan_is_error_g)
507                                 continue;
508
509                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
510                                         values_names[i]);
511                         return (RET_CRITICAL);
512                 }
513
514                 sum += values[i];
515         }
516
517         if (sum == 0.0)
518         {
519                 printf ("WARNING: Values sum up to zero\n");
520                 return (RET_WARNING);
521         }
522
523         percentage = 100.0 * values[0] / sum;
524
525         if (match_range (&range_critical_g, percentage) != 0)
526         {
527                 status_str  = "CRITICAL";
528                 status_code = RET_CRITICAL;
529         }
530         else if (match_range (&range_warning_g, percentage) != 0)
531         {
532                 status_str  = "WARNING";
533                 status_code = RET_WARNING;
534         }
535         else
536         {
537                 status_str  = "OKAY";
538                 status_code = RET_OKAY;
539         }
540
541         printf ("%s: %lf percent |", status_str, percentage);
542         for (i = 0; i < values_num; i++)
543                 printf (" %s=%lf;;;;", values_names[i], values[i]);
544         return (status_code);
545 } /* int do_check_con_percentage */
546
547 static int do_check (lcc_connection_t *connection)
548 {
549         gauge_t *values;
550         char   **values_names;
551         size_t   values_num;
552         char ident_str[1024];
553         lcc_identifier_t ident;
554         size_t i;
555         int status;
556
557         snprintf (ident_str, sizeof (ident_str), "%s/%s",
558                         hostname_g, value_string_g);
559         ident_str[sizeof (ident_str) - 1] = 0;
560
561         memset (&ident, 0, sizeof (ident));
562         status = lcc_string_to_identifier (connection, &ident, ident_str);
563         if (status != 0)
564         {
565                 printf ("ERROR: Creating an identifier failed: %s.\n",
566                                 lcc_strerror (connection));
567                 LCC_DESTROY (connection);
568                 return (RET_CRITICAL);
569         }
570
571         status = lcc_getval (connection, &ident,
572                         &values_num, &values, &values_names);
573         if (status != 0)
574         {
575                 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
576                                 lcc_strerror (connection));
577                 LCC_DESTROY (connection);
578                 return (RET_CRITICAL);
579         }
580
581         LCC_DESTROY (connection);
582
583         status = filter_ds (&values_num, &values, &values_names);
584         if (status != RET_OKAY)
585                 return (status);
586
587         status = RET_UNKNOWN;
588         if (consolitation_g == CON_NONE)
589                 status =  do_check_con_none (values_num, values, values_names);
590         else if (consolitation_g == CON_AVERAGE)
591                 status =  do_check_con_average (values_num, values, values_names);
592         else if (consolitation_g == CON_SUM)
593                 status = do_check_con_sum (values_num, values, values_names);
594         else if (consolitation_g == CON_PERCENTAGE)
595                 status = do_check_con_percentage (values_num, values, values_names);
596
597         free (values);
598         if (values_names != NULL)
599                 for (i = 0; i < values_num; i++)
600                         free (values_names[i]);
601         free (values_names);
602
603         return (status);
604 } /* int do_check */
605
606 int main (int argc, char **argv)
607 {
608         char address[1024];
609         lcc_connection_t *connection;
610
611         int status;
612
613         range_critical_g.min = NAN;
614         range_critical_g.max = NAN;
615         range_critical_g.invert = 0;
616
617         range_warning_g.min = NAN;
618         range_warning_g.max = NAN;
619         range_warning_g.invert = 0;
620
621         while (42)
622         {
623                 int c;
624
625                 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
626                 if (c < 0)
627                         break;
628
629                 switch (c)
630                 {
631                         case 'c':
632                                 parse_range (optarg, &range_critical_g);
633                                 break;
634                         case 'w':
635                                 parse_range (optarg, &range_warning_g);
636                                 break;
637                         case 's':
638                                 socket_file_g = optarg;
639                                 break;
640                         case 'n':
641                                 value_string_g = optarg;
642                                 break;
643                         case 'H':
644                                 hostname_g = optarg;
645                                 break;
646                         case 'g':
647                                 if (strcasecmp (optarg, "none") == 0)
648                                         consolitation_g = CON_NONE;
649                                 else if (strcasecmp (optarg, "average") == 0)
650                                         consolitation_g = CON_AVERAGE;
651                                 else if (strcasecmp (optarg, "sum") == 0)
652                                         consolitation_g = CON_SUM;
653                                 else if (strcasecmp (optarg, "percentage") == 0)
654                                         consolitation_g = CON_PERCENTAGE;
655                                 else
656                                 {
657                                         fprintf (stderr, "Unknown consolidation function `%s'.\n",
658                                                         optarg);
659                                         usage (argv[0]);
660                                 }
661                                 break;
662                         case 'd':
663                         {
664                                 char **tmp;
665                                 tmp = (char **) realloc (match_ds_g,
666                                                 (match_ds_num_g + 1)
667                                                 * sizeof (char *));
668                                 if (tmp == NULL)
669                                 {
670                                         fprintf (stderr, "realloc failed: %s\n",
671                                                         strerror (errno));
672                                         return (RET_UNKNOWN);
673                                 }
674                                 match_ds_g = tmp;
675                                 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
676                                 if (match_ds_g[match_ds_num_g] == NULL)
677                                 {
678                                         fprintf (stderr, "cn_strdup failed: %s\n",
679                                                         strerror (errno));
680                                         return (RET_UNKNOWN);
681                                 }
682                                 match_ds_num_g++;
683                                 break;
684                         }
685                         case 'm':
686                                 nan_is_error_g = 1;
687                                 break;
688                         default:
689                                 usage (argv[0]);
690                 } /* switch (c) */
691         }
692
693         if ((socket_file_g == NULL) || (value_string_g == NULL)
694                         || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST"))))
695         {
696                 fprintf (stderr, "Missing required arguments.\n");
697                 usage (argv[0]);
698         }
699
700         snprintf (address, sizeof (address), "unix:%s", socket_file_g);
701         address[sizeof (address) - 1] = 0;
702
703         connection = NULL;
704         status = lcc_connect (address, &connection);
705         if (status != 0)
706         {
707                 printf ("ERROR: Connecting to daemon at %s failed.\n",
708                                 socket_file_g);
709                 return (RET_CRITICAL);
710         }
711
712         if (0 == strcasecmp (value_string_g, "LIST"))
713                 return (do_listval (connection));
714
715         return (do_check (connection));
716 } /* int main */