Merge branch 'master' into ff/netlib
[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 #ifndef _ISOC99_SOURCE
27 # define _ISOC99_SOURCE
28 #endif
29
30 #ifndef _POSIX_C_SOURCE
31 # define _POSIX_C_SOURCE 200112L
32 #endif
33
34 #ifndef _XOPEN_SOURCE
35 # define _XOPEN_SOURCE 600
36 #endif
37
38 #if !defined(__GNUC__) || !__GNUC__
39 # define __attribute__(x) /**/
40 #endif
41
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <errno.h>
46 #include <string.h>
47 #include <strings.h>
48 #include <assert.h>
49 #include <math.h>
50
51 #include "libcollectdclient/collectd/client.h"
52
53 #define RET_OKAY     0
54 #define RET_WARNING  1
55 #define RET_CRITICAL 2
56 #define RET_UNKNOWN  3
57
58 #define CON_NONE     0
59 #define CON_AVERAGE  1
60 #define CON_SUM      2
61 #define CON_PERCENTAGE  3
62
63 struct range_s
64 {
65         double min;
66         double max;
67         int    invert;
68 };
69 typedef struct range_s range_t;
70
71 extern char *optarg;
72 extern int optind, opterr, optopt;
73
74 static char *socket_file_g = NULL;
75 static char *value_string_g = NULL;
76 static char *hostname_g = NULL;
77
78 static range_t range_critical_g;
79 static range_t range_warning_g;
80 static int consolitation_g = CON_NONE;
81 static _Bool nan_is_error_g = 0;
82
83 static char **match_ds_g = NULL;
84 static int    match_ds_num_g = 0;
85
86 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
87  * that, so here's an own implementation.. It's easy enough. The GCC attributes
88  * are supposed to get good performance..  -octo */
89 __attribute__((malloc, nonnull (1)))
90 static char *cn_strdup (const char *str) /* {{{ */
91 {
92   size_t strsize;
93   char *ret;
94
95   strsize = strlen (str) + 1;
96   ret = (char *) malloc (strsize);
97   if (ret != NULL)
98     memcpy (ret, str, strsize);
99   return (ret);
100 } /* }}} char *cn_strdup */
101
102 static int filter_ds (size_t *values_num,
103                 double **values, char ***values_names)
104 {
105         gauge_t *new_values;
106         char   **new_names;
107
108         size_t i;
109
110         if (match_ds_g == NULL)
111                 return (RET_OKAY);
112
113         new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values));
114         if (new_values == NULL)
115         {
116                 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
117                 return (RET_UNKNOWN);
118         }
119
120         new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names));
121         if (new_names == NULL)
122         {
123                 fprintf (stderr, "malloc failed: %s\n", strerror (errno));
124                 free (new_values);
125                 return (RET_UNKNOWN);
126         }
127
128         for (i = 0; i < (size_t) match_ds_num_g; i++)
129         {
130                 size_t j;
131
132                 /* match_ds_g keeps pointers into argv but the names will be freed */
133                 new_names[i] = cn_strdup (match_ds_g[i]);
134                 if (new_names[i] == NULL)
135                 {
136                         fprintf (stderr, "cn_strdup failed: %s\n", strerror (errno));
137                         free (new_values);
138                         for (j = 0; j < i; j++)
139                                 free (new_names[j]);
140                         free (new_names);
141                         return (RET_UNKNOWN);
142                 }
143
144                 for (j = 0; j < *values_num; j++)
145                         if (strcasecmp (new_names[i], (*values_names)[j]) == 0)
146                                 break;
147
148                 if (j == *values_num)
149                 {
150                         printf ("ERROR: DS `%s' is not available.\n", new_names[i]);
151                         free (new_values);
152                         for (j = 0; j <= i; j++)
153                                 free (new_names[j]);
154                         free (new_names);
155                         return (RET_CRITICAL);
156                 }
157
158                 new_values[i] = (*values)[j];
159         }
160
161         free (*values);
162         for (i = 0; i < *values_num; i++)
163                 free ((*values_names)[i]);
164         free (*values_names);
165
166         *values       = new_values;
167         *values_names = new_names;
168         *values_num   = match_ds_num_g;
169         return (RET_OKAY);
170 } /* int filter_ds */
171
172 static void parse_range (char *string, range_t *range)
173 {
174         char *min_ptr;
175         char *max_ptr;
176
177         if (*string == '@')
178         {
179                 range->invert = 1;
180                 string++;
181         }
182
183         max_ptr = strchr (string, ':');
184         if (max_ptr == NULL)
185         {
186                 min_ptr = NULL;
187                 max_ptr = string;
188         }
189         else
190         {
191                 min_ptr = string;
192                 *max_ptr = '\0';
193                 max_ptr++;
194         }
195
196         assert (max_ptr != NULL);
197
198         /* `10' == `0:10' */
199         if (min_ptr == NULL)
200                 range->min = 0.0;
201         /* :10 == ~:10 == -inf:10 */
202         else if ((*min_ptr == '\0') || (*min_ptr == '~'))
203                 range->min = NAN;
204         else
205                 range->min = atof (min_ptr);
206
207         if ((*max_ptr == '\0') || (*max_ptr == '~'))
208                 range->max = NAN;
209         else
210                 range->max = atof (max_ptr);
211 } /* void parse_range */
212
213 static int match_range (range_t *range, double value)
214 {
215         int ret = 0;
216
217         if (!isnan (range->min) && (range->min > value))
218                 ret = 1;
219         if (!isnan (range->max) && (range->max < value))
220                 ret = 1;
221
222         return (((ret - range->invert) == 0) ? 0 : 1);
223 } /* int match_range */
224
225 static void usage (const char *name)
226 {
227         fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
228                         "\n"
229                         "Valid options are:\n"
230                         "  -s <socket>    Path to collectd's UNIX-socket.\n"
231                         "  -n <v_spec>    Value specification to get from collectd.\n"
232                         "                 Format: `plugin-instance/type-instance'\n"
233                         "  -d <ds>        Select the DS to examine. May be repeated to examine multiple\n"
234                         "                 DSes. By default all DSes are used.\n"
235                         "  -g <consol>    Method to use to consolidate several DSes.\n"
236                         "                 See below for a list of valid arguments.\n"
237                         "  -H <host>      Hostname to query the values for.\n"
238                         "  -c <range>     Critical range\n"
239                         "  -w <range>     Warning range\n"
240                         "  -m             Treat \"Not a Number\" (NaN) as critical (default: warning)\n"
241                         "\n"
242                         "Consolidation functions:\n"
243                         "  none:          Apply the warning- and critical-ranges to each data-source\n"
244                         "                 individually.\n"
245                         "  average:       Calculate the average of all matching DSes and apply the\n"
246                         "                 warning- and critical-ranges to the calculated average.\n"
247                         "  sum:           Apply the ranges to the sum of all DSes.\n"
248                         "  percentage:    Apply the ranges to the ratio (in percent) of the first value\n"
249                         "                 and the sum of all values."
250                         "\n", name);
251         exit (1);
252 } /* void usage */
253
254 static int do_check_con_none (size_t values_num,
255                 double *values, char **values_names)
256 {
257         int num_critical = 0;
258         int num_warning  = 0;
259         int num_okay = 0;
260         const char *status_str = "UNKNOWN";
261         int status_code = RET_UNKNOWN;
262         size_t i;
263
264         for (i = 0; i < values_num; i++)
265         {
266                 if (isnan (values[i]))
267                 {
268                         if (nan_is_error_g)
269                                 num_critical++;
270                         else
271                                 num_warning++;
272                 }
273                 else if (match_range (&range_critical_g, values[i]) != 0)
274                         num_critical++;
275                 else if (match_range (&range_warning_g, values[i]) != 0)
276                         num_warning++;
277                 else
278                         num_okay++;
279         }
280
281         if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
282         {
283                 printf ("WARNING: No defined values found\n");
284                 return (RET_WARNING);
285         }
286         else if ((num_critical == 0) && (num_warning == 0))
287         {
288                 status_str = "OKAY";
289                 status_code = RET_OKAY;
290         }
291         else if (num_critical == 0)
292         {
293                 status_str = "WARNING";
294                 status_code = RET_WARNING;
295         }
296         else
297         {
298                 status_str = "CRITICAL";
299                 status_code = RET_CRITICAL;
300         }
301
302         printf ("%s: %i critical, %i warning, %i okay", status_str,
303                         num_critical, num_warning, num_okay);
304         if (values_num > 0)
305         {
306                 printf (" |");
307                 for (i = 0; i < values_num; i++)
308                         printf (" %s=%f;;;;", values_names[i], values[i]);
309         }
310         printf ("\n");
311
312         return (status_code);
313 } /* int do_check_con_none */
314
315 static int do_check_con_average (size_t values_num,
316                 double *values, char **values_names)
317 {
318         size_t i;
319         double total;
320         int total_num;
321         double average;
322         const char *status_str = "UNKNOWN";
323         int status_code = RET_UNKNOWN;
324
325         total = 0.0;
326         total_num = 0;
327         for (i = 0; i < values_num; i++)
328         {
329                 if (isnan (values[i]))
330                 {
331                         if (!nan_is_error_g)
332                                 continue;
333
334                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
335                                         values_names[i]);
336                         return (RET_CRITICAL);
337                 }
338
339                 total += values[i];
340                 total_num++;
341         }
342
343         if (total_num == 0)
344         {
345                 printf ("WARNING: No defined values found\n");
346                 return (RET_WARNING);
347         }
348
349         average = total / total_num;
350
351         if (match_range (&range_critical_g, average) != 0)
352         {
353                 status_str = "CRITICAL";
354                 status_code = RET_CRITICAL;
355         }
356         else if (match_range (&range_warning_g, average) != 0)
357         {
358                 status_str = "WARNING";
359                 status_code = RET_WARNING;
360         }
361         else
362         {
363                 status_str = "OKAY";
364                 status_code = RET_OKAY;
365         }
366
367         printf ("%s: %g average |", status_str, average);
368         for (i = 0; i < values_num; i++)
369                 printf (" %s=%f;;;;", values_names[i], values[i]);
370         printf ("\n");
371
372         return (status_code);
373 } /* int do_check_con_average */
374
375 static int do_check_con_sum (size_t values_num,
376                 double *values, char **values_names)
377 {
378         size_t i;
379         double total;
380         int total_num;
381         const char *status_str = "UNKNOWN";
382         int status_code = RET_UNKNOWN;
383
384         total = 0.0;
385         total_num = 0;
386         for (i = 0; i < values_num; i++)
387         {
388                 if (isnan (values[i]))
389                 {
390                         if (!nan_is_error_g)
391                                 continue;
392
393                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
394                                         values_names[i]);
395                         return (RET_CRITICAL);
396                 }
397
398                 total += values[i];
399                 total_num++;
400         }
401
402         if (total_num == 0)
403         {
404                 printf ("WARNING: No defined values found\n");
405                 return (RET_WARNING);
406         }
407
408         if (match_range (&range_critical_g, total) != 0)
409         {
410                 status_str = "CRITICAL";
411                 status_code = RET_CRITICAL;
412         }
413         else if (match_range (&range_warning_g, total) != 0)
414         {
415                 status_str = "WARNING";
416                 status_code = RET_WARNING;
417         }
418         else
419         {
420                 status_str = "OKAY";
421                 status_code = RET_OKAY;
422         }
423
424         printf ("%s: %g sum |", status_str, total);
425         for (i = 0; i < values_num; i++)
426                 printf (" %s=%f;;;;", values_names[i], values[i]);
427         printf ("\n");
428
429         return (status_code);
430 } /* int do_check_con_sum */
431
432 static int do_check_con_percentage (size_t values_num,
433                 double *values, char **values_names)
434 {
435         size_t i;
436         double sum = 0.0;
437         double percentage;
438
439         const char *status_str  = "UNKNOWN";
440         int         status_code = RET_UNKNOWN;
441
442         if ((values_num < 1) || (isnan (values[0])))
443         {
444                 printf ("WARNING: The first value is not defined\n");
445                 return (RET_WARNING);
446         }
447
448         for (i = 0; i < values_num; i++)
449         {
450                 if (isnan (values[i]))
451                 {
452                         if (!nan_is_error_g)
453                                 continue;
454
455                         printf ("CRITICAL: Data source \"%s\" is NaN\n",
456                                         values_names[i]);
457                         return (RET_CRITICAL);
458                 }
459
460                 sum += values[i];
461         }
462
463         if (sum == 0.0)
464         {
465                 printf ("WARNING: Values sum up to zero\n");
466                 return (RET_WARNING);
467         }
468
469         percentage = 100.0 * values[0] / sum;
470
471         if (match_range (&range_critical_g, percentage) != 0)
472         {
473                 status_str  = "CRITICAL";
474                 status_code = RET_CRITICAL;
475         }
476         else if (match_range (&range_warning_g, percentage) != 0)
477         {
478                 status_str  = "WARNING";
479                 status_code = RET_WARNING;
480         }
481         else
482         {
483                 status_str  = "OKAY";
484                 status_code = RET_OKAY;
485         }
486
487         printf ("%s: %lf percent |", status_str, percentage);
488         for (i = 0; i < values_num; i++)
489                 printf (" %s=%lf;;;;", values_names[i], values[i]);
490         return (status_code);
491 } /* int do_check_con_percentage */
492
493 static int do_check (void)
494 {
495         lcc_connection_t *connection;
496         gauge_t *values;
497         char   **values_names;
498         size_t   values_num;
499         char address[1024];
500         char ident_str[1024];
501         lcc_identifier_t ident;
502         size_t i;
503         int status;
504
505         snprintf (address, sizeof (address), "unix:%s", socket_file_g);
506         address[sizeof (address) - 1] = 0;
507
508         snprintf (ident_str, sizeof (ident_str), "%s/%s",
509                         hostname_g, value_string_g);
510         ident_str[sizeof (ident_str) - 1] = 0;
511
512         connection = NULL;
513         status = lcc_connect (address, &connection);
514         if (status != 0)
515         {
516                 printf ("ERROR: Connecting to daemon at %s failed.\n",
517                                 socket_file_g);
518                 return (RET_CRITICAL);
519         }
520
521         memset (&ident, 0, sizeof (ident));
522         status = lcc_string_to_identifier (connection, &ident, ident_str);
523         if (status != 0)
524         {
525                 printf ("ERROR: Creating an identifier failed: %s.\n",
526                                 lcc_strerror (connection));
527                 LCC_DESTROY (connection);
528                 return (RET_CRITICAL);
529         }
530
531         status = lcc_getval (connection, &ident,
532                         &values_num, &values, &values_names);
533         if (status != 0)
534         {
535                 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
536                                 lcc_strerror (connection));
537                 LCC_DESTROY (connection);
538                 return (RET_CRITICAL);
539         }
540
541         LCC_DESTROY (connection);
542
543         status = filter_ds (&values_num, &values, &values_names);
544         if (status != RET_OKAY)
545                 return (status);
546
547         status = RET_UNKNOWN;
548         if (consolitation_g == CON_NONE)
549                 status =  do_check_con_none (values_num, values, values_names);
550         else if (consolitation_g == CON_AVERAGE)
551                 status =  do_check_con_average (values_num, values, values_names);
552         else if (consolitation_g == CON_SUM)
553                 status = do_check_con_sum (values_num, values, values_names);
554         else if (consolitation_g == CON_PERCENTAGE)
555                 status = do_check_con_percentage (values_num, values, values_names);
556
557         free (values);
558         if (values_names != NULL)
559                 for (i = 0; i < values_num; i++)
560                         free (values_names[i]);
561         free (values_names);
562
563         return (status);
564 } /* int do_check */
565
566 int main (int argc, char **argv)
567 {
568         range_critical_g.min = NAN;
569         range_critical_g.max = NAN;
570         range_critical_g.invert = 0;
571
572         range_warning_g.min = NAN;
573         range_warning_g.max = NAN;
574         range_warning_g.invert = 0;
575
576         while (42)
577         {
578                 int c;
579
580                 c = getopt (argc, argv, "w:c:s:n:H:g:d:hm");
581                 if (c < 0)
582                         break;
583
584                 switch (c)
585                 {
586                         case 'c':
587                                 parse_range (optarg, &range_critical_g);
588                                 break;
589                         case 'w':
590                                 parse_range (optarg, &range_warning_g);
591                                 break;
592                         case 's':
593                                 socket_file_g = optarg;
594                                 break;
595                         case 'n':
596                                 value_string_g = optarg;
597                                 break;
598                         case 'H':
599                                 hostname_g = optarg;
600                                 break;
601                         case 'g':
602                                 if (strcasecmp (optarg, "none") == 0)
603                                         consolitation_g = CON_NONE;
604                                 else if (strcasecmp (optarg, "average") == 0)
605                                         consolitation_g = CON_AVERAGE;
606                                 else if (strcasecmp (optarg, "sum") == 0)
607                                         consolitation_g = CON_SUM;
608                                 else if (strcasecmp (optarg, "percentage") == 0)
609                                         consolitation_g = CON_PERCENTAGE;
610                                 else
611                                 {
612                                         fprintf (stderr, "Unknown consolidation function `%s'.\n",
613                                                         optarg);
614                                         usage (argv[0]);
615                                 }
616                                 break;
617                         case 'd':
618                         {
619                                 char **tmp;
620                                 tmp = (char **) realloc (match_ds_g,
621                                                 (match_ds_num_g + 1)
622                                                 * sizeof (char *));
623                                 if (tmp == NULL)
624                                 {
625                                         fprintf (stderr, "realloc failed: %s\n",
626                                                         strerror (errno));
627                                         return (RET_UNKNOWN);
628                                 }
629                                 match_ds_g = tmp;
630                                 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
631                                 if (match_ds_g[match_ds_num_g] == NULL)
632                                 {
633                                         fprintf (stderr, "cn_strdup failed: %s\n",
634                                                         strerror (errno));
635                                         return (RET_UNKNOWN);
636                                 }
637                                 match_ds_num_g++;
638                                 break;
639                         }
640                         case 'm':
641                                 nan_is_error_g = 1;
642                                 break;
643                         default:
644                                 usage (argv[0]);
645                 } /* switch (c) */
646         }
647
648         if ((socket_file_g == NULL) || (value_string_g == NULL)
649                         || (hostname_g == NULL))
650         {
651                 fprintf (stderr, "Missing required arguments.\n");
652                 usage (argv[0]);
653         }
654
655         return (do_check ());
656 } /* int main */