Merge branch 'collectd-4.5'
[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 <stdlib.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <strings.h>
57 #include <assert.h>
58
59 #include <sys/socket.h>
60 #include <sys/un.h>
61
62 #include "config.h"
63 #include "libcollectdclient/client.h"
64
65 /*
66  * This is copied directly from collectd.h. Make changes there!
67  */
68 #if NAN_STATIC_DEFAULT
69 # include <math.h>
70 /* #endif NAN_STATIC_DEFAULT*/
71 #elif NAN_STATIC_ISOC
72 # ifndef __USE_ISOC99
73 #  define DISABLE_ISOC99 1
74 #  define __USE_ISOC99 1
75 # endif /* !defined(__USE_ISOC99) */
76 # include <math.h>
77 # if DISABLE_ISOC99
78 #  undef DISABLE_ISOC99
79 #  undef __USE_ISOC99
80 # endif /* DISABLE_ISOC99 */
81 /* #endif NAN_STATIC_ISOC */
82 #elif NAN_ZERO_ZERO
83 # include <math.h>
84 # ifdef NAN
85 #  undef NAN
86 # endif
87 # define NAN (0.0 / 0.0)
88 # ifndef isnan
89 #  define isnan(f) ((f) != (f))
90 # endif /* !defined(isnan) */
91 #endif /* NAN_ZERO_ZERO */
92
93 #define RET_OKAY     0
94 #define RET_WARNING  1
95 #define RET_CRITICAL 2
96 #define RET_UNKNOWN  3
97
98 #define CON_NONE     0
99 #define CON_AVERAGE  1
100 #define CON_SUM      2
101
102 struct range_s
103 {
104         double min;
105         double max;
106         int    invert;
107 };
108 typedef struct range_s range_t;
109
110 extern char *optarg;
111 extern int optind, opterr, optopt;
112
113 static char *socket_file_g = NULL;
114 static char *value_string_g = NULL;
115 static char *hostname_g = NULL;
116
117 static range_t range_critical_g;
118 static range_t range_warning_g;
119 static int consolitation_g = CON_NONE;
120
121 static char **match_ds_g = NULL;
122 static int    match_ds_num_g = 0;
123
124 /* `strdup' is an XSI extension. I don't want to pull in all of XSI just for
125  * that, so here's an own implementation.. It's easy enough. The GCC attributes
126  * are supposed to get good performance..  -octo */
127 __attribute__((malloc, nonnull (1)))
128 static char *cn_strdup (const char *str) /* {{{ */
129 {
130   size_t strsize;
131   char *ret;
132
133   strsize = strlen (str) + 1;
134   ret = (char *) malloc (strsize);
135   if (ret != NULL)
136     memcpy (ret, str, strsize);
137   return (ret);
138 } /* }}} char *cn_strdup */
139
140 static int ignore_ds (const char *name)
141 {
142         int i;
143
144         if (match_ds_g == NULL)
145                 return (0);
146
147         for (i = 0; i < match_ds_num_g; i++)
148                 if (strcasecmp (match_ds_g[i], name) == 0)
149                         return (0);
150
151         return (1);
152 } /* int ignore_ds */
153
154 static void parse_range (char *string, range_t *range)
155 {
156         char *min_ptr;
157         char *max_ptr;
158
159         if (*string == '@')
160         {
161                 range->invert = 1;
162                 string++;
163         }
164
165         max_ptr = strchr (string, ':');
166         if (max_ptr == NULL)
167         {
168                 min_ptr = NULL;
169                 max_ptr = string;
170         }
171         else
172         {
173                 min_ptr = string;
174                 *max_ptr = '\0';
175                 max_ptr++;
176         }
177
178         assert (max_ptr != NULL);
179
180         /* `10' == `0:10' */
181         if (min_ptr == NULL)
182                 range->min = 0.0;
183         /* :10 == ~:10 == -inf:10 */
184         else if ((*min_ptr == '\0') || (*min_ptr == '~'))
185                 range->min = NAN;
186         else
187                 range->min = atof (min_ptr);
188
189         if ((*max_ptr == '\0') || (*max_ptr == '~'))
190                 range->max = NAN;
191         else
192                 range->max = atof (max_ptr);
193 } /* void parse_range */
194
195 static int match_range (range_t *range, double value)
196 {
197         int ret = 0;
198
199         if (!isnan (range->min) && (range->min > value))
200                 ret = 1;
201         if (!isnan (range->max) && (range->max < value))
202                 ret = 1;
203
204         return (((ret - range->invert) == 0) ? 0 : 1);
205 } /* int match_range */
206
207 static void usage (const char *name)
208 {
209         fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
210                         "\n"
211                         "Valid options are:\n"
212                         "  -s <socket>    Path to collectd's UNIX-socket.\n"
213                         "  -n <v_spec>    Value specification to get from collectd.\n"
214                         "                 Format: `plugin-instance/type-instance'\n"
215                         "  -d <ds>        Select the DS to examine. May be repeated to examine multiple\n"
216                         "                 DSes. By default all DSes are used.\n"
217                         "  -g <consol>    Method to use to consolidate several DSes.\n"
218                         "                 Valid arguments are `none', `average' and `sum'\n"
219                         "  -H <host>      Hostname to query the values for.\n"
220                         "  -c <range>     Critical range\n"
221                         "  -w <range>     Warning range\n"
222                         "\n"
223                         "Consolidation functions:\n"
224                         "  none:          Apply the warning- and critical-ranges to each data-source\n"
225                         "                 individually.\n"
226                         "  average:       Calculate the average of all matching DSes and apply the\n"
227                         "                 warning- and critical-ranges to the calculated average.\n"
228                         "  sum:           Apply the ranges to the sum of all DSes.\n"
229                         "\n", name);
230         exit (1);
231 } /* void usage */
232
233 static int do_check_con_none (int values_num,
234                 double *values, char **values_names)
235 {
236         int num_critical = 0;
237         int num_warning  = 0;
238         int num_okay = 0;
239         const char *status_str = "UNKNOWN";
240         int status_code = RET_UNKNOWN;
241         int i;
242
243         for (i = 0; i < values_num; i++)
244         {
245                 if (ignore_ds (values_names[i]))
246                         continue;
247
248                 if (isnan (values[i]))
249                         num_warning++;
250                 else if (match_range (&range_critical_g, values[i]) != 0)
251                         num_critical++;
252                 else if (match_range (&range_warning_g, values[i]) != 0)
253                         num_warning++;
254                 else
255                         num_okay++;
256         }
257
258         if ((num_critical == 0) && (num_warning == 0) && (num_okay == 0))
259         {
260                 printf ("WARNING: No defined values found\n");
261                 return (RET_WARNING);
262         }
263         else if ((num_critical == 0) && (num_warning == 0))
264         {
265                 status_str = "OKAY";
266                 status_code = RET_OKAY;
267         }
268         else if (num_critical == 0)
269         {
270                 status_str = "WARNING";
271                 status_code = RET_WARNING;
272         }
273         else
274         {
275                 status_str = "CRITICAL";
276                 status_code = RET_CRITICAL;
277         }
278
279         printf ("%s: %i critical, %i warning, %i okay", status_str,
280                         num_critical, num_warning, num_okay);
281         if (values_num > 0)
282         {
283                 printf (" |");
284                 for (i = 0; i < values_num; i++)
285                         printf (" %s=%g;;;;", values_names[i], values[i]);
286         }
287         printf ("\n");
288
289         return (status_code);
290 } /* int do_check_con_none */
291
292 static int do_check_con_average (int values_num,
293                 double *values, char **values_names)
294 {
295         int i;
296         double total;
297         int total_num;
298         double average;
299         const char *status_str = "UNKNOWN";
300         int status_code = RET_UNKNOWN;
301
302         total = 0.0;
303         total_num = 0;
304         for (i = 0; i < values_num; i++)
305         {
306                 if (ignore_ds (values_names[i]))
307                         continue;
308
309                 if (!isnan (values[i]))
310                 {
311                         total += values[i];
312                         total_num++;
313                 }
314         }
315
316         if (total_num == 0)
317         {
318                 printf ("WARNING: No defined values found\n");
319                 return (RET_WARNING);
320         }
321
322         average = total / total_num;
323
324         if (match_range (&range_critical_g, average) != 0)
325         {
326                 status_str = "CRITICAL";
327                 status_code = RET_CRITICAL;
328         }
329         else if (match_range (&range_warning_g, average) != 0)
330         {
331                 status_str = "WARNING";
332                 status_code = RET_WARNING;
333         }
334         else
335         {
336                 status_str = "OKAY";
337                 status_code = RET_OKAY;
338         }
339
340         printf ("%s: %g average |", status_str, average);
341         for (i = 0; i < values_num; i++)
342                 printf (" %s=%g;;;;", values_names[i], values[i]);
343         printf ("\n");
344
345         return (status_code);
346 } /* int do_check_con_average */
347
348 static int do_check_con_sum (int values_num, double *values, char **values_names)
349 {
350         int i;
351         double total;
352         int total_num;
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 (ignore_ds (values_names[i]))
361                         continue;
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         if (match_range (&range_critical_g, total) != 0)
377         {
378                 status_str = "CRITICAL";
379                 status_code = RET_CRITICAL;
380         }
381         else if (match_range (&range_warning_g, total) != 0)
382         {
383                 status_str = "WARNING";
384                 status_code = RET_WARNING;
385         }
386         else
387         {
388                 status_str = "OKAY";
389                 status_code = RET_OKAY;
390         }
391
392         printf ("%s: %g sum |", status_str, total);
393         for (i = 0; i < values_num; i++)
394                 printf (" %s=%g;;;;", values_names[i], values[i]);
395         printf ("\n");
396
397         return (status_code);
398 } /* int do_check_con_sum */
399
400 static int do_check (void)
401 {
402         lcc_connection_t *connection;
403         gauge_t *values;
404         char   **values_names;
405         size_t   values_num;
406         char address[1024];
407         char ident_str[1024];
408         lcc_identifier_t ident;
409         size_t i;
410         int status;
411
412         snprintf (address, sizeof (address), "unix:%s", socket_file_g);
413         address[sizeof (address) - 1] = 0;
414
415         snprintf (ident_str, sizeof (ident_str), "%s/%s",
416                         hostname_g, value_string_g);
417         ident_str[sizeof (ident_str) - 1] = 0;
418
419         connection = NULL;
420         status = lcc_connect (address, &connection);
421         if (status != 0)
422         {
423                 printf ("ERROR: Connecting to daemon at %s failed.\n",
424                                 socket_file_g);
425                 return (RET_CRITICAL);
426         }
427
428         memset (&ident, 0, sizeof (ident));
429         status = lcc_string_to_identifier (connection, &ident, ident_str);
430         if (status != 0)
431         {
432                 printf ("ERROR: Creating an identifier failed: %s.\n",
433                                 lcc_strerror (connection));
434                 LCC_DESTROY (connection);
435                 return (RET_CRITICAL);
436         }
437
438         status = lcc_getval (connection, &ident,
439                         &values_num, &values, &values_names);
440         if (status != 0)
441         {
442                 printf ("ERROR: Retrieving values from the daemon failed: %s.\n",
443                                 lcc_strerror (connection));
444                 LCC_DESTROY (connection);
445                 return (RET_CRITICAL);
446         }
447
448         LCC_DESTROY (connection);
449
450         status = RET_UNKNOWN;
451         if (consolitation_g == CON_NONE)
452                 status =  do_check_con_none (values_num, values, values_names);
453         else if (consolitation_g == CON_AVERAGE)
454                 status =  do_check_con_average (values_num, values, values_names);
455         else if (consolitation_g == CON_SUM)
456                 status = do_check_con_sum (values_num, values, values_names);
457
458         free (values);
459         if (values_names != NULL)
460                 for (i = 0; i < values_num; i++)
461                         free (values_names[i]);
462         free (values_names);
463
464         return (status);
465 } /* int do_check */
466
467 int main (int argc, char **argv)
468 {
469         range_critical_g.min = NAN;
470         range_critical_g.max = NAN;
471         range_critical_g.invert = 0;
472
473         range_warning_g.min = NAN;
474         range_warning_g.max = NAN;
475         range_warning_g.invert = 0;
476
477         while (42)
478         {
479                 int c;
480
481                 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
482                 if (c < 0)
483                         break;
484
485                 switch (c)
486                 {
487                         case 'c':
488                                 parse_range (optarg, &range_critical_g);
489                                 break;
490                         case 'w':
491                                 parse_range (optarg, &range_warning_g);
492                                 break;
493                         case 's':
494                                 socket_file_g = optarg;
495                                 break;
496                         case 'n':
497                                 value_string_g = optarg;
498                                 break;
499                         case 'H':
500                                 hostname_g = optarg;
501                                 break;
502                         case 'g':
503                                 if (strcasecmp (optarg, "none") == 0)
504                                         consolitation_g = CON_NONE;
505                                 else if (strcasecmp (optarg, "average") == 0)
506                                         consolitation_g = CON_AVERAGE;
507                                 else if (strcasecmp (optarg, "sum") == 0)
508                                         consolitation_g = CON_SUM;
509                                 else
510                                         usage (argv[0]);
511                                 break;
512                         case 'd':
513                         {
514                                 char **tmp;
515                                 tmp = (char **) realloc (match_ds_g,
516                                                 (match_ds_num_g + 1)
517                                                 * sizeof (char *));
518                                 if (tmp == NULL)
519                                 {
520                                         fprintf (stderr, "realloc failed: %s\n",
521                                                         strerror (errno));
522                                         return (RET_UNKNOWN);
523                                 }
524                                 match_ds_g = tmp;
525                                 match_ds_g[match_ds_num_g] = cn_strdup (optarg);
526                                 if (match_ds_g[match_ds_num_g] == NULL)
527                                 {
528                                         fprintf (stderr, "cn_strdup failed: %s\n",
529                                                         strerror (errno));
530                                         return (RET_UNKNOWN);
531                                 }
532                                 match_ds_num_g++;
533                                 break;
534                         }
535                         default:
536                                 usage (argv[0]);
537                 } /* switch (c) */
538         }
539
540         if ((socket_file_g == NULL) || (value_string_g == NULL)
541                         || (hostname_g == NULL))
542                 usage (argv[0]);
543
544         return (do_check ());
545 } /* int main */