From 8698a6d976012908ea0af36ca1a520c3111928c7 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 23 Mar 2024 11:22:06 +0100 Subject: check_swap: replace another fake boolen and small improvements (#1996) * check_swap: Change another fake boolen to a real one * check_swap: Rename type since *_t is reserved for C standard types * check_swap: Update copyright --- plugins/check_swap.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'plugins/check_swap.c') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index fddd93fa..499a8d2c 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -4,7 +4,7 @@ * * License: GPL * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) -* Copyright (c) 2000-2007 Monitoring Plugins Development Team +* Copyright (c) 2000-2024 Monitoring Plugins Development Team * * Description: * @@ -28,7 +28,7 @@ *****************************************************************************/ const char *progname = "check_swap"; -const char *copyright = "2000-2007"; +const char *copyright = "2000-2024"; const char *email = "devel@monitoring-plugins.org"; #include "common.h" @@ -52,9 +52,9 @@ const char *email = "devel@monitoring-plugins.org"; #endif typedef struct { - int is_percentage; + bool is_percentage; uint64_t value; -} threshold_t; +} threshold; int check_swap (float free_swap_mb, float total_swap_mb); int process_arguments (int argc, char **argv); @@ -62,8 +62,8 @@ int validate_arguments (void); void print_usage (void); void print_help (void); -threshold_t warn; -threshold_t crit; +threshold warn; +threshold crit; int verbose; bool allswaps = false; int no_swap_state = STATE_CRITICAL; @@ -465,7 +465,7 @@ process_arguments (int argc, char **argv) if (optarg[length - 1] == '%') { /* It's percentage */ - warn.is_percentage = 1; + warn.is_percentage = true; optarg[length - 1] = '\0'; if (is_uint64(optarg, &warn.value)) { if (warn.value > 100) { @@ -475,7 +475,7 @@ process_arguments (int argc, char **argv) break; } else { /* It's Bytes */ - warn.is_percentage = 0; + warn.is_percentage = false; if (is_uint64(optarg, &warn.value)) { break; } else { @@ -495,7 +495,7 @@ process_arguments (int argc, char **argv) if (optarg[length - 1] == '%') { /* It's percentage */ - crit.is_percentage = 1; + crit.is_percentage = true; optarg[length - 1] = '\0'; if (is_uint64(optarg, &crit.value)) { if (crit.value> 100) { @@ -505,7 +505,7 @@ process_arguments (int argc, char **argv) break; } else { /* It's Bytes */ - crit.is_percentage = 0; + crit.is_percentage = false; if (is_uint64(optarg, &crit.value)) { break; } else { -- cgit v1.2.3-74-g34f1 From ee0f70486f722e70d56eea2f7f3251a2d435e12a Mon Sep 17 00:00:00 2001 From: Napsty Date: Fri, 12 Apr 2024 07:51:47 +0200 Subject: Possibility to run check_swap without thresholds --- plugins/check_swap.c | 59 +++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) (limited to 'plugins/check_swap.c') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 499a8d2c..56c5f42b 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -399,28 +399,30 @@ check_swap(float free_swap_mb, float total_swap_mb) if (!total_swap_mb) return no_swap_state; uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ - - if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL; - if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING; - - uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100; - if (crit.is_percentage && - crit.value != 0 && - usage_percentage >= (100 - crit.value)) - { - return STATE_CRITICAL; - } - - if (warn.is_percentage && - warn.value != 0 && - usage_percentage >= (100 - warn.value)) - { - return STATE_WARNING; - } - - return STATE_OK; + if (warn.value && crit.value) { /* Thresholds defined */ + if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL; + if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING; + + if (crit.is_percentage && + crit.value != 0 && + usage_percentage >= (100 - crit.value)) + { + return STATE_CRITICAL; + } + + if (warn.is_percentage && + warn.value != 0 && + usage_percentage >= (100 - warn.value)) + { + return STATE_WARNING; + } + + return STATE_OK; + } else { /* Without thresholds */ + return STATE_OK; + } } @@ -443,9 +445,6 @@ process_arguments (int argc, char **argv) {0, 0, 0, 0} }; - if (argc < 2) - return ERROR; - while (1) { c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option); @@ -547,9 +546,12 @@ process_arguments (int argc, char **argv) int validate_arguments (void) { - if (warn.value == 0 && crit.value == 0) { - return ERROR; - } + if (warn.value && !crit.value) { + usage4(_("Must define both warning and critical thresholds")); + } + else if (crit.value && !warn.value) { + usage4(_("Must define both warning and critical thresholds")); + } else if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) { /* This is NOT triggered if warn and crit are different units, e.g warn is percentage * and crit is absolute. We cannot determine the condition at this point since we @@ -595,6 +597,7 @@ print_help (void) printf ("\n"); printf ("%s\n", _("Notes:")); printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); + printf (" %s\n", _("Without thresholds, the plugin shows free swap space and performance data, but always returns OK.")); printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); printf (UT_SUPPORT); @@ -605,6 +608,6 @@ void print_usage (void) { printf ("%s\n", _("Usage:")); - printf (" %s [-av] -w %% -c %%\n",progname); - printf (" -w -c [-n ]\n"); + printf (" %s [-av] [-w %%] [-c %%]\n",progname); + printf (" [-w ] [-c ] [-n ]\n"); } -- cgit v1.2.3-74-g34f1 From 9b4fab066492ca4065adfdf1080086fe6ffa53c2 Mon Sep 17 00:00:00 2001 From: Napsty Date: Fri, 12 Apr 2024 10:47:55 +0200 Subject: Allow single threshold --- plugins/check_swap.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'plugins/check_swap.c') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 56c5f42b..e7ee785d 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -401,7 +401,7 @@ check_swap(float free_swap_mb, float total_swap_mb) uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100; - if (warn.value && crit.value) { /* Thresholds defined */ + if (warn.value || crit.value) { /* Thresholds defined */ if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL; if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING; @@ -546,13 +546,7 @@ process_arguments (int argc, char **argv) int validate_arguments (void) { - if (warn.value && !crit.value) { - usage4(_("Must define both warning and critical thresholds")); - } - else if (crit.value && !warn.value) { - usage4(_("Must define both warning and critical thresholds")); - } - else if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) { + if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) { /* This is NOT triggered if warn and crit are different units, e.g warn is percentage * and crit is absolute. We cannot determine the condition at this point since we * dont know the value of total swap yet -- cgit v1.2.3-74-g34f1