summaryrefslogtreecommitdiffstats
path: root/plugins/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/utils.c')
-rw-r--r--plugins/utils.c175
1 files changed, 143 insertions, 32 deletions
diff --git a/plugins/utils.c b/plugins/utils.c
index 231af92..17dd581 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
39unsigned int timeout_state = STATE_CRITICAL;
40unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT;
41
42time_t start_time, end_time; 41time_t start_time, end_time;
43 42
44/* ************************************************************************** 43/* **************************************************************************
@@ -148,33 +147,6 @@ print_revision (const char *command_name, const char *revision)
148 command_name, revision, PACKAGE, VERSION); 147 command_name, revision, PACKAGE, VERSION);
149} 148}
150 149
151const char *
152state_text (int result)
153{
154 switch (result) {
155 case STATE_OK:
156 return "OK";
157 case STATE_WARNING:
158 return "WARNING";
159 case STATE_CRITICAL:
160 return "CRITICAL";
161 case STATE_DEPENDENT:
162 return "DEPENDENT";
163 default:
164 return "UNKNOWN";
165 }
166}
167
168void
169timeout_alarm_handler (int signo)
170{
171 if (signo == SIGALRM) {
172 printf (_("%s - Plugin timed out after %d seconds\n"),
173 state_text(timeout_state), timeout_interval);
174 exit (timeout_state);
175 }
176}
177
178int 150int
179is_numeric (char *number) 151is_numeric (char *number)
180{ 152{
@@ -269,6 +241,46 @@ is_intnonneg (char *number)
269 return FALSE; 241 return FALSE;
270} 242}
271 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 */
249int 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 */
269int 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
272int 284int
273is_intpercent (char *number) 285is_intpercent (char *number)
274{ 286{
@@ -577,10 +589,94 @@ char *perfdata (const char *label,
577 xasprintf (&data, "%s;", data); 589 xasprintf (&data, "%s;", data);
578 590
579 if (minp) 591 if (minp)
580 xasprintf (&data, "%s%ld", data, minv); 592 xasprintf (&data, "%s%ld;", data, minv);
593 else
594 xasprintf (&data, "%s;", data);
581 595
582 if (maxp) 596 if (maxp)
583 xasprintf (&data, "%s;%ld", data, maxv); 597 xasprintf (&data, "%s%ld", data, maxv);
598
599 return data;
600}
601
602
603char *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);
636
637 if (maxp)
638 xasprintf (&data, "%s%lu", data, maxv);
639
640 return data;
641}
642
643
644char *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);
584 680
585 return data; 681 return data;
586} 682}
@@ -709,3 +805,18 @@ char *sperfdata_int (const char *label,
709 return data; 805 return data;
710} 806}
711 807
808int
809open_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}