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