diff options
Diffstat (limited to 'plugins/utils.c')
-rw-r--r-- | plugins/utils.c | 532 |
1 files changed, 227 insertions, 305 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index aff17906..f11db731 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -1,38 +1,40 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Library of useful functions for plugins | 3 | * Library of useful functions for plugins |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2000 Karl DeBisschop (karl@debisschop.net) | 6 | * Copyright (c) 2000 Karl DeBisschop (karl@debisschop.net) |
7 | * Copyright (c) 2002-2007 Monitoring Plugins Development Team | 7 | * Copyright (c) 2002-2024 Monitoring Plugins Development Team |
8 | * | 8 | * |
9 | * This program is free software: you can redistribute it and/or modify | 9 | * This program is free software: you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by | 10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation, either version 3 of the License, or | 11 | * the Free Software Foundation, either version 3 of the License, or |
12 | * (at your option) any later version. | 12 | * (at your option) any later version. |
13 | * | 13 | * |
14 | * This program is distributed in the hope that it will be useful, | 14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. | 17 | * GNU General Public License for more details. |
18 | * | 18 | * |
19 | * You should have received a copy of the GNU General Public License | 19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | * | 22 | * |
23 | *****************************************************************************/ | 23 | *****************************************************************************/ |
24 | 24 | ||
25 | #include "common.h" | 25 | #include "common.h" |
26 | #include "utils.h" | 26 | #include "./utils.h" |
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> | 30 | #include <string.h> |
31 | #include <errno.h> | 31 | #include <errno.h> |
32 | 32 | ||
33 | #include <stdbool.h> | ||
34 | |||
33 | #include <arpa/inet.h> | 35 | #include <arpa/inet.h> |
34 | 36 | ||
35 | extern void print_usage (void); | 37 | extern void print_usage(void); |
36 | extern const char *progname; | 38 | extern const char *progname; |
37 | 39 | ||
38 | #define STRLEN 64 | 40 | #define STRLEN 64 |
@@ -48,9 +50,7 @@ time_t start_time, end_time; | |||
48 | * Note that numerically the above does not hold | 50 | * Note that numerically the above does not hold |
49 | ****************************************************************************/ | 51 | ****************************************************************************/ |
50 | 52 | ||
51 | int | 53 | int max_state(int a, int b) { |
52 | max_state (int a, int b) | ||
53 | { | ||
54 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) | 54 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) |
55 | return STATE_CRITICAL; | 55 | return STATE_CRITICAL; |
56 | else if (a == STATE_WARNING || b == STATE_WARNING) | 56 | else if (a == STATE_WARNING || b == STATE_WARNING) |
@@ -62,7 +62,7 @@ max_state (int a, int b) | |||
62 | else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) | 62 | else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) |
63 | return STATE_DEPENDENT; | 63 | return STATE_DEPENDENT; |
64 | else | 64 | else |
65 | return max (a, b); | 65 | return max(a, b); |
66 | } | 66 | } |
67 | 67 | ||
68 | /* ************************************************************************** | 68 | /* ************************************************************************** |
@@ -75,9 +75,7 @@ max_state (int a, int b) | |||
75 | * non-OK state. | 75 | * non-OK state. |
76 | ****************************************************************************/ | 76 | ****************************************************************************/ |
77 | 77 | ||
78 | int | 78 | int max_state_alt(int a, int b) { |
79 | max_state_alt (int a, int b) | ||
80 | { | ||
81 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) | 79 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) |
82 | return STATE_CRITICAL; | 80 | return STATE_CRITICAL; |
83 | else if (a == STATE_WARNING || b == STATE_WARNING) | 81 | else if (a == STATE_WARNING || b == STATE_WARNING) |
@@ -89,112 +87,127 @@ max_state_alt (int a, int b) | |||
89 | else if (a == STATE_OK || b == STATE_OK) | 87 | else if (a == STATE_OK || b == STATE_OK) |
90 | return STATE_OK; | 88 | return STATE_OK; |
91 | else | 89 | else |
92 | return max (a, b); | 90 | return max(a, b); |
93 | } | 91 | } |
94 | 92 | ||
95 | void usage (const char *msg) | 93 | void usage(const char *msg) { |
96 | { | 94 | printf("%s\n", msg); |
97 | printf ("%s\n", msg); | 95 | print_usage(); |
98 | print_usage (); | 96 | exit(STATE_UNKNOWN); |
99 | exit (STATE_UNKNOWN); | ||
100 | } | 97 | } |
101 | 98 | ||
102 | void usage_va (const char *fmt, ...) | 99 | void usage_va(const char *fmt, ...) { |
103 | { | ||
104 | va_list ap; | 100 | va_list ap; |
105 | printf("%s: ", progname); | 101 | printf("%s: ", progname); |
106 | va_start(ap, fmt); | 102 | va_start(ap, fmt); |
107 | vprintf(fmt, ap); | 103 | vprintf(fmt, ap); |
108 | va_end(ap); | 104 | va_end(ap); |
109 | printf("\n"); | 105 | printf("\n"); |
110 | exit (STATE_UNKNOWN); | 106 | exit(STATE_UNKNOWN); |
111 | } | 107 | } |
112 | 108 | ||
113 | void usage2(const char *msg, const char *arg) | 109 | void usage2(const char *msg, const char *arg) { |
114 | { | 110 | printf("%s: %s - %s\n", progname, msg, arg ? arg : "(null)"); |
115 | printf ("%s: %s - %s\n", progname, msg, arg?arg:"(null)" ); | 111 | print_usage(); |
116 | print_usage (); | 112 | exit(STATE_UNKNOWN); |
117 | exit (STATE_UNKNOWN); | ||
118 | } | 113 | } |
119 | 114 | ||
120 | void | 115 | void usage3(const char *msg, int arg) { |
121 | usage3 (const char *msg, int arg) | 116 | printf("%s: %s - %c\n", progname, msg, arg); |
122 | { | ||
123 | printf ("%s: %s - %c\n", progname, msg, arg); | ||
124 | print_usage(); | 117 | print_usage(); |
125 | exit (STATE_UNKNOWN); | 118 | exit(STATE_UNKNOWN); |
126 | } | 119 | } |
127 | 120 | ||
128 | void | 121 | void usage4(const char *msg) { |
129 | usage4 (const char *msg) | 122 | printf("%s: %s\n", progname, msg); |
130 | { | ||
131 | printf ("%s: %s\n", progname, msg); | ||
132 | print_usage(); | 123 | print_usage(); |
133 | exit (STATE_UNKNOWN); | 124 | exit(STATE_UNKNOWN); |
134 | } | 125 | } |
135 | 126 | ||
136 | void | 127 | void usage5(void) { |
137 | usage5 (void) | ||
138 | { | ||
139 | print_usage(); | 128 | print_usage(); |
140 | exit (STATE_UNKNOWN); | 129 | exit(STATE_UNKNOWN); |
141 | } | 130 | } |
142 | 131 | ||
143 | void | 132 | void print_revision(const char *command_name, const char *revision) { |
144 | print_revision (const char *command_name, const char *revision) | 133 | printf("%s v%s (%s %s)\n", command_name, revision, PACKAGE, VERSION); |
145 | { | ||
146 | printf ("%s v%s (%s %s)\n", | ||
147 | command_name, revision, PACKAGE, VERSION); | ||
148 | } | 134 | } |
149 | 135 | ||
150 | bool is_numeric (char *number) { | 136 | bool is_numeric(char *number) { |
151 | char tmp[1]; | 137 | char tmp[1]; |
152 | float x; | 138 | float x; |
153 | 139 | ||
154 | if (!number) | 140 | if (!number) |
155 | return false; | 141 | return false; |
156 | else if (sscanf (number, "%f%c", &x, tmp) == 1) | 142 | else if (sscanf(number, "%f%c", &x, tmp) == 1) |
157 | return true; | 143 | return true; |
158 | else | 144 | else |
159 | return false; | 145 | return false; |
160 | } | 146 | } |
161 | 147 | ||
162 | bool is_positive (char *number) { | 148 | bool is_positive(char *number) { |
163 | if (is_numeric (number) && atof (number) > 0.0) | 149 | if (is_numeric(number) && atof(number) > 0.0) |
164 | return true; | 150 | return true; |
165 | else | 151 | else |
166 | return false; | 152 | return false; |
167 | } | 153 | } |
168 | 154 | ||
169 | bool is_negative (char *number) { | 155 | bool is_negative(char *number) { |
170 | if (is_numeric (number) && atof (number) < 0.0) | 156 | if (is_numeric(number) && atof(number) < 0.0) |
171 | return true; | 157 | return true; |
172 | else | 158 | else |
173 | return false; | 159 | return false; |
174 | } | 160 | } |
175 | 161 | ||
176 | bool is_nonnegative (char *number) { | 162 | bool is_nonnegative(char *number) { |
177 | if (is_numeric (number) && atof (number) >= 0.0) | 163 | if (is_numeric(number) && atof(number) >= 0.0) |
178 | return true; | 164 | return true; |
179 | else | 165 | else |
180 | return false; | 166 | return false; |
181 | } | 167 | } |
182 | 168 | ||
183 | bool is_percentage (char *number) { | 169 | bool is_percentage(char *number) { |
184 | int x; | 170 | int x; |
185 | if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) | 171 | if (is_numeric(number) && (x = atof(number)) >= 0 && x <= 100) |
186 | return true; | 172 | return true; |
187 | else | 173 | else |
188 | return false; | 174 | return false; |
189 | } | 175 | } |
190 | 176 | ||
191 | bool is_integer (char *number) { | 177 | bool is_percentage_expression(const char str[]) { |
178 | if (!str) { | ||
179 | return false; | ||
180 | } | ||
181 | |||
182 | size_t len = strlen(str); | ||
183 | |||
184 | if (str[len - 1] != '%') { | ||
185 | return false; | ||
186 | } | ||
187 | |||
188 | char *foo = calloc(sizeof(char), len + 1); | ||
189 | |||
190 | if (!foo) { | ||
191 | die(STATE_UNKNOWN, _("calloc failed \n")); | ||
192 | } | ||
193 | |||
194 | strcpy(foo, str); | ||
195 | foo[len - 1] = '\0'; | ||
196 | |||
197 | bool result = is_numeric(foo); | ||
198 | |||
199 | free(foo); | ||
200 | |||
201 | return result; | ||
202 | } | ||
203 | |||
204 | bool is_integer(char *number) { | ||
192 | long int n; | 205 | long int n; |
193 | 206 | ||
194 | if (!number || (strspn (number, "-0123456789 ") != strlen (number))) | 207 | if (!number || (strspn(number, "-0123456789 ") != strlen(number))) |
195 | return false; | 208 | return false; |
196 | 209 | ||
197 | n = strtol (number, NULL, 10); | 210 | n = strtol(number, NULL, 10); |
198 | 211 | ||
199 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) | 212 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) |
200 | return true; | 213 | return true; |
@@ -202,22 +215,22 @@ bool is_integer (char *number) { | |||
202 | return false; | 215 | return false; |
203 | } | 216 | } |
204 | 217 | ||
205 | bool is_intpos (char *number) { | 218 | bool is_intpos(char *number) { |
206 | if (is_integer (number) && atoi (number) > 0) | 219 | if (is_integer(number) && atoi(number) > 0) |
207 | return true; | 220 | return true; |
208 | else | 221 | else |
209 | return false; | 222 | return false; |
210 | } | 223 | } |
211 | 224 | ||
212 | bool is_intneg (char *number) { | 225 | bool is_intneg(char *number) { |
213 | if (is_integer (number) && atoi (number) < 0) | 226 | if (is_integer(number) && atoi(number) < 0) |
214 | return true; | 227 | return true; |
215 | else | 228 | else |
216 | return false; | 229 | return false; |
217 | } | 230 | } |
218 | 231 | ||
219 | bool is_intnonneg (char *number) { | 232 | bool is_intnonneg(char *number) { |
220 | if (is_integer (number) && atoi (number) >= 0) | 233 | if (is_integer(number) && atoi(number) >= 0) |
221 | return true; | 234 | return true; |
222 | else | 235 | else |
223 | return false; | 236 | return false; |
@@ -230,7 +243,7 @@ bool is_intnonneg (char *number) { | |||
230 | */ | 243 | */ |
231 | bool is_int64(char *number, int64_t *target) { | 244 | bool is_int64(char *number, int64_t *target) { |
232 | errno = 0; | 245 | errno = 0; |
233 | char *endptr = { 0 }; | 246 | char *endptr = {0}; |
234 | 247 | ||
235 | int64_t tmp = strtoll(number, &endptr, 10); | 248 | int64_t tmp = strtoll(number, &endptr, 10); |
236 | if (errno != 0) { | 249 | if (errno != 0) { |
@@ -258,7 +271,7 @@ bool is_int64(char *number, int64_t *target) { | |||
258 | */ | 271 | */ |
259 | bool is_uint64(char *number, uint64_t *target) { | 272 | bool is_uint64(char *number, uint64_t *target) { |
260 | errno = 0; | 273 | errno = 0; |
261 | char *endptr = { 0 }; | 274 | char *endptr = {0}; |
262 | unsigned long long tmp = strtoull(number, &endptr, 10); | 275 | unsigned long long tmp = strtoull(number, &endptr, 10); |
263 | 276 | ||
264 | if (errno != 0) { | 277 | if (errno != 0) { |
@@ -280,66 +293,50 @@ bool is_uint64(char *number, uint64_t *target) { | |||
280 | return true; | 293 | return true; |
281 | } | 294 | } |
282 | 295 | ||
283 | bool is_intpercent (char *number) { | 296 | bool is_intpercent(char *number) { |
284 | int i; | 297 | int i; |
285 | if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) | 298 | if (is_integer(number) && (i = atoi(number)) >= 0 && i <= 100) |
286 | return true; | 299 | return true; |
287 | else | 300 | else |
288 | return false; | 301 | return false; |
289 | } | 302 | } |
290 | 303 | ||
291 | bool is_option (char *str) { | 304 | bool is_option(char *str) { |
292 | if (!str) | 305 | if (!str) |
293 | return false; | 306 | return false; |
294 | else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) | 307 | else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) |
295 | return true; | 308 | return true; |
296 | else | 309 | else |
297 | return false; | 310 | return false; |
298 | } | 311 | } |
299 | 312 | ||
300 | #ifdef NEED_GETTIMEOFDAY | 313 | #ifdef NEED_GETTIMEOFDAY |
301 | int | 314 | int gettimeofday(struct timeval *tv, struct timezone *tz) { |
302 | gettimeofday (struct timeval *tv, struct timezone *tz) | ||
303 | { | ||
304 | tv->tv_usec = 0; | 315 | tv->tv_usec = 0; |
305 | tv->tv_sec = (long) time ((time_t) 0); | 316 | tv->tv_sec = (long)time((time_t)0); |
306 | } | 317 | } |
307 | #endif | 318 | #endif |
308 | 319 | ||
309 | 320 | double delta_time(struct timeval tv) { | |
310 | |||
311 | double | ||
312 | delta_time (struct timeval tv) | ||
313 | { | ||
314 | struct timeval now; | 321 | struct timeval now; |
315 | 322 | ||
316 | gettimeofday (&now, NULL); | 323 | gettimeofday(&now, NULL); |
317 | return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000); | 324 | return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000); |
318 | } | 325 | } |
319 | 326 | ||
320 | 327 | long deltime(struct timeval tv) { | |
321 | |||
322 | long | ||
323 | deltime (struct timeval tv) | ||
324 | { | ||
325 | struct timeval now; | 328 | struct timeval now; |
326 | gettimeofday (&now, NULL); | 329 | gettimeofday(&now, NULL); |
327 | return (now.tv_sec - tv.tv_sec)*1000000 + now.tv_usec - tv.tv_usec; | 330 | return (now.tv_sec - tv.tv_sec) * 1000000 + now.tv_usec - tv.tv_usec; |
328 | } | 331 | } |
329 | 332 | ||
330 | 333 | void strip(char *buffer) { | |
331 | |||
332 | |||
333 | void | ||
334 | strip (char *buffer) | ||
335 | { | ||
336 | size_t x; | 334 | size_t x; |
337 | int i; | 335 | int i; |
338 | 336 | ||
339 | for (x = strlen (buffer); x >= 1; x--) { | 337 | for (x = strlen(buffer); x >= 1; x--) { |
340 | i = x - 1; | 338 | i = x - 1; |
341 | if (buffer[i] == ' ' || | 339 | if (buffer[i] == ' ' || buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t') |
342 | buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t') | ||
343 | buffer[i] = '\0'; | 340 | buffer[i] = '\0'; |
344 | else | 341 | else |
345 | break; | 342 | break; |
@@ -347,7 +344,6 @@ strip (char *buffer) | |||
347 | return; | 344 | return; |
348 | } | 345 | } |
349 | 346 | ||
350 | |||
351 | /****************************************************************************** | 347 | /****************************************************************************** |
352 | * | 348 | * |
353 | * Copies one string to another. Any previously existing data in | 349 | * Copies one string to another. Any previously existing data in |
@@ -360,19 +356,15 @@ strip (char *buffer) | |||
360 | * | 356 | * |
361 | *****************************************************************************/ | 357 | *****************************************************************************/ |
362 | 358 | ||
363 | char * | 359 | char *strscpy(char *dest, const char *src) { |
364 | strscpy (char *dest, const char *src) | ||
365 | { | ||
366 | if (src == NULL) | 360 | if (src == NULL) |
367 | return NULL; | 361 | return NULL; |
368 | 362 | ||
369 | xasprintf (&dest, "%s", src); | 363 | xasprintf(&dest, "%s", src); |
370 | 364 | ||
371 | return dest; | 365 | return dest; |
372 | } | 366 | } |
373 | 367 | ||
374 | |||
375 | |||
376 | /****************************************************************************** | 368 | /****************************************************************************** |
377 | * | 369 | * |
378 | * Returns a pointer to the next line of a multiline string buffer | 370 | * Returns a pointer to the next line of a multiline string buffer |
@@ -389,7 +381,7 @@ strscpy (char *dest, const char *src) | |||
389 | * This | 381 | * This |
390 | * is | 382 | * is |
391 | * a | 383 | * a |
392 | * | 384 | * |
393 | * multiline string buffer | 385 | * multiline string buffer |
394 | * ============================== | 386 | * ============================== |
395 | * | 387 | * |
@@ -402,7 +394,7 @@ strscpy (char *dest, const char *src) | |||
402 | * printf("%d %s",i++,firstword(ptr)); | 394 | * printf("%d %s",i++,firstword(ptr)); |
403 | * ptr = strnl(ptr); | 395 | * ptr = strnl(ptr); |
404 | * } | 396 | * } |
405 | * | 397 | * |
406 | * Produces the following: | 398 | * Produces the following: |
407 | * | 399 | * |
408 | * 1 This | 400 | * 1 This |
@@ -423,25 +415,22 @@ strscpy (char *dest, const char *src) | |||
423 | * | 415 | * |
424 | *****************************************************************************/ | 416 | *****************************************************************************/ |
425 | 417 | ||
426 | char * | 418 | char *strnl(char *str) { |
427 | strnl (char *str) | ||
428 | { | ||
429 | size_t len; | 419 | size_t len; |
430 | if (str == NULL) | 420 | if (str == NULL) |
431 | return NULL; | 421 | return NULL; |
432 | str = strpbrk (str, "\r\n"); | 422 | str = strpbrk(str, "\r\n"); |
433 | if (str == NULL) | 423 | if (str == NULL) |
434 | return NULL; | 424 | return NULL; |
435 | len = strspn (str, "\r\n"); | 425 | len = strspn(str, "\r\n"); |
436 | if (str[len] == '\0') | 426 | if (str[len] == '\0') |
437 | return NULL; | 427 | return NULL; |
438 | str += len; | 428 | str += len; |
439 | if (strlen (str) == 0) | 429 | if (strlen(str) == 0) |
440 | return NULL; | 430 | return NULL; |
441 | return str; | 431 | return str; |
442 | } | 432 | } |
443 | 433 | ||
444 | |||
445 | /****************************************************************************** | 434 | /****************************************************************************** |
446 | * | 435 | * |
447 | * Like strscpy, except only the portion of the source string up to | 436 | * Like strscpy, except only the portion of the source string up to |
@@ -458,29 +447,25 @@ strnl (char *str) | |||
458 | * | 447 | * |
459 | *****************************************************************************/ | 448 | *****************************************************************************/ |
460 | 449 | ||
461 | char * | 450 | char *strpcpy(char *dest, const char *src, const char *str) { |
462 | strpcpy (char *dest, const char *src, const char *str) | ||
463 | { | ||
464 | size_t len; | 451 | size_t len; |
465 | 452 | ||
466 | if (src) | 453 | if (src) |
467 | len = strcspn (src, str); | 454 | len = strcspn(src, str); |
468 | else | 455 | else |
469 | return NULL; | 456 | return NULL; |
470 | 457 | ||
471 | if (dest == NULL || strlen (dest) < len) | 458 | if (dest == NULL || strlen(dest) < len) |
472 | dest = realloc (dest, len + 1); | 459 | dest = realloc(dest, len + 1); |
473 | if (dest == NULL) | 460 | if (dest == NULL) |
474 | die (STATE_UNKNOWN, _("failed realloc in strpcpy\n")); | 461 | die(STATE_UNKNOWN, _("failed realloc in strpcpy\n")); |
475 | 462 | ||
476 | strncpy (dest, src, len); | 463 | strncpy(dest, src, len); |
477 | dest[len] = '\0'; | 464 | dest[len] = '\0'; |
478 | 465 | ||
479 | return dest; | 466 | return dest; |
480 | } | 467 | } |
481 | 468 | ||
482 | |||
483 | |||
484 | /****************************************************************************** | 469 | /****************************************************************************** |
485 | * | 470 | * |
486 | * Like strscat, except only the portion of the source string up to | 471 | * Like strscat, except only the portion of the source string up to |
@@ -489,62 +474,54 @@ strpcpy (char *dest, const char *src, const char *str) | |||
489 | * str = strpcpy(str,"This is a line of text with no trailing newline","x"); | 474 | * str = strpcpy(str,"This is a line of text with no trailing newline","x"); |
490 | * str = strpcat(str,"This is a line of text with no trailing newline","x"); | 475 | * str = strpcat(str,"This is a line of text with no trailing newline","x"); |
491 | * printf("%s\n",str); | 476 | * printf("%s\n",str); |
492 | * | 477 | * |
493 | *This is a line of texThis is a line of tex | 478 | *This is a line of texThis is a line of tex |
494 | * | 479 | * |
495 | *****************************************************************************/ | 480 | *****************************************************************************/ |
496 | 481 | ||
497 | char * | 482 | char *strpcat(char *dest, const char *src, const char *str) { |
498 | strpcat (char *dest, const char *src, const char *str) | ||
499 | { | ||
500 | size_t len, l2; | 483 | size_t len, l2; |
501 | 484 | ||
502 | if (dest) | 485 | if (dest) |
503 | len = strlen (dest); | 486 | len = strlen(dest); |
504 | else | 487 | else |
505 | len = 0; | 488 | len = 0; |
506 | 489 | ||
507 | if (src) { | 490 | if (src) { |
508 | l2 = strcspn (src, str); | 491 | l2 = strcspn(src, str); |
509 | } | 492 | } else { |
510 | else { | ||
511 | return dest; | 493 | return dest; |
512 | } | 494 | } |
513 | 495 | ||
514 | dest = realloc (dest, len + l2 + 1); | 496 | dest = realloc(dest, len + l2 + 1); |
515 | if (dest == NULL) | 497 | if (dest == NULL) |
516 | die (STATE_UNKNOWN, _("failed malloc in strscat\n")); | 498 | die(STATE_UNKNOWN, _("failed malloc in strscat\n")); |
517 | 499 | ||
518 | strncpy (dest + len, src, l2); | 500 | strncpy(dest + len, src, l2); |
519 | dest[len + l2] = '\0'; | 501 | dest[len + l2] = '\0'; |
520 | 502 | ||
521 | return dest; | 503 | return dest; |
522 | } | 504 | } |
523 | 505 | ||
524 | |||
525 | /****************************************************************************** | 506 | /****************************************************************************** |
526 | * | 507 | * |
527 | * asprintf, but die on failure | 508 | * asprintf, but die on failure |
528 | * | 509 | * |
529 | ******************************************************************************/ | 510 | ******************************************************************************/ |
530 | 511 | ||
531 | int | 512 | int xvasprintf(char **strp, const char *fmt, va_list ap) { |
532 | xvasprintf (char **strp, const char *fmt, va_list ap) | 513 | int result = vasprintf(strp, fmt, ap); |
533 | { | ||
534 | int result = vasprintf (strp, fmt, ap); | ||
535 | if (result == -1 || *strp == NULL) | 514 | if (result == -1 || *strp == NULL) |
536 | die (STATE_UNKNOWN, _("failed malloc in xvasprintf\n")); | 515 | die(STATE_UNKNOWN, _("failed malloc in xvasprintf\n")); |
537 | return result; | 516 | return result; |
538 | } | 517 | } |
539 | 518 | ||
540 | int | 519 | int xasprintf(char **strp, const char *fmt, ...) { |
541 | xasprintf (char **strp, const char *fmt, ...) | ||
542 | { | ||
543 | va_list ap; | 520 | va_list ap; |
544 | int result; | 521 | int result; |
545 | va_start (ap, fmt); | 522 | va_start(ap, fmt); |
546 | result = xvasprintf (strp, fmt, ap); | 523 | result = xvasprintf(strp, fmt, ap); |
547 | va_end (ap); | 524 | va_end(ap); |
548 | return result; | 525 | return result; |
549 | } | 526 | } |
550 | 527 | ||
@@ -554,247 +531,192 @@ xasprintf (char **strp, const char *fmt, ...) | |||
554 | * | 531 | * |
555 | ******************************************************************************/ | 532 | ******************************************************************************/ |
556 | 533 | ||
557 | char *perfdata (const char *label, | 534 | char *perfdata(const char *label, long int val, const char *uom, int warnp, long int warn, int critp, long int crit, int minp, |
558 | long int val, | 535 | long int minv, int maxp, long int maxv) { |
559 | const char *uom, | ||
560 | int warnp, | ||
561 | long int warn, | ||
562 | int critp, | ||
563 | long int crit, | ||
564 | int minp, | ||
565 | long int minv, | ||
566 | int maxp, | ||
567 | long int maxv) | ||
568 | { | ||
569 | char *data = NULL; | 536 | char *data = NULL; |
570 | 537 | ||
571 | if (strpbrk (label, "'= ")) | 538 | if (strpbrk(label, "'= ")) |
572 | xasprintf (&data, "'%s'=%ld%s;", label, val, uom); | 539 | xasprintf(&data, "'%s'=%ld%s;", label, val, uom); |
573 | else | 540 | else |
574 | xasprintf (&data, "%s=%ld%s;", label, val, uom); | 541 | xasprintf(&data, "%s=%ld%s;", label, val, uom); |
575 | 542 | ||
576 | if (warnp) | 543 | if (warnp) |
577 | xasprintf (&data, "%s%ld;", data, warn); | 544 | xasprintf(&data, "%s%ld;", data, warn); |
578 | else | 545 | else |
579 | xasprintf (&data, "%s;", data); | 546 | xasprintf(&data, "%s;", data); |
580 | 547 | ||
581 | if (critp) | 548 | if (critp) |
582 | xasprintf (&data, "%s%ld;", data, crit); | 549 | xasprintf(&data, "%s%ld;", data, crit); |
583 | else | 550 | else |
584 | xasprintf (&data, "%s;", data); | 551 | xasprintf(&data, "%s;", data); |
585 | 552 | ||
586 | if (minp) | 553 | if (minp) |
587 | xasprintf (&data, "%s%ld;", data, minv); | 554 | xasprintf(&data, "%s%ld;", data, minv); |
588 | else | 555 | else |
589 | xasprintf (&data, "%s;", data); | 556 | xasprintf(&data, "%s;", data); |
590 | 557 | ||
591 | if (maxp) | 558 | if (maxp) |
592 | xasprintf (&data, "%s%ld", data, maxv); | 559 | xasprintf(&data, "%s%ld", data, maxv); |
593 | 560 | ||
594 | return data; | 561 | return data; |
595 | } | 562 | } |
596 | 563 | ||
597 | 564 | char *perfdata_uint64(const char *label, uint64_t val, const char *uom, int warnp, /* Warning present */ | |
598 | char *perfdata_uint64 (const char *label, | 565 | uint64_t warn, int critp, /* Critical present */ |
599 | uint64_t val, | 566 | uint64_t crit, int minp, /* Minimum present */ |
600 | const char *uom, | 567 | uint64_t minv, int maxp, /* Maximum present */ |
601 | int warnp, /* Warning present */ | 568 | uint64_t maxv) { |
602 | uint64_t warn, | ||
603 | int critp, /* Critical present */ | ||
604 | uint64_t crit, | ||
605 | int minp, /* Minimum present */ | ||
606 | uint64_t minv, | ||
607 | int maxp, /* Maximum present */ | ||
608 | uint64_t maxv) | ||
609 | { | ||
610 | char *data = NULL; | 569 | char *data = NULL; |
611 | 570 | ||
612 | if (strpbrk (label, "'= ")) | 571 | if (strpbrk(label, "'= ")) |
613 | xasprintf (&data, "'%s'=%" PRIu64 "%s;", label, val, uom); | 572 | xasprintf(&data, "'%s'=%" PRIu64 "%s;", label, val, uom); |
614 | else | 573 | else |
615 | xasprintf (&data, "%s=%" PRIu64 "%s;", label, val, uom); | 574 | xasprintf(&data, "%s=%" PRIu64 "%s;", label, val, uom); |
616 | 575 | ||
617 | if (warnp) | 576 | if (warnp) |
618 | xasprintf (&data, "%s%" PRIu64 ";", data, warn); | 577 | xasprintf(&data, "%s%" PRIu64 ";", data, warn); |
619 | else | 578 | else |
620 | xasprintf (&data, "%s;", data); | 579 | xasprintf(&data, "%s;", data); |
621 | 580 | ||
622 | if (critp) | 581 | if (critp) |
623 | xasprintf (&data, "%s%" PRIu64 ";", data, crit); | 582 | xasprintf(&data, "%s%" PRIu64 ";", data, crit); |
624 | else | 583 | else |
625 | xasprintf (&data, "%s;", data); | 584 | xasprintf(&data, "%s;", data); |
626 | 585 | ||
627 | if (minp) | 586 | if (minp) |
628 | xasprintf (&data, "%s%" PRIu64 ";", data, minv); | 587 | xasprintf(&data, "%s%" PRIu64 ";", data, minv); |
629 | else | 588 | else |
630 | xasprintf (&data, "%s;", data); | 589 | xasprintf(&data, "%s;", data); |
631 | 590 | ||
632 | if (maxp) | 591 | if (maxp) |
633 | xasprintf (&data, "%s%" PRIu64, data, maxv); | 592 | xasprintf(&data, "%s%" PRIu64, data, maxv); |
634 | 593 | ||
635 | return data; | 594 | return data; |
636 | } | 595 | } |
637 | 596 | ||
638 | 597 | char *perfdata_int64(const char *label, int64_t val, const char *uom, int warnp, /* Warning present */ | |
639 | char *perfdata_int64 (const char *label, | 598 | int64_t warn, int critp, /* Critical present */ |
640 | int64_t val, | 599 | int64_t crit, int minp, /* Minimum present */ |
641 | const char *uom, | 600 | int64_t minv, int maxp, /* Maximum present */ |
642 | int warnp, /* Warning present */ | 601 | int64_t maxv) { |
643 | int64_t warn, | ||
644 | int critp, /* Critical present */ | ||
645 | int64_t crit, | ||
646 | int minp, /* Minimum present */ | ||
647 | int64_t minv, | ||
648 | int maxp, /* Maximum present */ | ||
649 | int64_t maxv) | ||
650 | { | ||
651 | char *data = NULL; | 602 | char *data = NULL; |
652 | 603 | ||
653 | if (strpbrk (label, "'= ")) | 604 | if (strpbrk(label, "'= ")) |
654 | xasprintf (&data, "'%s'=%" PRId64 "%s;", label, val, uom); | 605 | xasprintf(&data, "'%s'=%" PRId64 "%s;", label, val, uom); |
655 | else | 606 | else |
656 | xasprintf (&data, "%s=%" PRId64 "%s;", label, val, uom); | 607 | xasprintf(&data, "%s=%" PRId64 "%s;", label, val, uom); |
657 | 608 | ||
658 | if (warnp) | 609 | if (warnp) |
659 | xasprintf (&data, "%s%" PRId64 ";", data, warn); | 610 | xasprintf(&data, "%s%" PRId64 ";", data, warn); |
660 | else | 611 | else |
661 | xasprintf (&data, "%s;", data); | 612 | xasprintf(&data, "%s;", data); |
662 | 613 | ||
663 | if (critp) | 614 | if (critp) |
664 | xasprintf (&data, "%s%" PRId64 ";", data, crit); | 615 | xasprintf(&data, "%s%" PRId64 ";", data, crit); |
665 | else | 616 | else |
666 | xasprintf (&data, "%s;", data); | 617 | xasprintf(&data, "%s;", data); |
667 | 618 | ||
668 | if (minp) | 619 | if (minp) |
669 | xasprintf (&data, "%s%" PRId64 ";", data, minv); | 620 | xasprintf(&data, "%s%" PRId64 ";", data, minv); |
670 | else | 621 | else |
671 | xasprintf (&data, "%s;", data); | 622 | xasprintf(&data, "%s;", data); |
672 | 623 | ||
673 | if (maxp) | 624 | if (maxp) |
674 | xasprintf (&data, "%s%" PRId64, data, maxv); | 625 | xasprintf(&data, "%s%" PRId64, data, maxv); |
675 | 626 | ||
676 | return data; | 627 | return data; |
677 | } | 628 | } |
678 | 629 | ||
679 | 630 | char *fperfdata(const char *label, double val, const char *uom, int warnp, double warn, int critp, double crit, int minp, double minv, | |
680 | char *fperfdata (const char *label, | 631 | int maxp, double maxv) { |
681 | double val, | ||
682 | const char *uom, | ||
683 | int warnp, | ||
684 | double warn, | ||
685 | int critp, | ||
686 | double crit, | ||
687 | int minp, | ||
688 | double minv, | ||
689 | int maxp, | ||
690 | double maxv) | ||
691 | { | ||
692 | char *data = NULL; | 632 | char *data = NULL; |
693 | 633 | ||
694 | if (strpbrk (label, "'= ")) | 634 | if (strpbrk(label, "'= ")) |
695 | xasprintf (&data, "'%s'=", label); | 635 | xasprintf(&data, "'%s'=", label); |
696 | else | 636 | else |
697 | xasprintf (&data, "%s=", label); | 637 | xasprintf(&data, "%s=", label); |
698 | 638 | ||
699 | xasprintf (&data, "%s%f", data, val); | 639 | xasprintf(&data, "%s%f", data, val); |
700 | xasprintf (&data, "%s%s;", data, uom); | 640 | xasprintf(&data, "%s%s;", data, uom); |
701 | 641 | ||
702 | if (warnp) | 642 | if (warnp) |
703 | xasprintf (&data, "%s%f", data, warn); | 643 | xasprintf(&data, "%s%f", data, warn); |
704 | 644 | ||
705 | xasprintf (&data, "%s;", data); | 645 | xasprintf(&data, "%s;", data); |
706 | 646 | ||
707 | if (critp) | 647 | if (critp) |
708 | xasprintf (&data, "%s%f", data, crit); | 648 | xasprintf(&data, "%s%f", data, crit); |
709 | 649 | ||
710 | xasprintf (&data, "%s;", data); | 650 | xasprintf(&data, "%s;", data); |
711 | 651 | ||
712 | if (minp) | 652 | if (minp) |
713 | xasprintf (&data, "%s%f", data, minv); | 653 | xasprintf(&data, "%s%f", data, minv); |
714 | 654 | ||
715 | if (maxp) { | 655 | if (maxp) { |
716 | xasprintf (&data, "%s;", data); | 656 | xasprintf(&data, "%s;", data); |
717 | xasprintf (&data, "%s%f", data, maxv); | 657 | xasprintf(&data, "%s%f", data, maxv); |
718 | } | 658 | } |
719 | 659 | ||
720 | return data; | 660 | return data; |
721 | } | 661 | } |
722 | 662 | ||
723 | char *sperfdata (const char *label, | 663 | char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, int minp, double minv, int maxp, double maxv) { |
724 | double val, | ||
725 | const char *uom, | ||
726 | char *warn, | ||
727 | char *crit, | ||
728 | int minp, | ||
729 | double minv, | ||
730 | int maxp, | ||
731 | double maxv) | ||
732 | { | ||
733 | char *data = NULL; | 664 | char *data = NULL; |
734 | if (strpbrk (label, "'= ")) | 665 | if (strpbrk(label, "'= ")) |
735 | xasprintf (&data, "'%s'=", label); | 666 | xasprintf(&data, "'%s'=", label); |
736 | else | 667 | else |
737 | xasprintf (&data, "%s=", label); | 668 | xasprintf(&data, "%s=", label); |
738 | 669 | ||
739 | xasprintf (&data, "%s%f", data, val); | 670 | xasprintf(&data, "%s%f", data, val); |
740 | xasprintf (&data, "%s%s;", data, uom); | 671 | xasprintf(&data, "%s%s;", data, uom); |
741 | 672 | ||
742 | if (warn!=NULL) | 673 | if (warn != NULL) |
743 | xasprintf (&data, "%s%s", data, warn); | 674 | xasprintf(&data, "%s%s", data, warn); |
744 | 675 | ||
745 | xasprintf (&data, "%s;", data); | 676 | xasprintf(&data, "%s;", data); |
746 | 677 | ||
747 | if (crit!=NULL) | 678 | if (crit != NULL) |
748 | xasprintf (&data, "%s%s", data, crit); | 679 | xasprintf(&data, "%s%s", data, crit); |
749 | 680 | ||
750 | xasprintf (&data, "%s;", data); | 681 | xasprintf(&data, "%s;", data); |
751 | 682 | ||
752 | if (minp) | 683 | if (minp) |
753 | xasprintf (&data, "%s%f", data, minv); | 684 | xasprintf(&data, "%s%f", data, minv); |
754 | 685 | ||
755 | if (maxp) { | 686 | if (maxp) { |
756 | xasprintf (&data, "%s;", data); | 687 | xasprintf(&data, "%s;", data); |
757 | xasprintf (&data, "%s%f", data, maxv); | 688 | xasprintf(&data, "%s%f", data, maxv); |
758 | } | 689 | } |
759 | 690 | ||
760 | return data; | 691 | return data; |
761 | } | 692 | } |
762 | 693 | ||
763 | char *sperfdata_int (const char *label, | 694 | char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, int minp, int minv, int maxp, int maxv) { |
764 | int val, | ||
765 | const char *uom, | ||
766 | char *warn, | ||
767 | char *crit, | ||
768 | int minp, | ||
769 | int minv, | ||
770 | int maxp, | ||
771 | int maxv) | ||
772 | { | ||
773 | char *data = NULL; | 695 | char *data = NULL; |
774 | if (strpbrk (label, "'= ")) | 696 | if (strpbrk(label, "'= ")) |
775 | xasprintf (&data, "'%s'=", label); | 697 | xasprintf(&data, "'%s'=", label); |
776 | else | 698 | else |
777 | xasprintf (&data, "%s=", label); | 699 | xasprintf(&data, "%s=", label); |
778 | 700 | ||
779 | xasprintf (&data, "%s%d", data, val); | 701 | xasprintf(&data, "%s%d", data, val); |
780 | xasprintf (&data, "%s%s;", data, uom); | 702 | xasprintf(&data, "%s%s;", data, uom); |
781 | 703 | ||
782 | if (warn!=NULL) | 704 | if (warn != NULL) |
783 | xasprintf (&data, "%s%s", data, warn); | 705 | xasprintf(&data, "%s%s", data, warn); |
784 | 706 | ||
785 | xasprintf (&data, "%s;", data); | 707 | xasprintf(&data, "%s;", data); |
786 | 708 | ||
787 | if (crit!=NULL) | 709 | if (crit != NULL) |
788 | xasprintf (&data, "%s%s", data, crit); | 710 | xasprintf(&data, "%s%s", data, crit); |
789 | 711 | ||
790 | xasprintf (&data, "%s;", data); | 712 | xasprintf(&data, "%s;", data); |
791 | 713 | ||
792 | if (minp) | 714 | if (minp) |
793 | xasprintf (&data, "%s%d", data, minv); | 715 | xasprintf(&data, "%s%d", data, minv); |
794 | 716 | ||
795 | if (maxp) { | 717 | if (maxp) { |
796 | xasprintf (&data, "%s;", data); | 718 | xasprintf(&data, "%s;", data); |
797 | xasprintf (&data, "%s%d", data, maxv); | 719 | xasprintf(&data, "%s%d", data, maxv); |
798 | } | 720 | } |
799 | 721 | ||
800 | return data; | 722 | return data; |