diff options
Diffstat (limited to 'plugins/utils.c')
-rw-r--r-- | plugins/utils.c | 258 |
1 files changed, 224 insertions, 34 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index 58b153d8..17dd5814 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 | ||
@@ -36,9 +38,6 @@ extern const char *progname; | |||
36 | #define STRLEN 64 | 38 | #define STRLEN 64 |
37 | #define TXTBLK 128 | 39 | #define TXTBLK 128 |
38 | 40 | ||
39 | unsigned int timeout_state = STATE_CRITICAL; | ||
40 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; | ||
41 | |||
42 | time_t start_time, end_time; | 41 | time_t start_time, end_time; |
43 | 42 | ||
44 | /* ************************************************************************** | 43 | /* ************************************************************************** |
@@ -144,39 +143,10 @@ usage5 (void) | |||
144 | void | 143 | void |
145 | print_revision (const char *command_name, const char *revision) | 144 | print_revision (const char *command_name, const char *revision) |
146 | { | 145 | { |
147 | char plugin_revision[STRLEN]; | ||
148 | |||
149 | printf ("%s v%s (%s %s)\n", | 146 | printf ("%s v%s (%s %s)\n", |
150 | command_name, revision, PACKAGE, VERSION); | 147 | command_name, revision, PACKAGE, VERSION); |
151 | } | 148 | } |
152 | 149 | ||
153 | const char * | ||
154 | state_text (int result) | ||
155 | { | ||
156 | switch (result) { | ||
157 | case STATE_OK: | ||
158 | return "OK"; | ||
159 | case STATE_WARNING: | ||
160 | return "WARNING"; | ||
161 | case STATE_CRITICAL: | ||
162 | return "CRITICAL"; | ||
163 | case STATE_DEPENDENT: | ||
164 | return "DEPENDENT"; | ||
165 | default: | ||
166 | return "UNKNOWN"; | ||
167 | } | ||
168 | } | ||
169 | |||
170 | void | ||
171 | timeout_alarm_handler (int signo) | ||
172 | { | ||
173 | if (signo == SIGALRM) { | ||
174 | printf (_("%s - Plugin timed out after %d seconds\n"), | ||
175 | state_text(timeout_state), timeout_interval); | ||
176 | exit (timeout_state); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | int | 150 | int |
181 | is_numeric (char *number) | 151 | is_numeric (char *number) |
182 | { | 152 | { |
@@ -271,6 +241,46 @@ is_intnonneg (char *number) | |||
271 | return FALSE; | 241 | return FALSE; |
272 | } | 242 | } |
273 | 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 | |||
274 | int | 284 | int |
275 | is_intpercent (char *number) | 285 | is_intpercent (char *number) |
276 | { | 286 | { |
@@ -579,10 +589,94 @@ char *perfdata (const char *label, | |||
579 | xasprintf (&data, "%s;", data); | 589 | xasprintf (&data, "%s;", data); |
580 | 590 | ||
581 | if (minp) | 591 | if (minp) |
582 | xasprintf (&data, "%s%ld", data, minv); | 592 | xasprintf (&data, "%s%ld;", data, minv); |
593 | else | ||
594 | xasprintf (&data, "%s;", data); | ||
595 | |||
596 | if (maxp) | ||
597 | xasprintf (&data, "%s%ld", data, maxv); | ||
598 | |||
599 | return data; | ||
600 | } | ||
601 | |||
602 | |||
603 | char *perfdata_uint64 (const char *label, | ||
604 | uint64_t val, | ||
605 | const char *uom, | ||
606 | int warnp, /* Warning present */ | ||
607 | uint64_t warn, | ||
608 | int critp, /* Critical present */ | ||
609 | uint64_t crit, | ||
610 | int minp, /* Minimum present */ | ||
611 | uint64_t minv, | ||
612 | int maxp, /* Maximum present */ | ||
613 | uint64_t maxv) | ||
614 | { | ||
615 | char *data = NULL; | ||
616 | |||
617 | if (strpbrk (label, "'= ")) | ||
618 | xasprintf (&data, "'%s'=%ld%s;", label, val, uom); | ||
619 | else | ||
620 | xasprintf (&data, "%s=%ld%s;", label, val, uom); | ||
621 | |||
622 | if (warnp) | ||
623 | xasprintf (&data, "%s%lu;", data, warn); | ||
624 | else | ||
625 | xasprintf (&data, "%s;", data); | ||
626 | |||
627 | if (critp) | ||
628 | xasprintf (&data, "%s%lu;", data, crit); | ||
629 | else | ||
630 | xasprintf (&data, "%s;", data); | ||
631 | |||
632 | if (minp) | ||
633 | xasprintf (&data, "%s%lu;", data, minv); | ||
634 | else | ||
635 | xasprintf (&data, "%s;", data); | ||
583 | 636 | ||
584 | if (maxp) | 637 | if (maxp) |
585 | xasprintf (&data, "%s;%ld", data, maxv); | 638 | xasprintf (&data, "%s%lu", data, maxv); |
639 | |||
640 | return data; | ||
641 | } | ||
642 | |||
643 | |||
644 | char *perfdata_int64 (const char *label, | ||
645 | int64_t val, | ||
646 | const char *uom, | ||
647 | int warnp, /* Warning present */ | ||
648 | int64_t warn, | ||
649 | int critp, /* Critical present */ | ||
650 | int64_t crit, | ||
651 | int minp, /* Minimum present */ | ||
652 | int64_t minv, | ||
653 | int maxp, /* Maximum present */ | ||
654 | int64_t maxv) | ||
655 | { | ||
656 | char *data = NULL; | ||
657 | |||
658 | if (strpbrk (label, "'= ")) | ||
659 | xasprintf (&data, "'%s'=%ld%s;", label, val, uom); | ||
660 | else | ||
661 | xasprintf (&data, "%s=%ld%s;", label, val, uom); | ||
662 | |||
663 | if (warnp) | ||
664 | xasprintf (&data, "%s%ld;", data, warn); | ||
665 | else | ||
666 | xasprintf (&data, "%s;", data); | ||
667 | |||
668 | if (critp) | ||
669 | xasprintf (&data, "%s%ld;", data, crit); | ||
670 | else | ||
671 | xasprintf (&data, "%s;", data); | ||
672 | |||
673 | if (minp) | ||
674 | xasprintf (&data, "%s%ld;", data, minv); | ||
675 | else | ||
676 | xasprintf (&data, "%s;", data); | ||
677 | |||
678 | if (maxp) | ||
679 | xasprintf (&data, "%s%ld", data, maxv); | ||
586 | 680 | ||
587 | return data; | 681 | return data; |
588 | } | 682 | } |
@@ -630,3 +724,99 @@ char *fperfdata (const char *label, | |||
630 | 724 | ||
631 | return data; | 725 | return data; |
632 | } | 726 | } |
727 | |||
728 | char *sperfdata (const char *label, | ||
729 | double val, | ||
730 | const char *uom, | ||
731 | char *warn, | ||
732 | char *crit, | ||
733 | int minp, | ||
734 | double minv, | ||
735 | int maxp, | ||
736 | double maxv) | ||
737 | { | ||
738 | char *data = NULL; | ||
739 | if (strpbrk (label, "'= ")) | ||
740 | xasprintf (&data, "'%s'=", label); | ||
741 | else | ||
742 | xasprintf (&data, "%s=", label); | ||
743 | |||
744 | xasprintf (&data, "%s%f", data, val); | ||
745 | xasprintf (&data, "%s%s;", data, uom); | ||
746 | |||
747 | if (warn!=NULL) | ||
748 | xasprintf (&data, "%s%s", data, warn); | ||
749 | |||
750 | xasprintf (&data, "%s;", data); | ||
751 | |||
752 | if (crit!=NULL) | ||
753 | xasprintf (&data, "%s%s", data, crit); | ||
754 | |||
755 | xasprintf (&data, "%s;", data); | ||
756 | |||
757 | if (minp) | ||
758 | xasprintf (&data, "%s%f", data, minv); | ||
759 | |||
760 | if (maxp) { | ||
761 | xasprintf (&data, "%s;", data); | ||
762 | xasprintf (&data, "%s%f", data, maxv); | ||
763 | } | ||
764 | |||
765 | return data; | ||
766 | } | ||
767 | |||
768 | char *sperfdata_int (const char *label, | ||
769 | int val, | ||
770 | const char *uom, | ||
771 | char *warn, | ||
772 | char *crit, | ||
773 | int minp, | ||
774 | int minv, | ||
775 | int maxp, | ||
776 | int maxv) | ||
777 | { | ||
778 | char *data = NULL; | ||
779 | if (strpbrk (label, "'= ")) | ||
780 | xasprintf (&data, "'%s'=", label); | ||
781 | else | ||
782 | xasprintf (&data, "%s=", label); | ||
783 | |||
784 | xasprintf (&data, "%s%d", data, val); | ||
785 | xasprintf (&data, "%s%s;", data, uom); | ||
786 | |||
787 | if (warn!=NULL) | ||
788 | xasprintf (&data, "%s%s", data, warn); | ||
789 | |||
790 | xasprintf (&data, "%s;", data); | ||
791 | |||
792 | if (crit!=NULL) | ||
793 | xasprintf (&data, "%s%s", data, crit); | ||
794 | |||
795 | xasprintf (&data, "%s;", data); | ||
796 | |||
797 | if (minp) | ||
798 | xasprintf (&data, "%s%d", data, minv); | ||
799 | |||
800 | if (maxp) { | ||
801 | xasprintf (&data, "%s;", data); | ||
802 | xasprintf (&data, "%s%d", data, maxv); | ||
803 | } | ||
804 | |||
805 | return data; | ||
806 | } | ||
807 | |||
808 | int | ||
809 | open_max (void) | ||
810 | { | ||
811 | errno = 0; | ||
812 | if (maxfd > 0) | ||
813 | return(maxfd); | ||
814 | |||
815 | if ((maxfd = sysconf (_SC_OPEN_MAX)) < 0) { | ||
816 | if (errno == 0) | ||
817 | maxfd = DEFAULT_MAXFD; /* it's indeterminate */ | ||
818 | else | ||
819 | die (STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n")); | ||
820 | } | ||
821 | return(maxfd); | ||
822 | } | ||