From 2dec5182c508bd3cc286b4649836ead51aec50ef Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:18:51 +0100 Subject: check_swap: refactor to improve readability --- plugins/check_swap.d/check_swap.h | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 plugins/check_swap.d/check_swap.h (limited to 'plugins/check_swap.d/check_swap.h') diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h new file mode 100644 index 00000000..bad52917 --- /dev/null +++ b/plugins/check_swap.d/check_swap.h @@ -0,0 +1,45 @@ +#ifndef CHECK_SWAP_H +#define CHECK_SWAP_H + +#include "../common.h" + +#ifndef SWAP_CONVERSION +#define SWAP_CONVERSION 1 +#endif + +typedef struct { + bool is_percentage; + uint64_t value; +} threshold; + +typedef struct { + unsigned long long free; // Free swap in Bytes! + unsigned long long used; // Used swap in Bytes! + unsigned long long total; // Total swap size, you guessed it, in Bytes! +} swap_metrics; + +typedef struct { + int errorcode; + int statusCode; + swap_metrics metrics; +} swap_result; + +typedef struct { + int verbose; + bool allswaps; + int no_swap_state; + threshold warn; + threshold crit; + bool on_aix; + int conversion_factor; +} swap_config; + +swap_config swap_config_init(); + +swap_result get_swap_data(swap_config config); +swap_result getSwapFromProcMeminfo(swap_config config, char path_to_proc_meminfo[]); +swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]); +swap_result getSwapFromSwapctl_BSD(swap_config config); +swap_result getSwapFromSwap_SRV4(swap_config config); + +#endif -- cgit v1.2.3-74-g34f1 From 651925dffce258bf71a6717fd3d4e0969f29b6a6 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:58:41 +0100 Subject: check_swap: Make check_swap work without thresholds --- plugins/check_swap.c | 11 ++--------- plugins/check_swap.d/check_swap.h | 7 ++++--- plugins/check_swap.d/swap.c | 3 +++ 3 files changed, 9 insertions(+), 12 deletions(-) (limited to 'plugins/check_swap.d/check_swap.h') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index ef60ce1b..94f41a55 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -166,11 +166,6 @@ swap_config_wrapper process_arguments(int argc, char **argv) { swap_config_wrapper conf_wrapper = {.errorcode = OK}; conf_wrapper.config = swap_config_init(); - if (argc < 2) { - conf_wrapper.errorcode = ERROR; - return conf_wrapper; - } - static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"allswaps", no_argument, 0, 'a'}, {"no-swap", required_argument, 0, 'n'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, @@ -195,6 +190,7 @@ swap_config_wrapper process_arguments(int argc, char **argv) { */ size_t length; length = strlen(optarg); + conf_wrapper.config.warn.is_set = true; if (optarg[length - 1] == '%') { /* It's percentage */ @@ -224,6 +220,7 @@ swap_config_wrapper process_arguments(int argc, char **argv) { */ size_t length; length = strlen(optarg); + conf_wrapper.config.crit.is_set = true; if (optarg[length - 1] == '%') { /* It's percentage */ @@ -266,10 +263,6 @@ swap_config_wrapper process_arguments(int argc, char **argv) { } } - if (conf_wrapper.config.warn.value == 0 && conf_wrapper.config.crit.value == 0) { - conf_wrapper.errorcode = ERROR; - return conf_wrapper; - } if ((conf_wrapper.config.warn.is_percentage == conf_wrapper.config.crit.is_percentage) && (conf_wrapper.config.warn.value < conf_wrapper.config.crit.value)) { /* This is NOT triggered if warn and crit are different units, e.g warn diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 9e8be75f..e3e350c5 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h @@ -8,9 +8,10 @@ #endif typedef struct { + bool is_set; bool is_percentage; uint64_t value; -} threshold; +} check_swap_threshold; typedef struct { unsigned long long free; // Free swap in Bytes! @@ -27,8 +28,8 @@ typedef struct { typedef struct { bool allswaps; int no_swap_state; - threshold warn; - threshold crit; + check_swap_threshold warn; + check_swap_threshold crit; bool on_aix; int conversion_factor; } swap_config; diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 18db210c..293fdd71 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -10,6 +10,9 @@ swap_config swap_config_init(void) { tmp.no_swap_state = STATE_CRITICAL; tmp.conversion_factor = SWAP_CONVERSION; + tmp.warn.is_set = false; + tmp.crit.is_set = false; + #ifdef _AIX tmp.on_aix = true; #else -- cgit v1.2.3-74-g34f1 From 152cdcf3e425e11224b3c52cf0863b6825ae0874 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 10:42:17 +0100 Subject: check_swap: change threshold handling again --- plugins/check_swap.c | 35 ++++++++++++++++++++--------------- plugins/check_swap.d/check_swap.h | 3 ++- plugins/check_swap.d/swap.c | 4 ++-- 3 files changed, 24 insertions(+), 18 deletions(-) (limited to 'plugins/check_swap.d/check_swap.h') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index e0c246db..bc90a90b 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -114,24 +114,29 @@ int main(int argc, char **argv) { crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); } - char *perfdata = perfdata_uint64("swap", data.metrics.free, "B", true, warn_print, true, crit_print, true, 0, true, data.metrics.total); + char *perfdata = perfdata_uint64("swap", data.metrics.free, "B", config.warn_is_set, warn_print, config.crit_is_set, crit_print, true, + 0, true, data.metrics.total); - if (verbose > 1) { - printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); - } + if (config.warn_is_set) { + if (verbose > 1) { + printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); + } - if ((config.warn.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.warn.value))) || - config.warn.value >= data.metrics.free) { - data.statusCode = max_state(data.statusCode, STATE_WARNING); + if ((config.warn.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.warn.value))) || + config.warn.value >= data.metrics.free) { + data.statusCode = max_state(data.statusCode, STATE_WARNING); + } } - if (verbose > 1) { - printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); - } + if (config.crit_is_set) { + if (verbose > 1) { + printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); + } - if ((config.crit.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.crit.value))) || - config.crit.value >= data.metrics.free) { - data.statusCode = max_state(data.statusCode, STATE_CRITICAL); + if ((config.crit.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.crit.value))) || + config.crit.value >= data.metrics.free) { + data.statusCode = max_state(data.statusCode, STATE_CRITICAL); + } } printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), @@ -196,7 +201,7 @@ swap_config_wrapper process_arguments(int argc, char **argv) { */ size_t length; length = strlen(optarg); - conf_wrapper.config.warn.is_set = true; + conf_wrapper.config.warn_is_set = true; if (optarg[length - 1] == '%') { /* It's percentage */ @@ -226,7 +231,7 @@ swap_config_wrapper process_arguments(int argc, char **argv) { */ size_t length; length = strlen(optarg); - conf_wrapper.config.crit.is_set = true; + conf_wrapper.config.crit_is_set = true; if (optarg[length - 1] == '%') { /* It's percentage */ diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index e3e350c5..5d878989 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h @@ -8,7 +8,6 @@ #endif typedef struct { - bool is_set; bool is_percentage; uint64_t value; } check_swap_threshold; @@ -28,7 +27,9 @@ typedef struct { typedef struct { bool allswaps; int no_swap_state; + bool warn_is_set; check_swap_threshold warn; + bool crit_is_set; check_swap_threshold crit; bool on_aix; int conversion_factor; diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 6c94b33a..354efdbf 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -11,8 +11,8 @@ swap_config swap_config_init(void) { tmp.no_swap_state = STATE_CRITICAL; tmp.conversion_factor = SWAP_CONVERSION; - tmp.warn.is_set = false; - tmp.crit.is_set = false; + tmp.warn_is_set = false; + tmp.crit_is_set = false; #ifdef _AIX tmp.on_aix = true; -- cgit v1.2.3-74-g34f1 From a581465ca935d59d6627a537456517cef6334380 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:03:16 +0100 Subject: check_swap stuff: Use pragma once instead of include guard --- plugins/check_swap.d/check_swap.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'plugins/check_swap.d/check_swap.h') diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 5d878989..99039b21 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h @@ -1,5 +1,4 @@ -#ifndef CHECK_SWAP_H -#define CHECK_SWAP_H +#pragma once #include "../common.h" @@ -42,5 +41,3 @@ swap_result getSwapFromProcMeminfo(char path_to_proc_meminfo[]); swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]); swap_result getSwapFromSwapctl_BSD(swap_config config); swap_result getSwapFromSwap_SRV4(swap_config config); - -#endif -- cgit v1.2.3-74-g34f1