diff options
Diffstat (limited to 'plugins/utils.c')
-rw-r--r-- | plugins/utils.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index 348ec022..ebdae2e1 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -27,6 +27,8 @@ | |||
27 | #include "utils_base.h" | 27 | #include "utils_base.h" |
28 | #include <stdarg.h> | 28 | #include <stdarg.h> |
29 | #include <limits.h> | 29 | #include <limits.h> |
30 | #include <string.h> | ||
31 | #include <errno.h> | ||
30 | 32 | ||
31 | #include <arpa/inet.h> | 33 | #include <arpa/inet.h> |
32 | 34 | ||
@@ -239,6 +241,46 @@ is_intnonneg (char *number) | |||
239 | return FALSE; | 241 | return FALSE; |
240 | } | 242 | } |
241 | 243 | ||
244 | /* | ||
245 | * Checks whether the number in the string _number_ can be put inside a int64_t | ||
246 | * On success the number will be written to the _target_ address, if _target_ is not set | ||
247 | * to NULL. | ||
248 | */ | ||
249 | int is_int64(char *number, int64_t *target) { | ||
250 | errno = 0; | ||
251 | uint64_t tmp = strtoll(number, NULL, 10); | ||
252 | if (errno != 0) { | ||
253 | return 0; | ||
254 | } | ||
255 | if (tmp < INT64_MIN || tmp > INT64_MAX) { | ||
256 | return 0; | ||
257 | } | ||
258 | if (target != NULL) { | ||
259 | *target = tmp; | ||
260 | } | ||
261 | return 1; | ||
262 | } | ||
263 | |||
264 | /* | ||
265 | * Checks whether the number in the string _number_ can be put inside a uint64_t | ||
266 | * On success the number will be written to the _target_ address, if _target_ is not set | ||
267 | * to NULL. | ||
268 | */ | ||
269 | int is_uint64(char *number, uint64_t *target) { | ||
270 | errno = 0; | ||
271 | uint64_t tmp = strtoll(number, NULL, 10); | ||
272 | if (errno != 0) { | ||
273 | return 0; | ||
274 | } | ||
275 | if (tmp < 0 || tmp > UINT64_MAX) { | ||
276 | return 0; | ||
277 | } | ||
278 | if (target != NULL) { | ||
279 | *target = tmp; | ||
280 | } | ||
281 | return 1; | ||
282 | } | ||
283 | |||
242 | int | 284 | int |
243 | is_intpercent (char *number) | 285 | is_intpercent (char *number) |
244 | { | 286 | { |
@@ -556,6 +598,88 @@ char *perfdata (const char *label, | |||
556 | } | 598 | } |
557 | 599 | ||
558 | 600 | ||
601 | char *perfdata_uint64 (const char *label, | ||
602 | uint64_t val, | ||
603 | const char *uom, | ||
604 | int warnp, /* Warning present */ | ||
605 | uint64_t warn, | ||
606 | int critp, /* Critical present */ | ||
607 | uint64_t crit, | ||
608 | int minp, /* Minimum present */ | ||
609 | uint64_t minv, | ||
610 | int maxp, /* Maximum present */ | ||
611 | uint64_t maxv) | ||
612 | { | ||
613 | char *data = NULL; | ||
614 | |||
615 | if (strpbrk (label, "'= ")) | ||
616 | xasprintf (&data, "'%s'=%ld%s;", label, val, uom); | ||
617 | else | ||
618 | xasprintf (&data, "%s=%ld%s;", label, val, uom); | ||
619 | |||
620 | if (warnp) | ||
621 | xasprintf (&data, "%s%lu;", data, warn); | ||
622 | else | ||
623 | xasprintf (&data, "%s;", data); | ||
624 | |||
625 | if (critp) | ||
626 | xasprintf (&data, "%s%lu;", data, crit); | ||
627 | else | ||
628 | xasprintf (&data, "%s;", data); | ||
629 | |||
630 | if (minp) | ||
631 | xasprintf (&data, "%s%lu;", data, minv); | ||
632 | else | ||
633 | xasprintf (&data, "%s;", data); | ||
634 | |||
635 | if (maxp) | ||
636 | xasprintf (&data, "%s%lu", data, maxv); | ||
637 | |||
638 | return data; | ||
639 | } | ||
640 | |||
641 | |||
642 | char *perfdata_int64 (const char *label, | ||
643 | int64_t val, | ||
644 | const char *uom, | ||
645 | int warnp, /* Warning present */ | ||
646 | int64_t warn, | ||
647 | int critp, /* Critical present */ | ||
648 | int64_t crit, | ||
649 | int minp, /* Minimum present */ | ||
650 | int64_t minv, | ||
651 | int maxp, /* Maximum present */ | ||
652 | int64_t maxv) | ||
653 | { | ||
654 | char *data = NULL; | ||
655 | |||
656 | if (strpbrk (label, "'= ")) | ||
657 | xasprintf (&data, "'%s'=%ld%s;", label, val, uom); | ||
658 | else | ||
659 | xasprintf (&data, "%s=%ld%s;", label, val, uom); | ||
660 | |||
661 | if (warnp) | ||
662 | xasprintf (&data, "%s%ld;", data, warn); | ||
663 | else | ||
664 | xasprintf (&data, "%s;", data); | ||
665 | |||
666 | if (critp) | ||
667 | xasprintf (&data, "%s%ld;", data, crit); | ||
668 | else | ||
669 | xasprintf (&data, "%s;", data); | ||
670 | |||
671 | if (minp) | ||
672 | xasprintf (&data, "%s%ld;", data, minv); | ||
673 | else | ||
674 | xasprintf (&data, "%s;", data); | ||
675 | |||
676 | if (maxp) | ||
677 | xasprintf (&data, "%s%ld", data, maxv); | ||
678 | |||
679 | return data; | ||
680 | } | ||
681 | |||
682 | |||
559 | char *fperfdata (const char *label, | 683 | char *fperfdata (const char *label, |
560 | double val, | 684 | double val, |
561 | const char *uom, | 685 | const char *uom, |