10 #include <sys/socket.h>
14 * This is copied directly from collectd.h. Make changes there!
16 #if NAN_STATIC_DEFAULT
18 /* #endif NAN_STATIC_DEFAULT*/
21 # define DISABLE_ISOC99 1
22 # define __USE_ISOC99 1
23 # endif /* !defined(__USE_ISOC99) */
26 # undef DISABLE_ISOC99
28 # endif /* DISABLE_ISOC99 */
29 /* #endif NAN_STATIC_ISOC */
35 # define NAN (0.0 / 0.0)
37 # define isnan(f) ((f) != (f))
38 # endif /* !defined(isnan) */
39 #endif /* NAN_ZERO_ZERO */
43 #define RET_CRITICAL 2
56 typedef struct range_s range_t;
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)
76 if (match_ds_g == NULL)
79 for (i = 0; i < match_ds_num_g; i++)
80 if (strcasecmp (match_ds_g[i], name) == 0)
86 static void parse_range (char *string, range_t *range)
97 max_ptr = strchr (string, ':');
110 assert (max_ptr != NULL);
115 /* :10 == ~:10 == -inf:10 */
116 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
119 range->min = atof (min_ptr);
121 if ((*max_ptr == '\0') || (*max_ptr == '~'))
124 range->max = atof (max_ptr);
125 } /* void parse_range */
127 int match_range (range_t *range, double value)
131 if (!isnan (range->min) && (range->min > value))
133 if (!isnan (range->max) && (range->max < value))
136 return (((ret - range->invert) == 0) ? 0 : 1);
139 static int get_values (int *ret_values_num, double **ret_values,
140 char ***ret_values_names)
142 struct sockaddr_un sa;
145 FILE *fh_in, *fh_out;
155 fd = socket (PF_UNIX, SOCK_STREAM, 0);
158 fprintf (stderr, "socket failed: %s\n",
163 memset (&sa, '\0', sizeof (sa));
164 sa.sun_family = AF_UNIX;
165 strncpy (sa.sun_path, socket_file_g,
166 sizeof (sa.sun_path) - 1);
168 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
171 fprintf (stderr, "connect failed: %s\n",
176 fh_in = fdopen (fd, "r");
179 fprintf (stderr, "fdopen failed: %s\n",
185 fh_out = fdopen (fd, "w");
188 fprintf (stderr, "fdopen failed: %s\n",
194 fprintf (fh_out, "GETVAL %s/%s\n", hostname_g, value_string_g);
197 if (fgets (buffer, sizeof (buffer), fh_in) == NULL)
199 fprintf (stderr, "fgets failed: %s\n",
207 char *ptr = strchr (buffer, ' ');
212 values_num = atoi (buffer);
217 values = (double *) malloc (values_num * sizeof (double));
220 fprintf (stderr, "malloc failed: %s\n",
225 values_names = (char **) malloc (values_num * sizeof (char *));
226 if (values_names == NULL)
228 fprintf (stderr, "malloc failed: %s\n",
233 memset (values_names, 0, values_num * sizeof (char *));
235 i = 0; /* index of the values returned by the server */
236 j = 0; /* number of values in `values_names' and `values' */
237 while (fgets (buffer, sizeof (buffer), fh_in) != NULL)
247 value = strchr (key, '=');
250 fprintf (stderr, "Cannot parse line: %s\n", buffer);
256 if (ignore_ds (key) != 0)
261 values[j] = strtod (value, &endptr);
262 if ((endptr == value) || (errno != 0))
264 fprintf (stderr, "Could not parse buffer "
265 "as number: %s\n", value);
269 values_names[j] = strdup (key);
270 if (values_names[j] == NULL)
272 fprintf (stderr, "strdup failed.\n");
282 /* Set `values_num' to the number of values actually stored in the
286 fclose (fh_in); fh_in = NULL; fd = -1;
287 fclose (fh_out); fh_out = NULL;
289 *ret_values_num = values_num;
290 *ret_values = values;
291 *ret_values_names = values_names;
294 } /* int get_values */
296 static void usage (const char *name)
298 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
300 "Valid options are:\n"
301 " -s <socket> Path to collectd's UNIX-socket.\n"
302 " -n <v_spec> Value specification to get from collectd.\n"
303 " Format: `plugin-instance/type-instance'\n"
304 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
305 " DSes. By default all DSes are used.\n"
306 " -g <consol> Method to use to consolidate several DSes.\n"
307 " Valid arguments are `none', `average' and `sum'\n"
308 " -H <host> Hostname to query the values for.\n"
309 " -c <range> Critical range\n"
310 " -w <range> Warning range\n"
312 "Consolidation functions:\n"
313 " none: Apply the warning- and critical-ranges to each data-source\n"
315 " average: Calculate the average of all matching DSes and apply the\n"
316 " warning- and critical-ranges to the calculated average.\n"
317 " sum: Apply the ranges to the sum of all DSes.\n"
322 int do_check_con_none (int values_num, double *values, char **values_names)
326 int num_critical = 0;
330 for (i = 0; i < values_num; i++)
332 if (isnan (values[i]))
334 else if (match_range (&range_critical_g, values[i]) != 0)
336 else if (match_range (&range_warning_g, values[i]) != 0)
342 printf ("%i critical, %i warning, %i okay",
343 num_critical, num_warning, num_okay);
347 for (i = 0; i < values_num; i++)
348 printf (" %s=%lf;;;;", values_names[i], values[i]);
352 if ((num_critical != 0) || (values_num == 0))
353 return (RET_CRITICAL);
354 else if (num_warning != 0)
355 return (RET_WARNING);
358 } /* int do_check_con_none */
360 int do_check_con_average (int values_num, double *values, char **values_names)
369 for (i = 0; i < values_num; i++)
371 if (!isnan (values[i]))
381 average = total / total_num;
382 printf ("%lf average |", average);
383 for (i = 0; i < values_num; i++)
384 printf (" %s=%lf;;;;", values_names[i], values[i]);
387 return (RET_WARNING);
390 || match_range (&range_critical_g, average))
391 return (RET_CRITICAL);
392 else if (match_range (&range_warning_g, average) != 0)
393 return (RET_WARNING);
396 } /* int do_check_con_average */
398 int do_check_con_sum (int values_num, double *values, char **values_names)
406 for (i = 0; i < values_num; i++)
408 if (!isnan (values[i]))
417 printf ("WARNING: No defined values found\n");
418 return (RET_WARNING);
421 if (match_range (&range_critical_g, total) != 0)
423 printf ("CRITICAL: Sum = %lf\n", total);
424 return (RET_CRITICAL);
426 else if (match_range (&range_warning_g, total) != 0)
428 printf ("WARNING: Sum = %lf\n", total);
429 return (RET_WARNING);
433 printf ("OKAY: Sum = %lf\n", total);
437 return (RET_UNKNOWN);
438 } /* int do_check_con_sum */
446 if (get_values (&values_num, &values, &values_names) != 0)
448 fputs ("ERROR: Cannot get values from daemon\n", stdout);
449 return (RET_CRITICAL);
452 if (consolitation_g == CON_NONE)
453 return (do_check_con_none (values_num, values, values_names));
454 else if (consolitation_g == CON_AVERAGE)
455 return (do_check_con_average (values_num, values, values_names));
456 else if (consolitation_g == CON_SUM)
457 return (do_check_con_sum (values_num, values, values_names));
460 free (values_names); /* FIXME? */
462 return (RET_UNKNOWN);
465 int main (int argc, char **argv)
467 range_critical_g.min = NAN;
468 range_critical_g.max = NAN;
469 range_critical_g.invert = 0;
471 range_warning_g.min = NAN;
472 range_warning_g.max = NAN;
473 range_warning_g.invert = 0;
479 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
486 parse_range (optarg, &range_critical_g);
489 parse_range (optarg, &range_warning_g);
492 socket_file_g = optarg;
495 value_string_g = optarg;
501 if (strcasecmp (optarg, "none") == 0)
502 consolitation_g = CON_NONE;
503 else if (strcasecmp (optarg, "average") == 0)
504 consolitation_g = CON_AVERAGE;
505 else if (strcasecmp (optarg, "sum") == 0)
506 consolitation_g = CON_SUM;
513 tmp = (char **) realloc (match_ds_g,
518 fprintf (stderr, "realloc failed: %s\n",
520 return (RET_UNKNOWN);
523 match_ds_g[match_ds_num_g] = strdup (optarg);
524 if (match_ds_g[match_ds_num_g] == NULL)
526 fprintf (stderr, "strdup failed: %s\n",
528 return (RET_UNKNOWN);
538 if ((socket_file_g == NULL) || (value_string_g == NULL)
539 || (hostname_g == NULL))
542 return (do_check ());