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