diff options
author | Holger Weiss <holger@zedat.fu-berlin.de> | 2021-10-03 13:37:12 +0200 |
---|---|---|
committer | Holger Weiss <holger@zedat.fu-berlin.de> | 2021-10-03 13:37:12 +0200 |
commit | 2f80d55b8ed0c539a9141895a226c5fec4ff4c4d (patch) | |
tree | 06daa9142183c0f6c6624bebcffd7d11de1ab976 /plugins | |
parent | cfc43a327526d838db5ec81f5594df4a14319786 (diff) | |
parent | 46c5327e348540ab04dc37d42f6d1c5408179fa6 (diff) | |
download | monitoring-plugins-2f80d55.tar.gz |
Merge remote-tracking branch 'monitoring-plugins/pr/1707'
* monitoring-plugins/pr/1707:
Revert to poor man's logic
Change all to comments to old comment style
Fix comparing logic
Remove spaces from tests
check_swap: Fix perfdata und thresholds for big values and simplify code
Introduce new perfdata functions and stuff for using (u)int64_t
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_swap.c | 237 | ||||
-rw-r--r-- | plugins/t/check_swap.t | 6 | ||||
-rw-r--r-- | plugins/utils.c | 120 | ||||
-rw-r--r-- | plugins/utils.h | 9 |
4 files changed, 268 insertions, 104 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 0ff0c770..685c2cc5 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c | |||
@@ -34,6 +34,9 @@ const char *email = "devel@monitoring-plugins.org"; | |||
34 | #include "common.h" | 34 | #include "common.h" |
35 | #include "popen.h" | 35 | #include "popen.h" |
36 | #include "utils.h" | 36 | #include "utils.h" |
37 | #include <string.h> | ||
38 | #include <math.h> | ||
39 | #include <libintl.h> | ||
37 | 40 | ||
38 | #ifdef HAVE_DECL_SWAPCTL | 41 | #ifdef HAVE_DECL_SWAPCTL |
39 | # ifdef HAVE_SYS_PARAM_H | 42 | # ifdef HAVE_SYS_PARAM_H |
@@ -51,16 +54,19 @@ const char *email = "devel@monitoring-plugins.org"; | |||
51 | # define SWAP_CONVERSION 1 | 54 | # define SWAP_CONVERSION 1 |
52 | #endif | 55 | #endif |
53 | 56 | ||
54 | int check_swap (int usp, float free_swap_mb, float total_swap_mb); | 57 | typedef struct { |
58 | int is_percentage; | ||
59 | uint64_t value; | ||
60 | } threshold_t; | ||
61 | |||
62 | int check_swap (float free_swap_mb, float total_swap_mb); | ||
55 | int process_arguments (int argc, char **argv); | 63 | int process_arguments (int argc, char **argv); |
56 | int validate_arguments (void); | 64 | int validate_arguments (void); |
57 | void print_usage (void); | 65 | void print_usage (void); |
58 | void print_help (void); | 66 | void print_help (void); |
59 | 67 | ||
60 | int warn_percent = 0; | 68 | threshold_t warn; |
61 | int crit_percent = 0; | 69 | threshold_t crit; |
62 | float warn_size_bytes = 0; | ||
63 | float crit_size_bytes = 0; | ||
64 | int verbose; | 70 | int verbose; |
65 | int allswaps; | 71 | int allswaps; |
66 | int no_swap_state = STATE_CRITICAL; | 72 | int no_swap_state = STATE_CRITICAL; |
@@ -68,9 +74,10 @@ int no_swap_state = STATE_CRITICAL; | |||
68 | int | 74 | int |
69 | main (int argc, char **argv) | 75 | main (int argc, char **argv) |
70 | { | 76 | { |
71 | int percent_used, percent; | 77 | unsigned int percent_used, percent; |
72 | float total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0; | 78 | uint64_t total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0; |
73 | float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0; | 79 | uint64_t dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; |
80 | uint64_t tmp_KB = 0; | ||
74 | int result = STATE_UNKNOWN; | 81 | int result = STATE_UNKNOWN; |
75 | char input_buffer[MAX_INPUT_BUFFER]; | 82 | char input_buffer[MAX_INPUT_BUFFER]; |
76 | #ifdef HAVE_PROC_MEMINFO | 83 | #ifdef HAVE_PROC_MEMINFO |
@@ -116,10 +123,15 @@ main (int argc, char **argv) | |||
116 | } | 123 | } |
117 | fp = fopen (PROC_MEMINFO, "r"); | 124 | fp = fopen (PROC_MEMINFO, "r"); |
118 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | 125 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { |
119 | if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { | 126 | /* |
120 | dsktotal_mb = dsktotal_mb / 1048576; /* Apply conversion */ | 127 | * The following sscanf call looks for a line looking like: "Swap: 123 123 123" |
121 | dskused_mb = dskused_mb / 1048576; | 128 | * On which kind of system this format exists, I can not say, but I wanted to |
122 | dskfree_mb = dskfree_mb / 1048576; | 129 | * document this for people who are not adapt with sscanf anymore, like me |
130 | */ | ||
131 | if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { | ||
132 | dsktotal_mb = dsktotal_mb / (1024 * 1024); /* Apply conversion */ | ||
133 | dskused_mb = dskused_mb / (1024 * 1024); | ||
134 | dskfree_mb = dskfree_mb / (1024 * 1024); | ||
123 | total_swap_mb += dsktotal_mb; | 135 | total_swap_mb += dsktotal_mb; |
124 | used_swap_mb += dskused_mb; | 136 | used_swap_mb += dskused_mb; |
125 | free_swap_mb += dskfree_mb; | 137 | free_swap_mb += dskfree_mb; |
@@ -128,21 +140,25 @@ main (int argc, char **argv) | |||
128 | percent=100.0; | 140 | percent=100.0; |
129 | else | 141 | else |
130 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | 142 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); |
131 | result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); | 143 | result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); |
132 | if (verbose) | 144 | if (verbose) |
133 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); | 145 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); |
134 | } | 146 | } |
135 | } | 147 | } |
136 | else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) { | 148 | /* |
149 | * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123" | ||
150 | * This format exists at least on Debian Linux with a 5.* kernel | ||
151 | */ | ||
152 | else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) { | ||
137 | if (verbose >= 3) { | 153 | if (verbose >= 3) { |
138 | printf("Got %s with %f\n", str, tmp_mb); | 154 | printf("Got %s with %lu\n", str, tmp_KB); |
139 | } | 155 | } |
140 | /* I think this part is always in Kb, so convert to mb */ | 156 | /* I think this part is always in Kb, so convert to mb */ |
141 | if (strcmp ("Total", str) == 0) { | 157 | if (strcmp ("Total", str) == 0) { |
142 | dsktotal_mb = tmp_mb / 1024; | 158 | dsktotal_mb = tmp_KB / 1024; |
143 | } | 159 | } |
144 | else if (strcmp ("Free", str) == 0) { | 160 | else if (strcmp ("Free", str) == 0) { |
145 | dskfree_mb = tmp_mb / 1024; | 161 | dskfree_mb = tmp_KB / 1024; |
146 | } | 162 | } |
147 | } | 163 | } |
148 | } | 164 | } |
@@ -227,7 +243,7 @@ main (int argc, char **argv) | |||
227 | free_swap_mb += dskfree_mb; | 243 | free_swap_mb += dskfree_mb; |
228 | if (allswaps) { | 244 | if (allswaps) { |
229 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | 245 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); |
230 | result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); | 246 | result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); |
231 | if (verbose) | 247 | if (verbose) |
232 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); | 248 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); |
233 | } | 249 | } |
@@ -289,7 +305,7 @@ main (int argc, char **argv) | |||
289 | 305 | ||
290 | if(allswaps && dsktotal_mb > 0){ | 306 | if(allswaps && dsktotal_mb > 0){ |
291 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | 307 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); |
292 | result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); | 308 | result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); |
293 | if (verbose) { | 309 | if (verbose) { |
294 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); | 310 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); |
295 | } | 311 | } |
@@ -328,7 +344,7 @@ main (int argc, char **argv) | |||
328 | 344 | ||
329 | if(allswaps && dsktotal_mb > 0){ | 345 | if(allswaps && dsktotal_mb > 0){ |
330 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | 346 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); |
331 | result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); | 347 | result = max_state (result, check_swap(dskfree_mb, dsktotal_mb)); |
332 | if (verbose) { | 348 | if (verbose) { |
333 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); | 349 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); |
334 | } | 350 | } |
@@ -355,14 +371,19 @@ main (int argc, char **argv) | |||
355 | status = "- Swap is either disabled, not present, or of zero size. "; | 371 | status = "- Swap is either disabled, not present, or of zero size. "; |
356 | } | 372 | } |
357 | 373 | ||
358 | result = max_state (result, check_swap (percent_used, free_swap_mb, total_swap_mb)); | 374 | result = max_state (result, check_swap(free_swap_mb, total_swap_mb)); |
359 | printf (_("SWAP %s - %d%% free (%d MB out of %d MB) %s|"), | 375 | printf (_("SWAP %s - %d%% free (%dMB out of %dMB) %s|"), |
360 | state_text (result), | 376 | state_text (result), |
361 | (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status); | 377 | (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status); |
362 | 378 | ||
363 | puts (perfdata ("swap", (long) free_swap_mb, "MB", | 379 | uint64_t warn_print = warn.value; |
364 | TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_swap_mb), | 380 | if (warn.is_percentage) warn_print = warn.value * (total_swap_mb *1024 *1024/100); |
365 | TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_swap_mb), | 381 | uint64_t crit_print = crit.value; |
382 | if (crit.is_percentage) crit_print = crit.value * (total_swap_mb *1024 *1024/100); | ||
383 | |||
384 | puts (perfdata_uint64 ("swap", free_swap_mb *1024 *1024, "B", | ||
385 | TRUE, warn_print, | ||
386 | TRUE, crit_print, | ||
366 | TRUE, 0, | 387 | TRUE, 0, |
367 | TRUE, (long) total_swap_mb)); | 388 | TRUE, (long) total_swap_mb)); |
368 | 389 | ||
@@ -370,26 +391,37 @@ main (int argc, char **argv) | |||
370 | } | 391 | } |
371 | 392 | ||
372 | 393 | ||
373 | |||
374 | int | 394 | int |
375 | check_swap (int usp, float free_swap_mb, float total_swap_mb) | 395 | check_swap(float free_swap_mb, float total_swap_mb) |
376 | { | 396 | { |
377 | 397 | ||
378 | if (!total_swap_mb) return no_swap_state; | 398 | if (!total_swap_mb) return no_swap_state; |
379 | 399 | ||
380 | int result = STATE_UNKNOWN; | 400 | uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ |
381 | float free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ | 401 | |
382 | if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent)) | 402 | if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL; |
383 | result = STATE_CRITICAL; | 403 | if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING; |
384 | else if (crit_size_bytes > 0 && free_swap <= crit_size_bytes) | 404 | |
385 | result = STATE_CRITICAL; | 405 | |
386 | else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent)) | 406 | uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100; |
387 | result = STATE_WARNING; | 407 | |
388 | else if (warn_size_bytes > 0 && free_swap <= warn_size_bytes) | 408 | if (crit.is_percentage && |
389 | result = STATE_WARNING; | 409 | usage_percentage >= 0 && |
390 | else if (usp >= 0.0) | 410 | crit.value != 0 && |
391 | result = STATE_OK; | 411 | usage_percentage >= (100 - crit.value)) |
392 | return result; | 412 | { |
413 | return STATE_CRITICAL; | ||
414 | } | ||
415 | |||
416 | if (warn.is_percentage && | ||
417 | usage_percentage >= 0 && | ||
418 | warn.value != 0 && | ||
419 | usage_percentage >= (100 - warn.value)) | ||
420 | { | ||
421 | return STATE_WARNING; | ||
422 | } | ||
423 | |||
424 | return STATE_OK; | ||
393 | } | 425 | } |
394 | 426 | ||
395 | 427 | ||
@@ -422,42 +454,68 @@ process_arguments (int argc, char **argv) | |||
422 | break; | 454 | break; |
423 | 455 | ||
424 | switch (c) { | 456 | switch (c) { |
425 | case 'w': /* warning size threshold */ | 457 | case 'w': /* warning size threshold */ |
426 | if (is_intnonneg (optarg)) { | 458 | { |
427 | warn_size_bytes = (float) atoi (optarg); | 459 | /* |
428 | break; | 460 | * We expect either a positive integer value without a unit, which means |
429 | } | 461 | * the unit is Bytes or a positive integer value and a percentage sign (%), |
430 | else if (strstr (optarg, ",") && | 462 | * which means the value must be with 0 and 100 and is relative to the total swap |
431 | strstr (optarg, "%") && | 463 | */ |
432 | sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) { | 464 | size_t length; |
433 | warn_size_bytes = floorf(warn_size_bytes); | 465 | length = strlen(optarg); |
434 | break; | 466 | |
435 | } | 467 | if (optarg[length - 1] == '%') { |
436 | else if (strstr (optarg, "%") && | 468 | /* It's percentage */ |
437 | sscanf (optarg, "%d%%", &warn_percent) == 1) { | 469 | warn.is_percentage = 1; |
438 | break; | 470 | optarg[length - 1] = '\0'; |
439 | } | 471 | if (is_uint64(optarg, &warn.value)) { |
440 | else { | 472 | if (warn.value > 100) { |
441 | usage4 (_("Warning threshold must be integer or percentage!")); | 473 | usage4 (_("Warning threshold percentage must be <= 100!")); |
442 | } | 474 | } else { |
443 | case 'c': /* critical size threshold */ | 475 | break; |
444 | if (is_intnonneg (optarg)) { | 476 | } |
445 | crit_size_bytes = (float) atoi (optarg); | 477 | } |
446 | break; | 478 | } else { |
447 | } | 479 | /* It's Bytes */ |
448 | else if (strstr (optarg, ",") && | 480 | warn.is_percentage = 0; |
449 | strstr (optarg, "%") && | 481 | if (is_uint64(optarg, &warn.value)) { |
450 | sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) { | 482 | break; |
451 | crit_size_bytes = floorf(crit_size_bytes); | 483 | } else { |
452 | break; | 484 | usage4 (_("Warning threshold be positive integer or percentage!")); |
453 | } | 485 | } |
454 | else if (strstr (optarg, "%") && | 486 | } |
455 | sscanf (optarg, "%d%%", &crit_percent) == 1) { | ||
456 | break; | ||
457 | } | ||
458 | else { | ||
459 | usage4 (_("Critical threshold must be integer or percentage!")); | ||
460 | } | 487 | } |
488 | case 'c': /* critical size threshold */ | ||
489 | { | ||
490 | /* | ||
491 | * We expect either a positive integer value without a unit, which means | ||
492 | * the unit is Bytes or a positive integer value and a percentage sign (%), | ||
493 | * which means the value must be with 0 and 100 and is relative to the total swap | ||
494 | */ | ||
495 | size_t length; | ||
496 | length = strlen(optarg); | ||
497 | |||
498 | if (optarg[length - 1] == '%') { | ||
499 | /* It's percentage */ | ||
500 | crit.is_percentage = 1; | ||
501 | optarg[length - 1] = '\0'; | ||
502 | if (is_uint64(optarg, &crit.value)) { | ||
503 | if (crit.value> 100) { | ||
504 | usage4 (_("Critical threshold percentage must be <= 100!")); | ||
505 | } else { | ||
506 | break; | ||
507 | } | ||
508 | } | ||
509 | } else { | ||
510 | /* It's Bytes */ | ||
511 | crit.is_percentage = 0; | ||
512 | if (is_uint64(optarg, &crit.value)) { | ||
513 | break; | ||
514 | } else { | ||
515 | usage4 (_("Critical threshold be positive integer or percentage!")); | ||
516 | } | ||
517 | } | ||
518 | } | ||
461 | case 'a': /* all swap */ | 519 | case 'a': /* all swap */ |
462 | allswaps = TRUE; | 520 | allswaps = TRUE; |
463 | break; | 521 | break; |
@@ -482,23 +540,6 @@ process_arguments (int argc, char **argv) | |||
482 | c = optind; | 540 | c = optind; |
483 | if (c == argc) | 541 | if (c == argc) |
484 | return validate_arguments (); | 542 | return validate_arguments (); |
485 | if (warn_percent == 0 && is_intnonneg (argv[c])) | ||
486 | warn_percent = atoi (argv[c++]); | ||
487 | |||
488 | if (c == argc) | ||
489 | return validate_arguments (); | ||
490 | if (crit_percent == 0 && is_intnonneg (argv[c])) | ||
491 | crit_percent = atoi (argv[c++]); | ||
492 | |||
493 | if (c == argc) | ||
494 | return validate_arguments (); | ||
495 | if (warn_size_bytes == 0 && is_intnonneg (argv[c])) | ||
496 | warn_size_bytes = (float) atoi (argv[c++]); | ||
497 | |||
498 | if (c == argc) | ||
499 | return validate_arguments (); | ||
500 | if (crit_size_bytes == 0 && is_intnonneg (argv[c])) | ||
501 | crit_size_bytes = (float) atoi (argv[c++]); | ||
502 | 543 | ||
503 | return validate_arguments (); | 544 | return validate_arguments (); |
504 | } | 545 | } |
@@ -508,17 +549,12 @@ process_arguments (int argc, char **argv) | |||
508 | int | 549 | int |
509 | validate_arguments (void) | 550 | validate_arguments (void) |
510 | { | 551 | { |
511 | if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0 | 552 | if (warn.value == 0 && crit.value == 0) { |
512 | && crit_size_bytes == 0) { | ||
513 | return ERROR; | 553 | return ERROR; |
514 | } | 554 | } |
515 | else if (warn_percent < crit_percent) { | 555 | else if (warn.value < crit.value) { |
516 | usage4 | ||
517 | (_("Warning percentage should be more than critical percentage")); | ||
518 | } | ||
519 | else if (warn_size_bytes < crit_size_bytes) { | ||
520 | usage4 | 556 | usage4 |
521 | (_("Warning free space should be more than critical free space")); | 557 | (_("Warning should be more than critical")); |
522 | } | 558 | } |
523 | return OK; | 559 | return OK; |
524 | } | 560 | } |
@@ -564,7 +600,6 @@ print_help (void) | |||
564 | } | 600 | } |
565 | 601 | ||
566 | 602 | ||
567 | |||
568 | void | 603 | void |
569 | print_usage (void) | 604 | print_usage (void) |
570 | { | 605 | { |
diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index e44adc90..de9e0f05 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t | |||
@@ -8,9 +8,9 @@ use strict; | |||
8 | use Test::More tests => 8; | 8 | use Test::More tests => 8; |
9 | use NPTest; | 9 | use NPTest; |
10 | 10 | ||
11 | my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | 11 | my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; |
12 | my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | 12 | my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; |
13 | my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | 13 | my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; |
14 | 14 | ||
15 | my $result; | 15 | my $result; |
16 | 16 | ||
diff --git a/plugins/utils.c b/plugins/utils.c index 348ec022..f7f8952f 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -27,6 +27,8 @@ | |||
27 | #include "utils_base.h" | 27 | #include "utils_base.h" |
28 | #include <stdarg.h> | 28 | #include <stdarg.h> |
29 | #include <limits.h> | 29 | #include <limits.h> |
30 | #include <string.h> | ||
31 | #include <errno.h> | ||
30 | 32 | ||
31 | #include <arpa/inet.h> | 33 | #include <arpa/inet.h> |
32 | 34 | ||
@@ -239,6 +241,46 @@ is_intnonneg (char *number) | |||
239 | return FALSE; | 241 | return FALSE; |
240 | } | 242 | } |
241 | 243 | ||
244 | /* | ||
245 | * Checks whether the number in the string _number_ can be put inside a int64_t | ||
246 | * On success the number will be written to the _target_ address, if _target_ is not set | ||
247 | * to NULL. | ||
248 | */ | ||
249 | int is_int64(char *number, int64_t *target) { | ||
250 | errno = 0; | ||
251 | uint64_t tmp = strtoll(number, NULL, 10); | ||
252 | if (errno != 0) { | ||
253 | return 0; | ||
254 | } | ||
255 | if (tmp < INT64_MIN || tmp > INT64_MAX) { | ||
256 | return 0; | ||
257 | } | ||
258 | if (target != NULL) { | ||
259 | *target = tmp; | ||
260 | } | ||
261 | return 1; | ||
262 | } | ||
263 | |||
264 | /* | ||
265 | * Checks whether the number in the string _number_ can be put inside a uint64_t | ||
266 | * On success the number will be written to the _target_ address, if _target_ is not set | ||
267 | * to NULL. | ||
268 | */ | ||
269 | int is_uint64(char *number, uint64_t *target) { | ||
270 | errno = 0; | ||
271 | uint64_t tmp = strtoll(number, NULL, 10); | ||
272 | if (errno != 0) { | ||
273 | return 0; | ||
274 | } | ||
275 | if (tmp < 0 || tmp > UINT64_MAX) { | ||
276 | return 0; | ||
277 | } | ||
278 | if (target != NULL) { | ||
279 | *target = tmp; | ||
280 | } | ||
281 | return 1; | ||
282 | } | ||
283 | |||
242 | int | 284 | int |
243 | is_intpercent (char *number) | 285 | is_intpercent (char *number) |
244 | { | 286 | { |
@@ -556,6 +598,84 @@ char *perfdata (const char *label, | |||
556 | } | 598 | } |
557 | 599 | ||
558 | 600 | ||
601 | char *perfdata_uint64 (const char *label, | ||
602 | uint64_t val, | ||
603 | const char *uom, | ||
604 | int warnp, | ||
605 | uint64_t warn, | ||
606 | int critp, | ||
607 | uint64_t crit, | ||
608 | int minp, | ||
609 | uint64_t minv, | ||
610 | int maxp, | ||
611 | uint64_t maxv) | ||
612 | { | ||
613 | char *data = NULL; | ||
614 | |||
615 | if (strpbrk (label, "'= ")) | ||
616 | xasprintf (&data, "'%s'=%ld%s;", label, val, uom); | ||
617 | else | ||
618 | xasprintf (&data, "%s=%ld%s;", label, val, uom); | ||
619 | |||
620 | if (warnp) | ||
621 | xasprintf (&data, "%s%ld;", data, warn); | ||
622 | else | ||
623 | xasprintf (&data, "%s;", data); | ||
624 | |||
625 | if (critp) | ||
626 | xasprintf (&data, "%s%ld;", data, crit); | ||
627 | else | ||
628 | xasprintf (&data, "%s;", data); | ||
629 | |||
630 | if (minp) | ||
631 | xasprintf (&data, "%s%ld", data, minv); | ||
632 | |||
633 | if (maxp) | ||
634 | xasprintf (&data, "%s;%ld", data, maxv); | ||
635 | |||
636 | return data; | ||
637 | } | ||
638 | |||
639 | |||
640 | char *perfdata_int64 (const char *label, | ||
641 | int64_t val, | ||
642 | const char *uom, | ||
643 | int warnp, | ||
644 | int64_t warn, | ||
645 | int critp, | ||
646 | int64_t crit, | ||
647 | int minp, | ||
648 | int64_t minv, | ||
649 | int maxp, | ||
650 | int64_t maxv) | ||
651 | { | ||
652 | char *data = NULL; | ||
653 | |||
654 | if (strpbrk (label, "'= ")) | ||
655 | xasprintf (&data, "'%s'=%ld%s;", label, val, uom); | ||
656 | else | ||
657 | xasprintf (&data, "%s=%ld%s;", label, val, uom); | ||
658 | |||
659 | if (warnp) | ||
660 | xasprintf (&data, "%s%ld;", data, warn); | ||
661 | else | ||
662 | xasprintf (&data, "%s;", data); | ||
663 | |||
664 | if (critp) | ||
665 | xasprintf (&data, "%s%ld;", data, crit); | ||
666 | else | ||
667 | xasprintf (&data, "%s;", data); | ||
668 | |||
669 | if (minp) | ||
670 | xasprintf (&data, "%s%ld", data, minv); | ||
671 | |||
672 | if (maxp) | ||
673 | xasprintf (&data, "%s;%ld", data, maxv); | ||
674 | |||
675 | return data; | ||
676 | } | ||
677 | |||
678 | |||
559 | char *fperfdata (const char *label, | 679 | char *fperfdata (const char *label, |
560 | double val, | 680 | double val, |
561 | const char *uom, | 681 | const char *uom, |
diff --git a/plugins/utils.h b/plugins/utils.h index 33a20547..5b54da3c 100644 --- a/plugins/utils.h +++ b/plugins/utils.h | |||
@@ -16,6 +16,7 @@ suite of plugins. */ | |||
16 | /* now some functions etc are being defined in ../lib/utils_base.c */ | 16 | /* now some functions etc are being defined in ../lib/utils_base.c */ |
17 | #include "utils_base.h" | 17 | #include "utils_base.h" |
18 | 18 | ||
19 | |||
19 | #ifdef NP_EXTRA_OPTS | 20 | #ifdef NP_EXTRA_OPTS |
20 | /* Include extra-opts functions if compiled in */ | 21 | /* Include extra-opts functions if compiled in */ |
21 | #include "extra_opts.h" | 22 | #include "extra_opts.h" |
@@ -38,6 +39,8 @@ int is_intpos (char *); | |||
38 | int is_intneg (char *); | 39 | int is_intneg (char *); |
39 | int is_intnonneg (char *); | 40 | int is_intnonneg (char *); |
40 | int is_intpercent (char *); | 41 | int is_intpercent (char *); |
42 | int is_uint64(char *number, uint64_t *target); | ||
43 | int is_int64(char *number, int64_t *target); | ||
41 | 44 | ||
42 | int is_numeric (char *); | 45 | int is_numeric (char *); |
43 | int is_positive (char *); | 46 | int is_positive (char *); |
@@ -88,6 +91,12 @@ void usage_va(const char *fmt, ...) __attribute__((noreturn)); | |||
88 | char *perfdata (const char *, long int, const char *, int, long int, | 91 | char *perfdata (const char *, long int, const char *, int, long int, |
89 | int, long int, int, long int, int, long int); | 92 | int, long int, int, long int, int, long int); |
90 | 93 | ||
94 | char *perfdata_uint64 (const char *, uint64_t , const char *, int, uint64_t, | ||
95 | int, uint64_t, int, uint64_t, int, uint64_t); | ||
96 | |||
97 | char *perfdata_int64 (const char *, int64_t, const char *, int, int64_t, | ||
98 | int, int64_t, int, int64_t, int, int64_t); | ||
99 | |||
91 | char *fperfdata (const char *, double, const char *, int, double, | 100 | char *fperfdata (const char *, double, const char *, int, double, |
92 | int, double, int, double, int, double); | 101 | int, double, int, double, int, double); |
93 | 102 | ||