8 #include <sys/socket.h>
12 * This weird macro cascade forces the glibc to define `NAN'. I don't know
13 * another way to solve this, so more intelligent solutions are welcome. -octo
16 # define DISABLE__USE_ISOC99 1
17 # define __USE_ISOC99 1
20 #ifdef DISABLE__USE_ISOC99
21 # undef DISABLE__USE_ISOC99
27 #define RET_CRITICAL 2
40 typedef struct range_s range_t;
43 extern int optind, opterr, optopt;
45 static char *socket_file_g = NULL;
46 static char *value_string_g = NULL;
47 static char *hostname_g = NULL;
49 static range_t range_critical_g;
50 static range_t range_warning_g;
51 static int consolitation_g = CON_NONE;
53 static char **match_ds_g = NULL;
54 static int match_ds_num_g = 0;
56 static int ignore_ds (const char *name)
60 if (match_ds_g == NULL)
63 for (i = 0; i < match_ds_num_g; i++)
64 if (strcasecmp (match_ds_g[i], name) == 0)
70 static void parse_range (char *string, range_t *range)
81 max_ptr = strchr (string, ':');
94 assert (max_ptr != NULL);
99 /* :10 == ~:10 == -inf:10 */
100 else if ((*min_ptr == '\0') || (*min_ptr == '~'))
103 range->min = atof (min_ptr);
105 if ((*max_ptr == '\0') || (*max_ptr == '~'))
108 range->max = atof (max_ptr);
109 } /* void parse_range */
111 int match_range (range_t *range, double value)
115 if ((range->min != NAN) && (range->min > value))
117 if ((range->max != NAN) && (range->max < value))
120 return (((ret - range->invert) == 0) ? 0 : 1);
123 static int get_values (int *ret_values_num, double **ret_values,
124 char ***ret_values_names)
126 struct sockaddr_un sa;
138 fd = socket (PF_UNIX, SOCK_STREAM, 0);
141 fprintf (stderr, "socket failed: %s\n",
146 memset (&sa, '\0', sizeof (sa));
147 sa.sun_family = AF_UNIX;
148 strncpy (sa.sun_path, socket_file_g,
149 sizeof (sa.sun_path) - 1);
151 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
154 fprintf (stderr, "connect failed: %s\n",
159 fh = fdopen (fd, "r+");
162 fprintf (stderr, "fdopen failed: %s\n",
168 fprintf (fh, "GETVAL %s/%s\n", hostname_g, value_string_g);
171 if (fgets (buffer, sizeof (buffer), fh) == NULL)
173 fprintf (stderr, "fgets failed: %s\n",
180 values_num = atoi (buffer);
184 values = (double *) malloc (values_num * sizeof (double));
187 fprintf (stderr, "malloc failed: %s\n",
192 values_names = (char **) malloc (values_num * sizeof (char *));
193 if (values_names == NULL)
195 fprintf (stderr, "malloc failed: %s\n",
202 char *ptr = strchr (buffer, ' ') + 1;
207 while ((key = strtok (ptr, " \t")) != NULL)
210 value = strchr (key, '=');
213 *value = '\0'; value++;
215 if (ignore_ds (key) != 0)
218 values_names[i] = strdup (key);
219 values[i] = atof (value);
228 *ret_values_num = values_num;
229 *ret_values = values;
230 *ret_values_names = values_names;
233 } /* int get_values */
235 static void usage (const char *name)
237 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\n"
239 "Valid options are:\n"
240 " -s <socket> Path to collectd's UNIX-socket.\n"
241 " -n <v_spec> Value specification to get from collectd.\n"
242 " Format: `plugin-instance/type-instance'\n"
243 " -d <ds> Select the DS to examine. May be repeated to examine multiple\n"
244 " DSes. By default all DSes are used.\n"
245 " -g <consol> Method to use to consolidate several DSes.\n"
246 " Valid arguments are `none', `average' and `sum'\n"
247 " -H <host> Hostname to query the values for.\n"
248 " -c <range> Critical range\n"
249 " -w <range> Warning range\n"
251 "Consolidation functions:\n"
252 " none: Apply the warning- and critical-ranges to each data-source\n"
254 " average: Calculate the average of all matching DSes and apply the\n"
255 " warning- and critical-ranges to the calculated average.\n"
256 " sum: Apply the ranges to the sum of all DSes.\n"
261 int do_check_con_none (int values_num, double *values, char **values_names)
265 int num_critical = 0;
269 for (i = 0; i < values_num; i++)
271 if (values[i] == NAN)
273 else if (match_range (&range_critical_g, values[i]) != 0)
275 else if (match_range (&range_warning_g, values[i]) != 0)
281 if ((num_critical != 0) || (values_num == 0))
283 printf ("CRITICAL: %i critical, %i warning, %i okay\n",
284 num_critical, num_warning, num_okay);
285 return (RET_CRITICAL);
287 else if (num_warning != 0)
289 printf ("WARNING: %i warning, %i okay\n",
290 num_warning, num_okay);
291 return (RET_WARNING);
295 printf ("OKAY: %i okay\n", num_okay);
299 return (RET_UNKNOWN);
300 } /* int do_check_con_none */
302 int do_check_con_average (int values_num, double *values, char **values_names)
310 for (i = 0; i < values_num; i++)
312 if (values[i] != NAN)
321 printf ("WARNING: No defined values found\n");
322 return (RET_WARNING);
325 if (match_range (&range_critical_g, total / total_num) != 0)
327 printf ("CRITICAL: Average = %lf\n",
328 (double) (total / total_num));
329 return (RET_CRITICAL);
331 else if (match_range (&range_warning_g, total / total_num) != 0)
333 printf ("WARNING: Average = %lf\n",
334 (double) (total / total_num));
335 return (RET_WARNING);
339 printf ("OKAY: Average = %lf\n",
340 (double) (total / total_num));
344 return (RET_UNKNOWN);
345 } /* int do_check_con_average */
347 int do_check_con_sum (int values_num, double *values, char **values_names)
355 for (i = 0; i < values_num; i++)
357 if (values[i] != NAN)
366 printf ("WARNING: No defined values found\n");
367 return (RET_WARNING);
370 if (match_range (&range_critical_g, total) != 0)
372 printf ("CRITICAL: Sum = %lf\n", total);
373 return (RET_CRITICAL);
375 else if (match_range (&range_warning_g, total) != 0)
377 printf ("WARNING: Sum = %lf\n", total);
378 return (RET_WARNING);
382 printf ("OKAY: Sum = %lf\n", total);
386 return (RET_UNKNOWN);
387 } /* int do_check_con_sum */
395 if (get_values (&values_num, &values, &values_names) != 0)
397 fputs ("ERROR: Cannot get values from daemon\n", stdout);
398 return (RET_CRITICAL);
401 if (consolitation_g == CON_NONE)
402 return (do_check_con_none (values_num, values, values_names));
403 else if (consolitation_g == CON_AVERAGE)
404 return (do_check_con_average (values_num, values, values_names));
405 else if (consolitation_g == CON_SUM)
406 return (do_check_con_sum (values_num, values, values_names));
411 return (RET_UNKNOWN);
414 int main (int argc, char **argv)
416 range_critical_g.min = NAN;
417 range_critical_g.max = NAN;
418 range_critical_g.invert = 0;
420 range_warning_g.min = NAN;
421 range_warning_g.max = NAN;
422 range_warning_g.invert = 0;
428 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
435 parse_range (optarg, &range_critical_g);
438 parse_range (optarg, &range_warning_g);
441 socket_file_g = optarg;
444 value_string_g = optarg;
450 if (strcasecmp (optarg, "none") == 0)
451 consolitation_g = CON_NONE;
452 else if (strcasecmp (optarg, "average") == 0)
453 consolitation_g = CON_AVERAGE;
454 else if (strcasecmp (optarg, "sum") == 0)
455 consolitation_g = CON_SUM;
462 tmp = (char **) realloc (match_ds_g,
467 fprintf (stderr, "realloc failed: %s\n",
469 return (RET_UNKNOWN);
472 match_ds_g[match_ds_num_g] = strdup (optarg);
473 if (match_ds_g[match_ds_num_g] == NULL)
475 fprintf (stderr, "strdup failed: %s\n",
477 return (RET_UNKNOWN);
487 if ((socket_file_g == NULL) || (value_string_g == NULL)
488 || (hostname_g == NULL))
491 return (do_check ());