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