diff options
Diffstat (limited to 'plugins/utils.c')
| -rw-r--r-- | plugins/utils.c | 732 |
1 files changed, 316 insertions, 416 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index 77d6a6f9..dc6f5a85 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,181 +34,121 @@ | |||
| 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 |
| 41 | #define TXTBLK 128 | 41 | #define TXTBLK 128 |
| 42 | 42 | ||
| 43 | time_t start_time, end_time; | 43 | |
| 44 | 44 | void usage(const char *msg) { | |
| 45 | /* ************************************************************************** | 45 | printf("%s\n", msg); |
| 46 | * max_state(STATE_x, STATE_y) | 46 | print_usage(); |
| 47 | * compares STATE_x to STATE_y and returns result based on the following | 47 | exit(STATE_UNKNOWN); |
| 48 | * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL | 48 | } |
| 49 | * | 49 | |
| 50 | * Note that numerically the above does not hold | 50 | void usage_va(const char *fmt, ...) { |
| 51 | ****************************************************************************/ | ||
| 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; | 51 | va_list ap; |
| 107 | printf("%s: ", progname); | 52 | printf("%s: ", progname); |
| 108 | va_start(ap, fmt); | 53 | va_start(ap, fmt); |
| 109 | vprintf(fmt, ap); | 54 | vprintf(fmt, ap); |
| 110 | va_end(ap); | 55 | va_end(ap); |
| 111 | printf("\n"); | 56 | printf("\n"); |
| 112 | exit (STATE_UNKNOWN); | 57 | exit(STATE_UNKNOWN); |
| 113 | } | 58 | } |
| 114 | 59 | ||
| 115 | void usage2(const char *msg, const char *arg) | 60 | void usage2(const char *msg, const char *arg) { |
| 116 | { | 61 | printf("%s: %s - %s\n", progname, msg, arg ? arg : "(null)"); |
| 117 | printf ("%s: %s - %s\n", progname, msg, arg?arg:"(null)" ); | 62 | print_usage(); |
| 118 | print_usage (); | 63 | exit(STATE_UNKNOWN); |
| 119 | exit (STATE_UNKNOWN); | ||
| 120 | } | 64 | } |
| 121 | 65 | ||
| 122 | void | 66 | void usage3(const char *msg, int arg) { |
| 123 | usage3 (const char *msg, int arg) | 67 | printf("%s: %s - %c\n", progname, msg, arg); |
| 124 | { | ||
| 125 | printf ("%s: %s - %c\n", progname, msg, arg); | ||
| 126 | print_usage(); | 68 | print_usage(); |
| 127 | exit (STATE_UNKNOWN); | 69 | exit(STATE_UNKNOWN); |
| 128 | } | 70 | } |
| 129 | 71 | ||
| 130 | void | 72 | void usage4(const char *msg) { |
| 131 | usage4 (const char *msg) | 73 | printf("%s: %s\n", progname, msg); |
| 132 | { | ||
| 133 | printf ("%s: %s\n", progname, msg); | ||
| 134 | print_usage(); | 74 | print_usage(); |
| 135 | exit (STATE_UNKNOWN); | 75 | exit(STATE_UNKNOWN); |
| 136 | } | 76 | } |
| 137 | 77 | ||
| 138 | void | 78 | void usage5(void) { |
| 139 | usage5 (void) | ||
| 140 | { | ||
| 141 | print_usage(); | 79 | print_usage(); |
| 142 | exit (STATE_UNKNOWN); | 80 | exit(STATE_UNKNOWN); |
| 143 | } | 81 | } |
| 144 | 82 | ||
| 145 | void | 83 | void print_revision(const char *command_name, const char *revision) { |
| 146 | print_revision (const char *command_name, const char *revision) | 84 | 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 | } | 85 | } |
| 151 | 86 | ||
| 152 | bool is_numeric (char *number) { | 87 | bool is_numeric(char *number) { |
| 153 | char tmp[1]; | 88 | char tmp[1]; |
| 154 | float x; | 89 | float x; |
| 155 | 90 | ||
| 156 | if (!number) | 91 | if (!number) { |
| 157 | return false; | 92 | return false; |
| 158 | else if (sscanf (number, "%f%c", &x, tmp) == 1) | 93 | } else if (sscanf(number, "%f%c", &x, tmp) == 1) { |
| 159 | return true; | 94 | return true; |
| 160 | else | 95 | } else { |
| 161 | return false; | 96 | return false; |
| 97 | } | ||
| 162 | } | 98 | } |
| 163 | 99 | ||
| 164 | bool is_positive (char *number) { | 100 | bool is_positive(char *number) { |
| 165 | if (is_numeric (number) && atof (number) > 0.0) | 101 | if (is_numeric(number) && atof(number) > 0.0) { |
| 166 | return true; | 102 | return true; |
| 167 | else | 103 | } else { |
| 168 | return false; | 104 | return false; |
| 105 | } | ||
| 169 | } | 106 | } |
| 170 | 107 | ||
| 171 | bool is_negative (char *number) { | 108 | bool is_negative(char *number) { |
| 172 | if (is_numeric (number) && atof (number) < 0.0) | 109 | if (is_numeric(number) && atof(number) < 0.0) { |
| 173 | return true; | 110 | return true; |
| 174 | else | 111 | } else { |
| 175 | return false; | 112 | return false; |
| 113 | } | ||
| 176 | } | 114 | } |
| 177 | 115 | ||
| 178 | bool is_nonnegative (char *number) { | 116 | bool is_nonnegative(char *number) { |
| 179 | if (is_numeric (number) && atof (number) >= 0.0) | 117 | if (is_numeric(number) && atof(number) >= 0.0) { |
| 180 | return true; | 118 | return true; |
| 181 | else | 119 | } else { |
| 182 | return false; | 120 | return false; |
| 121 | } | ||
| 183 | } | 122 | } |
| 184 | 123 | ||
| 185 | bool is_percentage (char *number) { | 124 | bool is_percentage(char *number) { |
| 186 | int x; | 125 | int x; |
| 187 | if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) | 126 | if (is_numeric(number) && (x = atof(number)) >= 0 && x <= 100) { |
| 188 | return true; | 127 | return true; |
| 189 | else | 128 | } else { |
| 190 | return false; | 129 | return false; |
| 130 | } | ||
| 191 | } | 131 | } |
| 192 | 132 | ||
| 193 | bool is_percentage_expression (const char str[]) { | 133 | bool is_percentage_expression(const char str[]) { |
| 194 | if (!str) { | 134 | if (!str) { |
| 195 | return false; | 135 | return false; |
| 196 | } | 136 | } |
| 197 | 137 | ||
| 198 | size_t len = strlen(str); | 138 | size_t len = strlen(str); |
| 199 | 139 | ||
| 200 | if (str[len-1] != '%') { | 140 | if (str[len - 1] != '%') { |
| 201 | return false; | 141 | return false; |
| 202 | } | 142 | } |
| 203 | 143 | ||
| 204 | char *foo = calloc(sizeof(char), len + 1); | 144 | char *foo = calloc(len + 1, sizeof(char)); |
| 205 | 145 | ||
| 206 | if (!foo) { | 146 | if (!foo) { |
| 207 | die (STATE_UNKNOWN, _("calloc failed \n")); | 147 | die(STATE_UNKNOWN, _("calloc failed \n")); |
| 208 | } | 148 | } |
| 209 | 149 | ||
| 210 | strcpy(foo, str); | 150 | strcpy(foo, str); |
| 211 | foo[len-1] = '\0'; | 151 | foo[len - 1] = '\0'; |
| 212 | 152 | ||
| 213 | bool result = is_numeric(foo); | 153 | bool result = is_numeric(foo); |
| 214 | 154 | ||
| @@ -217,39 +157,44 @@ bool is_percentage_expression (const char str[]) { | |||
| 217 | return result; | 157 | return result; |
| 218 | } | 158 | } |
| 219 | 159 | ||
| 220 | bool is_integer (char *number) { | 160 | bool is_integer(char *number) { |
| 221 | long int n; | 161 | long int n; |
| 222 | 162 | ||
| 223 | if (!number || (strspn (number, "-0123456789 ") != strlen (number))) | 163 | if (!number || (strspn(number, "-0123456789 ") != strlen(number))) { |
| 224 | return false; | 164 | return false; |
| 165 | } | ||
| 225 | 166 | ||
| 226 | n = strtol (number, NULL, 10); | 167 | n = strtol(number, NULL, 10); |
| 227 | 168 | ||
| 228 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) | 169 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) { |
| 229 | return true; | 170 | return true; |
| 230 | else | 171 | } else { |
| 231 | return false; | 172 | return false; |
| 173 | } | ||
| 232 | } | 174 | } |
| 233 | 175 | ||
| 234 | bool is_intpos (char *number) { | 176 | bool is_intpos(char *number) { |
| 235 | if (is_integer (number) && atoi (number) > 0) | 177 | if (is_integer(number) && atoi(number) > 0) { |
| 236 | return true; | 178 | return true; |
| 237 | else | 179 | } else { |
| 238 | return false; | 180 | return false; |
| 181 | } | ||
| 239 | } | 182 | } |
| 240 | 183 | ||
| 241 | bool is_intneg (char *number) { | 184 | bool is_intneg(char *number) { |
| 242 | if (is_integer (number) && atoi (number) < 0) | 185 | if (is_integer(number) && atoi(number) < 0) { |
| 243 | return true; | 186 | return true; |
| 244 | else | 187 | } else { |
| 245 | return false; | 188 | return false; |
| 189 | } | ||
| 246 | } | 190 | } |
| 247 | 191 | ||
| 248 | bool is_intnonneg (char *number) { | 192 | bool is_intnonneg(char *number) { |
| 249 | if (is_integer (number) && atoi (number) >= 0) | 193 | if (is_integer(number) && atoi(number) >= 0) { |
| 250 | return true; | 194 | return true; |
| 251 | else | 195 | } else { |
| 252 | return false; | 196 | return false; |
| 197 | } | ||
| 253 | } | 198 | } |
| 254 | 199 | ||
| 255 | /* | 200 | /* |
| @@ -259,7 +204,7 @@ bool is_intnonneg (char *number) { | |||
| 259 | */ | 204 | */ |
| 260 | bool is_int64(char *number, int64_t *target) { | 205 | bool is_int64(char *number, int64_t *target) { |
| 261 | errno = 0; | 206 | errno = 0; |
| 262 | char *endptr = { 0 }; | 207 | char *endptr = {0}; |
| 263 | 208 | ||
| 264 | int64_t tmp = strtoll(number, &endptr, 10); | 209 | int64_t tmp = strtoll(number, &endptr, 10); |
| 265 | if (errno != 0) { | 210 | if (errno != 0) { |
| @@ -287,7 +232,7 @@ bool is_int64(char *number, int64_t *target) { | |||
| 287 | */ | 232 | */ |
| 288 | bool is_uint64(char *number, uint64_t *target) { | 233 | bool is_uint64(char *number, uint64_t *target) { |
| 289 | errno = 0; | 234 | errno = 0; |
| 290 | char *endptr = { 0 }; | 235 | char *endptr = {0}; |
| 291 | unsigned long long tmp = strtoull(number, &endptr, 10); | 236 | unsigned long long tmp = strtoull(number, &endptr, 10); |
| 292 | 237 | ||
| 293 | if (errno != 0) { | 238 | if (errno != 0) { |
| @@ -309,74 +254,61 @@ bool is_uint64(char *number, uint64_t *target) { | |||
| 309 | return true; | 254 | return true; |
| 310 | } | 255 | } |
| 311 | 256 | ||
| 312 | bool is_intpercent (char *number) { | 257 | bool is_intpercent(char *number) { |
| 313 | int i; | 258 | int i; |
| 314 | if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) | 259 | if (is_integer(number) && (i = atoi(number)) >= 0 && i <= 100) { |
| 315 | return true; | 260 | return true; |
| 316 | else | 261 | } else { |
| 317 | return false; | 262 | return false; |
| 263 | } | ||
| 318 | } | 264 | } |
| 319 | 265 | ||
| 320 | bool is_option (char *str) { | 266 | bool is_option(char *str) { |
| 321 | if (!str) | 267 | if (!str) { |
| 322 | return false; | 268 | return false; |
| 323 | else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) | 269 | } else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { |
| 324 | return true; | 270 | return true; |
| 325 | else | 271 | } else { |
| 326 | return false; | 272 | return false; |
| 273 | } | ||
| 327 | } | 274 | } |
| 328 | 275 | ||
| 329 | #ifdef NEED_GETTIMEOFDAY | 276 | #ifdef NEED_GETTIMEOFDAY |
| 330 | int | 277 | int gettimeofday(struct timeval *tv, struct timezone *tz) { |
| 331 | gettimeofday (struct timeval *tv, struct timezone *tz) | ||
| 332 | { | ||
| 333 | tv->tv_usec = 0; | 278 | tv->tv_usec = 0; |
| 334 | tv->tv_sec = (long) time ((time_t) 0); | 279 | tv->tv_sec = (long)time((time_t)0); |
| 335 | } | 280 | } |
| 336 | #endif | 281 | #endif |
| 337 | 282 | ||
| 338 | 283 | double delta_time(struct timeval tv) { | |
| 339 | |||
| 340 | double | ||
| 341 | delta_time (struct timeval tv) | ||
| 342 | { | ||
| 343 | struct timeval now; | 284 | struct timeval now; |
| 344 | 285 | ||
| 345 | gettimeofday (&now, NULL); | 286 | gettimeofday(&now, NULL); |
| 346 | return ((double)(now.tv_sec - tv.tv_sec) + (double)(now.tv_usec - tv.tv_usec) / (double)1000000); | 287 | return ((double)(now.tv_sec - tv.tv_sec) + |
| 288 | (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,223 @@ 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, |
| 587 | long int val, | 511 | bool critp, long int crit, bool minp, 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, | |
| 627 | char *perfdata_uint64 (const char *label, | 546 | bool warnp, /* Warning present */ |
| 628 | uint64_t val, | 547 | uint64_t warn, bool critp, /* Critical present */ |
| 629 | const char *uom, | 548 | uint64_t crit, bool minp, /* Minimum present */ |
| 630 | int warnp, /* Warning present */ | 549 | uint64_t minv, bool maxp, /* Maximum present */ |
| 631 | uint64_t warn, | 550 | uint64_t maxv) { |
| 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; | 551 | char *data = NULL; |
| 640 | 552 | ||
| 641 | if (strpbrk (label, "'= ")) | 553 | if (strpbrk(label, "'= ")) { |
| 642 | xasprintf (&data, "'%s'=%" PRIu64 "%s;", label, val, uom); | 554 | xasprintf(&data, "'%s'=%" PRIu64 "%s;", label, val, uom); |
| 643 | else | 555 | } else { |
| 644 | xasprintf (&data, "%s=%" PRIu64 "%s;", label, val, uom); | 556 | xasprintf(&data, "%s=%" PRIu64 "%s;", label, val, uom); |
| 557 | } | ||
| 645 | 558 | ||
| 646 | if (warnp) | 559 | if (warnp) { |
| 647 | xasprintf (&data, "%s%" PRIu64 ";", data, warn); | 560 | xasprintf(&data, "%s%" PRIu64 ";", data, warn); |
| 648 | else | 561 | } else { |
| 649 | xasprintf (&data, "%s;", data); | 562 | xasprintf(&data, "%s;", data); |
| 563 | } | ||
| 650 | 564 | ||
| 651 | if (critp) | 565 | if (critp) { |
| 652 | xasprintf (&data, "%s%" PRIu64 ";", data, crit); | 566 | xasprintf(&data, "%s%" PRIu64 ";", data, crit); |
| 653 | else | 567 | } else { |
| 654 | xasprintf (&data, "%s;", data); | 568 | xasprintf(&data, "%s;", data); |
| 569 | } | ||
| 655 | 570 | ||
| 656 | if (minp) | 571 | if (minp) { |
| 657 | xasprintf (&data, "%s%" PRIu64 ";", data, minv); | 572 | xasprintf(&data, "%s%" PRIu64 ";", data, minv); |
| 658 | else | 573 | } else { |
| 659 | xasprintf (&data, "%s;", data); | 574 | xasprintf(&data, "%s;", data); |
| 575 | } | ||
| 660 | 576 | ||
| 661 | if (maxp) | 577 | if (maxp) { |
| 662 | xasprintf (&data, "%s%" PRIu64, data, maxv); | 578 | xasprintf(&data, "%s%" PRIu64, data, maxv); |
| 579 | } | ||
| 663 | 580 | ||
| 664 | return data; | 581 | return data; |
| 665 | } | 582 | } |
| 666 | 583 | ||
| 667 | 584 | char *perfdata_int64(const char *label, int64_t val, const char *uom, | |
| 668 | char *perfdata_int64 (const char *label, | 585 | bool warnp, /* Warning present */ |
| 669 | int64_t val, | 586 | int64_t warn, bool critp, /* Critical present */ |
| 670 | const char *uom, | 587 | int64_t crit, bool minp, /* Minimum present */ |
| 671 | int warnp, /* Warning present */ | 588 | int64_t minv, bool maxp, /* Maximum present */ |
| 672 | int64_t warn, | 589 | int64_t maxv) { |
| 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; | 590 | char *data = NULL; |
| 681 | 591 | ||
| 682 | if (strpbrk (label, "'= ")) | 592 | if (strpbrk(label, "'= ")) { |
| 683 | xasprintf (&data, "'%s'=%" PRId64 "%s;", label, val, uom); | 593 | xasprintf(&data, "'%s'=%" PRId64 "%s;", label, val, uom); |
| 684 | else | 594 | } else { |
| 685 | xasprintf (&data, "%s=%" PRId64 "%s;", label, val, uom); | 595 | xasprintf(&data, "%s=%" PRId64 "%s;", label, val, uom); |
| 596 | } | ||
| 686 | 597 | ||
| 687 | if (warnp) | 598 | if (warnp) { |
| 688 | xasprintf (&data, "%s%" PRId64 ";", data, warn); | 599 | xasprintf(&data, "%s%" PRId64 ";", data, warn); |
| 689 | else | 600 | } else { |
| 690 | xasprintf (&data, "%s;", data); | 601 | xasprintf(&data, "%s;", data); |
| 602 | } | ||
| 691 | 603 | ||
| 692 | if (critp) | 604 | if (critp) { |
| 693 | xasprintf (&data, "%s%" PRId64 ";", data, crit); | 605 | xasprintf(&data, "%s%" PRId64 ";", data, crit); |
| 694 | else | 606 | } else { |
| 695 | xasprintf (&data, "%s;", data); | 607 | xasprintf(&data, "%s;", data); |
| 608 | } | ||
| 696 | 609 | ||
| 697 | if (minp) | 610 | if (minp) { |
| 698 | xasprintf (&data, "%s%" PRId64 ";", data, minv); | 611 | xasprintf(&data, "%s%" PRId64 ";", data, minv); |
| 699 | else | 612 | } else { |
| 700 | xasprintf (&data, "%s;", data); | 613 | xasprintf(&data, "%s;", data); |
| 614 | } | ||
| 701 | 615 | ||
| 702 | if (maxp) | 616 | if (maxp) { |
| 703 | xasprintf (&data, "%s%" PRId64, data, maxv); | 617 | xasprintf(&data, "%s%" PRId64, data, maxv); |
| 618 | } | ||
| 704 | 619 | ||
| 705 | return data; | 620 | return data; |
| 706 | } | 621 | } |
| 707 | 622 | ||
| 708 | 623 | char *fperfdata(const char *label, double val, const char *uom, bool warnp, double warn, bool critp, | |
| 709 | char *fperfdata (const char *label, | 624 | double crit, bool minp, double minv, 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; | 625 | char *data = NULL; |
| 722 | 626 | ||
| 723 | if (strpbrk (label, "'= ")) | 627 | if (strpbrk(label, "'= ")) { |
| 724 | xasprintf (&data, "'%s'=", label); | 628 | xasprintf(&data, "'%s'=", label); |
| 725 | else | 629 | } else { |
| 726 | xasprintf (&data, "%s=", label); | 630 | xasprintf(&data, "%s=", label); |
| 631 | } | ||
| 727 | 632 | ||
| 728 | xasprintf (&data, "%s%f", data, val); | 633 | xasprintf(&data, "%s%f", data, val); |
| 729 | xasprintf (&data, "%s%s;", data, uom); | 634 | xasprintf(&data, "%s%s;", data, uom); |
| 730 | 635 | ||
| 731 | if (warnp) | 636 | if (warnp) { |
| 732 | xasprintf (&data, "%s%f", data, warn); | 637 | xasprintf(&data, "%s%f", data, warn); |
| 638 | } | ||
| 733 | 639 | ||
| 734 | xasprintf (&data, "%s;", data); | 640 | xasprintf(&data, "%s;", data); |
| 735 | 641 | ||
| 736 | if (critp) | 642 | if (critp) { |
| 737 | xasprintf (&data, "%s%f", data, crit); | 643 | xasprintf(&data, "%s%f", data, crit); |
| 644 | } | ||
| 738 | 645 | ||
| 739 | xasprintf (&data, "%s;", data); | 646 | xasprintf(&data, "%s;", data); |
| 740 | 647 | ||
| 741 | if (minp) | 648 | if (minp) { |
| 742 | xasprintf (&data, "%s%f", data, minv); | 649 | xasprintf(&data, "%s%f", data, minv); |
| 650 | } | ||
| 743 | 651 | ||
| 744 | if (maxp) { | 652 | if (maxp) { |
| 745 | xasprintf (&data, "%s;", data); | 653 | xasprintf(&data, "%s;", data); |
| 746 | xasprintf (&data, "%s%f", data, maxv); | 654 | xasprintf(&data, "%s%f", data, maxv); |
| 747 | } | 655 | } |
| 748 | 656 | ||
| 749 | return data; | 657 | return data; |
| 750 | } | 658 | } |
| 751 | 659 | ||
| 752 | char *sperfdata (const char *label, | 660 | char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, bool minp, |
| 753 | double val, | 661 | double minv, bool maxp, double maxv) { |
| 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; | 662 | char *data = NULL; |
| 763 | if (strpbrk (label, "'= ")) | 663 | if (strpbrk(label, "'= ")) { |
| 764 | xasprintf (&data, "'%s'=", label); | 664 | xasprintf(&data, "'%s'=", label); |
| 765 | else | 665 | } else { |
| 766 | xasprintf (&data, "%s=", label); | 666 | xasprintf(&data, "%s=", label); |
| 667 | } | ||
| 767 | 668 | ||
| 768 | xasprintf (&data, "%s%f", data, val); | 669 | xasprintf(&data, "%s%f", data, val); |
| 769 | xasprintf (&data, "%s%s;", data, uom); | 670 | xasprintf(&data, "%s%s;", data, uom); |
| 770 | 671 | ||
| 771 | if (warn!=NULL) | 672 | if (warn != NULL) { |
| 772 | xasprintf (&data, "%s%s", data, warn); | 673 | xasprintf(&data, "%s%s", data, warn); |
| 674 | } | ||
| 773 | 675 | ||
| 774 | xasprintf (&data, "%s;", data); | 676 | xasprintf(&data, "%s;", data); |
| 775 | 677 | ||
| 776 | if (crit!=NULL) | 678 | if (crit != NULL) { |
| 777 | xasprintf (&data, "%s%s", data, crit); | 679 | xasprintf(&data, "%s%s", data, crit); |
| 680 | } | ||
| 778 | 681 | ||
| 779 | xasprintf (&data, "%s;", data); | 682 | xasprintf(&data, "%s;", data); |
| 780 | 683 | ||
| 781 | if (minp) | 684 | if (minp) { |
| 782 | xasprintf (&data, "%s%f", data, minv); | 685 | xasprintf(&data, "%s%f", data, minv); |
| 686 | } | ||
| 783 | 687 | ||
| 784 | if (maxp) { | 688 | if (maxp) { |
| 785 | xasprintf (&data, "%s;", data); | 689 | xasprintf(&data, "%s;", data); |
| 786 | xasprintf (&data, "%s%f", data, maxv); | 690 | xasprintf(&data, "%s%f", data, maxv); |
| 787 | } | 691 | } |
| 788 | 692 | ||
| 789 | return data; | 693 | return data; |
| 790 | } | 694 | } |
| 791 | 695 | ||
| 792 | char *sperfdata_int (const char *label, | 696 | char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, bool minp, |
| 793 | int val, | 697 | int minv, bool maxp, int maxv) { |
| 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; | 698 | char *data = NULL; |
| 803 | if (strpbrk (label, "'= ")) | 699 | if (strpbrk(label, "'= ")) { |
| 804 | xasprintf (&data, "'%s'=", label); | 700 | xasprintf(&data, "'%s'=", label); |
| 805 | else | 701 | } else { |
| 806 | xasprintf (&data, "%s=", label); | 702 | xasprintf(&data, "%s=", label); |
| 703 | } | ||
| 807 | 704 | ||
| 808 | xasprintf (&data, "%s%d", data, val); | 705 | xasprintf(&data, "%s%d", data, val); |
| 809 | xasprintf (&data, "%s%s;", data, uom); | 706 | xasprintf(&data, "%s%s;", data, uom); |
| 810 | 707 | ||
| 811 | if (warn!=NULL) | 708 | if (warn != NULL) { |
| 812 | xasprintf (&data, "%s%s", data, warn); | 709 | xasprintf(&data, "%s%s", data, warn); |
| 710 | } | ||
| 813 | 711 | ||
| 814 | xasprintf (&data, "%s;", data); | 712 | xasprintf(&data, "%s;", data); |
| 815 | 713 | ||
| 816 | if (crit!=NULL) | 714 | if (crit != NULL) { |
| 817 | xasprintf (&data, "%s%s", data, crit); | 715 | xasprintf(&data, "%s%s", data, crit); |
| 716 | } | ||
| 818 | 717 | ||
| 819 | xasprintf (&data, "%s;", data); | 718 | xasprintf(&data, "%s;", data); |
| 820 | 719 | ||
| 821 | if (minp) | 720 | if (minp) { |
| 822 | xasprintf (&data, "%s%d", data, minv); | 721 | xasprintf(&data, "%s%d", data, minv); |
| 722 | } | ||
| 823 | 723 | ||
| 824 | if (maxp) { | 724 | if (maxp) { |
| 825 | xasprintf (&data, "%s;", data); | 725 | xasprintf(&data, "%s;", data); |
| 826 | xasprintf (&data, "%s%d", data, maxv); | 726 | xasprintf(&data, "%s%d", data, maxv); |
| 827 | } | 727 | } |
| 828 | 728 | ||
| 829 | return data; | 729 | return data; |
