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