diff options
Diffstat (limited to 'plugins/negate.c')
-rw-r--r-- | plugins/negate.c | 354 |
1 files changed, 175 insertions, 179 deletions
diff --git a/plugins/negate.c b/plugins/negate.c index c5fe7e13..0520d298 100644 --- a/plugins/negate.c +++ b/plugins/negate.c | |||
@@ -1,36 +1,36 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring negate plugin | 3 | * Monitoring negate plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2002-2008 Monitoring Plugins Development Team | 6 | * Copyright (c) 2002-2024 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains the negate plugin | 10 | * This file contains the negate plugin |
11 | * | 11 | * |
12 | * Negates the status of a plugin (returns OK for CRITICAL, and vice-versa). | 12 | * Negates the status of a plugin (returns OK for CRITICAL, and vice-versa). |
13 | * Can also perform custom state switching. | 13 | * Can also perform custom state switching. |
14 | * | 14 | * |
15 | * | 15 | * |
16 | * This program is free software: you can redistribute it and/or modify | 16 | * This program is free software: you can redistribute it and/or modify |
17 | * it under the terms of the GNU General Public License as published by | 17 | * it under the terms of the GNU General Public License as published by |
18 | * the Free Software Foundation, either version 3 of the License, or | 18 | * the Free Software Foundation, either version 3 of the License, or |
19 | * (at your option) any later version. | 19 | * (at your option) any later version. |
20 | * | 20 | * |
21 | * This program is distributed in the hope that it will be useful, | 21 | * This program is distributed in the hope that it will be useful, |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24 | * GNU General Public License for more details. | 24 | * GNU General Public License for more details. |
25 | * | 25 | * |
26 | * You should have received a copy of the GNU General Public License | 26 | * You should have received a copy of the GNU General Public License |
27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
28 | * | 28 | * |
29 | * | 29 | * |
30 | *****************************************************************************/ | 30 | *****************************************************************************/ |
31 | 31 | ||
32 | const char *progname = "negate"; | 32 | const char *progname = "negate"; |
33 | const char *copyright = "2002-2008"; | 33 | const char *copyright = "2002-2024"; |
34 | const char *email = "devel@monitoring-plugins.org"; | 34 | const char *email = "devel@monitoring-plugins.org"; |
35 | 35 | ||
36 | #define DEFAULT_TIMEOUT 11 | 36 | #define DEFAULT_TIMEOUT 11 |
@@ -38,203 +38,203 @@ 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 | /* char *command_line; */ | 51 | static void print_help(void); |
52 | void print_usage(void); | ||
45 | 53 | ||
46 | static const char **process_arguments (int, char **); | 54 | int main(int argc, char **argv) { |
47 | void validate_arguments (char **); | 55 | setlocale(LC_ALL, ""); |
48 | void print_help (void); | 56 | bindtextdomain(PACKAGE, LOCALEDIR); |
49 | void print_usage (void); | 57 | textdomain(PACKAGE); |
50 | bool subst_text = false; | ||
51 | 58 | ||
52 | static int state[4] = { | 59 | timeout_interval = DEFAULT_TIMEOUT; |
53 | STATE_OK, | ||
54 | STATE_WARNING, | ||
55 | STATE_CRITICAL, | ||
56 | STATE_UNKNOWN, | ||
57 | }; | ||
58 | 60 | ||
59 | int | 61 | negate_config_wrapper tmp_config = process_arguments(argc, argv); |
60 | main (int argc, char **argv) | ||
61 | { | ||
62 | int result = STATE_UNKNOWN; | ||
63 | char *sub; | ||
64 | char **command_line; | ||
65 | output chld_out, chld_err; | ||
66 | 62 | ||
67 | setlocale (LC_ALL, ""); | 63 | if (tmp_config.errorcode == ERROR) { |
68 | bindtextdomain (PACKAGE, LOCALEDIR); | 64 | die(STATE_UNKNOWN, _("negate: Failed to parse input")); |
69 | textdomain (PACKAGE); | 65 | } |
70 | 66 | ||
71 | timeout_interval = DEFAULT_TIMEOUT; | 67 | negate_config config = tmp_config.config; |
72 | 68 | ||
73 | command_line = (char **) process_arguments (argc, argv); | 69 | char **command_line = config.command_line; |
74 | 70 | ||
75 | /* Set signal handling and alarm */ | 71 | /* Set signal handling and alarm */ |
76 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) | 72 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
77 | die (STATE_UNKNOWN, _("Cannot catch SIGALRM")); | 73 | die(STATE_UNKNOWN, _("Cannot catch SIGALRM")); |
74 | } | ||
75 | |||
76 | (void)alarm(timeout_interval); | ||
78 | 77 | ||
79 | (void) alarm ((unsigned) timeout_interval); | 78 | mp_state_enum result = STATE_UNKNOWN; |
79 | output chld_out; | ||
80 | output chld_err; | ||
80 | 81 | ||
81 | /* catch when the command is quoted */ | 82 | /* catch when the command is quoted */ |
82 | if(command_line[1] == NULL) { | 83 | if (command_line[1] == NULL) { |
83 | result = cmd_run (command_line[0], &chld_out, &chld_err, 0); | 84 | result = cmd_run(command_line[0], &chld_out, &chld_err, 0); |
84 | } else { | 85 | } else { |
85 | result = cmd_run_array (command_line, &chld_out, &chld_err, 0); | 86 | result = cmd_run_array(command_line, &chld_out, &chld_err, 0); |
86 | } | 87 | } |
87 | if (chld_err.lines > 0) { | 88 | if (chld_err.lines > 0) { |
88 | for (size_t i = 0; i < chld_err.lines; i++) { | 89 | for (size_t i = 0; i < chld_err.lines; i++) { |
89 | fprintf (stderr, "%s\n", chld_err.line[i]); | 90 | fprintf(stderr, "%s\n", chld_err.line[i]); |
90 | } | 91 | } |
91 | } | 92 | } |
92 | 93 | ||
93 | /* Return UNKNOWN or worse if no output is returned */ | 94 | /* Return UNKNOWN or worse if no output is returned */ |
94 | if (chld_out.lines == 0) | 95 | if (chld_out.lines == 0) { |
95 | 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 | } | ||
96 | 98 | ||
99 | char *sub; | ||
97 | for (size_t i = 0; i < chld_out.lines; i++) { | 100 | for (size_t i = 0; i < chld_out.lines; i++) { |
98 | if (subst_text && result >= 0 && result <= 4 && result != state[result]) { | 101 | if (config.subst_text && result >= 0 && result <= 4 && result != config.state[result]) { |
99 | /* Loop over each match found */ | 102 | /* Loop over each match found */ |
100 | while ((sub = strstr (chld_out.line[i], state_text (result)))) { | 103 | while ((sub = strstr(chld_out.line[i], state_text(result)))) { |
101 | /* 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 */ |
102 | *sub = '\0'; | 105 | *sub = '\0'; |
103 | sub += strlen (state_text (result)); | 106 | sub += strlen(state_text(result)); |
104 | /* then put everything back together */ | 107 | /* then put everything back together */ |
105 | 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); |
106 | } | 109 | } |
107 | } | 110 | } |
108 | printf ("%s\n", chld_out.line[i]); | 111 | printf("%s\n", chld_out.line[i]); |
109 | } | 112 | } |
110 | 113 | ||
111 | if (result >= 0 && result <= 4) { | 114 | if (result >= 0 && result <= 4) { |
112 | exit (state[result]); | 115 | exit(config.state[result]); |
113 | } else { | 116 | } else { |
114 | exit (result); | 117 | exit(result); |
115 | } | 118 | } |
116 | } | 119 | } |
117 | 120 | ||
118 | |||
119 | /* process command-line arguments */ | 121 | /* process command-line arguments */ |
120 | static const char ** | 122 | static negate_config_wrapper process_arguments(int argc, char **argv) { |
121 | process_arguments (int argc, char **argv) | 123 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, |
122 | { | 124 | {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, |
123 | int c; | 125 | {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, |
124 | bool permute = true; | 126 | {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, |
125 | 127 | {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; | |
126 | int option = 0; | 128 | |
127 | static struct option longopts[] = { | 129 | negate_config_wrapper result = { |
128 | {"help", no_argument, 0, 'h'}, | 130 | .errorcode = OK, |
129 | {"version", no_argument, 0, 'V'}, | 131 | .config = negate_config_init(), |
130 | {"timeout", required_argument, 0, 't'}, | ||
131 | {"timeout-result", required_argument, 0, 'T'}, | ||
132 | {"ok", required_argument, 0, 'o'}, | ||
133 | {"warning", required_argument, 0, 'w'}, | ||
134 | {"critical", required_argument, 0, 'c'}, | ||
135 | {"unknown", required_argument, 0, 'u'}, | ||
136 | {"substitute", no_argument, 0, 's'}, | ||
137 | {0, 0, 0, 0} | ||
138 | }; | 132 | }; |
133 | bool permute = true; | ||
134 | while (true) { | ||
135 | int option = 0; | ||
136 | int option_char = getopt_long(argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option); | ||
139 | 137 | ||
140 | while (1) { | 138 | if (option_char == -1 || option_char == EOF) { |
141 | c = getopt_long (argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option); | ||
142 | |||
143 | if (c == -1 || c == EOF) | ||
144 | break; | 139 | break; |
140 | } | ||
145 | 141 | ||
146 | switch (c) { | 142 | switch (option_char) { |
147 | case '?': /* help */ | 143 | case '?': /* help */ |
148 | usage5 (); | 144 | usage5(); |
149 | break; | 145 | break; |
150 | case 'h': /* help */ | 146 | case 'h': /* help */ |
151 | print_help (); | 147 | print_help(); |
152 | exit (EXIT_SUCCESS); | 148 | exit(STATE_UNKNOWN); |
153 | break; | 149 | break; |
154 | case 'V': /* version */ | 150 | case 'V': /* version */ |
155 | print_revision (progname, NP_VERSION); | 151 | print_revision(progname, NP_VERSION); |
156 | exit (EXIT_SUCCESS); | 152 | exit(STATE_UNKNOWN); |
157 | case 't': /* timeout period */ | 153 | case 't': /* timeout period */ |
158 | if (!is_integer (optarg)) | 154 | if (!is_integer(optarg)) { |
159 | usage2 (_("Timeout interval must be a positive integer"), optarg); | 155 | usage2(_("Timeout interval must be a positive integer"), optarg); |
160 | else | 156 | } else { |
161 | timeout_interval = atoi (optarg); | 157 | timeout_interval = atoi(optarg); |
158 | } | ||
162 | break; | 159 | break; |
163 | case 'T': /* Result to return on timeouts */ | 160 | case 'T': /* Result to return on timeouts */ |
164 | if ((timeout_state = mp_translate_state(optarg)) == ERROR) | 161 | if ((timeout_state = mp_translate_state(optarg)) == ERROR) { |
165 | 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 | } | ||
166 | break; | 164 | break; |
167 | case 'o': /* replacement for OK */ | 165 | case 'o': /* replacement for OK */ |
168 | if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR) | 166 | if ((result.config.state[STATE_OK] = mp_translate_state(optarg)) == ERROR) { |
169 | 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 | } | ||
170 | permute = false; | 169 | permute = false; |
171 | break; | 170 | break; |
172 | 171 | ||
173 | case 'w': /* replacement for WARNING */ | 172 | case 'w': /* replacement for WARNING */ |
174 | if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) | 173 | if ((result.config.state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) { |
175 | 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 | } | ||
176 | permute = false; | 176 | permute = false; |
177 | break; | 177 | break; |
178 | case 'c': /* replacement for CRITICAL */ | 178 | case 'c': /* replacement for CRITICAL */ |
179 | if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) | 179 | if ((result.config.state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) { |
180 | 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 | } | ||
181 | permute = false; | 182 | permute = false; |
182 | break; | 183 | break; |
183 | case 'u': /* replacement for UNKNOWN */ | 184 | case 'u': /* replacement for UNKNOWN */ |
184 | if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) | 185 | if ((result.config.state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) { |
185 | 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 | } | ||
186 | permute = false; | 188 | permute = false; |
187 | break; | 189 | break; |
188 | case 's': /* Substitute status text */ | 190 | case 's': /* Substitute status text */ |
189 | subst_text = true; | 191 | result.config.subst_text = true; |
190 | break; | 192 | break; |
191 | } | 193 | } |
192 | } | 194 | } |
193 | 195 | ||
194 | validate_arguments (&argv[optind]); | ||
195 | |||
196 | if (permute) { /* No [owcu] switch specified, default to this */ | 196 | if (permute) { /* No [owcu] switch specified, default to this */ |
197 | state[STATE_OK] = STATE_CRITICAL; | 197 | result.config.state[STATE_OK] = STATE_CRITICAL; |
198 | state[STATE_CRITICAL] = STATE_OK; | 198 | result.config.state[STATE_CRITICAL] = STATE_OK; |
199 | } | 199 | } |
200 | 200 | ||
201 | return (const char **) &argv[optind]; | 201 | result.config.command_line = &argv[optind]; |
202 | |||
203 | return validate_arguments(result); | ||
202 | } | 204 | } |
203 | 205 | ||
206 | negate_config_wrapper validate_arguments(negate_config_wrapper config_wrapper) { | ||
207 | if (config_wrapper.config.command_line[0] == NULL) { | ||
208 | usage4(_("Could not parse arguments")); | ||
209 | } | ||
204 | 210 | ||
205 | void | 211 | if (strncmp(config_wrapper.config.command_line[0], "/", 1) != 0 && strncmp(config_wrapper.config.command_line[0], "./", 2) != 0) { |
206 | validate_arguments (char **command_line) | 212 | usage4(_("Require path to command")); |
207 | { | 213 | } |
208 | if (command_line[0] == NULL) | ||
209 | usage4 (_("Could not parse arguments")); | ||
210 | 214 | ||
211 | if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0) | 215 | return config_wrapper; |
212 | usage4 (_("Require path to command")); | ||
213 | } | 216 | } |
214 | 217 | ||
218 | void print_help(void) { | ||
219 | print_revision(progname, NP_VERSION); | ||
215 | 220 | ||
216 | void | 221 | printf(COPYRIGHT, copyright, email); |
217 | print_help (void) | ||
218 | { | ||
219 | print_revision (progname, NP_VERSION); | ||
220 | 222 | ||
221 | printf (COPYRIGHT, copyright, email); | 223 | printf("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and vice-versa) by default.")); |
224 | printf("%s\n", _("Additional switches can be used to control:\n")); | ||
225 | printf("\t - which state becomes what\n"); | ||
226 | printf("\t - changing the plugin output text to match the return code"); | ||
222 | 227 | ||
223 | printf ("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and vice-versa) by default.")); | 228 | printf("\n\n"); |
224 | printf ("%s\n", _("Additional switches can be used to control:\n")); | ||
225 | printf ("\t - which state becomes what\n"); | ||
226 | printf ("\t - changing the plugin output text to match the return code"); | ||
227 | 229 | ||
228 | printf ("\n\n"); | 230 | print_usage(); |
229 | 231 | ||
230 | print_usage (); | 232 | printf(UT_HELP_VRSN); |
231 | 233 | ||
232 | printf (UT_HELP_VRSN); | 234 | printf(UT_PLUG_TIMEOUT, timeout_interval); |
233 | 235 | printf(" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status.")); | |
234 | printf (UT_PLUG_TIMEOUT, timeout_interval); | 236 | printf(" -T, --timeout-result=STATUS\n"); |
235 | printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status.")); | 237 | printf(" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n")); |
236 | printf (" -T, --timeout-result=STATUS\n"); | ||
237 | printf (" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n")); | ||
238 | 238 | ||
239 | printf(" -o, --ok=STATUS\n"); | 239 | printf(" -o, --ok=STATUS\n"); |
240 | printf(" -w, --warning=STATUS\n"); | 240 | printf(" -w, --warning=STATUS\n"); |
@@ -246,31 +246,27 @@ print_help (void) | |||
246 | printf(" -s, --substitute\n"); | 246 | printf(" -s, --substitute\n"); |
247 | printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n")); | 247 | printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n")); |
248 | 248 | ||
249 | printf ("\n"); | 249 | printf("\n"); |
250 | printf ("%s\n", _("Examples:")); | 250 | printf("%s\n", _("Examples:")); |
251 | printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host"); | 251 | printf(" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host"); |
252 | printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin")); | 252 | printf(" %s\n", _("Run check_ping and invert result. Must use full path to plugin")); |
253 | printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'"); | 253 | printf(" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'"); |
254 | printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL")); | 254 | printf(" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL")); |
255 | printf ("\n"); | 255 | printf("\n"); |
256 | printf ("%s\n", _("Notes:")); | 256 | printf("%s\n", _("Notes:")); |
257 | printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); | 257 | printf(" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); |
258 | printf (" %s\n", _("The full path of the plugin must be provided.")); | 258 | printf(" %s\n", _("The full path of the plugin must be provided.")); |
259 | printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL.")); | 259 | printf(" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL.")); |
260 | printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK.")); | 260 | printf(" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK.")); |
261 | printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); | 261 | printf(" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); |
262 | printf ("\n"); | 262 | printf("\n"); |
263 | printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a")); | 263 | printf(" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a")); |
264 | printf (" %s\n", _("plugin by setting the negate timeout a bit lower.")); | 264 | printf(" %s\n", _("plugin by setting the negate timeout a bit lower.")); |
265 | 265 | ||
266 | printf (UT_SUPPORT); | 266 | printf(UT_SUPPORT); |
267 | } | 267 | } |
268 | 268 | ||
269 | 269 | void print_usage(void) { | |
270 | 270 | printf("%s\n", _("Usage:")); | |
271 | void | 271 | printf("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname); |
272 | print_usage (void) | ||
273 | { | ||
274 | printf ("%s\n", _("Usage:")); | ||
275 | printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname); | ||
276 | } | 272 | } |