diff options
Diffstat (limited to 'plugins/utils.c')
-rw-r--r-- | plugins/utils.c | 133 |
1 files changed, 113 insertions, 20 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index dbb2520..685a638 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -265,44 +265,44 @@ is_option (char *str) | |||
265 | return FALSE; | 265 | return FALSE; |
266 | } | 266 | } |
267 | 267 | ||
268 | void set_threshold_start (threshold *this, double value) { | 268 | void set_range_start (range *this, double value) { |
269 | this->start = value; | 269 | this->start = value; |
270 | this->start_infinity = FALSE; | 270 | this->start_infinity = FALSE; |
271 | } | 271 | } |
272 | 272 | ||
273 | void set_threshold_end (threshold *this, double value) { | 273 | void set_range_end (range *this, double value) { |
274 | this->end = value; | 274 | this->end = value; |
275 | this->end_infinity = FALSE; | 275 | this->end_infinity = FALSE; |
276 | } | 276 | } |
277 | 277 | ||
278 | threshold | 278 | range |
279 | *parse_threshold (char *str) { | 279 | *parse_range_string (char *str) { |
280 | threshold *temp_threshold; | 280 | range *temp_range; |
281 | double start; | 281 | double start; |
282 | double end; | 282 | double end; |
283 | char *end_str; | 283 | char *end_str; |
284 | 284 | ||
285 | temp_threshold = (threshold *) malloc(sizeof(threshold)); | 285 | temp_range = (range *) malloc(sizeof(range)); |
286 | 286 | ||
287 | /* Set defaults */ | 287 | /* Set defaults */ |
288 | temp_threshold->start = 0; | 288 | temp_range->start = 0; |
289 | temp_threshold->start_infinity = FALSE; | 289 | temp_range->start_infinity = FALSE; |
290 | temp_threshold->end = 0; | 290 | temp_range->end = 0; |
291 | temp_threshold->end_infinity = TRUE; | 291 | temp_range->end_infinity = TRUE; |
292 | temp_threshold->alert_on = OUTSIDE; | 292 | temp_range->alert_on = OUTSIDE; |
293 | 293 | ||
294 | if (str[0] == '@') { | 294 | if (str[0] == '@') { |
295 | temp_threshold->alert_on = INSIDE; | 295 | temp_range->alert_on = INSIDE; |
296 | str++; | 296 | str++; |
297 | } | 297 | } |
298 | 298 | ||
299 | end_str = index(str, ':'); | 299 | end_str = index(str, ':'); |
300 | if (end_str != NULL) { | 300 | if (end_str != NULL) { |
301 | if (str[0] == '~') { | 301 | if (str[0] == '~') { |
302 | temp_threshold->start_infinity = TRUE; | 302 | temp_range->start_infinity = TRUE; |
303 | } else { | 303 | } else { |
304 | start = strtod(str, NULL); /* Will stop at the ':' */ | 304 | start = strtod(str, NULL); /* Will stop at the ':' */ |
305 | set_threshold_start(temp_threshold, start); | 305 | set_range_start(temp_range, start); |
306 | } | 306 | } |
307 | end_str++; /* Move past the ':' */ | 307 | end_str++; /* Move past the ':' */ |
308 | } else { | 308 | } else { |
@@ -310,18 +310,111 @@ threshold | |||
310 | } | 310 | } |
311 | end = strtod(end_str, NULL); | 311 | end = strtod(end_str, NULL); |
312 | if (strcmp(end_str, "") != 0) { | 312 | if (strcmp(end_str, "") != 0) { |
313 | set_threshold_end(temp_threshold, end); | 313 | set_range_end(temp_range, end); |
314 | } | 314 | } |
315 | 315 | ||
316 | if (temp_threshold->start_infinity == TRUE || | 316 | if (temp_range->start_infinity == TRUE || |
317 | temp_threshold->end_infinity == TRUE || | 317 | temp_range->end_infinity == TRUE || |
318 | temp_threshold->start <= temp_threshold->end) { | 318 | temp_range->start <= temp_range->end) { |
319 | return temp_threshold; | 319 | return temp_range; |
320 | } | 320 | } |
321 | free(temp_threshold); | 321 | free(temp_range); |
322 | return NULL; | 322 | return NULL; |
323 | } | 323 | } |
324 | 324 | ||
325 | /* returns 0 if okay, otherwise 1 */ | ||
326 | int | ||
327 | _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) | ||
328 | { | ||
329 | thresholds *temp_thresholds = NULL; | ||
330 | |||
331 | temp_thresholds = malloc(sizeof(temp_thresholds)); | ||
332 | |||
333 | temp_thresholds->warning = NULL; | ||
334 | temp_thresholds->critical = NULL; | ||
335 | |||
336 | if (warn_string != NULL) { | ||
337 | if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) { | ||
338 | return 1; | ||
339 | } | ||
340 | } | ||
341 | if (critical_string != NULL) { | ||
342 | if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) { | ||
343 | return 1; | ||
344 | } | ||
345 | } | ||
346 | |||
347 | if (*my_thresholds != 0) { | ||
348 | /* printf("Freeing here: %d\n", *my_thresholds); */ | ||
349 | free(*my_thresholds); | ||
350 | } | ||
351 | *my_thresholds = temp_thresholds; | ||
352 | |||
353 | return 0; | ||
354 | } | ||
355 | |||
356 | void | ||
357 | set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) | ||
358 | { | ||
359 | if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) { | ||
360 | return; | ||
361 | } else { | ||
362 | usage("Range format incorrect"); | ||
363 | } | ||
364 | } | ||
365 | |||
366 | /* Returns TRUE if alert should be raised based on the range */ | ||
367 | int | ||
368 | check_range(double value, range *my_range) | ||
369 | { | ||
370 | int false = FALSE; | ||
371 | int true = TRUE; | ||
372 | |||
373 | if (my_range->alert_on == INSIDE) { | ||
374 | false = TRUE; | ||
375 | true = FALSE; | ||
376 | } | ||
377 | |||
378 | if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) { | ||
379 | if ((my_range->start <= value) && (value <= my_range->end)) { | ||
380 | return false; | ||
381 | } else { | ||
382 | return true; | ||
383 | } | ||
384 | } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) { | ||
385 | if (my_range->start <= value) { | ||
386 | return false; | ||
387 | } else { | ||
388 | return true; | ||
389 | } | ||
390 | } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) { | ||
391 | if (value <= my_range->end) { | ||
392 | return false; | ||
393 | } else { | ||
394 | return true; | ||
395 | } | ||
396 | } else { | ||
397 | return false; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | /* Returns status */ | ||
402 | int | ||
403 | get_status(double value, thresholds *my_thresholds) | ||
404 | { | ||
405 | if (my_thresholds->critical != NULL) { | ||
406 | if (check_range(value, my_thresholds->critical) == TRUE) { | ||
407 | return STATE_CRITICAL; | ||
408 | } | ||
409 | } | ||
410 | if (my_thresholds->warning != NULL) { | ||
411 | if (check_range(value, my_thresholds->warning) == TRUE) { | ||
412 | return STATE_WARNING; | ||
413 | } | ||
414 | } | ||
415 | return STATE_OK; | ||
416 | } | ||
417 | |||
325 | #ifdef NEED_GETTIMEOFDAY | 418 | #ifdef NEED_GETTIMEOFDAY |
326 | int | 419 | int |
327 | gettimeofday (struct timeval *tv, struct timezone *tz) | 420 | gettimeofday (struct timeval *tv, struct timezone *tz) |