diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-02-18 21:58:59 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-02-18 21:58:59 +0100 |
| commit | 5acd14fcfb419c9ef0d6bc38384dde4cd6b70bd9 (patch) | |
| tree | 21e2fc86952bdea86c519d6df88b8ba03b10d27c | |
| parent | 0645c9fc2c7f801ba3c7d68a17c137a63ada299f (diff) | |
| download | monitoring-plugins-5acd14fcfb419c9ef0d6bc38384dde4cd6b70bd9.tar.gz | |
Implement new output functionality for check_swap
| -rw-r--r-- | plugins/check_swap.c | 132 | ||||
| -rw-r--r-- | plugins/check_swap.d/check_swap.h | 4 | ||||
| -rw-r--r-- | plugins/check_swap.d/swap.c | 2 |
3 files changed, 104 insertions, 34 deletions
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 @@ | |||
| 28 | *****************************************************************************/ | 28 | *****************************************************************************/ |
| 29 | 29 | ||
| 30 | #include "common.h" | 30 | #include "common.h" |
| 31 | #include "output.h" | ||
| 32 | #include "states.h" | ||
| 33 | #include <limits.h> | ||
| 31 | #ifdef HAVE_DECL_SWAPCTL | 34 | #ifdef HAVE_DECL_SWAPCTL |
| 32 | # ifdef HAVE_SYS_PARAM_H | 35 | # ifdef HAVE_SYS_PARAM_H |
| 33 | # include <sys/param.h> | 36 | # include <sys/param.h> |
| @@ -69,8 +72,6 @@ int main(int argc, char **argv) { | |||
| 69 | bindtextdomain(PACKAGE, LOCALEDIR); | 72 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 70 | textdomain(PACKAGE); | 73 | textdomain(PACKAGE); |
| 71 | 74 | ||
| 72 | char *status = strdup(""); | ||
| 73 | |||
| 74 | /* Parse extra opts if any */ | 75 | /* Parse extra opts if any */ |
| 75 | argv = np_extra_opts(&argc, argv, progname); | 76 | argv = np_extra_opts(&argc, argv, progname); |
| 76 | 77 | ||
| @@ -90,59 +91,101 @@ int main(int argc, char **argv) { | |||
| 90 | } | 91 | } |
| 91 | 92 | ||
| 92 | double percent_used; | 93 | double percent_used; |
| 94 | mp_check overall = mp_check_init(); | ||
| 95 | if (config.output_format_is_set) { | ||
| 96 | overall.format = config.output_format; | ||
| 97 | } | ||
| 98 | mp_subcheck sc1 = mp_subcheck_init(); | ||
| 99 | sc1 = mp_set_subcheck_default_state(sc1, STATE_OK); | ||
| 100 | |||
| 93 | /* if total_swap_mb == 0, let's not divide by 0 */ | 101 | /* if total_swap_mb == 0, let's not divide by 0 */ |
| 94 | if (data.metrics.total != 0) { | 102 | if (data.metrics.total != 0) { |
| 95 | percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); | 103 | percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); |
| 96 | } else { | 104 | } else { |
| 97 | printf(_("SWAP %s - Swap is either disabled, not present, or of zero " | 105 | sc1 = mp_set_subcheck_state(sc1, config.no_swap_state); |
| 98 | "size."), | 106 | sc1.output = (char *)_("Swap is either disabled, not present, or of zero size."); |
| 99 | state_text(data.statusCode)); | 107 | |
| 100 | exit(config.no_swap_state); | 108 | mp_add_subcheck_to_check(&overall, sc1); |
| 109 | mp_exit(overall); | ||
| 101 | } | 110 | } |
| 102 | 111 | ||
| 103 | if (verbose) { | 112 | if (verbose) { |
| 104 | printf("Computed usage percentage: %g\n", percent_used); | 113 | printf("Computed usage percentage: %g\n", percent_used); |
| 105 | } | 114 | } |
| 106 | 115 | ||
| 107 | uint64_t warn_print = config.warn.value; | 116 | mp_perfdata pd = perfdata_init(); |
| 108 | if (config.warn.is_percentage) { | 117 | pd.label = "swap"; |
| 109 | warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT); | 118 | pd = mp_set_pd_value(pd, data.metrics.free); |
| 119 | pd.uom = "B"; | ||
| 120 | |||
| 121 | if (config.warn_is_set) { | ||
| 122 | uint64_t warn_print = config.warn.value; | ||
| 123 | if (config.warn.is_percentage) { | ||
| 124 | warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT); | ||
| 125 | } | ||
| 126 | |||
| 127 | mp_perfdata_value warn_pd = mp_create_pd_value(warn_print); | ||
| 128 | |||
| 129 | mp_range warn_range = mp_range_init(); | ||
| 130 | warn_range.end_infinity = false; | ||
| 131 | warn_range.end = warn_pd; | ||
| 132 | |||
| 133 | pd.warn = warn_range; | ||
| 134 | pd.warn_present = true; | ||
| 110 | } | 135 | } |
| 111 | 136 | ||
| 112 | uint64_t crit_print = config.crit.value; | 137 | if (config.crit_is_set) { |
| 113 | if (config.crit.is_percentage) { | 138 | uint64_t crit_print = config.crit.value; |
| 114 | crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); | 139 | if (config.crit.is_percentage) { |
| 140 | crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); | ||
| 141 | } | ||
| 142 | |||
| 143 | mp_perfdata_value crit_pd = mp_create_pd_value(crit_print); | ||
| 144 | |||
| 145 | mp_range crit_range = mp_range_init(); | ||
| 146 | crit_range.end_infinity = false; | ||
| 147 | crit_range.end = crit_pd; | ||
| 148 | |||
| 149 | pd.crit = crit_range; | ||
| 150 | pd.crit_present = true; | ||
| 115 | } | 151 | } |
| 116 | 152 | ||
| 117 | char *perfdata = perfdata_uint64("swap", data.metrics.free, "B", config.warn_is_set, warn_print, config.crit_is_set, crit_print, true, | 153 | mp_perfdata_value max = mp_create_pd_value(data.metrics.total); |
| 118 | 0, true, data.metrics.total); | 154 | pd.max = max; |
| 155 | pd.max_present = true; | ||
| 156 | |||
| 157 | mp_perfdata_value min = mp_create_pd_value(0); | ||
| 158 | pd.min = min; | ||
| 159 | pd.min_present = true; | ||
| 160 | |||
| 161 | mp_add_perfdata_to_subcheck(&sc1, pd); | ||
| 162 | if (verbose > 1) { | ||
| 163 | printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); | ||
| 164 | } | ||
| 119 | 165 | ||
| 120 | if (config.warn_is_set) { | 166 | if (config.warn_is_set) { |
| 121 | if (verbose > 1) { | 167 | if ((config.warn.is_percentage && (percent_used >= (100 - (double)config.warn.value))) || config.warn.value >= data.metrics.free) { |
| 122 | printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); | 168 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); |
| 123 | } | 169 | } |
| 170 | } | ||
| 124 | 171 | ||
| 125 | if ((config.warn.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.warn.value))) || | 172 | if (verbose > 1) { |
| 126 | config.warn.value >= data.metrics.free) { | 173 | printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); |
| 127 | data.statusCode = max_state(data.statusCode, STATE_WARNING); | ||
| 128 | } | ||
| 129 | } | 174 | } |
| 130 | 175 | ||
| 131 | if (config.crit_is_set) { | 176 | if (config.crit_is_set) { |
| 132 | if (verbose > 1) { | 177 | if ((config.crit.is_percentage && (percent_used >= (100 - (double)config.crit.value))) || config.crit.value >= data.metrics.free) { |
| 133 | printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); | 178 | sc1 = mp_set_subcheck_state(sc1, STATE_CRITICAL); |
| 134 | } | ||
| 135 | |||
| 136 | if ((config.crit.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.crit.value))) || | ||
| 137 | config.crit.value >= data.metrics.free) { | ||
| 138 | data.statusCode = max_state(data.statusCode, STATE_CRITICAL); | ||
| 139 | } | 179 | } |
| 140 | } | 180 | } |
| 141 | 181 | ||
| 142 | printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), | 182 | xasprintf(&sc1.output, _("%g%% free (%lluMiB out of %lluMiB)"), (100 - percent_used), data.metrics.free >> 20, |
| 143 | BYTES_TO_MiB(data.metrics.free), BYTES_TO_MiB(data.metrics.total), status, perfdata); | 183 | data.metrics.total >> 20); |
| 184 | |||
| 185 | overall.summary = "Swap"; | ||
| 186 | mp_add_subcheck_to_check(&overall, sc1); | ||
| 144 | 187 | ||
| 145 | exit(data.statusCode); | 188 | mp_exit(overall); |
| 146 | } | 189 | } |
| 147 | 190 | ||
| 148 | int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { | 191 | 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) { | |||
| 172 | return STATE_OK; | 215 | return STATE_OK; |
| 173 | } | 216 | } |
| 174 | 217 | ||
| 218 | #define output_format_index CHAR_MAX + 1 | ||
| 219 | |||
| 175 | /* process command-line arguments */ | 220 | /* process command-line arguments */ |
| 176 | swap_config_wrapper process_arguments(int argc, char **argv) { | 221 | swap_config_wrapper process_arguments(int argc, char **argv) { |
| 177 | swap_config_wrapper conf_wrapper = {.errorcode = OK}; | 222 | swap_config_wrapper conf_wrapper = {.errorcode = OK}; |
| 178 | conf_wrapper.config = swap_config_init(); | 223 | conf_wrapper.config = swap_config_init(); |
| 179 | 224 | ||
| 180 | static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, | 225 | static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, |
| 181 | {"allswaps", no_argument, 0, 'a'}, {"no-swap", required_argument, 0, 'n'}, | 226 | {"critical", required_argument, 0, 'c'}, |
| 182 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 227 | {"allswaps", no_argument, 0, 'a'}, |
| 183 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | 228 | {"no-swap", required_argument, 0, 'n'}, |
| 229 | {"verbose", no_argument, 0, 'v'}, | ||
| 230 | {"version", no_argument, 0, 'V'}, | ||
| 231 | {"help", no_argument, 0, 'h'}, | ||
| 232 | {"output-format", required_argument, 0, output_format_index}, | ||
| 233 | {0, 0, 0, 0}}; | ||
| 184 | 234 | ||
| 185 | while (true) { | 235 | while (true) { |
| 186 | int option = 0; | 236 | int option = 0; |
| @@ -263,6 +313,18 @@ swap_config_wrapper process_arguments(int argc, char **argv) { | |||
| 263 | case 'v': /* verbose */ | 313 | case 'v': /* verbose */ |
| 264 | verbose++; | 314 | verbose++; |
| 265 | break; | 315 | break; |
| 316 | case output_format_index: { | ||
| 317 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 318 | if (!parser.parsing_success) { | ||
| 319 | // TODO List all available formats here, maybe add anothoer usage function | ||
| 320 | printf("Invalid output format: %s\n", optarg); | ||
| 321 | exit(STATE_UNKNOWN); | ||
| 322 | } | ||
| 323 | |||
| 324 | conf_wrapper.config.output_format_is_set = true; | ||
| 325 | conf_wrapper.config.output_format = parser.output_format; | ||
| 326 | break; | ||
| 327 | } | ||
| 266 | case 'V': /* version */ | 328 | case 'V': /* version */ |
| 267 | print_revision(progname, NP_VERSION); | 329 | print_revision(progname, NP_VERSION); |
| 268 | exit(STATE_UNKNOWN); | 330 | exit(STATE_UNKNOWN); |
| @@ -319,6 +381,8 @@ void print_help(swap_config config) { | |||
| 319 | _("Resulting state when there is no swap regardless of thresholds. " | 381 | _("Resulting state when there is no swap regardless of thresholds. " |
| 320 | "Default:"), | 382 | "Default:"), |
| 321 | state_text(config.no_swap_state)); | 383 | state_text(config.no_swap_state)); |
| 384 | printf(" %s\n", "--output-format"); | ||
| 385 | printf(" %s\n", _("Select output format. Valid values: \"one-line\", \"icingaweb2\", \"summary-only\", \"mp-test-json\"")); | ||
| 322 | printf(UT_VERBOSE); | 386 | printf(UT_VERBOSE); |
| 323 | 387 | ||
| 324 | printf("\n"); | 388 | 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 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include "../common.h" | 3 | #include "../common.h" |
| 4 | #include "output.h" | ||
| 4 | 5 | ||
| 5 | #ifndef SWAP_CONVERSION | 6 | #ifndef SWAP_CONVERSION |
| 6 | # define SWAP_CONVERSION 1 | 7 | # define SWAP_CONVERSION 1 |
| @@ -32,6 +33,9 @@ typedef struct { | |||
| 32 | check_swap_threshold crit; | 33 | check_swap_threshold crit; |
| 33 | bool on_aix; | 34 | bool on_aix; |
| 34 | int conversion_factor; | 35 | int conversion_factor; |
| 36 | |||
| 37 | bool output_format_is_set; | ||
| 38 | mp_output_format output_format; | ||
| 35 | } swap_config; | 39 | } swap_config; |
| 36 | 40 | ||
| 37 | swap_config swap_config_init(void); | 41 | 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) { | |||
| 14 | tmp.warn_is_set = false; | 14 | tmp.warn_is_set = false; |
| 15 | tmp.crit_is_set = false; | 15 | tmp.crit_is_set = false; |
| 16 | 16 | ||
| 17 | tmp.output_format_is_set = false; | ||
| 18 | |||
| 17 | #ifdef _AIX | 19 | #ifdef _AIX |
| 18 | tmp.on_aix = true; | 20 | tmp.on_aix = true; |
| 19 | #else | 21 | #else |
