diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-12 21:22:50 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-12 21:22:50 +0100 |
| commit | 5ae0a8d49559d66695f6f2591f4bbe0bb185ef73 (patch) | |
| tree | 8f4d0dc54b2c858688b3428efbcd00b59bf761d8 /plugins | |
| parent | 53e72bfa25a17b253c171f6ec4d936bb2cb87cca (diff) | |
| download | monitoring-plugins-5ae0a8d49559d66695f6f2591f4bbe0bb185ef73.tar.gz | |
Refactor negate
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/Makefile.am | 1 | ||||
| -rw-r--r-- | plugins/negate.c | 73 | ||||
| -rw-r--r-- | plugins/negate.d/config.h | 24 |
3 files changed, 67 insertions, 31 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index bb3f029e..a420d23d 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -49,6 +49,7 @@ np_test_scripts = tests/test_check_swap.t | |||
| 49 | EXTRA_DIST = t \ | 49 | EXTRA_DIST = t \ |
| 50 | tests \ | 50 | tests \ |
| 51 | $(np_test_scripts) \ | 51 | $(np_test_scripts) \ |
| 52 | negate.d \ | ||
| 52 | check_swap.d \ | 53 | check_swap.d \ |
| 53 | check_ldap.d \ | 54 | check_ldap.d \ |
| 54 | check_hpjd.d \ | 55 | check_hpjd.d \ |
diff --git a/plugins/negate.c b/plugins/negate.c index 6c53a1b9..08fa1470 100644 --- a/plugins/negate.c +++ b/plugins/negate.c | |||
| @@ -29,6 +29,7 @@ | |||
| 29 | * | 29 | * |
| 30 | *****************************************************************************/ | 30 | *****************************************************************************/ |
| 31 | 31 | ||
| 32 | #include "states.h" | ||
| 32 | const char *progname = "negate"; | 33 | const char *progname = "negate"; |
| 33 | const char *copyright = "2002-2024"; | 34 | const char *copyright = "2002-2024"; |
| 34 | const char *email = "devel@monitoring-plugins.org"; | 35 | const char *email = "devel@monitoring-plugins.org"; |
| @@ -38,21 +39,17 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 38 | #include "common.h" | 39 | #include "common.h" |
| 39 | #include "utils.h" | 40 | #include "utils.h" |
| 40 | #include "utils_cmd.h" | 41 | #include "utils_cmd.h" |
| 42 | #include "negate.d/config.h" | ||
| 41 | 43 | ||
| 42 | #include <ctype.h> | 44 | typedef struct { |
| 45 | int errorcode; | ||
| 46 | negate_config config; | ||
| 47 | } negate_config_wrapper; | ||
| 48 | static negate_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 49 | static negate_config_wrapper validate_arguments(negate_config_wrapper /*config_wrapper*/); | ||
| 43 | 50 | ||
| 44 | static const char **process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 45 | static void validate_arguments(char ** /*command_line*/); | ||
| 46 | static void print_help(void); | 51 | static void print_help(void); |
| 47 | void print_usage(void); | 52 | void print_usage(void); |
| 48 | static bool subst_text = false; | ||
| 49 | |||
| 50 | static int state[4] = { | ||
| 51 | STATE_OK, | ||
| 52 | STATE_WARNING, | ||
| 53 | STATE_CRITICAL, | ||
| 54 | STATE_UNKNOWN, | ||
| 55 | }; | ||
| 56 | 53 | ||
| 57 | int main(int argc, char **argv) { | 54 | int main(int argc, char **argv) { |
| 58 | setlocale(LC_ALL, ""); | 55 | setlocale(LC_ALL, ""); |
| @@ -61,16 +58,24 @@ int main(int argc, char **argv) { | |||
| 61 | 58 | ||
| 62 | timeout_interval = DEFAULT_TIMEOUT; | 59 | timeout_interval = DEFAULT_TIMEOUT; |
| 63 | 60 | ||
| 64 | char **command_line = (char **)process_arguments(argc, argv); | 61 | negate_config_wrapper tmp_config = process_arguments(argc, argv); |
| 62 | |||
| 63 | if (tmp_config.errorcode == ERROR) { | ||
| 64 | die(STATE_UNKNOWN, _("negate: Failed to parse input")); | ||
| 65 | } | ||
| 66 | |||
| 67 | negate_config config = tmp_config.config; | ||
| 68 | |||
| 69 | char **command_line = config.command_line; | ||
| 65 | 70 | ||
| 66 | /* Set signal handling and alarm */ | 71 | /* Set signal handling and alarm */ |
| 67 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 72 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
| 68 | die(STATE_UNKNOWN, _("Cannot catch SIGALRM")); | 73 | die(STATE_UNKNOWN, _("Cannot catch SIGALRM")); |
| 69 | } | 74 | } |
| 70 | 75 | ||
| 71 | (void)alarm((unsigned)timeout_interval); | 76 | (void)alarm(timeout_interval); |
| 72 | 77 | ||
| 73 | int result = STATE_UNKNOWN; | 78 | mp_state_enum result = STATE_UNKNOWN; |
| 74 | output chld_out; | 79 | output chld_out; |
| 75 | output chld_err; | 80 | output chld_err; |
| 76 | 81 | ||
| @@ -93,34 +98,38 @@ int main(int argc, char **argv) { | |||
| 93 | 98 | ||
| 94 | char *sub; | 99 | char *sub; |
| 95 | for (size_t i = 0; i < chld_out.lines; i++) { | 100 | for (size_t i = 0; i < chld_out.lines; i++) { |
| 96 | if (subst_text && result >= 0 && result <= 4 && result != state[result]) { | 101 | if (config.subst_text && result >= 0 && result <= 4 && result != config.state[result]) { |
| 97 | /* Loop over each match found */ | 102 | /* Loop over each match found */ |
| 98 | while ((sub = strstr(chld_out.line[i], state_text(result)))) { | 103 | while ((sub = strstr(chld_out.line[i], state_text(result)))) { |
| 99 | /* Terminate the first part and skip over the string we'll substitute */ | 104 | /* Terminate the first part and skip over the string we'll substitute */ |
| 100 | *sub = '\0'; | 105 | *sub = '\0'; |
| 101 | sub += strlen(state_text(result)); | 106 | sub += strlen(state_text(result)); |
| 102 | /* then put everything back together */ | 107 | /* then put everything back together */ |
| 103 | xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text(state[result]), sub); | 108 | xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text(config.state[result]), sub); |
| 104 | } | 109 | } |
| 105 | } | 110 | } |
| 106 | printf("%s\n", chld_out.line[i]); | 111 | printf("%s\n", chld_out.line[i]); |
| 107 | } | 112 | } |
| 108 | 113 | ||
| 109 | if (result >= 0 && result <= 4) { | 114 | if (result >= 0 && result <= 4) { |
| 110 | exit(state[result]); | 115 | exit(config.state[result]); |
| 111 | } else { | 116 | } else { |
| 112 | exit(result); | 117 | exit(result); |
| 113 | } | 118 | } |
| 114 | } | 119 | } |
| 115 | 120 | ||
| 116 | /* process command-line arguments */ | 121 | /* process command-line arguments */ |
| 117 | static const char **process_arguments(int argc, char **argv) { | 122 | static negate_config_wrapper process_arguments(int argc, char **argv) { |
| 118 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, | 123 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, |
| 119 | {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, | 124 | {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, |
| 120 | {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, | 125 | {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, |
| 121 | {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, | 126 | {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, |
| 122 | {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; | 127 | {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; |
| 123 | 128 | ||
| 129 | negate_config_wrapper result = { | ||
| 130 | .errorcode = OK, | ||
| 131 | .config = negate_config_init(), | ||
| 132 | }; | ||
| 124 | bool permute = true; | 133 | bool permute = true; |
| 125 | while (true) { | 134 | while (true) { |
| 126 | int option = 0; | 135 | int option = 0; |
| @@ -154,54 +163,56 @@ static const char **process_arguments(int argc, char **argv) { | |||
| 154 | } | 163 | } |
| 155 | break; | 164 | break; |
| 156 | case 'o': /* replacement for OK */ | 165 | case 'o': /* replacement for OK */ |
| 157 | if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR) { | 166 | if ((result.config.state[STATE_OK] = mp_translate_state(optarg)) == ERROR) { |
| 158 | usage4(_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 167 | usage4(_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
| 159 | } | 168 | } |
| 160 | permute = false; | 169 | permute = false; |
| 161 | break; | 170 | break; |
| 162 | 171 | ||
| 163 | case 'w': /* replacement for WARNING */ | 172 | case 'w': /* replacement for WARNING */ |
| 164 | if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) { | 173 | if ((result.config.state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) { |
| 165 | usage4(_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 174 | usage4(_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
| 166 | } | 175 | } |
| 167 | permute = false; | 176 | permute = false; |
| 168 | break; | 177 | break; |
| 169 | case 'c': /* replacement for CRITICAL */ | 178 | case 'c': /* replacement for CRITICAL */ |
| 170 | if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) { | 179 | if ((result.config.state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) { |
| 171 | usage4(_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 180 | usage4(_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
| 172 | } | 181 | } |
| 173 | permute = false; | 182 | permute = false; |
| 174 | break; | 183 | break; |
| 175 | case 'u': /* replacement for UNKNOWN */ | 184 | case 'u': /* replacement for UNKNOWN */ |
| 176 | if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) { | 185 | if ((result.config.state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) { |
| 177 | usage4(_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 186 | usage4(_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
| 178 | } | 187 | } |
| 179 | permute = false; | 188 | permute = false; |
| 180 | break; | 189 | break; |
| 181 | case 's': /* Substitute status text */ | 190 | case 's': /* Substitute status text */ |
| 182 | subst_text = true; | 191 | result.config.subst_text = true; |
| 183 | break; | 192 | break; |
| 184 | } | 193 | } |
| 185 | } | 194 | } |
| 186 | 195 | ||
| 187 | validate_arguments(&argv[optind]); | ||
| 188 | |||
| 189 | if (permute) { /* No [owcu] switch specified, default to this */ | 196 | if (permute) { /* No [owcu] switch specified, default to this */ |
| 190 | state[STATE_OK] = STATE_CRITICAL; | 197 | result.config.state[STATE_OK] = STATE_CRITICAL; |
| 191 | state[STATE_CRITICAL] = STATE_OK; | 198 | result.config.state[STATE_CRITICAL] = STATE_OK; |
| 192 | } | 199 | } |
| 193 | 200 | ||
| 194 | return (const char **)&argv[optind]; | 201 | result.config.command_line = &argv[optind]; |
| 202 | |||
| 203 | return validate_arguments(result); | ||
| 195 | } | 204 | } |
| 196 | 205 | ||
| 197 | void validate_arguments(char **command_line) { | 206 | negate_config_wrapper validate_arguments(negate_config_wrapper config_wrapper) { |
| 198 | if (command_line[0] == NULL) { | 207 | if (config_wrapper.config.command_line[0] == NULL) { |
| 199 | usage4(_("Could not parse arguments")); | 208 | usage4(_("Could not parse arguments")); |
| 200 | } | 209 | } |
| 201 | 210 | ||
| 202 | if (strncmp(command_line[0], "/", 1) != 0 && strncmp(command_line[0], "./", 2) != 0) { | 211 | if (strncmp(config_wrapper.config.command_line[0], "/", 1) != 0 && strncmp(config_wrapper.config.command_line[0], "./", 2) != 0) { |
| 203 | usage4(_("Require path to command")); | 212 | usage4(_("Require path to command")); |
| 204 | } | 213 | } |
| 214 | |||
| 215 | return config_wrapper; | ||
| 205 | } | 216 | } |
| 206 | 217 | ||
| 207 | void print_help(void) { | 218 | void print_help(void) { |
diff --git a/plugins/negate.d/config.h b/plugins/negate.d/config.h new file mode 100644 index 00000000..0cf30cd4 --- /dev/null +++ b/plugins/negate.d/config.h | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "states.h" | ||
| 4 | |||
| 5 | typedef struct { | ||
| 6 | mp_state_enum state[4]; | ||
| 7 | bool subst_text; | ||
| 8 | char **command_line; | ||
| 9 | } negate_config; | ||
| 10 | |||
| 11 | negate_config negate_config_init() { | ||
| 12 | negate_config tmp = { | ||
| 13 | .state = | ||
| 14 | { | ||
| 15 | STATE_OK, | ||
| 16 | STATE_WARNING, | ||
| 17 | STATE_CRITICAL, | ||
| 18 | STATE_UNKNOWN, | ||
| 19 | }, | ||
| 20 | .subst_text = false, | ||
| 21 | .command_line = NULL, | ||
| 22 | }; | ||
| 23 | return tmp; | ||
| 24 | } | ||
