summaryrefslogtreecommitdiffstats
path: root/plugins/check_swap.c
diff options
context:
space:
mode:
authorLorenz <12514511+RincewindsHat@users.noreply.github.com>2022-01-03 12:48:39 (GMT)
committerGitHub <noreply@github.com>2022-01-03 12:48:39 (GMT)
commit2714df42fd27ec377228a102acff3744d6bff86d (patch)
tree3098fa3d9be4826e5da9d1df39ecf6b42bf3570b /plugins/check_swap.c
parent11af74de386ba7c02d5d0e53f2500b5029a4537d (diff)
parent78a999edd4dd3305ef8fa3e06c43f6a893fb6fea (diff)
downloadmonitoring-plugins-2714df42fd27ec377228a102acff3744d6bff86d.tar.gz
Merge branch 'master' into master
Diffstat (limited to 'plugins/check_swap.c')
-rw-r--r--plugins/check_swap.c275
1 files changed, 157 insertions, 118 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index ce3f1d7..2b8013b 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
54int check_swap (int usp, float free_swap_mb, float total_swap_mb); 57typedef struct {
58 int is_percentage;
59 uint64_t value;
60} threshold_t;
61
62int check_swap (float free_swap_mb, float total_swap_mb);
55int process_arguments (int argc, char **argv); 63int process_arguments (int argc, char **argv);
56int validate_arguments (void); 64int validate_arguments (void);
57void print_usage (void); 65void print_usage (void);
58void print_help (void); 66void print_help (void);
59 67
60int warn_percent = 0; 68threshold_t warn;
61int crit_percent = 0; 69threshold_t crit;
62float warn_size_bytes = 0;
63float crit_size_bytes = 0;
64int verbose; 70int verbose;
65int allswaps; 71int allswaps;
66int no_swap_state = STATE_CRITICAL; 72int no_swap_state = STATE_CRITICAL;
@@ -68,9 +74,10 @@ int no_swap_state = STATE_CRITICAL;
68int 74int
69main (int argc, char **argv) 75main (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,18 +140,23 @@ 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 [%lu (%d%%)]", status, dskfree_mb, 100 - percent);
134 } 146 }
135 } 147 }
148
149 /*
150 * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123"
151 * This format exists at least on Debian Linux with a 5.* kernel
152 */
136 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) { 153 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) {
137 if (verbose >= 3) { 154 if (verbose >= 3) {
138 printf("Got %s with %f\n", str, tmp_mb); 155 printf("Got %s with %lu\n", str, tmp_KB);
139 } 156 }
140 /* I think this part is always in Kb, so convert to mb */ 157 /* I think this part is always in Kb, so convert to mb */
141 if (strcmp ("Total", str) == 0) { 158 if (strcmp ("Total", str) == 0) {
142 dsktotal_mb = tmp_mb / 1024; 159 dsktotal_mb = tmp_KB / 1024;
143 } 160 }
144 else if (strcmp ("Free", str) == 0) { 161 else if (strcmp ("Free", str) == 0) {
145 dskfree_mb = dskfree_mb + tmp_mb / 1024; 162 dskfree_mb = dskfree_mb + tmp_mb / 1024;
@@ -230,7 +247,7 @@ main (int argc, char **argv)
230 free_swap_mb += dskfree_mb; 247 free_swap_mb += dskfree_mb;
231 if (allswaps) { 248 if (allswaps) {
232 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 249 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
233 result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); 250 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb));
234 if (verbose) 251 if (verbose)
235 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 252 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
236 } 253 }
@@ -292,7 +309,7 @@ main (int argc, char **argv)
292 309
293 if(allswaps && dsktotal_mb > 0){ 310 if(allswaps && dsktotal_mb > 0){
294 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 311 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
295 result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); 312 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb));
296 if (verbose) { 313 if (verbose) {
297 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 314 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
298 } 315 }
@@ -331,7 +348,7 @@ main (int argc, char **argv)
331 348
332 if(allswaps && dsktotal_mb > 0){ 349 if(allswaps && dsktotal_mb > 0){
333 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 350 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
334 result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); 351 result = max_state (result, check_swap(dskfree_mb, dsktotal_mb));
335 if (verbose) { 352 if (verbose) {
336 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 353 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
337 } 354 }
@@ -358,14 +375,19 @@ main (int argc, char **argv)
358 status = "- Swap is either disabled, not present, or of zero size. "; 375 status = "- Swap is either disabled, not present, or of zero size. ";
359 } 376 }
360 377
361 result = max_state (result, check_swap (percent_used, free_swap_mb, total_swap_mb)); 378 result = max_state (result, check_swap(free_swap_mb, total_swap_mb));
362 printf (_("SWAP %s - %d%% free (%d MB out of %d MB) %s|"), 379 printf (_("SWAP %s - %d%% free (%dMB out of %dMB) %s|"),
363 state_text (result), 380 state_text (result),
364 (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status); 381 (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status);
365 382
366 puts (perfdata ("swap", (long) free_swap_mb, "MB", 383 uint64_t warn_print = warn.value;
367 TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_swap_mb), 384 if (warn.is_percentage) warn_print = warn.value * (total_swap_mb *1024 *1024/100);
368 TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_swap_mb), 385 uint64_t crit_print = crit.value;
386 if (crit.is_percentage) crit_print = crit.value * (total_swap_mb *1024 *1024/100);
387
388 puts (perfdata_uint64 ("swap", free_swap_mb *1024 *1024, "B",
389 TRUE, warn_print,
390 TRUE, crit_print,
369 TRUE, 0, 391 TRUE, 0,
370 TRUE, (long) total_swap_mb)); 392 TRUE, (long) total_swap_mb));
371 393
@@ -373,26 +395,37 @@ main (int argc, char **argv)
373} 395}
374 396
375 397
376
377int 398int
378check_swap (int usp, float free_swap_mb, float total_swap_mb) 399check_swap(float free_swap_mb, float total_swap_mb)
379{ 400{
380 401
381 if (!total_swap_mb) return no_swap_state; 402 if (!total_swap_mb) return no_swap_state;
382 403
383 int result = STATE_UNKNOWN; 404 uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */
384 float free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ 405
385 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent)) 406 if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL;
386 result = STATE_CRITICAL; 407 if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING;
387 else if (crit_size_bytes > 0 && free_swap <= crit_size_bytes) 408
388 result = STATE_CRITICAL; 409
389 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent)) 410 uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100;
390 result = STATE_WARNING; 411
391 else if (warn_size_bytes > 0 && free_swap <= warn_size_bytes) 412 if (crit.is_percentage &&
392 result = STATE_WARNING; 413 usage_percentage >= 0 &&
393 else if (usp >= 0.0) 414 crit.value != 0 &&
394 result = STATE_OK; 415 usage_percentage >= (100 - crit.value))
395 return result; 416 {
417 return STATE_CRITICAL;
418 }
419
420 if (warn.is_percentage &&
421 usage_percentage >= 0 &&
422 warn.value != 0 &&
423 usage_percentage >= (100 - warn.value))
424 {
425 return STATE_WARNING;
426 }
427
428 return STATE_OK;
396} 429}
397 430
398 431
@@ -425,42 +458,68 @@ process_arguments (int argc, char **argv)
425 break; 458 break;
426 459
427 switch (c) { 460 switch (c) {
428 case 'w': /* warning size threshold */ 461 case 'w': /* warning size threshold */
429 if (is_intnonneg (optarg)) { 462 {
430 warn_size_bytes = (float) atoi (optarg); 463 /*
431 break; 464 * We expect either a positive integer value without a unit, which means
432 } 465 * the unit is Bytes or a positive integer value and a percentage sign (%),
433 else if (strstr (optarg, ",") && 466 * which means the value must be with 0 and 100 and is relative to the total swap
434 strstr (optarg, "%") && 467 */
435 sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) { 468 size_t length;
436 warn_size_bytes = floorf(warn_size_bytes); 469 length = strlen(optarg);
437 break; 470
438 } 471 if (optarg[length - 1] == '%') {
439 else if (strstr (optarg, "%") && 472 /* It's percentage */
440 sscanf (optarg, "%d%%", &warn_percent) == 1) { 473 warn.is_percentage = 1;
441 break; 474 optarg[length - 1] = '\0';
442 } 475 if (is_uint64(optarg, &warn.value)) {
443 else { 476 if (warn.value > 100) {
444 usage4 (_("Warning threshold must be integer or percentage!")); 477 usage4 (_("Warning threshold percentage must be <= 100!"));
445 } 478 } else {
446 case 'c': /* critical size threshold */ 479 break;
447 if (is_intnonneg (optarg)) { 480 }
448 crit_size_bytes = (float) atoi (optarg); 481 }
449 break; 482 } else {
450 } 483 /* It's Bytes */
451 else if (strstr (optarg, ",") && 484 warn.is_percentage = 0;
452 strstr (optarg, "%") && 485 if (is_uint64(optarg, &warn.value)) {
453 sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) { 486 break;
454 crit_size_bytes = floorf(crit_size_bytes); 487 } else {
455 break; 488 usage4 (_("Warning threshold be positive integer or percentage!"));
456 } 489 }
457 else if (strstr (optarg, "%") && 490 }
458 sscanf (optarg, "%d%%", &crit_percent) == 1) {
459 break;
460 }
461 else {
462 usage4 (_("Critical threshold must be integer or percentage!"));
463 } 491 }
492 case 'c': /* critical size threshold */
493 {
494 /*
495 * We expect either a positive integer value without a unit, which means
496 * the unit is Bytes or a positive integer value and a percentage sign (%),
497 * which means the value must be with 0 and 100 and is relative to the total swap
498 */
499 size_t length;
500 length = strlen(optarg);
501
502 if (optarg[length - 1] == '%') {
503 /* It's percentage */
504 crit.is_percentage = 1;
505 optarg[length - 1] = '\0';
506 if (is_uint64(optarg, &crit.value)) {
507 if (crit.value> 100) {
508 usage4 (_("Critical threshold percentage must be <= 100!"));
509 } else {
510 break;
511 }
512 }
513 } else {
514 /* It's Bytes */
515 crit.is_percentage = 0;
516 if (is_uint64(optarg, &crit.value)) {
517 break;
518 } else {
519 usage4 (_("Critical threshold be positive integer or percentage!"));
520 }
521 }
522 }
464 case 'a': /* all swap */ 523 case 'a': /* all swap */
465 allswaps = TRUE; 524 allswaps = TRUE;
466 break; 525 break;
@@ -485,23 +544,6 @@ process_arguments (int argc, char **argv)
485 c = optind; 544 c = optind;
486 if (c == argc) 545 if (c == argc)
487 return validate_arguments (); 546 return validate_arguments ();
488 if (warn_percent == 0 && is_intnonneg (argv[c]))
489 warn_percent = atoi (argv[c++]);
490
491 if (c == argc)
492 return validate_arguments ();
493 if (crit_percent == 0 && is_intnonneg (argv[c]))
494 crit_percent = atoi (argv[c++]);
495
496 if (c == argc)
497 return validate_arguments ();
498 if (warn_size_bytes == 0 && is_intnonneg (argv[c]))
499 warn_size_bytes = (float) atoi (argv[c++]);
500
501 if (c == argc)
502 return validate_arguments ();
503 if (crit_size_bytes == 0 && is_intnonneg (argv[c]))
504 crit_size_bytes = (float) atoi (argv[c++]);
505 547
506 return validate_arguments (); 548 return validate_arguments ();
507} 549}
@@ -511,17 +553,15 @@ process_arguments (int argc, char **argv)
511int 553int
512validate_arguments (void) 554validate_arguments (void)
513{ 555{
514 if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0 556 if (warn.value == 0 && crit.value == 0) {
515 && crit_size_bytes == 0) {
516 return ERROR; 557 return ERROR;
517 } 558 }
518 else if (warn_percent < crit_percent) { 559 else if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) {
519 usage4 560 /* This is NOT triggered if warn and crit are different units, e.g warn is percentage
520 (_("Warning percentage should be more than critical percentage")); 561 * and crit is absolut. We cannot determine the condition at this point since we
521 } 562 * dont know the value of total swap yet
522 else if (warn_size_bytes < crit_size_bytes) { 563 */
523 usage4 564 usage4(_("Warning should be more than critical"));
524 (_("Warning free space should be more than critical free space"));
525 } 565 }
526 return OK; 566 return OK;
527} 567}
@@ -537,7 +577,7 @@ print_help (void)
537 577
538 printf ("%s\n", _("Check swap space on local machine.")); 578 printf ("%s\n", _("Check swap space on local machine."));
539 579
540 printf ("\n\n"); 580 printf ("\n\n");
541 581
542 print_usage (); 582 print_usage ();
543 583
@@ -545,33 +585,32 @@ print_help (void)
545 printf (UT_EXTRA_OPTS); 585 printf (UT_EXTRA_OPTS);
546 586
547 printf (" %s\n", "-w, --warning=INTEGER"); 587 printf (" %s\n", "-w, --warning=INTEGER");
548 printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free")); 588 printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free"));
549 printf (" %s\n", "-w, --warning=PERCENT%%"); 589 printf (" %s\n", "-w, --warning=PERCENT%");
550 printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free")); 590 printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free"));
551 printf (" %s\n", "-c, --critical=INTEGER"); 591 printf (" %s\n", "-c, --critical=INTEGER");
552 printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free")); 592 printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free"));
553 printf (" %s\n", "-c, --critical=PERCENT%%"); 593 printf (" %s\n", "-c, --critical=PERCENT%");
554 printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free")); 594 printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free"));
555 printf (" %s\n", "-a, --allswaps"); 595 printf (" %s\n", "-a, --allswaps");
556 printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one")); 596 printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one"));
557 printf (" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>"); 597 printf (" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>");
558 printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(no_swap_state)); 598 printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(no_swap_state));
559 printf (UT_VERBOSE); 599 printf (UT_VERBOSE);
560 600
561 printf ("\n"); 601 printf ("\n");
562 printf ("%s\n", _("Notes:")); 602 printf ("%s\n", _("Notes:"));
563 printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); 603 printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked."));
564 printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); 604 printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s."));
565 605
566 printf (UT_SUPPORT); 606 printf (UT_SUPPORT);
567} 607}
568 608
569 609
570
571void 610void
572print_usage (void) 611print_usage (void)
573{ 612{
574 printf ("%s\n", _("Usage:")); 613 printf ("%s\n", _("Usage:"));
575 printf (" %s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname); 614 printf (" %s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname);
576 printf (" -w <bytes_free> -c <bytes_free> [-n <state>]\n"); 615 printf (" -w <bytes_free> -c <bytes_free> [-n <state>]\n");
577} 616}