From 0645c9fc2c7f801ba3c7d68a17c137a63ada299f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 18 Feb 2025 21:58:34 +0100 Subject: Implement new output functionality --- plugins/common.h | 9 +-------- plugins/popen.c | 1 - plugins/runcmd.c | 1 + plugins/sslutils.c | 1 + plugins/utils.c | 48 ------------------------------------------------ plugins/utils.h | 11 ++++------- 6 files changed, 7 insertions(+), 64 deletions(-) (limited to 'plugins') diff --git a/plugins/common.h b/plugins/common.h index 833479ce..47b1e4df 100644 --- a/plugins/common.h +++ b/plugins/common.h @@ -32,6 +32,7 @@ #define _COMMON_H_ #include "config.h" +#include "../lib/monitoringplug.h" #ifdef HAVE_FEATURES_H #include @@ -184,14 +185,6 @@ enum { ERROR = -1 }; -enum { - STATE_OK, - STATE_WARNING, - STATE_CRITICAL, - STATE_UNKNOWN, - STATE_DEPENDENT -}; - enum { DEFAULT_SOCKET_TIMEOUT = 10, /* timeout after 10 seconds */ MAX_INPUT_BUFFER = 8192, /* max size of most buffers we use */ diff --git a/plugins/popen.c b/plugins/popen.c index 2b9824bc..cfe930b6 100644 --- a/plugins/popen.c +++ b/plugins/popen.c @@ -40,7 +40,6 @@ #include "./common.h" #include "./utils.h" -#include "../lib/maxfd.h" /* extern so plugin has pid to kill exec'd process on timeouts */ extern pid_t *childpid; diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 74843149..4429ceb0 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c @@ -40,6 +40,7 @@ /** includes **/ #include "runcmd.h" +#include "../lib/monitoringplug.h" #ifdef HAVE_SYS_WAIT_H # include #endif diff --git a/plugins/sslutils.c b/plugins/sslutils.c index 3c928413..719de575 100644 --- a/plugins/sslutils.c +++ b/plugins/sslutils.c @@ -29,6 +29,7 @@ #define MAX_CN_LENGTH 256 #include "common.h" #include "netutils.h" +#include "../lib/monitoringplug.h" #ifdef HAVE_SSL static SSL_CTX *ctx = NULL; diff --git a/plugins/utils.c b/plugins/utils.c index 6d366e3d..09649429 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -42,54 +42,6 @@ extern const char *progname; time_t start_time, end_time; -/* ************************************************************************** - * max_state(STATE_x, STATE_y) - * compares STATE_x to STATE_y and returns result based on the following - * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL - * - * Note that numerically the above does not hold - ****************************************************************************/ - -int max_state(int a, int b) { - if (a == STATE_CRITICAL || b == STATE_CRITICAL) - return STATE_CRITICAL; - else if (a == STATE_WARNING || b == STATE_WARNING) - return STATE_WARNING; - else if (a == STATE_OK || b == STATE_OK) - return STATE_OK; - else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) - return STATE_UNKNOWN; - else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) - return STATE_DEPENDENT; - else - return max(a, b); -} - -/* ************************************************************************** - * max_state_alt(STATE_x, STATE_y) - * compares STATE_x to STATE_y and returns result based on the following - * STATE_OK < STATE_DEPENDENT < STATE_UNKNOWN < STATE_WARNING < STATE_CRITICAL - * - * The main difference between max_state_alt and max_state it that it doesn't - * allow setting a default to UNKNOWN. It will instead prioritixe any valid - * non-OK state. - ****************************************************************************/ - -int max_state_alt(int a, int b) { - if (a == STATE_CRITICAL || b == STATE_CRITICAL) - return STATE_CRITICAL; - else if (a == STATE_WARNING || b == STATE_WARNING) - return STATE_WARNING; - else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) - return STATE_UNKNOWN; - else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) - return STATE_DEPENDENT; - else if (a == STATE_OK || b == STATE_OK) - return STATE_OK; - else - return max(a, b); -} - void usage(const char *msg) { printf("%s\n", msg); print_usage(); diff --git a/plugins/utils.h b/plugins/utils.h index f939e337..c7073990 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -13,11 +13,11 @@ in order to resist overflow attacks. In addition, a few functions are provided to standardize version and error reporting across the entire suite of plugins. */ -/* now some functions etc are being defined in ../lib/utils_base.c */ -#include "utils_base.h" - +#include "../config.h" #include - +#include +#include +#include #ifdef NP_EXTRA_OPTS /* Include extra-opts functions if compiled in */ @@ -78,9 +78,6 @@ char *strpcat (char *, const char *, const char *); int xvasprintf (char **strp, const char *fmt, va_list ap); int xasprintf (char **strp, const char *fmt, ...); -int max_state (int a, int b); -int max_state_alt (int a, int b); - void usage (const char *) __attribute__((noreturn)); void usage2(const char *, const char *) __attribute__((noreturn)); void usage3(const char *, int) __attribute__((noreturn)); -- cgit v1.2.3-74-g34f1 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 ++++++++++++++++++++++++++++---------- plugins/check_swap.d/check_swap.h | 4 ++ plugins/check_swap.d/swap.c | 2 + 3 files changed, 104 insertions(+), 34 deletions(-) (limited to 'plugins') 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"); diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 99039b21..1000fc9e 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h @@ -1,6 +1,7 @@ #pragma once #include "../common.h" +#include "output.h" #ifndef SWAP_CONVERSION # define SWAP_CONVERSION 1 @@ -32,6 +33,9 @@ typedef struct { check_swap_threshold crit; bool on_aix; int conversion_factor; + + bool output_format_is_set; + mp_output_format output_format; } swap_config; swap_config swap_config_init(void); diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 2fe4544f..180d5037 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -14,6 +14,8 @@ swap_config swap_config_init(void) { tmp.warn_is_set = false; tmp.crit_is_set = false; + tmp.output_format_is_set = false; + #ifdef _AIX tmp.on_aix = true; #else -- cgit v1.2.3-74-g34f1 From d27e0862a9e514149d81622a82622d31ab3bee9b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:57:25 +0100 Subject: Fix previous check_swap tests --- plugins/t/check_swap.t | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'plugins') diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index eaa81083..6ef2323d 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t @@ -5,39 +5,41 @@ # use strict; +use warnings; use Test::More tests => 14; use NPTest; -my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; -my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; -my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; +my $successOutput = '/^OK.* - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; +my $failureOutput = '/^CRITICAL: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; +my $warnOutput = '/^WARNING: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; +my $outputFormat = '--output-format one-line'; my $result; -$result = NPTest->testCmd( "./check_swap" ); # Always OK +$result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK cmp_ok( $result->return_code, "==", 0, "Always OK" ); like( $result->output, $successOutput, "Right output" ); -$result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576" ); # 1 MB free +$result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); like( $result->output, $successOutput, "Right output" ); -$result = NPTest->testCmd( "./check_swap -w 1% -c 1%" ); # 1% free +$result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); like( $result->output, $successOutput, "Right output" ); -$result = NPTest->testCmd( "./check_swap -w 100% -c 100%" ); # 100% (always critical) +$result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); like( $result->output, $failureOutput, "Right output" ); -$result = NPTest->testCmd( "./check_swap -w 100% -c 1%" ); # 100% (always warn) +$result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); like( $result->output, $warnOutput, "Right output" ); -$result = NPTest->testCmd( "./check_swap -w 100%" ); # 100% (single threshold, always warn) +$result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); like( $result->output, $warnOutput, "Right output" ); -$result = NPTest->testCmd( "./check_swap -c 100%" ); # 100% (single threshold, always critical) +$result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); like( $result->output, $failureOutput, "Right output" ); -- cgit v1.2.3-74-g34f1 From ec18b80cdbdc2c4c1e7eb587e251177e8cc7ca11 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 19 Feb 2025 12:07:01 +0100 Subject: Add tests for check_swap with JSON output --- plugins/t/check_swap.t | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index 6ef2323d..93e481c3 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t @@ -6,8 +6,9 @@ use strict; use warnings; -use Test::More tests => 14; +use Test::More tests => 35; use NPTest; +use JSON; my $successOutput = '/^OK.* - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; my $failureOutput = '/^CRITICAL: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; @@ -43,3 +44,50 @@ like( $result->output, $warnOutput, "Right output" ); $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); like( $result->output, $failureOutput, "Right output" ); + + +$outputFormat = '--output-format mp-test-json'; +my $output; +my $message = '/^[0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; + +$result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK +cmp_ok( $result->return_code, "==", 0, "Always OK" ); +$output = decode_json($result->output); +is($output->{'state'}, "OK", "State was correct"); +like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); + +$result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free +cmp_ok( $result->return_code, "==", 0, "Always OK" ); +$output = decode_json($result->output); +is($output->{'state'}, "OK", "State was correct"); +like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); + +$result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free +cmp_ok( $result->return_code, "==", 0, "Always OK" ); +$output = decode_json($result->output); +is($output->{'state'}, "OK", "State was correct"); +like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); + +$result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) +cmp_ok( $result->return_code, "==", 0, "Always OK" ); +$output = decode_json($result->output); +is($output->{'state'}, "CRITICAL", "State was correct"); +like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); + +$result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) +cmp_ok( $result->return_code, "==", 0, "Always OK" ); +$output = decode_json($result->output); +is($output->{'state'}, "WARNING", "State was correct"); +like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); + +$result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) +cmp_ok( $result->return_code, "==", 0, "Always OK" ); +$output = decode_json($result->output); +is($output->{'state'}, "WARNING", "State was correct"); +like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); + +$result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) +cmp_ok( $result->return_code, "==", 0, "Always OK" ); +$output = decode_json($result->output); +is($output->{'state'}, "CRITICAL", "State was correct"); +like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); -- 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') 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') 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 From 1d590a0efe4193ac9298d3eba448ab8bc3bb665b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 20 Feb 2025 23:52:32 +0100 Subject: Rename icingaweb2 format to multi-line --- lib/output.c | 8 ++++---- lib/output.h | 4 ++-- plugins/utils.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'plugins') diff --git a/lib/output.c b/lib/output.c index 07a77165..17919afc 100644 --- a/lib/output.c +++ b/lib/output.c @@ -235,7 +235,7 @@ char *mp_fmt_output(mp_check check) { char *result = NULL; switch (check.format) { - case MP_FORMAT_ICINGA_WEB_2: { + case MP_FORMAT_MULTI_LINE: { if (check.summary == NULL) { check.summary = get_subcheck_summary(check); } @@ -245,7 +245,7 @@ char *mp_fmt_output(mp_check check) { mp_subcheck_list *subchecks = check.subchecks; while (subchecks != NULL) { - asprintf(&result, "%s\n%s", result, fmt_subcheck_output(MP_FORMAT_ICINGA_WEB_2, subchecks->subcheck, 1)); + asprintf(&result, "%s\n%s", result, fmt_subcheck_output(MP_FORMAT_MULTI_LINE, subchecks->subcheck, 1)); subchecks = subchecks->next; } @@ -330,7 +330,7 @@ static inline char *fmt_subcheck_output(mp_output_format output_format, mp_subch mp_subcheck_list *subchecks = NULL; switch (output_format) { - case MP_FORMAT_ICINGA_WEB_2: + case MP_FORMAT_MULTI_LINE: asprintf(&result, "%s\\_[%s] - %s", generate_indentation_string(indentation), state_text(mp_compute_subcheck_state(check)), check.output); @@ -510,7 +510,7 @@ mp_subcheck mp_set_subcheck_default_state(mp_subcheck check, mp_state_enum state } char *mp_output_format_map[] = { - [MP_FORMAT_ICINGA_WEB_2] = "icingaweb2", + [MP_FORMAT_MULTI_LINE] = "multi-line", [MP_FORMAT_TEST_JSON] = "mp-test-json", }; diff --git a/lib/output.h b/lib/output.h index 14c4bcf4..ffc36f53 100644 --- a/lib/output.h +++ b/lib/output.h @@ -29,11 +29,11 @@ typedef struct subcheck_list { * Possible output formats */ typedef enum output_format { - MP_FORMAT_ICINGA_WEB_2, + MP_FORMAT_MULTI_LINE, MP_FORMAT_TEST_JSON, } mp_output_format; -#define MP_FORMAT_DEFAULT MP_FORMAT_ICINGA_WEB_2 +#define MP_FORMAT_DEFAULT MP_FORMAT_MULTI_LINE /* * The main state object of a plugin. Exists only ONCE per plugin. diff --git a/plugins/utils.h b/plugins/utils.h index bc26f704..029ae5a6 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -197,6 +197,6 @@ 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") + Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") #endif /* NP_UTILS_H */ -- cgit v1.2.3-74-g34f1 From 7c8c9d9b3e7bb6c29d82788d05d74e3f18f01aa5 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 21 Feb 2025 13:40:09 +0100 Subject: Remove check_swap tests with one-line format --- plugins/t/check_swap.t | 39 ++------------------------------------- 1 file changed, 2 insertions(+), 37 deletions(-) (limited to 'plugins') diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index 93e481c3..7e61b766 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t @@ -6,47 +6,12 @@ use strict; use warnings; -use Test::More tests => 35; +use Test::More tests => 21; use NPTest; use JSON; -my $successOutput = '/^OK.* - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; -my $failureOutput = '/^CRITICAL: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; -my $warnOutput = '/^WARNING: .*- [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; - -my $outputFormat = '--output-format one-line'; my $result; - -$result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK -cmp_ok( $result->return_code, "==", 0, "Always OK" ); -like( $result->output, $successOutput, "Right output" ); - -$result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free -cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); -like( $result->output, $successOutput, "Right output" ); - -$result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free -cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); -like( $result->output, $successOutput, "Right output" ); - -$result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) -cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); -like( $result->output, $failureOutput, "Right output" ); - -$result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) -cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); -like( $result->output, $warnOutput, "Right output" ); - -$result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) -cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); -like( $result->output, $warnOutput, "Right output" ); - -$result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) -cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); -like( $result->output, $failureOutput, "Right output" ); - - -$outputFormat = '--output-format mp-test-json'; +my $outputFormat = '--output-format mp-test-json'; my $output; my $message = '/^[0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; -- cgit v1.2.3-74-g34f1