1 #include "config.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <assert.h>
10 #include <sys/socket.h>
11 #include <sys/un.h>
13 /*
14 * This is copied directly from collectd.h. Make changes there!
15 */
16 #if NAN_STATIC_DEFAULT
17 # include <math.h>
18 /* #endif NAN_STATIC_DEFAULT*/
19 #elif NAN_STATIC_ISOC
20 # ifndef __USE_ISOC99
21 # define DISABLE_ISOC99 1
22 # define __USE_ISOC99 1
23 # endif /* !defined(__USE_ISOC99) */
24 # include <math.h>
25 # if DISABLE_ISOC99
26 # undef DISABLE_ISOC99
27 # undef __USE_ISOC99
28 # endif /* DISABLE_ISOC99 */
29 /* #endif NAN_STATIC_ISOC */
30 #elif NAN_ZERO_ZERO
31 # include <math.h>
32 # ifdef NAN
33 # undef NAN
34 # endif
35 # define NAN (0.0 / 0.0)
36 # ifndef isnan
37 # define isnan(f) ((f) != (f))
38 # endif /* !defined(isnan) */
39 #endif /* NAN_ZERO_ZERO */
41 #define RET_OKAY 0
42 #define RET_WARNING 1
43 #define RET_CRITICAL 2
44 #define RET_UNKNOWN 3
46 #define CON_NONE 0
47 #define CON_AVERAGE 1
48 #define CON_SUM 2
50 struct range_s
51 {
52 double min;
53 double max;
54 int invert;
55 };
56 typedef struct range_s range_t;
58 extern char *optarg;
59 extern int optind, opterr, optopt;
61 static char *socket_file_g = NULL;
62 static char *value_string_g = NULL;
63 static char *hostname_g = NULL;
65 static range_t range_critical_g;
66 static range_t range_warning_g;
67 static int consolitation_g = CON_NONE;
69 static char **match_ds_g = NULL;
70 static int match_ds_num_g = 0;
72 static int ignore_ds (const char *name)
73 {
74 int i;
76 if (match_ds_g == NULL)
77 return (0);
79 for (i = 0; i < match_ds_num_g; i++)
80 if (strcasecmp (match_ds_g[i], name) == 0)
81 return (0);
83 return (1);
84 } /* int ignore_ds */
86 static void parse_range (char *string, range_t *range)
87 {
88 char *min_ptr;
89 char *max_ptr;
91 if (*string == '@')
92 {
93 range->invert = 1;
94 string++;
95 }
97 max_ptr = strchr (string, ':');
98 if (max_ptr == NULL)
99 {
100 min_ptr = NULL;
101 max_ptr = string;
102 }
103 else
104 {
105 min_ptr = string;
106 *max_ptr = '\0';
107 max_ptr++;
108 }
110 assert (max_ptr != NULL);
112 /* `10' == `0:10' */
113 if (min_ptr == NULL)
114 range->min = 0.0;
115 /* :10 == ~:10 == -inf:10 */
116 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
117 range->min = NAN;
118 else
119 range->min = atof (min_ptr);
121 if ((*max_ptr == '\0') || (*max_ptr == '~'))
122 range->max = NAN;
123 else
124 range->max = atof (max_ptr);
125 } /* void parse_range */
127 int match_range (range_t *range, double value)
128 {
129 int ret = 0;
131 if (!isnan (range->min) && (range->min > value))
132 ret = 1;
133 if (!isnan (range->max) && (range->max < value))
134 ret = 1;
136 return (((ret - range->invert) == 0) ? 0 : 1);
137 }
139 static int get_values (int *ret_values_num, double **ret_values,
140 char ***ret_values_names)
141 {
142 struct sockaddr_un sa;
143 int status;
144 int fd;
145 FILE *fh;
146 char buffer[4096];
148 int values_num;
149 double *values;
150 char **values_names;
152 int i;
154 fd = socket (PF_UNIX, SOCK_STREAM, 0);
155 if (fd < 0)
156 {
157 fprintf (stderr, "socket failed: %s\n",
158 strerror (errno));
159 return (-1);
160 }
162 memset (&sa, '\0', sizeof (sa));
163 sa.sun_family = AF_UNIX;
164 strncpy (sa.sun_path, socket_file_g,
165 sizeof (sa.sun_path) - 1);
167 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
168 if (status != 0)
169 {
170 fprintf (stderr, "connect failed: %s\n",
171 strerror (errno));
172 return (-1);
173 }
175 fh = fdopen (fd, "r+");
176 if (fh == NULL)
177 {
178 fprintf (stderr, "fdopen failed: %s\n",
179 strerror (errno));
180 close (fd);
181 return (-1);
182 }
184 fprintf (fh, "GETVAL %s/%s\n", hostname_g, value_string_g);
185 fflush (fh);
187 if (fgets (buffer, sizeof (buffer), fh) == NULL)
188 {
189 fprintf (stderr, "fgets failed: %s\n",
190 strerror (errno));
191 close (fd);
192 return (-1);
193 }
194 close (fd); fd = -1;
196 values_num = atoi (buffer);
197 if (values_num < 1)
198 return (-1);
200 values = (double *) malloc (values_num * sizeof (double));
201 if (values == NULL)
202 {
203 fprintf (stderr, "malloc failed: %s\n",
204 strerror (errno));
205 return (-1);
206 }
208 values_names = (char **) malloc (values_num * sizeof (char *));
209 if (values_names == NULL)
210 {
211 fprintf (stderr, "malloc failed: %s\n",
212 strerror (errno));
213 free (values);
214 return (-1);
215 }
217 {
218 char *ptr = strchr (buffer, ' ') + 1;
219 char *key;
220 char *value;
222 i = 0;
223 while ((key = strtok (ptr, " \t")) != NULL)
224 {
225 ptr = NULL;
226 value = strchr (key, '=');
227 if (value == NULL)
228 continue;
229 *value = '\0'; value++;
231 if (ignore_ds (key) != 0)
232 continue;
234 values_names[i] = strdup (key);
235 values[i] = atof (value);
237 i++;
238 if (i >= values_num)
239 break;
240 }
241 values_num = i;
242 }
244 *ret_values_num = values_num;
245 *ret_values = values;
246 *ret_values_names = values_names;
248 return (0);
249 } /* int get_values */
251 static void usage (const char *name)
252 {
253 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
254 "\n"
255 "Valid options are:\n"
256 " -s <socket> Path to collectd's UNIX-socket.\n"
257 " -n <v_spec> Value specification to get from collectd.\n"
258 " Format: `plugin-instance/type-instance'\n"
259 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
260 " DSes. By default all DSes are used.\n"
261 " -g <consol> Method to use to consolidate several DSes.\n"
262 " Valid arguments are `none', `average' and `sum'\n"
263 " -H <host> Hostname to query the values for.\n"
264 " -c <range> Critical range\n"
265 " -w <range> Warning range\n"
266 "\n"
267 "Consolidation functions:\n"
268 " none: Apply the warning- and critical-ranges to each data-source\n"
269 " individually.\n"
270 " average: Calculate the average of all matching DSes and apply the\n"
271 " warning- and critical-ranges to the calculated average.\n"
272 " sum: Apply the ranges to the sum of all DSes.\n"
273 "\n", name);
274 exit (1);
275 } /* void usage */
277 int do_check_con_none (int values_num, double *values, char **values_names)
278 {
279 int i;
281 int num_critical = 0;
282 int num_warning = 0;
283 int num_okay = 0;
285 for (i = 0; i < values_num; i++)
286 {
287 if (isnan (values[i]))
288 num_warning++;
289 else if (match_range (&range_critical_g, values[i]) != 0)
290 num_critical++;
291 else if (match_range (&range_warning_g, values[i]) != 0)
292 num_warning++;
293 else
294 num_okay++;
295 }
297 printf ("%i critical, %i warning, %i okay",
298 num_critical, num_warning, num_okay);
299 if (values_num > 0)
300 {
301 printf (" |");
302 for (i = 0; i < values_num; i++)
303 printf (" %s=%lf;;;;", values_names[i], values[i]);
304 }
305 printf ("\n");
307 if ((num_critical != 0) || (values_num == 0))
308 return (RET_CRITICAL);
309 else if (num_warning != 0)
310 return (RET_WARNING);
312 return (RET_OKAY);
313 } /* int do_check_con_none */
315 int do_check_con_average (int values_num, double *values, char **values_names)
316 {
317 int i;
318 double total;
319 int total_num;
320 double average;
322 total = 0.0;
323 total_num = 0;
324 for (i = 0; i < values_num; i++)
325 {
326 if (!isnan (values[i]))
327 {
328 total += values[i];
329 total_num++;
330 }
331 }
333 if (total_num == 0)
334 average = NAN;
335 else
336 average = total / total_num;
337 printf ("%lf average |", average);
338 for (i = 0; i < values_num; i++)
339 printf (" %s=%lf;;;;", values_names[i], values[i]);
341 if (total_num == 0)
342 return (RET_WARNING);
344 if (isnan (average)
345 || match_range (&range_critical_g, average))
346 return (RET_CRITICAL);
347 else if (match_range (&range_warning_g, average) != 0)
348 return (RET_WARNING);
350 return (RET_OKAY);
351 } /* int do_check_con_average */
353 int do_check_con_sum (int values_num, double *values, char **values_names)
354 {
355 int i;
356 double total;
357 int total_num;
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 }
370 if (total_num == 0)
371 {
372 printf ("WARNING: No defined values found\n");
373 return (RET_WARNING);
374 }
376 if (match_range (&range_critical_g, total) != 0)
377 {
378 printf ("CRITICAL: Sum = %lf\n", total);
379 return (RET_CRITICAL);
380 }
381 else if (match_range (&range_warning_g, total) != 0)
382 {
383 printf ("WARNING: Sum = %lf\n", total);
384 return (RET_WARNING);
385 }
386 else
387 {
388 printf ("OKAY: Sum = %lf\n", total);
389 return (RET_OKAY);
390 }
392 return (RET_UNKNOWN);
393 } /* int do_check_con_sum */
395 int do_check (void)
396 {
397 double *values;
398 char **values_names;
399 int values_num;
401 if (get_values (&values_num, &values, &values_names) != 0)
402 {
403 fputs ("ERROR: Cannot get values from daemon\n", stdout);
404 return (RET_CRITICAL);
405 }
407 if (consolitation_g == CON_NONE)
408 return (do_check_con_none (values_num, values, values_names));
409 else if (consolitation_g == CON_AVERAGE)
410 return (do_check_con_average (values_num, values, values_names));
411 else if (consolitation_g == CON_SUM)
412 return (do_check_con_sum (values_num, values, values_names));
414 free (values);
415 free (values_names); /* FIXME? */
417 return (RET_UNKNOWN);
418 }
420 int main (int argc, char **argv)
421 {
422 range_critical_g.min = NAN;
423 range_critical_g.max = NAN;
424 range_critical_g.invert = 0;
426 range_warning_g.min = NAN;
427 range_warning_g.max = NAN;
428 range_warning_g.invert = 0;
430 while (42)
431 {
432 int c;
434 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
435 if (c < 0)
436 break;
438 switch (c)
439 {
440 case 'c':
441 parse_range (optarg, &range_critical_g);
442 break;
443 case 'w':
444 parse_range (optarg, &range_warning_g);
445 break;
446 case 's':
447 socket_file_g = optarg;
448 break;
449 case 'n':
450 value_string_g = optarg;
451 break;
452 case 'H':
453 hostname_g = optarg;
454 break;
455 case 'g':
456 if (strcasecmp (optarg, "none") == 0)
457 consolitation_g = CON_NONE;
458 else if (strcasecmp (optarg, "average") == 0)
459 consolitation_g = CON_AVERAGE;
460 else if (strcasecmp (optarg, "sum") == 0)
461 consolitation_g = CON_SUM;
462 else
463 usage (argv[0]);
464 break;
465 case 'd':
466 {
467 char **tmp;
468 tmp = (char **) realloc (match_ds_g,
469 (match_ds_num_g + 1)
470 * sizeof (char *));
471 if (tmp == NULL)
472 {
473 fprintf (stderr, "realloc failed: %s\n",
474 strerror (errno));
475 return (RET_UNKNOWN);
476 }
477 match_ds_g = tmp;
478 match_ds_g[match_ds_num_g] = strdup (optarg);
479 if (match_ds_g[match_ds_num_g] == NULL)
480 {
481 fprintf (stderr, "strdup failed: %s\n",
482 strerror (errno));
483 return (RET_UNKNOWN);
484 }
485 match_ds_num_g++;
486 break;
487 }
488 default:
489 usage (argv[0]);
490 } /* switch (c) */
491 }
493 if ((socket_file_g == NULL) || (value_string_g == NULL)
494 || (hostname_g == NULL))
495 usage (argv[0]);
497 return (do_check ());
498 } /* int main */