From 5acd14fcfb419c9ef0d6bc38384dde4cd6b70bd9 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 18 Feb 2025 21:58:59 +0100 Subject: Implement new output functionality for check_swap --- plugins/check_swap.c | 132 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 98 insertions(+), 34 deletions(-) (limited to 'plugins/check_swap.c') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index bc90a90b..1f2d0273 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -28,6 +28,9 @@ *****************************************************************************/ #include "common.h" +#include "output.h" +#include "states.h" +#include #ifdef HAVE_DECL_SWAPCTL # ifdef HAVE_SYS_PARAM_H # include @@ -69,8 +72,6 @@ int main(int argc, char **argv) { bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - char *status = strdup(""); - /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); @@ -90,59 +91,101 @@ int main(int argc, char **argv) { } double percent_used; + mp_check overall = mp_check_init(); + if (config.output_format_is_set) { + overall.format = config.output_format; + } + mp_subcheck sc1 = mp_subcheck_init(); + sc1 = mp_set_subcheck_default_state(sc1, STATE_OK); + /* if total_swap_mb == 0, let's not divide by 0 */ if (data.metrics.total != 0) { percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); } else { - printf(_("SWAP %s - Swap is either disabled, not present, or of zero " - "size."), - state_text(data.statusCode)); - exit(config.no_swap_state); + sc1 = mp_set_subcheck_state(sc1, config.no_swap_state); + sc1.output = (char *)_("Swap is either disabled, not present, or of zero size."); + + mp_add_subcheck_to_check(&overall, sc1); + mp_exit(overall); } if (verbose) { printf("Computed usage percentage: %g\n", percent_used); } - uint64_t warn_print = config.warn.value; - if (config.warn.is_percentage) { - warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT); + mp_perfdata pd = perfdata_init(); + pd.label = "swap"; + pd = mp_set_pd_value(pd, data.metrics.free); + pd.uom = "B"; + + if (config.warn_is_set) { + uint64_t warn_print = config.warn.value; + if (config.warn.is_percentage) { + warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT); + } + + mp_perfdata_value warn_pd = mp_create_pd_value(warn_print); + + mp_range warn_range = mp_range_init(); + warn_range.end_infinity = false; + warn_range.end = warn_pd; + + pd.warn = warn_range; + pd.warn_present = true; } - uint64_t crit_print = config.crit.value; - if (config.crit.is_percentage) { - crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); + if (config.crit_is_set) { + uint64_t crit_print = config.crit.value; + if (config.crit.is_percentage) { + crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); + } + + mp_perfdata_value crit_pd = mp_create_pd_value(crit_print); + + mp_range crit_range = mp_range_init(); + crit_range.end_infinity = false; + crit_range.end = crit_pd; + + pd.crit = crit_range; + pd.crit_present = true; } - 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); + mp_perfdata_value max = mp_create_pd_value(data.metrics.total); + pd.max = max; + pd.max_present = true; + + mp_perfdata_value min = mp_create_pd_value(0); + pd.min = min; + pd.min_present = true; + + mp_add_perfdata_to_subcheck(&sc1, pd); + 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 >= (100 - (double)config.warn.value))) || config.warn.value >= data.metrics.free) { + sc1 = mp_set_subcheck_state(sc1, 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 >= (100 - (double)config.crit.value))) || config.crit.value >= data.metrics.free) { + sc1 = mp_set_subcheck_state(sc1, STATE_CRITICAL); } } - printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), - BYTES_TO_MiB(data.metrics.free), BYTES_TO_MiB(data.metrics.total), status, perfdata); + xasprintf(&sc1.output, _("%g%% free (%lluMiB out of %lluMiB)"), (100 - percent_used), data.metrics.free >> 20, + data.metrics.total >> 20); + + overall.summary = "Swap"; + mp_add_subcheck_to_check(&overall, sc1); - exit(data.statusCode); + mp_exit(overall); } int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { @@ -172,15 +215,22 @@ int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { return STATE_OK; } +#define output_format_index CHAR_MAX + 1 + /* process command-line arguments */ swap_config_wrapper process_arguments(int argc, char **argv) { swap_config_wrapper conf_wrapper = {.errorcode = OK}; conf_wrapper.config = swap_config_init(); - 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'}, - {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; + 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'}, + {"help", no_argument, 0, 'h'}, + {"output-format", required_argument, 0, output_format_index}, + {0, 0, 0, 0}}; while (true) { int option = 0; @@ -263,6 +313,18 @@ swap_config_wrapper process_arguments(int argc, char **argv) { case 'v': /* verbose */ verbose++; break; + case output_format_index: { + parsed_output_format parser = mp_parse_output_format(optarg); + if (!parser.parsing_success) { + // TODO List all available formats here, maybe add anothoer usage function + printf("Invalid output format: %s\n", optarg); + exit(STATE_UNKNOWN); + } + + conf_wrapper.config.output_format_is_set = true; + conf_wrapper.config.output_format = parser.output_format; + break; + } case 'V': /* version */ print_revision(progname, NP_VERSION); exit(STATE_UNKNOWN); @@ -319,6 +381,8 @@ void print_help(swap_config config) { _("Resulting state when there is no swap regardless of thresholds. " "Default:"), state_text(config.no_swap_state)); + printf(" %s\n", "--output-format"); + printf(" %s\n", _("Select output format. Valid values: \"one-line\", \"icingaweb2\", \"summary-only\", \"mp-test-json\"")); printf(UT_VERBOSE); printf("\n"); -- cgit v1.2.3-74-g34f1 From 3cd29d86cc51b763a0cf706e64884602cb3c9314 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 20 Feb 2025 23:45:13 +0100 Subject: Remove output formats one-line and summary-only --- lib/output.c | 43 ------------------------------------------- lib/output.h | 2 -- plugins/check_swap.c | 2 +- 3 files changed, 1 insertion(+), 46 deletions(-) (limited to 'plugins/check_swap.c') diff --git a/lib/output.c b/lib/output.c index 2c537a01..07a77165 100644 --- a/lib/output.c +++ b/lib/output.c @@ -235,35 +235,6 @@ char *mp_fmt_output(mp_check check) { char *result = NULL; switch (check.format) { - case MP_FORMAT_SUMMARY_ONLY: - if (check.summary == NULL) { - check.summary = get_subcheck_summary(check); - } - - asprintf(&result, "%s: %s", state_text(mp_compute_check_state(check)), check.summary); - return result; - - case MP_FORMAT_ONE_LINE: { - /* SERVICE STATUS: First line of output | First part of performance data - * Any number of subsequent lines of output, but note that buffers - * may have a limited size | Second part of performance data, which - * may have continuation lines, too - */ - if (check.summary == NULL) { - check.summary = get_subcheck_summary(check); - } - - asprintf(&result, "%s: %s", state_text(mp_compute_check_state(check)), check.summary); - - mp_subcheck_list *subchecks = check.subchecks; - - while (subchecks != NULL) { - asprintf(&result, "%s - %s", result, fmt_subcheck_output(MP_FORMAT_ONE_LINE, subchecks->subcheck, 1)); - subchecks = subchecks->next; - } - - return result; - } case MP_FORMAT_ICINGA_WEB_2: { if (check.summary == NULL) { check.summary = get_subcheck_summary(check); @@ -370,18 +341,6 @@ static inline char *fmt_subcheck_output(mp_output_format output_format, mp_subch subchecks = subchecks->next; } return result; - case MP_FORMAT_ONE_LINE: - asprintf(&result, "[%s] - %s", state_text(mp_compute_subcheck_state(check)), check.output); - - subchecks = check.subchecks; - - while (subchecks != NULL) { - asprintf(&result, " - %s\n%s", result, fmt_subcheck_output(output_format, subchecks->subcheck, indentation + 1)); - subchecks = subchecks->next; - } - return result; - case MP_FORMAT_SUMMARY_ONLY: - return result; default: die(STATE_UNKNOWN, "Invalid format"); } @@ -551,9 +510,7 @@ mp_subcheck mp_set_subcheck_default_state(mp_subcheck check, mp_state_enum state } char *mp_output_format_map[] = { - [MP_FORMAT_ONE_LINE] = "one-line", [MP_FORMAT_ICINGA_WEB_2] = "icingaweb2", - [MP_FORMAT_SUMMARY_ONLY] = "summary-only", [MP_FORMAT_TEST_JSON] = "mp-test-json", }; diff --git a/lib/output.h b/lib/output.h index c7455d29..14c4bcf4 100644 --- a/lib/output.h +++ b/lib/output.h @@ -29,9 +29,7 @@ typedef struct subcheck_list { * Possible output formats */ typedef enum output_format { - MP_FORMAT_ONE_LINE, MP_FORMAT_ICINGA_WEB_2, - MP_FORMAT_SUMMARY_ONLY, MP_FORMAT_TEST_JSON, } mp_output_format; diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 1f2d0273..262d8d51 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -382,7 +382,7 @@ void print_help(swap_config config) { "Default:"), state_text(config.no_swap_state)); printf(" %s\n", "--output-format"); - printf(" %s\n", _("Select output format. Valid values: \"one-line\", \"icingaweb2\", \"summary-only\", \"mp-test-json\"")); + printf(" %s\n", _("Select output format. Valid values: \"icingaweb2\", \"mp-test-json\"")); printf(UT_VERBOSE); printf("\n"); -- cgit v1.2.3-74-g34f1 From 07873c765b73c85482436880f122882dfccd7e1b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 20 Feb 2025 23:49:22 +0100 Subject: Place output-format help string with the other common ones --- plugins/check_swap.c | 3 +-- plugins/utils.h | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'plugins/check_swap.c') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 262d8d51..4d3b6099 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -381,8 +381,7 @@ void print_help(swap_config config) { _("Resulting state when there is no swap regardless of thresholds. " "Default:"), state_text(config.no_swap_state)); - printf(" %s\n", "--output-format"); - printf(" %s\n", _("Select output format. Valid values: \"icingaweb2\", \"mp-test-json\"")); + printf(UT_OUTPUT_FORMAT); printf(UT_VERBOSE); printf("\n"); diff --git a/plugins/utils.h b/plugins/utils.h index c7073990..bc26f704 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -195,4 +195,8 @@ The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ copies of the plugins under the terms of the GNU General Public License.\n\ For more information about these matters, see the file named COPYING.\n") +#define UT_OUTPUT_FORMAT _("\ + --output-format=OUTPUT_FORMAT\n\ + Select output format. Valid values: \"icingaweb2\", \"mp-test-json\"\n") + #endif /* NP_UTILS_H */ -- cgit v1.2.3-74-g34f1