diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-12 21:54:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-12 21:54:29 +0100 |
| commit | 02acc76edc5c646af90a6168df61c711aa3d11d6 (patch) | |
| tree | 81abfdceb51f71333e4e36e8f233de73bf7c7674 /plugins | |
| parent | 84b823ae4ffeebc34aebf485774552b92244b62a (diff) | |
| parent | 33f44c4c5a96196661d596d082ed1a6b9e0236fc (diff) | |
| download | monitoring-plugins-02acc76edc5c646af90a6168df61c711aa3d11d6.tar.gz | |
Merge pull request #2106 from RincewindsHat/refactor/negate
Refactor/negate
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/Makefile.am | 1 | ||||
| -rw-r--r-- | plugins/negate.c | 96 | ||||
| -rw-r--r-- | plugins/negate.d/config.h | 24 |
3 files changed, 84 insertions, 37 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 5cd20319..9e4924c3 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 750c0bfb..0520d298 100644 --- a/plugins/negate.c +++ b/plugins/negate.c | |||
| @@ -38,21 +38,18 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 38 | #include "common.h" | 38 | #include "common.h" |
| 39 | #include "utils.h" | 39 | #include "utils.h" |
| 40 | #include "utils_cmd.h" | 40 | #include "utils_cmd.h" |
| 41 | #include "negate.d/config.h" | ||
| 42 | #include "../lib/states.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,15 +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")); |
| 74 | } | ||
| 69 | 75 | ||
| 70 | (void)alarm((unsigned)timeout_interval); | 76 | (void)alarm(timeout_interval); |
| 71 | 77 | ||
| 72 | int result = STATE_UNKNOWN; | 78 | mp_state_enum result = STATE_UNKNOWN; |
| 73 | output chld_out; | 79 | output chld_out; |
| 74 | output chld_err; | 80 | output chld_err; |
| 75 | 81 | ||
| @@ -86,46 +92,52 @@ int main(int argc, char **argv) { | |||
| 86 | } | 92 | } |
| 87 | 93 | ||
| 88 | /* Return UNKNOWN or worse if no output is returned */ | 94 | /* Return UNKNOWN or worse if no output is returned */ |
| 89 | if (chld_out.lines == 0) | 95 | if (chld_out.lines == 0) { |
| 90 | die(max_state_alt(result, STATE_UNKNOWN), _("No data returned from command\n")); | 96 | die(max_state_alt(result, STATE_UNKNOWN), _("No data returned from command\n")); |
| 97 | } | ||
| 91 | 98 | ||
| 92 | char *sub; | 99 | char *sub; |
| 93 | for (size_t i = 0; i < chld_out.lines; i++) { | 100 | for (size_t i = 0; i < chld_out.lines; i++) { |
| 94 | if (subst_text && result >= 0 && result <= 4 && result != state[result]) { | 101 | if (config.subst_text && result >= 0 && result <= 4 && result != config.state[result]) { |
| 95 | /* Loop over each match found */ | 102 | /* Loop over each match found */ |
| 96 | while ((sub = strstr(chld_out.line[i], state_text(result)))) { | 103 | while ((sub = strstr(chld_out.line[i], state_text(result)))) { |
| 97 | /* 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 */ |
| 98 | *sub = '\0'; | 105 | *sub = '\0'; |
| 99 | sub += strlen(state_text(result)); | 106 | sub += strlen(state_text(result)); |
| 100 | /* then put everything back together */ | 107 | /* then put everything back together */ |
| 101 | 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); |
| 102 | } | 109 | } |
| 103 | } | 110 | } |
| 104 | printf("%s\n", chld_out.line[i]); | 111 | printf("%s\n", chld_out.line[i]); |
| 105 | } | 112 | } |
| 106 | 113 | ||
| 107 | if (result >= 0 && result <= 4) { | 114 | if (result >= 0 && result <= 4) { |
| 108 | exit(state[result]); | 115 | exit(config.state[result]); |
| 109 | } else { | 116 | } else { |
| 110 | exit(result); | 117 | exit(result); |
| 111 | } | 118 | } |
| 112 | } | 119 | } |
| 113 | 120 | ||
| 114 | /* process command-line arguments */ | 121 | /* process command-line arguments */ |
| 115 | static const char **process_arguments(int argc, char **argv) { | 122 | static negate_config_wrapper process_arguments(int argc, char **argv) { |
| 116 | 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'}, |
| 117 | {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, | 124 | {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, |
| 118 | {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, | 125 | {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, |
| 119 | {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, | 126 | {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, |
| 120 | {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; | 127 | {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; |
| 121 | 128 | ||
| 129 | negate_config_wrapper result = { | ||
| 130 | .errorcode = OK, | ||
| 131 | .config = negate_config_init(), | ||
| 132 | }; | ||
| 122 | bool permute = true; | 133 | bool permute = true; |
| 123 | while (true) { | 134 | while (true) { |
| 124 | int option = 0; | 135 | int option = 0; |
| 125 | int option_char = getopt_long(argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option); | 136 | int option_char = getopt_long(argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option); |
| 126 | 137 | ||
| 127 | if (option_char == -1 || option_char == EOF) | 138 | if (option_char == -1 || option_char == EOF) { |
| 128 | break; | 139 | break; |
| 140 | } | ||
| 129 | 141 | ||
| 130 | switch (option_char) { | 142 | switch (option_char) { |
| 131 | case '?': /* help */ | 143 | case '?': /* help */ |
| @@ -139,58 +151,68 @@ static const char **process_arguments(int argc, char **argv) { | |||
| 139 | print_revision(progname, NP_VERSION); | 151 | print_revision(progname, NP_VERSION); |
| 140 | exit(STATE_UNKNOWN); | 152 | exit(STATE_UNKNOWN); |
| 141 | case 't': /* timeout period */ | 153 | case 't': /* timeout period */ |
| 142 | if (!is_integer(optarg)) | 154 | if (!is_integer(optarg)) { |
| 143 | usage2(_("Timeout interval must be a positive integer"), optarg); | 155 | usage2(_("Timeout interval must be a positive integer"), optarg); |
| 144 | else | 156 | } else { |
| 145 | timeout_interval = atoi(optarg); | 157 | timeout_interval = atoi(optarg); |
| 158 | } | ||
| 146 | break; | 159 | break; |
| 147 | case 'T': /* Result to return on timeouts */ | 160 | case 'T': /* Result to return on timeouts */ |
| 148 | if ((timeout_state = mp_translate_state(optarg)) == ERROR) | 161 | if ((timeout_state = mp_translate_state(optarg)) == ERROR) { |
| 149 | usage4(_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 162 | usage4(_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
| 163 | } | ||
| 150 | break; | 164 | break; |
| 151 | case 'o': /* replacement for OK */ | 165 | case 'o': /* replacement for OK */ |
| 152 | if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR) | 166 | if ((result.config.state[STATE_OK] = mp_translate_state(optarg)) == ERROR) { |
| 153 | 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).")); |
| 168 | } | ||
| 154 | permute = false; | 169 | permute = false; |
| 155 | break; | 170 | break; |
| 156 | 171 | ||
| 157 | case 'w': /* replacement for WARNING */ | 172 | case 'w': /* replacement for WARNING */ |
| 158 | if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) | 173 | if ((result.config.state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) { |
| 159 | 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).")); |
| 175 | } | ||
| 160 | permute = false; | 176 | permute = false; |
| 161 | break; | 177 | break; |
| 162 | case 'c': /* replacement for CRITICAL */ | 178 | case 'c': /* replacement for CRITICAL */ |
| 163 | if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) | 179 | if ((result.config.state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) { |
| 164 | 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).")); |
| 181 | } | ||
| 165 | permute = false; | 182 | permute = false; |
| 166 | break; | 183 | break; |
| 167 | case 'u': /* replacement for UNKNOWN */ | 184 | case 'u': /* replacement for UNKNOWN */ |
| 168 | if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) | 185 | if ((result.config.state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) { |
| 169 | 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).")); |
| 187 | } | ||
| 170 | permute = false; | 188 | permute = false; |
| 171 | break; | 189 | break; |
| 172 | case 's': /* Substitute status text */ | 190 | case 's': /* Substitute status text */ |
| 173 | subst_text = true; | 191 | result.config.subst_text = true; |
| 174 | break; | 192 | break; |
| 175 | } | 193 | } |
| 176 | } | 194 | } |
| 177 | 195 | ||
| 178 | validate_arguments(&argv[optind]); | ||
| 179 | |||
| 180 | if (permute) { /* No [owcu] switch specified, default to this */ | 196 | if (permute) { /* No [owcu] switch specified, default to this */ |
| 181 | state[STATE_OK] = STATE_CRITICAL; | 197 | result.config.state[STATE_OK] = STATE_CRITICAL; |
| 182 | state[STATE_CRITICAL] = STATE_OK; | 198 | result.config.state[STATE_CRITICAL] = STATE_OK; |
| 183 | } | 199 | } |
| 184 | 200 | ||
| 185 | return (const char **)&argv[optind]; | 201 | result.config.command_line = &argv[optind]; |
| 202 | |||
| 203 | return validate_arguments(result); | ||
| 186 | } | 204 | } |
| 187 | 205 | ||
| 188 | void validate_arguments(char **command_line) { | 206 | negate_config_wrapper validate_arguments(negate_config_wrapper config_wrapper) { |
| 189 | if (command_line[0] == NULL) | 207 | if (config_wrapper.config.command_line[0] == NULL) { |
| 190 | usage4(_("Could not parse arguments")); | 208 | usage4(_("Could not parse arguments")); |
| 209 | } | ||
| 191 | 210 | ||
| 192 | 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) { |
| 193 | usage4(_("Require path to command")); | 212 | usage4(_("Require path to command")); |
| 213 | } | ||
| 214 | |||
| 215 | return config_wrapper; | ||
| 194 | } | 216 | } |
| 195 | 217 | ||
| 196 | 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 | } | ||
