diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-07 15:57:46 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-07 15:57:46 +0100 |
| commit | 065ed65a870bd973c751940ef8e6f47f62d88d26 (patch) | |
| tree | 81eb677cb53c2e2dff10f75d9e20f404bd4fb84e /plugins | |
| parent | 06fa1036f9e7216aac27107cd7d4c4903fa61ab2 (diff) | |
| download | monitoring-plugins-065ed65a870bd973c751940ef8e6f47f62d88d26.tar.gz | |
Fix types in perfdata functions
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/utils.c | 243 | ||||
| -rw-r--r-- | plugins/utils.h | 141 |
2 files changed, 220 insertions, 164 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index 09649429..34335c89 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
| @@ -89,41 +89,46 @@ bool is_numeric(char *number) { | |||
| 89 | char tmp[1]; | 89 | char tmp[1]; |
| 90 | float x; | 90 | float x; |
| 91 | 91 | ||
| 92 | if (!number) | 92 | if (!number) { |
| 93 | return false; | 93 | return false; |
| 94 | else if (sscanf(number, "%f%c", &x, tmp) == 1) | 94 | } else if (sscanf(number, "%f%c", &x, tmp) == 1) { |
| 95 | return true; | 95 | return true; |
| 96 | else | 96 | } else { |
| 97 | return false; | 97 | return false; |
| 98 | } | ||
| 98 | } | 99 | } |
| 99 | 100 | ||
| 100 | bool is_positive(char *number) { | 101 | bool is_positive(char *number) { |
| 101 | if (is_numeric(number) && atof(number) > 0.0) | 102 | if (is_numeric(number) && atof(number) > 0.0) { |
| 102 | return true; | 103 | return true; |
| 103 | else | 104 | } else { |
| 104 | return false; | 105 | return false; |
| 106 | } | ||
| 105 | } | 107 | } |
| 106 | 108 | ||
| 107 | bool is_negative(char *number) { | 109 | bool is_negative(char *number) { |
| 108 | if (is_numeric(number) && atof(number) < 0.0) | 110 | if (is_numeric(number) && atof(number) < 0.0) { |
| 109 | return true; | 111 | return true; |
| 110 | else | 112 | } else { |
| 111 | return false; | 113 | return false; |
| 114 | } | ||
| 112 | } | 115 | } |
| 113 | 116 | ||
| 114 | bool is_nonnegative(char *number) { | 117 | bool is_nonnegative(char *number) { |
| 115 | if (is_numeric(number) && atof(number) >= 0.0) | 118 | if (is_numeric(number) && atof(number) >= 0.0) { |
| 116 | return true; | 119 | return true; |
| 117 | else | 120 | } else { |
| 118 | return false; | 121 | return false; |
| 122 | } | ||
| 119 | } | 123 | } |
| 120 | 124 | ||
| 121 | bool is_percentage(char *number) { | 125 | bool is_percentage(char *number) { |
| 122 | int x; | 126 | int x; |
| 123 | if (is_numeric(number) && (x = atof(number)) >= 0 && x <= 100) | 127 | if (is_numeric(number) && (x = atof(number)) >= 0 && x <= 100) { |
| 124 | return true; | 128 | return true; |
| 125 | else | 129 | } else { |
| 126 | return false; | 130 | return false; |
| 131 | } | ||
| 127 | } | 132 | } |
| 128 | 133 | ||
| 129 | bool is_percentage_expression(const char str[]) { | 134 | bool is_percentage_expression(const char str[]) { |
| @@ -156,36 +161,41 @@ bool is_percentage_expression(const char str[]) { | |||
| 156 | bool is_integer(char *number) { | 161 | bool is_integer(char *number) { |
| 157 | long int n; | 162 | long int n; |
| 158 | 163 | ||
| 159 | if (!number || (strspn(number, "-0123456789 ") != strlen(number))) | 164 | if (!number || (strspn(number, "-0123456789 ") != strlen(number))) { |
| 160 | return false; | 165 | return false; |
| 166 | } | ||
| 161 | 167 | ||
| 162 | n = strtol(number, NULL, 10); | 168 | n = strtol(number, NULL, 10); |
| 163 | 169 | ||
| 164 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) | 170 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) { |
| 165 | return true; | 171 | return true; |
| 166 | else | 172 | } else { |
| 167 | return false; | 173 | return false; |
| 174 | } | ||
| 168 | } | 175 | } |
| 169 | 176 | ||
| 170 | bool is_intpos(char *number) { | 177 | bool is_intpos(char *number) { |
| 171 | if (is_integer(number) && atoi(number) > 0) | 178 | if (is_integer(number) && atoi(number) > 0) { |
| 172 | return true; | 179 | return true; |
| 173 | else | 180 | } else { |
| 174 | return false; | 181 | return false; |
| 182 | } | ||
| 175 | } | 183 | } |
| 176 | 184 | ||
| 177 | bool is_intneg(char *number) { | 185 | bool is_intneg(char *number) { |
| 178 | if (is_integer(number) && atoi(number) < 0) | 186 | if (is_integer(number) && atoi(number) < 0) { |
| 179 | return true; | 187 | return true; |
| 180 | else | 188 | } else { |
| 181 | return false; | 189 | return false; |
| 190 | } | ||
| 182 | } | 191 | } |
| 183 | 192 | ||
| 184 | bool is_intnonneg(char *number) { | 193 | bool is_intnonneg(char *number) { |
| 185 | if (is_integer(number) && atoi(number) >= 0) | 194 | if (is_integer(number) && atoi(number) >= 0) { |
| 186 | return true; | 195 | return true; |
| 187 | else | 196 | } else { |
| 188 | return false; | 197 | return false; |
| 198 | } | ||
| 189 | } | 199 | } |
| 190 | 200 | ||
| 191 | /* | 201 | /* |
| @@ -247,19 +257,21 @@ bool is_uint64(char *number, uint64_t *target) { | |||
| 247 | 257 | ||
| 248 | bool is_intpercent(char *number) { | 258 | bool is_intpercent(char *number) { |
| 249 | int i; | 259 | int i; |
| 250 | if (is_integer(number) && (i = atoi(number)) >= 0 && i <= 100) | 260 | if (is_integer(number) && (i = atoi(number)) >= 0 && i <= 100) { |
| 251 | return true; | 261 | return true; |
| 252 | else | 262 | } else { |
| 253 | return false; | 263 | return false; |
| 264 | } | ||
| 254 | } | 265 | } |
| 255 | 266 | ||
| 256 | bool is_option(char *str) { | 267 | bool is_option(char *str) { |
| 257 | if (!str) | 268 | if (!str) { |
| 258 | return false; | 269 | return false; |
| 259 | else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) | 270 | } else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { |
| 260 | return true; | 271 | return true; |
| 261 | else | 272 | } else { |
| 262 | return false; | 273 | return false; |
| 274 | } | ||
| 263 | } | 275 | } |
| 264 | 276 | ||
| 265 | #ifdef NEED_GETTIMEOFDAY | 277 | #ifdef NEED_GETTIMEOFDAY |
| @@ -288,10 +300,11 @@ void strip(char *buffer) { | |||
| 288 | 300 | ||
| 289 | for (x = strlen(buffer); x >= 1; x--) { | 301 | for (x = strlen(buffer); x >= 1; x--) { |
| 290 | i = x - 1; | 302 | i = x - 1; |
| 291 | if (buffer[i] == ' ' || buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t') | 303 | if (buffer[i] == ' ' || buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t') { |
| 292 | buffer[i] = '\0'; | 304 | buffer[i] = '\0'; |
| 293 | else | 305 | } else { |
| 294 | break; | 306 | break; |
| 307 | } | ||
| 295 | } | 308 | } |
| 296 | return; | 309 | return; |
| 297 | } | 310 | } |
| @@ -309,8 +322,9 @@ void strip(char *buffer) { | |||
| 309 | *****************************************************************************/ | 322 | *****************************************************************************/ |
| 310 | 323 | ||
| 311 | char *strscpy(char *dest, const char *src) { | 324 | char *strscpy(char *dest, const char *src) { |
| 312 | if (src == NULL) | 325 | if (src == NULL) { |
| 313 | return NULL; | 326 | return NULL; |
| 327 | } | ||
| 314 | 328 | ||
| 315 | xasprintf(&dest, "%s", src); | 329 | xasprintf(&dest, "%s", src); |
| 316 | 330 | ||
| @@ -369,17 +383,21 @@ char *strscpy(char *dest, const char *src) { | |||
| 369 | 383 | ||
| 370 | char *strnl(char *str) { | 384 | char *strnl(char *str) { |
| 371 | size_t len; | 385 | size_t len; |
| 372 | if (str == NULL) | 386 | if (str == NULL) { |
| 373 | return NULL; | 387 | return NULL; |
| 388 | } | ||
| 374 | str = strpbrk(str, "\r\n"); | 389 | str = strpbrk(str, "\r\n"); |
| 375 | if (str == NULL) | 390 | if (str == NULL) { |
| 376 | return NULL; | 391 | return NULL; |
| 392 | } | ||
| 377 | len = strspn(str, "\r\n"); | 393 | len = strspn(str, "\r\n"); |
| 378 | if (str[len] == '\0') | 394 | if (str[len] == '\0') { |
| 379 | return NULL; | 395 | return NULL; |
| 396 | } | ||
| 380 | str += len; | 397 | str += len; |
| 381 | if (strlen(str) == 0) | 398 | if (strlen(str) == 0) { |
| 382 | return NULL; | 399 | return NULL; |
| 400 | } | ||
| 383 | return str; | 401 | return str; |
| 384 | } | 402 | } |
| 385 | 403 | ||
| @@ -402,15 +420,18 @@ char *strnl(char *str) { | |||
| 402 | char *strpcpy(char *dest, const char *src, const char *str) { | 420 | char *strpcpy(char *dest, const char *src, const char *str) { |
| 403 | size_t len; | 421 | size_t len; |
| 404 | 422 | ||
| 405 | if (src) | 423 | if (src) { |
| 406 | len = strcspn(src, str); | 424 | len = strcspn(src, str); |
| 407 | else | 425 | } else { |
| 408 | return NULL; | 426 | return NULL; |
| 427 | } | ||
| 409 | 428 | ||
| 410 | if (dest == NULL || strlen(dest) < len) | 429 | if (dest == NULL || strlen(dest) < len) { |
| 411 | dest = realloc(dest, len + 1); | 430 | dest = realloc(dest, len + 1); |
| 412 | if (dest == NULL) | 431 | } |
| 432 | if (dest == NULL) { | ||
| 413 | die(STATE_UNKNOWN, _("failed realloc in strpcpy\n")); | 433 | die(STATE_UNKNOWN, _("failed realloc in strpcpy\n")); |
| 434 | } | ||
| 414 | 435 | ||
| 415 | strncpy(dest, src, len); | 436 | strncpy(dest, src, len); |
| 416 | dest[len] = '\0'; | 437 | dest[len] = '\0'; |
| @@ -434,10 +455,11 @@ char *strpcpy(char *dest, const char *src, const char *str) { | |||
| 434 | char *strpcat(char *dest, const char *src, const char *str) { | 455 | char *strpcat(char *dest, const char *src, const char *str) { |
| 435 | size_t len, l2; | 456 | size_t len, l2; |
| 436 | 457 | ||
| 437 | if (dest) | 458 | if (dest) { |
| 438 | len = strlen(dest); | 459 | len = strlen(dest); |
| 439 | else | 460 | } else { |
| 440 | len = 0; | 461 | len = 0; |
| 462 | } | ||
| 441 | 463 | ||
| 442 | if (src) { | 464 | if (src) { |
| 443 | l2 = strcspn(src, str); | 465 | l2 = strcspn(src, str); |
| @@ -446,8 +468,9 @@ char *strpcat(char *dest, const char *src, const char *str) { | |||
| 446 | } | 468 | } |
| 447 | 469 | ||
| 448 | dest = realloc(dest, len + l2 + 1); | 470 | dest = realloc(dest, len + l2 + 1); |
| 449 | if (dest == NULL) | 471 | if (dest == NULL) { |
| 450 | die(STATE_UNKNOWN, _("failed malloc in strscat\n")); | 472 | die(STATE_UNKNOWN, _("failed malloc in strscat\n")); |
| 473 | } | ||
| 451 | 474 | ||
| 452 | strncpy(dest + len, src, l2); | 475 | strncpy(dest + len, src, l2); |
| 453 | dest[len + l2] = '\0'; | 476 | dest[len + l2] = '\0'; |
| @@ -463,8 +486,9 @@ char *strpcat(char *dest, const char *src, const char *str) { | |||
| 463 | 486 | ||
| 464 | int xvasprintf(char **strp, const char *fmt, va_list ap) { | 487 | int xvasprintf(char **strp, const char *fmt, va_list ap) { |
| 465 | int result = vasprintf(strp, fmt, ap); | 488 | int result = vasprintf(strp, fmt, ap); |
| 466 | if (result == -1 || *strp == NULL) | 489 | if (result == -1 || *strp == NULL) { |
| 467 | die(STATE_UNKNOWN, _("failed malloc in xvasprintf\n")); | 490 | die(STATE_UNKNOWN, _("failed malloc in xvasprintf\n")); |
| 491 | } | ||
| 468 | return result; | 492 | return result; |
| 469 | } | 493 | } |
| 470 | 494 | ||
| @@ -483,126 +507,145 @@ int xasprintf(char **strp, const char *fmt, ...) { | |||
| 483 | * | 507 | * |
| 484 | ******************************************************************************/ | 508 | ******************************************************************************/ |
| 485 | 509 | ||
| 486 | char *perfdata(const char *label, long int val, const char *uom, int warnp, long int warn, int critp, long int crit, int minp, | 510 | char *perfdata(const char *label, long int val, const char *uom, bool warnp, long int warn, bool critp, long int crit, bool minp, |
| 487 | long int minv, int maxp, long int maxv) { | 511 | long int minv, bool maxp, long int maxv) { |
| 488 | char *data = NULL; | 512 | char *data = NULL; |
| 489 | 513 | ||
| 490 | if (strpbrk(label, "'= ")) | 514 | if (strpbrk(label, "'= ")) { |
| 491 | xasprintf(&data, "'%s'=%ld%s;", label, val, uom); | 515 | xasprintf(&data, "'%s'=%ld%s;", label, val, uom); |
| 492 | else | 516 | } else { |
| 493 | xasprintf(&data, "%s=%ld%s;", label, val, uom); | 517 | xasprintf(&data, "%s=%ld%s;", label, val, uom); |
| 518 | } | ||
| 494 | 519 | ||
| 495 | if (warnp) | 520 | if (warnp) { |
| 496 | xasprintf(&data, "%s%ld;", data, warn); | 521 | xasprintf(&data, "%s%ld;", data, warn); |
| 497 | else | 522 | } else { |
| 498 | xasprintf(&data, "%s;", data); | 523 | xasprintf(&data, "%s;", data); |
| 524 | } | ||
| 499 | 525 | ||
| 500 | if (critp) | 526 | if (critp) { |
| 501 | xasprintf(&data, "%s%ld;", data, crit); | 527 | xasprintf(&data, "%s%ld;", data, crit); |
| 502 | else | 528 | } else { |
| 503 | xasprintf(&data, "%s;", data); | 529 | xasprintf(&data, "%s;", data); |
| 530 | } | ||
| 504 | 531 | ||
| 505 | if (minp) | 532 | if (minp) { |
| 506 | xasprintf(&data, "%s%ld;", data, minv); | 533 | xasprintf(&data, "%s%ld;", data, minv); |
| 507 | else | 534 | } else { |
| 508 | xasprintf(&data, "%s;", data); | 535 | xasprintf(&data, "%s;", data); |
| 536 | } | ||
| 509 | 537 | ||
| 510 | if (maxp) | 538 | if (maxp) { |
| 511 | xasprintf(&data, "%s%ld", data, maxv); | 539 | xasprintf(&data, "%s%ld", data, maxv); |
| 540 | } | ||
| 512 | 541 | ||
| 513 | return data; | 542 | return data; |
| 514 | } | 543 | } |
| 515 | 544 | ||
| 516 | char *perfdata_uint64(const char *label, uint64_t val, const char *uom, int warnp, /* Warning present */ | 545 | char *perfdata_uint64(const char *label, uint64_t val, const char *uom, bool warnp, /* Warning present */ |
| 517 | uint64_t warn, int critp, /* Critical present */ | 546 | uint64_t warn, bool critp, /* Critical present */ |
| 518 | uint64_t crit, int minp, /* Minimum present */ | 547 | uint64_t crit, bool minp, /* Minimum present */ |
| 519 | uint64_t minv, int maxp, /* Maximum present */ | 548 | uint64_t minv, bool maxp, /* Maximum present */ |
| 520 | uint64_t maxv) { | 549 | uint64_t maxv) { |
| 521 | char *data = NULL; | 550 | char *data = NULL; |
| 522 | 551 | ||
| 523 | if (strpbrk(label, "'= ")) | 552 | if (strpbrk(label, "'= ")) { |
| 524 | xasprintf(&data, "'%s'=%" PRIu64 "%s;", label, val, uom); | 553 | xasprintf(&data, "'%s'=%" PRIu64 "%s;", label, val, uom); |
| 525 | else | 554 | } else { |
| 526 | xasprintf(&data, "%s=%" PRIu64 "%s;", label, val, uom); | 555 | xasprintf(&data, "%s=%" PRIu64 "%s;", label, val, uom); |
| 556 | } | ||
| 527 | 557 | ||
| 528 | if (warnp) | 558 | if (warnp) { |
| 529 | xasprintf(&data, "%s%" PRIu64 ";", data, warn); | 559 | xasprintf(&data, "%s%" PRIu64 ";", data, warn); |
| 530 | else | 560 | } else { |
| 531 | xasprintf(&data, "%s;", data); | 561 | xasprintf(&data, "%s;", data); |
| 562 | } | ||
| 532 | 563 | ||
| 533 | if (critp) | 564 | if (critp) { |
| 534 | xasprintf(&data, "%s%" PRIu64 ";", data, crit); | 565 | xasprintf(&data, "%s%" PRIu64 ";", data, crit); |
| 535 | else | 566 | } else { |
| 536 | xasprintf(&data, "%s;", data); | 567 | xasprintf(&data, "%s;", data); |
| 568 | } | ||
| 537 | 569 | ||
| 538 | if (minp) | 570 | if (minp) { |
| 539 | xasprintf(&data, "%s%" PRIu64 ";", data, minv); | 571 | xasprintf(&data, "%s%" PRIu64 ";", data, minv); |
| 540 | else | 572 | } else { |
| 541 | xasprintf(&data, "%s;", data); | 573 | xasprintf(&data, "%s;", data); |
| 574 | } | ||
| 542 | 575 | ||
| 543 | if (maxp) | 576 | if (maxp) { |
| 544 | xasprintf(&data, "%s%" PRIu64, data, maxv); | 577 | xasprintf(&data, "%s%" PRIu64, data, maxv); |
| 578 | } | ||
| 545 | 579 | ||
| 546 | return data; | 580 | return data; |
| 547 | } | 581 | } |
| 548 | 582 | ||
| 549 | char *perfdata_int64(const char *label, int64_t val, const char *uom, int warnp, /* Warning present */ | 583 | char *perfdata_int64(const char *label, int64_t val, const char *uom, bool warnp, /* Warning present */ |
| 550 | int64_t warn, int critp, /* Critical present */ | 584 | int64_t warn, bool critp, /* Critical present */ |
| 551 | int64_t crit, int minp, /* Minimum present */ | 585 | int64_t crit, bool minp, /* Minimum present */ |
| 552 | int64_t minv, int maxp, /* Maximum present */ | 586 | int64_t minv, bool maxp, /* Maximum present */ |
| 553 | int64_t maxv) { | 587 | int64_t maxv) { |
| 554 | char *data = NULL; | 588 | char *data = NULL; |
| 555 | 589 | ||
| 556 | if (strpbrk(label, "'= ")) | 590 | if (strpbrk(label, "'= ")) { |
| 557 | xasprintf(&data, "'%s'=%" PRId64 "%s;", label, val, uom); | 591 | xasprintf(&data, "'%s'=%" PRId64 "%s;", label, val, uom); |
| 558 | else | 592 | } else { |
| 559 | xasprintf(&data, "%s=%" PRId64 "%s;", label, val, uom); | 593 | xasprintf(&data, "%s=%" PRId64 "%s;", label, val, uom); |
| 594 | } | ||
| 560 | 595 | ||
| 561 | if (warnp) | 596 | if (warnp) { |
| 562 | xasprintf(&data, "%s%" PRId64 ";", data, warn); | 597 | xasprintf(&data, "%s%" PRId64 ";", data, warn); |
| 563 | else | 598 | } else { |
| 564 | xasprintf(&data, "%s;", data); | 599 | xasprintf(&data, "%s;", data); |
| 600 | } | ||
| 565 | 601 | ||
| 566 | if (critp) | 602 | if (critp) { |
| 567 | xasprintf(&data, "%s%" PRId64 ";", data, crit); | 603 | xasprintf(&data, "%s%" PRId64 ";", data, crit); |
| 568 | else | 604 | } else { |
| 569 | xasprintf(&data, "%s;", data); | 605 | xasprintf(&data, "%s;", data); |
| 606 | } | ||
| 570 | 607 | ||
| 571 | if (minp) | 608 | if (minp) { |
| 572 | xasprintf(&data, "%s%" PRId64 ";", data, minv); | 609 | xasprintf(&data, "%s%" PRId64 ";", data, minv); |
| 573 | else | 610 | } else { |
| 574 | xasprintf(&data, "%s;", data); | 611 | xasprintf(&data, "%s;", data); |
| 612 | } | ||
| 575 | 613 | ||
| 576 | if (maxp) | 614 | if (maxp) { |
| 577 | xasprintf(&data, "%s%" PRId64, data, maxv); | 615 | xasprintf(&data, "%s%" PRId64, data, maxv); |
| 616 | } | ||
| 578 | 617 | ||
| 579 | return data; | 618 | return data; |
| 580 | } | 619 | } |
| 581 | 620 | ||
| 582 | char *fperfdata(const char *label, double val, const char *uom, int warnp, double warn, int critp, double crit, int minp, double minv, | 621 | char *fperfdata(const char *label, double val, const char *uom, bool warnp, double warn, bool critp, double crit, bool minp, double minv, |
| 583 | int maxp, double maxv) { | 622 | bool maxp, double maxv) { |
| 584 | char *data = NULL; | 623 | char *data = NULL; |
| 585 | 624 | ||
| 586 | if (strpbrk(label, "'= ")) | 625 | if (strpbrk(label, "'= ")) { |
| 587 | xasprintf(&data, "'%s'=", label); | 626 | xasprintf(&data, "'%s'=", label); |
| 588 | else | 627 | } else { |
| 589 | xasprintf(&data, "%s=", label); | 628 | xasprintf(&data, "%s=", label); |
| 629 | } | ||
| 590 | 630 | ||
| 591 | xasprintf(&data, "%s%f", data, val); | 631 | xasprintf(&data, "%s%f", data, val); |
| 592 | xasprintf(&data, "%s%s;", data, uom); | 632 | xasprintf(&data, "%s%s;", data, uom); |
| 593 | 633 | ||
| 594 | if (warnp) | 634 | if (warnp) { |
| 595 | xasprintf(&data, "%s%f", data, warn); | 635 | xasprintf(&data, "%s%f", data, warn); |
| 636 | } | ||
| 596 | 637 | ||
| 597 | xasprintf(&data, "%s;", data); | 638 | xasprintf(&data, "%s;", data); |
| 598 | 639 | ||
| 599 | if (critp) | 640 | if (critp) { |
| 600 | xasprintf(&data, "%s%f", data, crit); | 641 | xasprintf(&data, "%s%f", data, crit); |
| 642 | } | ||
| 601 | 643 | ||
| 602 | xasprintf(&data, "%s;", data); | 644 | xasprintf(&data, "%s;", data); |
| 603 | 645 | ||
| 604 | if (minp) | 646 | if (minp) { |
| 605 | xasprintf(&data, "%s%f", data, minv); | 647 | xasprintf(&data, "%s%f", data, minv); |
| 648 | } | ||
| 606 | 649 | ||
| 607 | if (maxp) { | 650 | if (maxp) { |
| 608 | xasprintf(&data, "%s;", data); | 651 | xasprintf(&data, "%s;", data); |
| @@ -612,28 +655,32 @@ char *fperfdata(const char *label, double val, const char *uom, int warnp, doubl | |||
| 612 | return data; | 655 | return data; |
| 613 | } | 656 | } |
| 614 | 657 | ||
| 615 | char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, int minp, double minv, int maxp, double maxv) { | 658 | char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, bool minp, double minv, bool maxp, double maxv) { |
| 616 | char *data = NULL; | 659 | char *data = NULL; |
| 617 | if (strpbrk(label, "'= ")) | 660 | if (strpbrk(label, "'= ")) { |
| 618 | xasprintf(&data, "'%s'=", label); | 661 | xasprintf(&data, "'%s'=", label); |
| 619 | else | 662 | } else { |
| 620 | xasprintf(&data, "%s=", label); | 663 | xasprintf(&data, "%s=", label); |
| 664 | } | ||
| 621 | 665 | ||
| 622 | xasprintf(&data, "%s%f", data, val); | 666 | xasprintf(&data, "%s%f", data, val); |
| 623 | xasprintf(&data, "%s%s;", data, uom); | 667 | xasprintf(&data, "%s%s;", data, uom); |
| 624 | 668 | ||
| 625 | if (warn != NULL) | 669 | if (warn != NULL) { |
| 626 | xasprintf(&data, "%s%s", data, warn); | 670 | xasprintf(&data, "%s%s", data, warn); |
| 671 | } | ||
| 627 | 672 | ||
| 628 | xasprintf(&data, "%s;", data); | 673 | xasprintf(&data, "%s;", data); |
| 629 | 674 | ||
| 630 | if (crit != NULL) | 675 | if (crit != NULL) { |
| 631 | xasprintf(&data, "%s%s", data, crit); | 676 | xasprintf(&data, "%s%s", data, crit); |
| 677 | } | ||
| 632 | 678 | ||
| 633 | xasprintf(&data, "%s;", data); | 679 | xasprintf(&data, "%s;", data); |
| 634 | 680 | ||
| 635 | if (minp) | 681 | if (minp) { |
| 636 | xasprintf(&data, "%s%f", data, minv); | 682 | xasprintf(&data, "%s%f", data, minv); |
| 683 | } | ||
| 637 | 684 | ||
| 638 | if (maxp) { | 685 | if (maxp) { |
| 639 | xasprintf(&data, "%s;", data); | 686 | xasprintf(&data, "%s;", data); |
| @@ -643,28 +690,32 @@ char *sperfdata(const char *label, double val, const char *uom, char *warn, char | |||
| 643 | return data; | 690 | return data; |
| 644 | } | 691 | } |
| 645 | 692 | ||
| 646 | char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, int minp, int minv, int maxp, int maxv) { | 693 | char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, bool minp, int minv, bool maxp, int maxv) { |
| 647 | char *data = NULL; | 694 | char *data = NULL; |
| 648 | if (strpbrk(label, "'= ")) | 695 | if (strpbrk(label, "'= ")) { |
| 649 | xasprintf(&data, "'%s'=", label); | 696 | xasprintf(&data, "'%s'=", label); |
| 650 | else | 697 | } else { |
| 651 | xasprintf(&data, "%s=", label); | 698 | xasprintf(&data, "%s=", label); |
| 699 | } | ||
| 652 | 700 | ||
| 653 | xasprintf(&data, "%s%d", data, val); | 701 | xasprintf(&data, "%s%d", data, val); |
| 654 | xasprintf(&data, "%s%s;", data, uom); | 702 | xasprintf(&data, "%s%s;", data, uom); |
| 655 | 703 | ||
| 656 | if (warn != NULL) | 704 | if (warn != NULL) { |
| 657 | xasprintf(&data, "%s%s", data, warn); | 705 | xasprintf(&data, "%s%s", data, warn); |
| 706 | } | ||
| 658 | 707 | ||
| 659 | xasprintf(&data, "%s;", data); | 708 | xasprintf(&data, "%s;", data); |
| 660 | 709 | ||
| 661 | if (crit != NULL) | 710 | if (crit != NULL) { |
| 662 | xasprintf(&data, "%s%s", data, crit); | 711 | xasprintf(&data, "%s%s", data, crit); |
| 712 | } | ||
| 663 | 713 | ||
| 664 | xasprintf(&data, "%s;", data); | 714 | xasprintf(&data, "%s;", data); |
| 665 | 715 | ||
| 666 | if (minp) | 716 | if (minp) { |
| 667 | xasprintf(&data, "%s%d", data, minv); | 717 | xasprintf(&data, "%s%d", data, minv); |
| 718 | } | ||
| 668 | 719 | ||
| 669 | if (maxp) { | 720 | if (maxp) { |
| 670 | xasprintf(&data, "%s;", data); | 721 | xasprintf(&data, "%s;", data); |
diff --git a/plugins/utils.h b/plugins/utils.h index 029ae5a6..92a6c115 100644 --- a/plugins/utils.h +++ b/plugins/utils.h | |||
| @@ -21,43 +21,43 @@ suite of plugins. */ | |||
| 21 | 21 | ||
| 22 | #ifdef NP_EXTRA_OPTS | 22 | #ifdef NP_EXTRA_OPTS |
| 23 | /* Include extra-opts functions if compiled in */ | 23 | /* Include extra-opts functions if compiled in */ |
| 24 | #include "extra_opts.h" | 24 | # include "extra_opts.h" |
| 25 | #else | 25 | #else |
| 26 | /* else, fake np_extra_opts */ | 26 | /* else, fake np_extra_opts */ |
| 27 | #define np_extra_opts(acptr,av,pr) av | 27 | # define np_extra_opts(acptr, av, pr) av |
| 28 | #endif | 28 | #endif |
| 29 | 29 | ||
| 30 | /* Standardize version information, termination */ | 30 | /* Standardize version information, termination */ |
| 31 | 31 | ||
| 32 | void support (void); | 32 | void support(void); |
| 33 | void print_revision (const char *, const char *); | 33 | void print_revision(const char *, const char *); |
| 34 | 34 | ||
| 35 | extern time_t start_time, end_time; | 35 | extern time_t start_time, end_time; |
| 36 | 36 | ||
| 37 | /* Test input types */ | 37 | /* Test input types */ |
| 38 | 38 | ||
| 39 | bool is_integer (char *); | 39 | bool is_integer(char *); |
| 40 | bool is_intpos (char *); | 40 | bool is_intpos(char *); |
| 41 | bool is_intneg (char *); | 41 | bool is_intneg(char *); |
| 42 | bool is_intnonneg (char *); | 42 | bool is_intnonneg(char *); |
| 43 | bool is_intpercent (char *); | 43 | bool is_intpercent(char *); |
| 44 | bool is_uint64(char *number, uint64_t *target); | 44 | bool is_uint64(char *number, uint64_t *target); |
| 45 | bool is_int64(char *number, int64_t *target); | 45 | bool is_int64(char *number, int64_t *target); |
| 46 | 46 | ||
| 47 | bool is_numeric (char *); | 47 | bool is_numeric(char *); |
| 48 | bool is_positive (char *); | 48 | bool is_positive(char *); |
| 49 | bool is_negative (char *); | 49 | bool is_negative(char *); |
| 50 | bool is_nonnegative (char *); | 50 | bool is_nonnegative(char *); |
| 51 | bool is_percentage (char *); | 51 | bool is_percentage(char *); |
| 52 | bool is_percentage_expression (const char[]); | 52 | bool is_percentage_expression(const char[]); |
| 53 | 53 | ||
| 54 | bool is_option (char *); | 54 | bool is_option(char *); |
| 55 | 55 | ||
| 56 | /* Generalized timer that will do milliseconds if available */ | 56 | /* Generalized timer that will do milliseconds if available */ |
| 57 | #ifndef HAVE_STRUCT_TIMEVAL | 57 | #ifndef HAVE_STRUCT_TIMEVAL |
| 58 | struct timeval { | 58 | struct timeval { |
| 59 | long tv_sec; /* seconds */ | 59 | long tv_sec; /* seconds */ |
| 60 | long tv_usec; /* microseconds */ | 60 | long tv_usec; /* microseconds */ |
| 61 | }; | 61 | }; |
| 62 | #endif | 62 | #endif |
| 63 | 63 | ||
| @@ -65,137 +65,142 @@ struct timeval { | |||
| 65 | int gettimeofday(struct timeval *, struct timezone *); | 65 | int gettimeofday(struct timeval *, struct timezone *); |
| 66 | #endif | 66 | #endif |
| 67 | 67 | ||
| 68 | double delta_time (struct timeval tv); | 68 | double delta_time(struct timeval tv); |
| 69 | long deltime (struct timeval tv); | 69 | long deltime(struct timeval tv); |
| 70 | 70 | ||
| 71 | /* Handle strings safely */ | 71 | /* Handle strings safely */ |
| 72 | 72 | ||
| 73 | void strip (char *); | 73 | void strip(char *); |
| 74 | char *strscpy (char *, const char *); | 74 | char *strscpy(char *, const char *); |
| 75 | char *strnl (char *); | 75 | char *strnl(char *); |
| 76 | char *strpcpy (char *, const char *, const char *); | 76 | char *strpcpy(char *, const char *, const char *); |
| 77 | char *strpcat (char *, const char *, const char *); | 77 | char *strpcat(char *, const char *, const char *); |
| 78 | int xvasprintf (char **strp, const char *fmt, va_list ap); | 78 | int xvasprintf(char **strp, const char *fmt, va_list ap); |
| 79 | int xasprintf (char **strp, const char *fmt, ...); | 79 | int xasprintf(char **strp, const char *fmt, ...); |
| 80 | 80 | ||
| 81 | void usage (const char *) __attribute__((noreturn)); | 81 | void usage(const char *) __attribute__((noreturn)); |
| 82 | void usage2(const char *, const char *) __attribute__((noreturn)); | 82 | void usage2(const char *, const char *) __attribute__((noreturn)); |
| 83 | void usage3(const char *, int) __attribute__((noreturn)); | 83 | void usage3(const char *, int) __attribute__((noreturn)); |
| 84 | void usage4(const char *) __attribute__((noreturn)); | 84 | void usage4(const char *) __attribute__((noreturn)); |
| 85 | void usage5(void) __attribute__((noreturn)); | 85 | void usage5(void) __attribute__((noreturn)); |
| 86 | void usage_va(const char *fmt, ...) __attribute__((noreturn)); | 86 | void usage_va(const char *fmt, ...) __attribute__((noreturn)); |
| 87 | 87 | ||
| 88 | #define max(a,b) (((a)>(b))?(a):(b)) | 88 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 89 | #define min(a,b) (((a)<(b))?(a):(b)) | 89 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
| 90 | 90 | ||
| 91 | char *perfdata (const char *, long int, const char *, int, long int, | 91 | char *perfdata(const char *, long int, const char *, bool, long int, bool, long int, bool, long int, bool, long int); |
| 92 | int, long int, int, long int, int, long int); | ||
| 93 | 92 | ||
| 94 | char *perfdata_uint64 (const char *, uint64_t , const char *, int, uint64_t, | 93 | char *perfdata_uint64(const char *, uint64_t, const char *, bool, uint64_t, bool, uint64_t, bool, uint64_t, bool, uint64_t); |
| 95 | int, uint64_t, int, uint64_t, int, uint64_t); | ||
| 96 | 94 | ||
| 97 | char *perfdata_int64 (const char *, int64_t, const char *, int, int64_t, | 95 | char *perfdata_int64(const char *, int64_t, const char *, bool, int64_t, bool, int64_t, bool, int64_t, bool, int64_t); |
| 98 | int, int64_t, int, int64_t, int, int64_t); | ||
| 99 | 96 | ||
| 100 | char *fperfdata (const char *, double, const char *, int, double, | 97 | char *fperfdata(const char *, double, const char *, bool, double, bool, double, bool, double, bool, double); |
| 101 | int, double, int, double, int, double); | ||
| 102 | 98 | ||
| 103 | char *sperfdata (const char *, double, const char *, char *, char *, | 99 | char *sperfdata(const char *, double, const char *, char *, char *, bool, double, bool, double); |
| 104 | int, double, int, double); | ||
| 105 | 100 | ||
| 106 | char *sperfdata_int (const char *, int, const char *, char *, char *, | 101 | char *sperfdata_int(const char *, int, const char *, char *, char *, bool, int, bool, int); |
| 107 | int, int, int, int); | ||
| 108 | 102 | ||
| 109 | /* The idea here is that, although not every plugin will use all of these, | 103 | /* The idea here is that, although not every plugin will use all of these, |
| 110 | most will or should. Therefore, for consistency, these very common | 104 | most will or should. Therefore, for consistency, these very common |
| 111 | options should have only these meanings throughout the overall suite */ | 105 | options should have only these meanings throughout the overall suite */ |
| 112 | 106 | ||
| 113 | #define STD_LONG_OPTS \ | 107 | #define STD_LONG_OPTS \ |
| 114 | {"version",no_argument,0,'V'},\ | 108 | {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, \ |
| 115 | {"verbose",no_argument,0,'v'},\ | 109 | {"timeout", required_argument, 0, 't'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, \ |
| 116 | {"help",no_argument,0,'h'},\ | 110 | {"hostname", required_argument, 0, 'H'} |
| 117 | {"timeout",required_argument,0,'t'},\ | ||
| 118 | {"critical",required_argument,0,'c'},\ | ||
| 119 | {"warning",required_argument,0,'w'},\ | ||
| 120 | {"hostname",required_argument,0,'H'} | ||
| 121 | 111 | ||
| 122 | #define COPYRIGHT "Copyright (c) %s Monitoring Plugins Development Team\n\ | 112 | #define COPYRIGHT \ |
| 113 | "Copyright (c) %s Monitoring Plugins Development Team\n\ | ||
| 123 | \t<%s>\n\n" | 114 | \t<%s>\n\n" |
| 124 | 115 | ||
| 125 | #define UT_HLP_VRS _("\ | 116 | #define UT_HLP_VRS \ |
| 117 | _("\ | ||
| 126 | %s (-h | --help) for detailed help\n\ | 118 | %s (-h | --help) for detailed help\n\ |
| 127 | %s (-V | --version) for version information\n") | 119 | %s (-V | --version) for version information\n") |
| 128 | 120 | ||
| 129 | #define UT_HELP_VRSN _("\ | 121 | #define UT_HELP_VRSN \ |
| 122 | _("\ | ||
| 130 | \nOptions:\n\ | 123 | \nOptions:\n\ |
| 131 | -h, --help\n\ | 124 | -h, --help\n\ |
| 132 | Print detailed help screen\n\ | 125 | Print detailed help screen\n\ |
| 133 | -V, --version\n\ | 126 | -V, --version\n\ |
| 134 | Print version information\n") | 127 | Print version information\n") |
| 135 | 128 | ||
| 136 | #define UT_HOST_PORT _("\ | 129 | #define UT_HOST_PORT \ |
| 130 | _("\ | ||
| 137 | -H, --hostname=ADDRESS\n\ | 131 | -H, --hostname=ADDRESS\n\ |
| 138 | Host name, IP Address, or unix socket (must be an absolute path)\n\ | 132 | Host name, IP Address, or unix socket (must be an absolute path)\n\ |
| 139 | -%c, --port=INTEGER\n\ | 133 | -%c, --port=INTEGER\n\ |
| 140 | Port number (default: %s)\n") | 134 | Port number (default: %s)\n") |
| 141 | 135 | ||
| 142 | #define UT_IPv46 _("\ | 136 | #define UT_IPv46 \ |
| 137 | _("\ | ||
| 143 | -4, --use-ipv4\n\ | 138 | -4, --use-ipv4\n\ |
| 144 | Use IPv4 connection\n\ | 139 | Use IPv4 connection\n\ |
| 145 | -6, --use-ipv6\n\ | 140 | -6, --use-ipv6\n\ |
| 146 | Use IPv6 connection\n") | 141 | Use IPv6 connection\n") |
| 147 | 142 | ||
| 148 | #define UT_VERBOSE _("\ | 143 | #define UT_VERBOSE \ |
| 144 | _("\ | ||
| 149 | -v, --verbose\n\ | 145 | -v, --verbose\n\ |
| 150 | Show details for command-line debugging (output may be truncated by\n\ | 146 | Show details for command-line debugging (output may be truncated by\n\ |
| 151 | the monitoring system)\n") | 147 | the monitoring system)\n") |
| 152 | 148 | ||
| 153 | #define UT_WARN_CRIT _("\ | 149 | #define UT_WARN_CRIT \ |
| 150 | _("\ | ||
| 154 | -w, --warning=DOUBLE\n\ | 151 | -w, --warning=DOUBLE\n\ |
| 155 | Response time to result in warning status (seconds)\n\ | 152 | Response time to result in warning status (seconds)\n\ |
| 156 | -c, --critical=DOUBLE\n\ | 153 | -c, --critical=DOUBLE\n\ |
| 157 | Response time to result in critical status (seconds)\n") | 154 | Response time to result in critical status (seconds)\n") |
| 158 | 155 | ||
| 159 | #define UT_WARN_CRIT_RANGE _("\ | 156 | #define UT_WARN_CRIT_RANGE \ |
| 157 | _("\ | ||
| 160 | -w, --warning=RANGE\n\ | 158 | -w, --warning=RANGE\n\ |
| 161 | Warning range (format: start:end). Alert if outside this range\n\ | 159 | Warning range (format: start:end). Alert if outside this range\n\ |
| 162 | -c, --critical=RANGE\n\ | 160 | -c, --critical=RANGE\n\ |
| 163 | Critical range\n") | 161 | Critical range\n") |
| 164 | 162 | ||
| 165 | #define UT_CONN_TIMEOUT _("\ | 163 | #define UT_CONN_TIMEOUT \ |
| 164 | _("\ | ||
| 166 | -t, --timeout=INTEGER\n\ | 165 | -t, --timeout=INTEGER\n\ |
| 167 | Seconds before connection times out (default: %d)\n") | 166 | Seconds before connection times out (default: %d)\n") |
| 168 | 167 | ||
| 169 | #define UT_PLUG_TIMEOUT _("\ | 168 | #define UT_PLUG_TIMEOUT \ |
| 169 | _("\ | ||
| 170 | -t, --timeout=INTEGER\n\ | 170 | -t, --timeout=INTEGER\n\ |
| 171 | Seconds before plugin times out (default: %d)\n") | 171 | Seconds before plugin times out (default: %d)\n") |
| 172 | 172 | ||
| 173 | #ifdef NP_EXTRA_OPTS | 173 | #ifdef NP_EXTRA_OPTS |
| 174 | #define UT_EXTRA_OPTS _("\ | 174 | # define UT_EXTRA_OPTS \ |
| 175 | _("\ | ||
| 175 | --extra-opts=[section][@file]\n\ | 176 | --extra-opts=[section][@file]\n\ |
| 176 | Read options from an ini file. See\n\ | 177 | Read options from an ini file. See\n\ |
| 177 | https://www.monitoring-plugins.org/doc/extra-opts.html\n\ | 178 | https://www.monitoring-plugins.org/doc/extra-opts.html\n\ |
| 178 | for usage and examples.\n") | 179 | for usage and examples.\n") |
| 179 | #else | 180 | #else |
| 180 | #define UT_EXTRA_OPTS " \b" | 181 | # define UT_EXTRA_OPTS " \b" |
| 181 | #endif | 182 | #endif |
| 182 | 183 | ||
| 183 | #define UT_THRESHOLDS_NOTES _("\ | 184 | #define UT_THRESHOLDS_NOTES \ |
| 185 | _("\ | ||
| 184 | See:\n\ | 186 | See:\n\ |
| 185 | https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n\ | 187 | https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n\ |
| 186 | for THRESHOLD format and examples.\n") | 188 | for THRESHOLD format and examples.\n") |
| 187 | 189 | ||
| 188 | #define UT_SUPPORT _("\n\ | 190 | #define UT_SUPPORT \ |
| 191 | _("\n\ | ||
| 189 | Send email to help@monitoring-plugins.org if you have questions regarding\n\ | 192 | Send email to help@monitoring-plugins.org if you have questions regarding\n\ |
| 190 | use of this software. To submit patches or suggest improvements, send email\n\ | 193 | use of this software. To submit patches or suggest improvements, send email\n\ |
| 191 | to devel@monitoring-plugins.org\n\n") | 194 | to devel@monitoring-plugins.org\n\n") |
| 192 | 195 | ||
| 193 | #define UT_NOWARRANTY _("\n\ | 196 | #define UT_NOWARRANTY \ |
| 197 | _("\n\ | ||
| 194 | The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ | 198 | The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ |
| 195 | copies of the plugins under the terms of the GNU General Public License.\n\ | 199 | copies of the plugins under the terms of the GNU General Public License.\n\ |
| 196 | For more information about these matters, see the file named COPYING.\n") | 200 | For more information about these matters, see the file named COPYING.\n") |
| 197 | 201 | ||
| 198 | #define UT_OUTPUT_FORMAT _("\ | 202 | #define UT_OUTPUT_FORMAT \ |
| 203 | _("\ | ||
| 199 | --output-format=OUTPUT_FORMAT\n\ | 204 | --output-format=OUTPUT_FORMAT\n\ |
| 200 | Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") | 205 | Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") |
| 201 | 206 | ||
