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 diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 499a8d2..56c5f42 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 v0.10-9-g596f