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