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;
154 fd = socket (PF_UNIX, SOCK_STREAM, 0);
157 fprintf (stderr, "socket failed: %s\n",
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));
170 fprintf (stderr, "connect failed: %s\n",
175 fh = fdopen (fd, "r+");
178 fprintf (stderr, "fdopen failed: %s\n",
184 fprintf (fh, "GETVAL %s/%s\n", hostname_g, value_string_g);
187 if (fgets (buffer, sizeof (buffer), fh) == NULL)
189 fprintf (stderr, "fgets failed: %s\n",
196 values_num = atoi (buffer);
200 values = (double *) malloc (values_num * sizeof (double));
203 fprintf (stderr, "malloc failed: %s\n",
208 values_names = (char **) malloc (values_num * sizeof (char *));
209 if (values_names == NULL)
211 fprintf (stderr, "malloc failed: %s\n",
218 char *ptr = strchr (buffer, ' ') + 1;
223 while ((key = strtok (ptr, " \t")) != NULL)
226 value = strchr (key, '=');
229 *value = '\0'; value++;
231 if (ignore_ds (key) != 0)
234 values_names[i] = strdup (key);
235 values[i] = atof (value);
244 *ret_values_num = values_num;
245 *ret_values = values;
246 *ret_values_names = values_names;
249 } /* int get_values */
251 static void usage (const char *name)
253 fprintf (stderr, "Usage: %s <-s socket> <-n value_spec> <-H hostname> [options]\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"
267 "Consolidation functions:\n"
268 " none: Apply the warning- and critical-ranges to each data-source\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"
277 int do_check_con_none (int values_num, double *values, char **values_names)
281 int num_critical = 0;
285 for (i = 0; i < values_num; i++)
287 if (isnan (values[i]))
289 else if (match_range (&range_critical_g, values[i]) != 0)
291 else if (match_range (&range_warning_g, values[i]) != 0)
297 printf ("%i critical, %i warning, %i okay",
298 num_critical, num_warning, num_okay);
302 for (i = 0; i < values_num; i++)
303 printf (" %s=%lf;;;;", values_names[i], values[i]);
307 if ((num_critical != 0) || (values_num == 0))
308 return (RET_CRITICAL);
309 else if (num_warning != 0)
310 return (RET_WARNING);
313 } /* int do_check_con_none */
315 int do_check_con_average (int values_num, double *values, char **values_names)
324 for (i = 0; i < values_num; i++)
326 if (!isnan (values[i]))
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]);
342 return (RET_WARNING);
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);
351 } /* int do_check_con_average */
353 int do_check_con_sum (int values_num, double *values, char **values_names)
361 for (i = 0; i < values_num; i++)
363 if (!isnan (values[i]))
372 printf ("WARNING: No defined values found\n");
373 return (RET_WARNING);
376 if (match_range (&range_critical_g, total) != 0)
378 printf ("CRITICAL: Sum = %lf\n", total);
379 return (RET_CRITICAL);
381 else if (match_range (&range_warning_g, total) != 0)
383 printf ("WARNING: Sum = %lf\n", total);
384 return (RET_WARNING);
388 printf ("OKAY: Sum = %lf\n", total);
392 return (RET_UNKNOWN);
393 } /* int do_check_con_sum */
401 if (get_values (&values_num, &values, &values_names) != 0)
403 fputs ("ERROR: Cannot get values from daemon\n", stdout);
404 return (RET_CRITICAL);
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));
415 free (values_names); /* FIXME? */
417 return (RET_UNKNOWN);
420 int main (int argc, char **argv)
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;
434 c = getopt (argc, argv, "w:c:s:n:H:g:d:h");
441 parse_range (optarg, &range_critical_g);
444 parse_range (optarg, &range_warning_g);
447 socket_file_g = optarg;
450 value_string_g = optarg;
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;
468 tmp = (char **) realloc (match_ds_g,
473 fprintf (stderr, "realloc failed: %s\n",
475 return (RET_UNKNOWN);
478 match_ds_g[match_ds_num_g] = strdup (optarg);
479 if (match_ds_g[match_ds_num_g] == NULL)
481 fprintf (stderr, "strdup failed: %s\n",
483 return (RET_UNKNOWN);
493 if ((socket_file_g == NULL) || (value_string_g == NULL)
494 || (hostname_g == NULL))
497 return (do_check ());