Merge pull request #1821 from rubenk/memset
[collectd.git] / src / collectd-nagios.c
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  **/
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #if !defined(__GNUC__) || !__GNUC__
32 # define __attribute__(x) /**/
33 #endif
34
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>
42
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 */
73
74 #include "libcollectdclient/collectd/client.h"
75
76 #define RET_OKAY     0
77 #define RET_WARNING  1
78 #define RET_CRITICAL 2
79 #define RET_UNKNOWN  3
80
81 #define CON_NONE     0
82 #define CON_AVERAGE  1
83 #define CON_SUM      2
84 #define CON_PERCENTAGE  3
85
86 struct range_s
87 {
88         double min;
89         double max;
90         int    invert;
91 };
92 typedef struct range_s range_t;
93
94 extern char *optarg;
95 extern int optind, opterr, optopt;
96
97 static char *socket_file_g = NULL;
98 static char *value_string_g = NULL;
99 static char *hostname_g = NULL;
100
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;
105
106 static char **match_ds_g = NULL;
107 static size_t match_ds_num_g = 0;
108
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;
117
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 */
124
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;
130
131         size_t i;
132
133         if (match_ds_g == NULL)
134                 return (RET_OKAY);
135
136         new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
137         if (new_values == NULL)
138         {
139                 fprintf (stderr, "calloc failed: %s\n", strerror (errno));
140                 return (RET_UNKNOWN);
141         }
142
143         new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
144         if (new_names == NULL)
145         {
146                 fprintf (stderr, "calloc failed: %s\n", strerror (errno));
147                 free (new_values);
148                 return (RET_UNKNOWN);
149         }
150
151         for (i = 0; i < match_ds_num_g; i++)
152         {
153                 size_t j;
154
155                 /* match_ds_g keeps pointers into argv but the names will be freed */
156                 new_names[i] = cn_strdup (match_ds_g[i]);
157                 if (new_names[i] == NULL)
158                 {
159                         fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
160                         free (new_values);
161                         for (j = 0; j < i; j++)
162                                 free (new_names[j]);
163                         free (new_names);
164                         return (RET_UNKNOWN);
165                 }
166
167                 for (j = 0; j < *values_num; j++)
168                         if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
169                                 break;
170
171                 if (j == *values_num)
172                 {
173                         printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
174                         free (new_values);
175                         for (j = 0; j <= i; j++)
176                                 free (new_names[j]);
177                         free (new_names);
178                         return (RET_CRITICAL);
179                 }
180
181                 new_values[i] = (*values)[j];
182         }
183
184         free (*values);
185         for (i = 0; i < *values_num; i++)
186                 free ((*values_names)[i]);
187         free (*values_names);
188
189         *values       = new_values;
190         *values_names = new_names;
191         *values_num   = match_ds_num_g;
192         return (RET_OKAY);
193 } /* int filter_ds */
194
195 static void parse_range (char *string, range_t *range)
196 {
197         char *min_ptr;
198         char *max_ptr;
199
200         if (*string == '@')
201         {
202                 range->invert = 1;
203                 string++;
204         }
205
206         max_ptr = strchr (string, ':');
207         if (max_ptr == NULL)
208         {
209                 min_ptr = NULL;
210                 max_ptr = string;
211         }
212         else
213         {
214                 min_ptr = string;
215                 *max_ptr = '\0';
216                 max_ptr++;
217         }
218
219         assert (max_ptr != NULL);
220
221         /* `10' == `0:10' */
222         if (min_ptr == NULL)
223                 range->min = 0.0;
224         /* :10 == ~:10 == -inf:10 */
225         else if ((*min_ptr == '\0') || (*min_ptr == '~'))
226                 range->min = NAN;
227         else
228                 range->min = atof (min_ptr);
229
230         if ((*max_ptr == '\0') || (*max_ptr == '~'))
231                 range->max = NAN;
232         else
233                 range->max = atof (max_ptr);
234 } /* void parse_range */
235
236 static int match_range (range_t *range, double value)
237 {
238         int ret = 0;
239
240         if (!isnan (range->min) && (range->min > value))
241                 ret = 1;
242         if (!isnan (range->max) && (range->max < value))
243                 ret = 1;
244
245         return (((ret - range->invert) == 0) ? 0 : 1);
246 } /* int match_range */
247
248 __attribute__((noreturn))
249 static void usage (const char *name)
250 {
251         fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
252                         "\n"
253                         "Valid options are:\n"
254                         "  -s <socket>    Path to collectd's UNIX-socket.\n"
255                         "  -n <v_spec>    Value specification to get from collectd.\n"
256                         "                 Format: `plugin-instance/type-instance'\n"
257                         "  -d <ds>        Select the DS to examine. May be repeated to examine multiple\n"
258                         "                 DSes. By default all DSes are used.\n"
259                         "  -g <consol>    Method to use to consolidate several DSes.\n"
260                         "                 See below for a list of valid arguments.\n"
261                         "  -H <host>      Hostname to query the values for.\n"
262                         "  -c <range>     Critical range\n"
263                         "  -w <range>     Warning range\n"
264                         "  -m             Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
265                         "\n"
266                         "Consolidation functions:\n"
267                         "  none:          Apply the warning- and critical-ranges to each data-source\n"
268                         "                 individually.\n"
269                         "  average:       Calculate the average of all matching DSes and apply the\n"
270                         "                 warning- and critical-ranges to the calculated average.\n"
271                         "  sum:           Apply the ranges to the sum of all DSes.\n"
272                         "  percentage:    Apply the ranges to the ratio (in percent) of the first value\n"
273                         "                 and the sum of all values."
274                         "\n", name);
275         exit (1);
276 } /* void usage */
277
278 static int do_listval (lcc_connection_t *connection)
279 {
280         lcc_identifier_t *ret_ident = NULL;
281         size_t ret_ident_num = 0;
282
283         char *hostname = NULL;
284
285         int status;
286         size_t i;
287
288         status = lcc_listval (connection, &ret_ident, &ret_ident_num);
289         if (status != 0) {
290                 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
291                 if (ret_ident != NULL)
292                         free (ret_ident);
293                 return (RET_UNKNOWN);
294         }
295
296         status = lcc_sort_identifiers (connection, ret_ident, ret_ident_num);
297         if (status != 0) {
298                 printf ("UNKNOWN: %s\n", lcc_strerror (connection));
299                 if (ret_ident != NULL)
300                         free (ret_ident);
301                 return (RET_UNKNOWN);
302         }
303
304         for (i = 0; i < ret_ident_num; ++i) {
305                 char id[1024];
306
307                 if ((hostname_g != NULL) && (strcasecmp (hostname_g, ret_ident[i].host)))
308                         continue;
309
310                 if ((hostname == NULL) || strcasecmp (hostname, ret_ident[i].host))
311                 {
312                         free (hostname);
313                         hostname = strdup (ret_ident[i].host);
314                         printf ("Host: %s\n", hostname);
315                 }
316
317                 /* empty hostname; not to be printed again */
318                 ret_ident[i].host[0] = '\0';
319
320                 status = lcc_identifier_to_string (connection,
321                                 id, sizeof (id), ret_ident + i);
322                 if (status != 0) {
323                         printf ("ERROR: listval: Failed to convert returned "
324                                         "identifier to a string: %s\n",
325                                         lcc_strerror (connection));
326                         free (hostname);
327                         hostname = NULL;
328                         continue;
329                 }
330
331                 /* skip over the (empty) hostname and following '/' */
332                 printf ("\t%s\n", id + 1);
333         }
334
335         free (ret_ident);
336         free (hostname);
337         return (RET_OKAY);
338 } /* int do_listval */
339
340 static int do_check_con_none (size_t values_num,
341                 double *values, char **values_names)
342 {
343         int num_critical = 0;
344         int num_warning  = 0;
345         int num_okay = 0;
346         const char *status_str = "UNKNOWN";
347         int status_code = RET_UNKNOWN;
348         size_t i;
349
350         for (i = 0; i < values_num; i++)
351         {
352                 if (isnan (values[i]))
353                 {
354                         if (nan_is_error_g)
355                                 num_critical++;
356                         else
357                                 num_warning++;
358                 }
359                 else if (match_range (&range_critical_g, values[i]) != 0)
360                         num_critical++;
361                 else if (match_range (&range_warning_g, values[i]) != 0)
362                         num_warning++;
363                 else
364                         num_okay++;
365         }
366
367         if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
368         {
369                 printf ("WARNING: No defined values found\n");
370                 return (RET_WARNING);
371         }
372         else if ((num_critical == 0) && (num_warning == 0))
373         {
374                 status_str = "OKAY";
375                 status_code = RET_OKAY;
376         }
377         else if (num_critical == 0)
378         {
379                 status_str = "WARNING";
380                 status_code = RET_WARNING;
381         }
382         else
383         {
384                 status_str = "CRITICAL";
385                 status_code = RET_CRITICAL;
386         }
387
388         printf ("%s: %i critical, %i warning, %i okay", status_str,
389                         num_critical, num_warning, num_okay);
390         if (values_num > 0)
391         {
392                 printf (" |");
393                 for (i = 0; i < values_num; i++)
394                         printf (" %s=%f;;;;", values_names[i], values[i]);
395         }
396         printf ("\n");
397
398         return (status_code);
399 } /* int do_check_con_none */
400
401 static int do_check_con_average (size_t values_num,
402                 double *values, char **values_names)
403 {
404         size_t i;
405         double total;
406         int total_num;
407         double average;
408         const char *status_str = "UNKNOWN";
409         int status_code = RET_UNKNOWN;
410
411         total = 0.0;
412         total_num = 0;
413         for (i = 0; i < values_num; i++)
414         {
415                 if (isnan (values[i]))
416                 {
417                         if (!nan_is_error_g)
418                                 continue;
419
420                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
421                                         values_names[i]);
422                         return (RET_CRITICAL);
423                 }
424
425                 total += values[i];
426                 total_num++;
427         }
428
429         if (total_num == 0)
430         {
431                 printf ("WARNING: No defined values found\n");
432                 return (RET_WARNING);
433         }
434
435         average = total / total_num;
436
437         if (match_range (&range_critical_g, average) != 0)
438         {
439                 status_str = "CRITICAL";
440                 status_code = RET_CRITICAL;
441         }
442         else if (match_range (&range_warning_g, average) != 0)
443         {
444                 status_str = "WARNING";
445                 status_code = RET_WARNING;
446         }
447         else
448         {
449                 status_str = "OKAY";
450                 status_code = RET_OKAY;
451         }
452
453         printf ("%s: %g average |", status_str, average);
454         for (i = 0; i < values_num; i++)
455                 printf (" %s=%f;;;;", values_names[i], values[i]);
456         printf ("\n");
457
458         return (status_code);
459 } /* int do_check_con_average */
460
461 static int do_check_con_sum (size_t values_num,
462                 double *values, char **values_names)
463 {
464         size_t i;
465         double total;
466         int total_num;
467         const char *status_str = "UNKNOWN";
468         int status_code = RET_UNKNOWN;
469
470         total = 0.0;
471         total_num = 0;
472         for (i = 0; i < values_num; i++)
473         {
474                 if (isnan (values[i]))
475                 {
476                         if (!nan_is_error_g)
477                                 continue;
478
479                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
480                                         values_names[i]);
481                         return (RET_CRITICAL);
482                 }
483
484                 total += values[i];
485                 total_num++;
486         }
487
488         if (total_num == 0)
489         {
490                 printf ("WARNING: No defined values found\n");
491                 return (RET_WARNING);
492         }
493
494         if (match_range (&range_critical_g, total) != 0)
495         {
496                 status_str = "CRITICAL";
497                 status_code = RET_CRITICAL;
498         }
499         else if (match_range (&range_warning_g, total) != 0)
500         {
501                 status_str = "WARNING";
502                 status_code = RET_WARNING;
503         }
504         else
505         {
506                 status_str = "OKAY";
507                 status_code = RET_OKAY;
508         }
509
510         printf ("%s: %g sum |", status_str, total);
511         for (i = 0; i < values_num; i++)
512                 printf (" %s=%f;;;;", values_names[i], values[i]);
513         printf ("\n");
514
515         return (status_code);
516 } /* int do_check_con_sum */
517
518 static int do_check_con_percentage (size_t values_num,
519                 double *values, char **values_names)
520 {
521         size_t i;
522         double sum = 0.0;
523         double percentage;
524
525         const char *status_str  = "UNKNOWN";
526         int         status_code = RET_UNKNOWN;
527
528         if ((values_num < 1) || (isnan (values[0])))
529         {
530                 printf ("WARNING: The first value is not defined\n");
531                 return (RET_WARNING);
532         }
533
534         for (i = 0; i < values_num; i++)
535         {
536                 if (isnan (values[i]))
537                 {
538                         if (!nan_is_error_g)
539                                 continue;
540
541                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
542                                         values_names[i]);
543                         return (RET_CRITICAL);
544                 }
545
546                 sum += values[i];
547         }
548
549         if (sum == 0.0)
550         {
551                 printf ("WARNING: Values sum up to zero\n");
552                 return (RET_WARNING);
553         }
554
555         percentage = 100.0 * values[0] / sum;
556
557         if (match_range (&range_critical_g, percentage) != 0)
558         {
559                 status_str  = "CRITICAL";
560                 status_code = RET_CRITICAL;
561         }
562         else if (match_range (&range_warning_g, percentage) != 0)
563         {
564                 status_str  = "WARNING";
565                 status_code = RET_WARNING;
566         }
567         else
568         {
569                 status_str  = "OKAY";
570                 status_code = RET_OKAY;
571         }
572
573         printf ("%s: %lf percent |", status_str, percentage);
574         for (i = 0; i < values_num; i++)
575                 printf (" %s=%lf;;;;", values_names[i], values[i]);
576         return (status_code);
577 } /* int do_check_con_percentage */
578
579 static int do_check (lcc_connection_t *connection)
580 {
581         gauge_t *values;
582         char   **values_names;
583         size_t   values_num;
584         char ident_str[1024];
585         lcc_identifier_t ident;
586         size_t i;
587         int status;
588
589         snprintf (ident_str, sizeof (ident_str), "%s/%s",
590                         hostname_g, value_string_g);
591         ident_str[sizeof (ident_str) - 1] = 0;
592
593         status = lcc_string_to_identifier (connection, &ident, ident_str);
594         if (status != 0)
595         {
596                 printf ("ERROR: Creating an identifier failed: %s.\n",
597                                 lcc_strerror (connection));
598                 LCC_DESTROY (connection);
599                 return (RET_CRITICAL);
600         }
601
602         status = lcc_getval (connection, &ident,
603                         &values_num, &values, &values_names);
604         if (status != 0)
605         {
606                 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
607                                 lcc_strerror (connection));
608                 LCC_DESTROY (connection);
609                 return (RET_CRITICAL);
610         }
611
612         LCC_DESTROY (connection);
613
614         status = filter_ds (&values_num, &values, &values_names);
615         if (status != RET_OKAY)
616                 return (status);
617
618         status = RET_UNKNOWN;
619         if (consolitation_g == CON_NONE)
620                 status = do_check_con_none (values_num, values, values_names);
621         else if (consolitation_g == CON_AVERAGE)
622                 status = do_check_con_average (values_num, values, values_names);
623         else if (consolitation_g == CON_SUM)
624                 status = do_check_con_sum (values_num, values, values_names);
625         else if (consolitation_g == CON_PERCENTAGE)
626                 status = do_check_con_percentage (values_num, values, values_names);
627
628         free (values);
629         if (values_names != NULL)
630                 for (i = 0; i < values_num; i++)
631                         free (values_names[i]);
632         free (values_names);
633
634         return (status);
635 } /* int do_check */
636
637 int main (int argc, char **argv)
638 {
639         char address[1024];
640         lcc_connection_t *connection;
641
642         int status;
643
644         range_critical_g.min = NAN;
645         range_critical_g.max = NAN;
646         range_critical_g.invert = 0;
647
648         range_warning_g.min = NAN;
649         range_warning_g.max = NAN;
650         range_warning_g.invert = 0;
651
652         while (42)
653         {
654                 int c;
655
656                 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
657                 if (c < 0)
658                         break;
659
660                 switch (c)
661                 {
662                         case 'c':
663                                 parse_range (optarg, &range_critical_g);
664                                 break;
665                         case 'w':
666                                 parse_range (optarg, &range_warning_g);
667                                 break;
668                         case 's':
669                                 socket_file_g = optarg;
670                                 break;
671                         case 'n':
672                                 value_string_g = optarg;
673                                 break;
674                         case 'H':
675                                 hostname_g = optarg;
676                                 break;
677                         case 'g':
678                                 if (strcasecmp (optarg, "none") == 0)
679                                         consolitation_g = CON_NONE;
680                                 else if (strcasecmp (optarg, "average") == 0)
681                                         consolitation_g = CON_AVERAGE;
682                                 else if (strcasecmp (optarg, "sum") == 0)
683                                         consolitation_g = CON_SUM;
684                                 else if (strcasecmp (optarg, "percentage") == 0)
685                                         consolitation_g = CON_PERCENTAGE;
686                                 else
687                                 {
688                                         fprintf (stderr, "Unknown consolidation function `%s'.\n",
689                                                         optarg);
690                                         usage (argv[0]);
691                                 }
692                                 break;
693                         case 'd':
694                         {
695                                 char **tmp;
696                                 tmp = realloc (match_ds_g,
697                                                 (match_ds_num_g + 1)
698                                                 * sizeof (char *));
699                                 if (tmp == NULL)
700                                 {
701                                         fprintf (stderr, "realloc failed: %s\n",
702                                                         strerror (errno));
703                                         return (RET_UNKNOWN);
704                                 }
705                                 match_ds_g = tmp;
706                                 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
707                                 if (match_ds_g[match_ds_num_g] == NULL)
708                                 {
709                                         fprintf (stderr, "cn_strdup failed: %s\n",
710                                                         strerror (errno));
711                                         return (RET_UNKNOWN);
712                                 }
713                                 match_ds_num_g++;
714                                 break;
715                         }
716                         case 'm':
717                                 nan_is_error_g = 1;
718                                 break;
719                         default:
720                                 usage (argv[0]);
721                 } /* switch (c) */
722         }
723
724         if ((socket_file_g == NULL) || (value_string_g == NULL)
725                         || ((hostname_g == NULL) && (strcasecmp (value_string_g, "LIST"))))
726         {
727                 fprintf (stderr, "Missing required arguments.\n");
728                 usage (argv[0]);
729         }
730
731         snprintf (address, sizeof (address), "unix:%s", socket_file_g);
732         address[sizeof (address) - 1] = 0;
733
734         connection = NULL;
735         status = lcc_connect (address, &connection);
736         if (status != 0)
737         {
738                 printf ("ERROR: Connecting to daemon at %s failed.\n",
739                                 socket_file_g);
740                 return (RET_CRITICAL);
741         }
742
743         if (0 == strcasecmp (value_string_g, "LIST"))
744                 return (do_listval (connection));
745
746         return (do_check (connection));
747 } /* int main */