diff options
Diffstat (limited to 'plugins/utils.c')
-rw-r--r-- | plugins/utils.c | 191 |
1 files changed, 1 insertions, 190 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index cb013412..2b3accea 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -16,6 +16,7 @@ | |||
16 | 16 | ||
17 | #include "common.h" | 17 | #include "common.h" |
18 | #include "utils.h" | 18 | #include "utils.h" |
19 | #include "utils_base.h" | ||
19 | #include <stdarg.h> | 20 | #include <stdarg.h> |
20 | #include <limits.h> | 21 | #include <limits.h> |
21 | 22 | ||
@@ -132,16 +133,6 @@ state_text (int result) | |||
132 | } | 133 | } |
133 | 134 | ||
134 | void | 135 | void |
135 | die (int result, const char *fmt, ...) | ||
136 | { | ||
137 | va_list ap; | ||
138 | va_start (ap, fmt); | ||
139 | vprintf (fmt, ap); | ||
140 | va_end (ap); | ||
141 | exit (result); | ||
142 | } | ||
143 | |||
144 | void | ||
145 | timeout_alarm_handler (int signo) | 136 | timeout_alarm_handler (int signo) |
146 | { | 137 | { |
147 | if (signo == SIGALRM) { | 138 | if (signo == SIGALRM) { |
@@ -266,156 +257,6 @@ is_option (char *str) | |||
266 | return FALSE; | 257 | return FALSE; |
267 | } | 258 | } |
268 | 259 | ||
269 | void set_range_start (range *this, double value) { | ||
270 | this->start = value; | ||
271 | this->start_infinity = FALSE; | ||
272 | } | ||
273 | |||
274 | void set_range_end (range *this, double value) { | ||
275 | this->end = value; | ||
276 | this->end_infinity = FALSE; | ||
277 | } | ||
278 | |||
279 | range | ||
280 | *parse_range_string (char *str) { | ||
281 | range *temp_range; | ||
282 | double start; | ||
283 | double end; | ||
284 | char *end_str; | ||
285 | |||
286 | temp_range = (range *) malloc(sizeof(range)); | ||
287 | |||
288 | /* Set defaults */ | ||
289 | temp_range->start = 0; | ||
290 | temp_range->start_infinity = FALSE; | ||
291 | temp_range->end = 0; | ||
292 | temp_range->end_infinity = TRUE; | ||
293 | temp_range->alert_on = OUTSIDE; | ||
294 | |||
295 | if (str[0] == '@') { | ||
296 | temp_range->alert_on = INSIDE; | ||
297 | str++; | ||
298 | } | ||
299 | |||
300 | end_str = index(str, ':'); | ||
301 | if (end_str != NULL) { | ||
302 | if (str[0] == '~') { | ||
303 | temp_range->start_infinity = TRUE; | ||
304 | } else { | ||
305 | start = strtod(str, NULL); /* Will stop at the ':' */ | ||
306 | set_range_start(temp_range, start); | ||
307 | } | ||
308 | end_str++; /* Move past the ':' */ | ||
309 | } else { | ||
310 | end_str = str; | ||
311 | } | ||
312 | end = strtod(end_str, NULL); | ||
313 | if (strcmp(end_str, "") != 0) { | ||
314 | set_range_end(temp_range, end); | ||
315 | } | ||
316 | |||
317 | if (temp_range->start_infinity == TRUE || | ||
318 | temp_range->end_infinity == TRUE || | ||
319 | temp_range->start <= temp_range->end) { | ||
320 | return temp_range; | ||
321 | } | ||
322 | free(temp_range); | ||
323 | return NULL; | ||
324 | } | ||
325 | |||
326 | /* returns 0 if okay, otherwise 1 */ | ||
327 | int | ||
328 | _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) | ||
329 | { | ||
330 | thresholds *temp_thresholds = NULL; | ||
331 | |||
332 | temp_thresholds = malloc(sizeof(temp_thresholds)); | ||
333 | |||
334 | temp_thresholds->warning = NULL; | ||
335 | temp_thresholds->critical = NULL; | ||
336 | |||
337 | if (warn_string != NULL) { | ||
338 | if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) { | ||
339 | return 1; | ||
340 | } | ||
341 | } | ||
342 | if (critical_string != NULL) { | ||
343 | if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) { | ||
344 | return 1; | ||
345 | } | ||
346 | } | ||
347 | |||
348 | if (*my_thresholds != 0) { | ||
349 | /* printf("Freeing here: %d\n", *my_thresholds); */ | ||
350 | free(*my_thresholds); | ||
351 | } | ||
352 | *my_thresholds = temp_thresholds; | ||
353 | |||
354 | return 0; | ||
355 | } | ||
356 | |||
357 | void | ||
358 | set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) | ||
359 | { | ||
360 | if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) { | ||
361 | return; | ||
362 | } else { | ||
363 | usage(_("Range format incorrect")); | ||
364 | } | ||
365 | } | ||
366 | |||
367 | /* Returns TRUE if alert should be raised based on the range */ | ||
368 | int | ||
369 | check_range(double value, range *my_range) | ||
370 | { | ||
371 | int false = FALSE; | ||
372 | int true = TRUE; | ||
373 | |||
374 | if (my_range->alert_on == INSIDE) { | ||
375 | false = TRUE; | ||
376 | true = FALSE; | ||
377 | } | ||
378 | |||
379 | if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) { | ||
380 | if ((my_range->start <= value) && (value <= my_range->end)) { | ||
381 | return false; | ||
382 | } else { | ||
383 | return true; | ||
384 | } | ||
385 | } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) { | ||
386 | if (my_range->start <= value) { | ||
387 | return false; | ||
388 | } else { | ||
389 | return true; | ||
390 | } | ||
391 | } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) { | ||
392 | if (value <= my_range->end) { | ||
393 | return false; | ||
394 | } else { | ||
395 | return true; | ||
396 | } | ||
397 | } else { | ||
398 | return false; | ||
399 | } | ||
400 | } | ||
401 | |||
402 | /* Returns status */ | ||
403 | int | ||
404 | get_status(double value, thresholds *my_thresholds) | ||
405 | { | ||
406 | if (my_thresholds->critical != NULL) { | ||
407 | if (check_range(value, my_thresholds->critical) == TRUE) { | ||
408 | return STATE_CRITICAL; | ||
409 | } | ||
410 | } | ||
411 | if (my_thresholds->warning != NULL) { | ||
412 | if (check_range(value, my_thresholds->warning) == TRUE) { | ||
413 | return STATE_WARNING; | ||
414 | } | ||
415 | } | ||
416 | return STATE_OK; | ||
417 | } | ||
418 | |||
419 | #ifdef NEED_GETTIMEOFDAY | 260 | #ifdef NEED_GETTIMEOFDAY |
420 | int | 261 | int |
421 | gettimeofday (struct timeval *tv, struct timezone *tz) | 262 | gettimeofday (struct timeval *tv, struct timezone *tz) |
@@ -727,33 +568,3 @@ char *fperfdata (const char *label, | |||
727 | 568 | ||
728 | return data; | 569 | return data; |
729 | } | 570 | } |
730 | |||
731 | char *np_escaped_string (const char *string) { | ||
732 | char *data; | ||
733 | int i, j=0; | ||
734 | data = strdup(string); | ||
735 | for (i=0; data[i]; i++) { | ||
736 | if (data[i] == '\\') { | ||
737 | switch(data[++i]) { | ||
738 | case 'n': | ||
739 | data[j++] = '\n'; | ||
740 | break; | ||
741 | case 'r': | ||
742 | data[j++] = '\r'; | ||
743 | break; | ||
744 | case 't': | ||
745 | data[j++] = '\t'; | ||
746 | break; | ||
747 | case '\\': | ||
748 | data[j++] = '\\'; | ||
749 | break; | ||
750 | default: | ||
751 | data[j++] = data[i]; | ||
752 | } | ||
753 | } else { | ||
754 | data[j++] = data[i]; | ||
755 | } | ||
756 | } | ||
757 | data[j] = '\0'; | ||
758 | return data; | ||
759 | } | ||