summaryrefslogtreecommitdiffstats
path: root/plugins/check_swap.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_swap.c')
-rw-r--r--plugins/check_swap.c87
1 files changed, 42 insertions, 45 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index cd965e31..e7ee785d 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -4,7 +4,7 @@
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) 6* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
7* Copyright (c) 2000-2007 Monitoring Plugins Development Team 7* Copyright (c) 2000-2024 Monitoring Plugins Development Team
8* 8*
9* Description: 9* Description:
10* 10*
@@ -28,7 +28,7 @@
28*****************************************************************************/ 28*****************************************************************************/
29 29
30const char *progname = "check_swap"; 30const char *progname = "check_swap";
31const char *copyright = "2000-2007"; 31const char *copyright = "2000-2024";
32const char *email = "devel@monitoring-plugins.org"; 32const char *email = "devel@monitoring-plugins.org";
33 33
34#include "common.h" 34#include "common.h"
@@ -52,9 +52,9 @@ const char *email = "devel@monitoring-plugins.org";
52#endif 52#endif
53 53
54typedef struct { 54typedef struct {
55 int is_percentage; 55 bool is_percentage;
56 uint64_t value; 56 uint64_t value;
57} threshold_t; 57} threshold;
58 58
59int check_swap (float free_swap_mb, float total_swap_mb); 59int check_swap (float free_swap_mb, float total_swap_mb);
60int process_arguments (int argc, char **argv); 60int process_arguments (int argc, char **argv);
@@ -62,10 +62,10 @@ int validate_arguments (void);
62void print_usage (void); 62void print_usage (void);
63void print_help (void); 63void print_help (void);
64 64
65threshold_t warn; 65threshold warn;
66threshold_t crit; 66threshold crit;
67int verbose; 67int verbose;
68int allswaps; 68bool allswaps = false;
69int no_swap_state = STATE_CRITICAL; 69int no_swap_state = STATE_CRITICAL;
70 70
71int 71int
@@ -383,10 +383,10 @@ main (int argc, char **argv)
383 if (crit.is_percentage) crit_print = crit.value * (total_swap_mb *1024 *1024/100); 383 if (crit.is_percentage) crit_print = crit.value * (total_swap_mb *1024 *1024/100);
384 384
385 puts (perfdata_uint64 ("swap", free_swap_mb *1024 *1024, "B", 385 puts (perfdata_uint64 ("swap", free_swap_mb *1024 *1024, "B",
386 TRUE, warn_print, 386 true, warn_print,
387 TRUE, crit_print, 387 true, crit_print,
388 TRUE, 0, 388 true, 0,
389 TRUE, (long) total_swap_mb * 1024 * 1024)); 389 true, (long) total_swap_mb * 1024 * 1024));
390 390
391 return result; 391 return result;
392} 392}
@@ -399,28 +399,30 @@ check_swap(float free_swap_mb, float total_swap_mb)
399 if (!total_swap_mb) return no_swap_state; 399 if (!total_swap_mb) return no_swap_state;
400 400
401 uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ 401 uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */
402
403 if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL;
404 if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING;
405
406
407 uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100; 402 uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100;
408 403
409 if (crit.is_percentage && 404 if (warn.value || crit.value) { /* Thresholds defined */
410 crit.value != 0 && 405 if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL;
411 usage_percentage >= (100 - crit.value)) 406 if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING;
412 { 407
413 return STATE_CRITICAL; 408 if (crit.is_percentage &&
414 } 409 crit.value != 0 &&
415 410 usage_percentage >= (100 - crit.value))
416 if (warn.is_percentage && 411 {
417 warn.value != 0 && 412 return STATE_CRITICAL;
418 usage_percentage >= (100 - warn.value)) 413 }
419 { 414
420 return STATE_WARNING; 415 if (warn.is_percentage &&
421 } 416 warn.value != 0 &&
422 417 usage_percentage >= (100 - warn.value))
423 return STATE_OK; 418 {
419 return STATE_WARNING;
420 }
421
422 return STATE_OK;
423 } else { /* Without thresholds */
424 return STATE_OK;
425 }
424} 426}
425 427
426 428
@@ -443,9 +445,6 @@ process_arguments (int argc, char **argv)
443 {0, 0, 0, 0} 445 {0, 0, 0, 0}
444 }; 446 };
445 447
446 if (argc < 2)
447 return ERROR;
448
449 while (1) { 448 while (1) {
450 c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option); 449 c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option);
451 450
@@ -465,7 +464,7 @@ process_arguments (int argc, char **argv)
465 464
466 if (optarg[length - 1] == '%') { 465 if (optarg[length - 1] == '%') {
467 /* It's percentage */ 466 /* It's percentage */
468 warn.is_percentage = 1; 467 warn.is_percentage = true;
469 optarg[length - 1] = '\0'; 468 optarg[length - 1] = '\0';
470 if (is_uint64(optarg, &warn.value)) { 469 if (is_uint64(optarg, &warn.value)) {
471 if (warn.value > 100) { 470 if (warn.value > 100) {
@@ -475,7 +474,7 @@ process_arguments (int argc, char **argv)
475 break; 474 break;
476 } else { 475 } else {
477 /* It's Bytes */ 476 /* It's Bytes */
478 warn.is_percentage = 0; 477 warn.is_percentage = false;
479 if (is_uint64(optarg, &warn.value)) { 478 if (is_uint64(optarg, &warn.value)) {
480 break; 479 break;
481 } else { 480 } else {
@@ -495,7 +494,7 @@ process_arguments (int argc, char **argv)
495 494
496 if (optarg[length - 1] == '%') { 495 if (optarg[length - 1] == '%') {
497 /* It's percentage */ 496 /* It's percentage */
498 crit.is_percentage = 1; 497 crit.is_percentage = true;
499 optarg[length - 1] = '\0'; 498 optarg[length - 1] = '\0';
500 if (is_uint64(optarg, &crit.value)) { 499 if (is_uint64(optarg, &crit.value)) {
501 if (crit.value> 100) { 500 if (crit.value> 100) {
@@ -505,7 +504,7 @@ process_arguments (int argc, char **argv)
505 break; 504 break;
506 } else { 505 } else {
507 /* It's Bytes */ 506 /* It's Bytes */
508 crit.is_percentage = 0; 507 crit.is_percentage = false;
509 if (is_uint64(optarg, &crit.value)) { 508 if (is_uint64(optarg, &crit.value)) {
510 break; 509 break;
511 } else { 510 } else {
@@ -514,7 +513,7 @@ process_arguments (int argc, char **argv)
514 } 513 }
515 } 514 }
516 case 'a': /* all swap */ 515 case 'a': /* all swap */
517 allswaps = TRUE; 516 allswaps = true;
518 break; 517 break;
519 case 'n': 518 case 'n':
520 if ((no_swap_state = mp_translate_state(optarg)) == ERROR) { 519 if ((no_swap_state = mp_translate_state(optarg)) == ERROR) {
@@ -547,10 +546,7 @@ process_arguments (int argc, char **argv)
547int 546int
548validate_arguments (void) 547validate_arguments (void)
549{ 548{
550 if (warn.value == 0 && crit.value == 0) { 549 if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) {
551 return ERROR;
552 }
553 else if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) {
554 /* This is NOT triggered if warn and crit are different units, e.g warn is percentage 550 /* This is NOT triggered if warn and crit are different units, e.g warn is percentage
555 * and crit is absolute. We cannot determine the condition at this point since we 551 * and crit is absolute. We cannot determine the condition at this point since we
556 * dont know the value of total swap yet 552 * dont know the value of total swap yet
@@ -595,6 +591,7 @@ print_help (void)
595 printf ("\n"); 591 printf ("\n");
596 printf ("%s\n", _("Notes:")); 592 printf ("%s\n", _("Notes:"));
597 printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); 593 printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked."));
594 printf (" %s\n", _("Without thresholds, the plugin shows free swap space and performance data, but always returns OK."));
598 printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); 595 printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s."));
599 596
600 printf (UT_SUPPORT); 597 printf (UT_SUPPORT);
@@ -605,6 +602,6 @@ void
605print_usage (void) 602print_usage (void)
606{ 603{
607 printf ("%s\n", _("Usage:")); 604 printf ("%s\n", _("Usage:"));
608 printf (" %s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname); 605 printf (" %s [-av] [-w <percent_free>%%] [-c <percent_free>%%]\n",progname);
609 printf (" -w <bytes_free> -c <bytes_free> [-n <state>]\n"); 606 printf (" [-w <bytes_free>] [-c <bytes_free>] [-n <state>]\n");
610} 607}