diff options
author | rincewind <rincewind@example.com> | 2021-09-25 23:24:34 +0200 |
---|---|---|
committer | rincewind <rincewind@example.com> | 2021-09-25 23:24:34 +0200 |
commit | 66e245375992c3942dbd5761f8b991e52bf5f9ab (patch) | |
tree | 117025d6b36974a749914efd4d2623ea0fcd9ad5 | |
parent | cfc43a327526d838db5ec81f5594df4a14319786 (diff) | |
download | monitoring-plugins-66e2453.tar.gz |
Introduce new perfdata functions and stuff for using (u)int64_t
-rw-r--r-- | plugins/utils.c | 120 | ||||
-rw-r--r-- | plugins/utils.h | 10 |
2 files changed, 130 insertions, 0 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index 348ec022..011f715d 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 | bool is_int64(char *number, int64_t *target) { | ||
250 | errno = 0; | ||
251 | uint64_t tmp = strtoll(number, NULL, 10); | ||
252 | if (errno != 0) { | ||
253 | return false; | ||
254 | } | ||
255 | if (tmp < INT64_MIN || tmp > INT64_MAX) { | ||
256 | return false; | ||
257 | } | ||
258 | if (target != NULL) { | ||
259 | *target = tmp; | ||
260 | } | ||
261 | return true; | ||
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 | bool is_uint64(char *number, uint64_t *target) { | ||
270 | errno = 0; | ||
271 | uint64_t tmp = strtoll(number, NULL, 10); | ||
272 | if (errno != 0) { | ||
273 | return false; | ||
274 | } | ||
275 | if (tmp < 0 || tmp > UINT64_MAX) { | ||
276 | return false; | ||
277 | } | ||
278 | if (target != NULL) { | ||
279 | *target = tmp; | ||
280 | } | ||
281 | return true; | ||
282 | } | ||
283 | |||
242 | int | 284 | int |
243 | is_intpercent (char *number) | 285 | is_intpercent (char *number) |
244 | { | 286 | { |
@@ -556,6 +598,84 @@ 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, | ||
605 | uint64_t warn, | ||
606 | int critp, | ||
607 | uint64_t crit, | ||
608 | int minp, | ||
609 | uint64_t minv, | ||
610 | int maxp, | ||
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%ld;", data, warn); | ||
622 | else | ||
623 | xasprintf (&data, "%s;", data); | ||
624 | |||
625 | if (critp) | ||
626 | xasprintf (&data, "%s%ld;", data, crit); | ||
627 | else | ||
628 | xasprintf (&data, "%s;", data); | ||
629 | |||
630 | if (minp) | ||
631 | xasprintf (&data, "%s%ld", data, minv); | ||
632 | |||
633 | if (maxp) | ||
634 | xasprintf (&data, "%s;%ld", data, maxv); | ||
635 | |||
636 | return data; | ||
637 | } | ||
638 | |||
639 | |||
640 | char *perfdata_int64 (const char *label, | ||
641 | int64_t val, | ||
642 | const char *uom, | ||
643 | int warnp, | ||
644 | int64_t warn, | ||
645 | int critp, | ||
646 | int64_t crit, | ||
647 | int minp, | ||
648 | int64_t minv, | ||
649 | int maxp, | ||
650 | int64_t maxv) | ||
651 | { | ||
652 | char *data = NULL; | ||
653 | |||
654 | if (strpbrk (label, "'= ")) | ||
655 | xasprintf (&data, "'%s'=%ld%s;", label, val, uom); | ||
656 | else | ||
657 | xasprintf (&data, "%s=%ld%s;", label, val, uom); | ||
658 | |||
659 | if (warnp) | ||
660 | xasprintf (&data, "%s%ld;", data, warn); | ||
661 | else | ||
662 | xasprintf (&data, "%s;", data); | ||
663 | |||
664 | if (critp) | ||
665 | xasprintf (&data, "%s%ld;", data, crit); | ||
666 | else | ||
667 | xasprintf (&data, "%s;", data); | ||
668 | |||
669 | if (minp) | ||
670 | xasprintf (&data, "%s%ld", data, minv); | ||
671 | |||
672 | if (maxp) | ||
673 | xasprintf (&data, "%s;%ld", data, maxv); | ||
674 | |||
675 | return data; | ||
676 | } | ||
677 | |||
678 | |||
559 | char *fperfdata (const char *label, | 679 | char *fperfdata (const char *label, |
560 | double val, | 680 | double val, |
561 | const char *uom, | 681 | const char *uom, |
diff --git a/plugins/utils.h b/plugins/utils.h index 33a20547..91a9c3f9 100644 --- a/plugins/utils.h +++ b/plugins/utils.h | |||
@@ -16,6 +16,9 @@ suite of plugins. */ | |||
16 | /* now some functions etc are being defined in ../lib/utils_base.c */ | 16 | /* now some functions etc are being defined in ../lib/utils_base.c */ |
17 | #include "utils_base.h" | 17 | #include "utils_base.h" |
18 | 18 | ||
19 | |||
20 | #include <stdbool.h> | ||
21 | |||
19 | #ifdef NP_EXTRA_OPTS | 22 | #ifdef NP_EXTRA_OPTS |
20 | /* Include extra-opts functions if compiled in */ | 23 | /* Include extra-opts functions if compiled in */ |
21 | #include "extra_opts.h" | 24 | #include "extra_opts.h" |
@@ -38,6 +41,7 @@ int is_intpos (char *); | |||
38 | int is_intneg (char *); | 41 | int is_intneg (char *); |
39 | int is_intnonneg (char *); | 42 | int is_intnonneg (char *); |
40 | int is_intpercent (char *); | 43 | int is_intpercent (char *); |
44 | bool is_uint64(char *number, uint64_t *target); | ||
41 | 45 | ||
42 | int is_numeric (char *); | 46 | int is_numeric (char *); |
43 | int is_positive (char *); | 47 | int is_positive (char *); |
@@ -88,6 +92,12 @@ void usage_va(const char *fmt, ...) __attribute__((noreturn)); | |||
88 | char *perfdata (const char *, long int, const char *, int, long int, | 92 | char *perfdata (const char *, long int, const char *, int, long int, |
89 | int, long int, int, long int, int, long int); | 93 | int, long int, int, long int, int, long int); |
90 | 94 | ||
95 | char *perfdata_uint64 (const char *, uint64_t , const char *, int, uint64_t, | ||
96 | int, uint64_t, int, uint64_t, int, uint64_t); | ||
97 | |||
98 | char *perfdata_int64 (const char *, int64_t, const char *, int, int64_t, | ||
99 | int, int64_t, int, int64_t, int, int64_t); | ||
100 | |||
91 | char *fperfdata (const char *, double, const char *, int, double, | 101 | char *fperfdata (const char *, double, const char *, int, double, |
92 | int, double, int, double, int, double); | 102 | int, double, int, double, int, double); |
93 | 103 | ||