diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 13:57:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-31 13:57:43 (GMT) |
commit | b1d260a821b7d4916d6bf1a026fbc9b4f2b268ae (patch) | |
tree | 84a3fc6b26b181a1ed10d7ca55c32ac9170efbd4 /plugins/negate.c | |
parent | b4c5956591e9741ce9b190210e7b10940a6adbdd (diff) | |
parent | 8955f56de355403941bc8f02a4edb2bc72d1397a (diff) | |
download | monitoring-plugins-b1d260a821b7d4916d6bf1a026fbc9b4f2b268ae.tar.gz |
Merge pull request #2035 from RincewindsHat/cleanup/rest-of-plugins
Cleanup for some more plugins
Diffstat (limited to 'plugins/negate.c')
-rw-r--r-- | plugins/negate.c | 298 |
1 files changed, 136 insertions, 162 deletions
diff --git a/plugins/negate.c b/plugins/negate.c index c5fe7e1..7e52fe6 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 |
@@ -41,13 +41,11 @@ const char *email = "devel@monitoring-plugins.org"; | |||
41 | 41 | ||
42 | #include <ctype.h> | 42 | #include <ctype.h> |
43 | 43 | ||
44 | /* char *command_line; */ | 44 | static const char **process_arguments(int /*argc*/, char ** /*argv*/); |
45 | 45 | static void validate_arguments(char ** /*command_line*/); | |
46 | static const char **process_arguments (int, char **); | 46 | static void print_help(void); |
47 | void validate_arguments (char **); | 47 | void print_usage(void); |
48 | void print_help (void); | 48 | static bool subst_text = false; |
49 | void print_usage (void); | ||
50 | bool subst_text = false; | ||
51 | 49 | ||
52 | static int state[4] = { | 50 | static int state[4] = { |
53 | STATE_OK, | 51 | STATE_OK, |
@@ -56,185 +54,165 @@ static int state[4] = { | |||
56 | STATE_UNKNOWN, | 54 | STATE_UNKNOWN, |
57 | }; | 55 | }; |
58 | 56 | ||
59 | int | 57 | int main(int argc, char **argv) { |
60 | main (int argc, char **argv) | 58 | setlocale(LC_ALL, ""); |
61 | { | 59 | bindtextdomain(PACKAGE, LOCALEDIR); |
62 | int result = STATE_UNKNOWN; | 60 | textdomain(PACKAGE); |
63 | char *sub; | ||
64 | char **command_line; | ||
65 | output chld_out, chld_err; | ||
66 | |||
67 | setlocale (LC_ALL, ""); | ||
68 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
69 | textdomain (PACKAGE); | ||
70 | 61 | ||
71 | timeout_interval = DEFAULT_TIMEOUT; | 62 | timeout_interval = DEFAULT_TIMEOUT; |
72 | 63 | ||
73 | command_line = (char **) process_arguments (argc, argv); | 64 | char **command_line = (char **)process_arguments(argc, argv); |
74 | 65 | ||
75 | /* Set signal handling and alarm */ | 66 | /* Set signal handling and alarm */ |
76 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) | 67 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) |
77 | die (STATE_UNKNOWN, _("Cannot catch SIGALRM")); | 68 | die(STATE_UNKNOWN, _("Cannot catch SIGALRM")); |
78 | 69 | ||
79 | (void) alarm ((unsigned) timeout_interval); | 70 | (void)alarm((unsigned)timeout_interval); |
71 | |||
72 | int result = STATE_UNKNOWN; | ||
73 | output chld_out; | ||
74 | output chld_err; | ||
80 | 75 | ||
81 | /* catch when the command is quoted */ | 76 | /* catch when the command is quoted */ |
82 | if(command_line[1] == NULL) { | 77 | if (command_line[1] == NULL) { |
83 | result = cmd_run (command_line[0], &chld_out, &chld_err, 0); | 78 | result = cmd_run(command_line[0], &chld_out, &chld_err, 0); |
84 | } else { | 79 | } else { |
85 | result = cmd_run_array (command_line, &chld_out, &chld_err, 0); | 80 | result = cmd_run_array(command_line, &chld_out, &chld_err, 0); |
86 | } | 81 | } |
87 | if (chld_err.lines > 0) { | 82 | if (chld_err.lines > 0) { |
88 | for (size_t i = 0; i < chld_err.lines; i++) { | 83 | for (size_t i = 0; i < chld_err.lines; i++) { |
89 | fprintf (stderr, "%s\n", chld_err.line[i]); | 84 | fprintf(stderr, "%s\n", chld_err.line[i]); |
90 | } | 85 | } |
91 | } | 86 | } |
92 | 87 | ||
93 | /* Return UNKNOWN or worse if no output is returned */ | 88 | /* Return UNKNOWN or worse if no output is returned */ |
94 | if (chld_out.lines == 0) | 89 | if (chld_out.lines == 0) |
95 | die (max_state_alt (result, STATE_UNKNOWN), _("No data returned from command\n")); | 90 | die(max_state_alt(result, STATE_UNKNOWN), _("No data returned from command\n")); |
96 | 91 | ||
92 | char *sub; | ||
97 | for (size_t i = 0; i < chld_out.lines; i++) { | 93 | for (size_t i = 0; i < chld_out.lines; i++) { |
98 | if (subst_text && result >= 0 && result <= 4 && result != state[result]) { | 94 | if (subst_text && result >= 0 && result <= 4 && result != state[result]) { |
99 | /* Loop over each match found */ | 95 | /* Loop over each match found */ |
100 | while ((sub = strstr (chld_out.line[i], state_text (result)))) { | 96 | while ((sub = strstr(chld_out.line[i], state_text(result)))) { |
101 | /* Terminate the first part and skip over the string we'll substitute */ | 97 | /* Terminate the first part and skip over the string we'll substitute */ |
102 | *sub = '\0'; | 98 | *sub = '\0'; |
103 | sub += strlen (state_text (result)); | 99 | sub += strlen(state_text(result)); |
104 | /* then put everything back together */ | 100 | /* then put everything back together */ |
105 | xasprintf (&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text (state[result]), sub); | 101 | xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text(state[result]), sub); |
106 | } | 102 | } |
107 | } | 103 | } |
108 | printf ("%s\n", chld_out.line[i]); | 104 | printf("%s\n", chld_out.line[i]); |
109 | } | 105 | } |
110 | 106 | ||
111 | if (result >= 0 && result <= 4) { | 107 | if (result >= 0 && result <= 4) { |
112 | exit (state[result]); | 108 | exit(state[result]); |
113 | } else { | 109 | } else { |
114 | exit (result); | 110 | exit(result); |
115 | } | 111 | } |
116 | } | 112 | } |
117 | 113 | ||
118 | |||
119 | /* process command-line arguments */ | 114 | /* process command-line arguments */ |
120 | static const char ** | 115 | static const char **process_arguments(int argc, char **argv) { |
121 | process_arguments (int argc, char **argv) | 116 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, |
122 | { | 117 | {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, |
123 | int c; | 118 | {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, |
119 | {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, | ||
120 | {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; | ||
121 | |||
124 | bool permute = true; | 122 | bool permute = true; |
123 | while (true) { | ||
124 | int option = 0; | ||
125 | int option_char = getopt_long(argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option); | ||
125 | 126 | ||
126 | int option = 0; | 127 | if (option_char == -1 || option_char == EOF) |
127 | static struct option longopts[] = { | ||
128 | {"help", no_argument, 0, 'h'}, | ||
129 | {"version", no_argument, 0, 'V'}, | ||
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 | }; | ||
139 | |||
140 | while (1) { | ||
141 | c = getopt_long (argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option); | ||
142 | |||
143 | if (c == -1 || c == EOF) | ||
144 | break; | 128 | break; |
145 | 129 | ||
146 | switch (c) { | 130 | switch (option_char) { |
147 | case '?': /* help */ | 131 | case '?': /* help */ |
148 | usage5 (); | 132 | usage5(); |
149 | break; | 133 | break; |
150 | case 'h': /* help */ | 134 | case 'h': /* help */ |
151 | print_help (); | 135 | print_help(); |
152 | exit (EXIT_SUCCESS); | 136 | exit(EXIT_SUCCESS); |
153 | break; | 137 | break; |
154 | case 'V': /* version */ | 138 | case 'V': /* version */ |
155 | print_revision (progname, NP_VERSION); | 139 | print_revision(progname, NP_VERSION); |
156 | exit (EXIT_SUCCESS); | 140 | exit(EXIT_SUCCESS); |
157 | case 't': /* timeout period */ | 141 | case 't': /* timeout period */ |
158 | if (!is_integer (optarg)) | 142 | if (!is_integer(optarg)) |
159 | usage2 (_("Timeout interval must be a positive integer"), optarg); | 143 | usage2(_("Timeout interval must be a positive integer"), optarg); |
160 | else | 144 | else |
161 | timeout_interval = atoi (optarg); | 145 | timeout_interval = atoi(optarg); |
162 | break; | 146 | break; |
163 | case 'T': /* Result to return on timeouts */ | 147 | case 'T': /* Result to return on timeouts */ |
164 | if ((timeout_state = mp_translate_state(optarg)) == ERROR) | 148 | 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).")); | 149 | usage4(_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
166 | break; | 150 | break; |
167 | case 'o': /* replacement for OK */ | 151 | case 'o': /* replacement for OK */ |
168 | if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR) | 152 | if ((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).")); | 153 | usage4(_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
170 | permute = false; | 154 | permute = false; |
171 | break; | 155 | break; |
172 | 156 | ||
173 | case 'w': /* replacement for WARNING */ | 157 | case 'w': /* replacement for WARNING */ |
174 | if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) | 158 | if ((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).")); | 159 | usage4(_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
176 | permute = false; | 160 | permute = false; |
177 | break; | 161 | break; |
178 | case 'c': /* replacement for CRITICAL */ | 162 | case 'c': /* replacement for CRITICAL */ |
179 | if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) | 163 | if ((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).")); | 164 | usage4(_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
181 | permute = false; | 165 | permute = false; |
182 | break; | 166 | break; |
183 | case 'u': /* replacement for UNKNOWN */ | 167 | case 'u': /* replacement for UNKNOWN */ |
184 | if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) | 168 | if ((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).")); | 169 | usage4(_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
186 | permute = false; | 170 | permute = false; |
187 | break; | 171 | break; |
188 | case 's': /* Substitute status text */ | 172 | case 's': /* Substitute status text */ |
189 | subst_text = true; | 173 | subst_text = true; |
190 | break; | 174 | break; |
191 | } | 175 | } |
192 | } | 176 | } |
193 | 177 | ||
194 | validate_arguments (&argv[optind]); | 178 | validate_arguments(&argv[optind]); |
195 | 179 | ||
196 | if (permute) { /* No [owcu] switch specified, default to this */ | 180 | if (permute) { /* No [owcu] switch specified, default to this */ |
197 | state[STATE_OK] = STATE_CRITICAL; | 181 | state[STATE_OK] = STATE_CRITICAL; |
198 | state[STATE_CRITICAL] = STATE_OK; | 182 | state[STATE_CRITICAL] = STATE_OK; |
199 | } | 183 | } |
200 | 184 | ||
201 | return (const char **) &argv[optind]; | 185 | return (const char **)&argv[optind]; |
202 | } | 186 | } |
203 | 187 | ||
204 | 188 | void validate_arguments(char **command_line) { | |
205 | void | ||
206 | validate_arguments (char **command_line) | ||
207 | { | ||
208 | if (command_line[0] == NULL) | 189 | if (command_line[0] == NULL) |
209 | usage4 (_("Could not parse arguments")); | 190 | usage4(_("Could not parse arguments")); |
210 | 191 | ||
211 | if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0) | 192 | if (strncmp(command_line[0], "/", 1) != 0 && strncmp(command_line[0], "./", 2) != 0) |
212 | usage4 (_("Require path to command")); | 193 | usage4(_("Require path to command")); |
213 | } | 194 | } |
214 | 195 | ||
196 | void print_help(void) { | ||
197 | print_revision(progname, NP_VERSION); | ||
215 | 198 | ||
216 | void | 199 | printf(COPYRIGHT, copyright, email); |
217 | print_help (void) | ||
218 | { | ||
219 | print_revision (progname, NP_VERSION); | ||
220 | 200 | ||
221 | printf (COPYRIGHT, copyright, email); | 201 | printf("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and vice-versa) by default.")); |
202 | printf("%s\n", _("Additional switches can be used to control:\n")); | ||
203 | printf("\t - which state becomes what\n"); | ||
204 | printf("\t - changing the plugin output text to match the return code"); | ||
222 | 205 | ||
223 | printf ("%s\n", _("Negates only the return code of a plugin (returns OK for CRITICAL and vice-versa) by default.")); | 206 | 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 | 207 | ||
228 | printf ("\n\n"); | 208 | print_usage(); |
229 | 209 | ||
230 | print_usage (); | 210 | printf(UT_HELP_VRSN); |
231 | 211 | ||
232 | printf (UT_HELP_VRSN); | 212 | printf(UT_PLUG_TIMEOUT, timeout_interval); |
233 | 213 | printf(" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status.")); | |
234 | printf (UT_PLUG_TIMEOUT, timeout_interval); | 214 | printf(" -T, --timeout-result=STATUS\n"); |
235 | printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status.")); | 215 | 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 | 216 | ||
239 | printf(" -o, --ok=STATUS\n"); | 217 | printf(" -o, --ok=STATUS\n"); |
240 | printf(" -w, --warning=STATUS\n"); | 218 | printf(" -w, --warning=STATUS\n"); |
@@ -246,31 +224,27 @@ print_help (void) | |||
246 | printf(" -s, --substitute\n"); | 224 | printf(" -s, --substitute\n"); |
247 | printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n")); | 225 | printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n")); |
248 | 226 | ||
249 | printf ("\n"); | 227 | printf("\n"); |
250 | printf ("%s\n", _("Examples:")); | 228 | printf("%s\n", _("Examples:")); |
251 | printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host"); | 229 | 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")); | 230 | 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'"); | 231 | 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")); | 232 | printf(" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL")); |
255 | printf ("\n"); | 233 | printf("\n"); |
256 | printf ("%s\n", _("Notes:")); | 234 | printf("%s\n", _("Notes:")); |
257 | printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); | 235 | 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.")); | 236 | 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.")); | 237 | 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.")); | 238 | 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.")); | 239 | printf(" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); |
262 | printf ("\n"); | 240 | printf("\n"); |
263 | printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a")); | 241 | 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.")); | 242 | printf(" %s\n", _("plugin by setting the negate timeout a bit lower.")); |
265 | 243 | ||
266 | printf (UT_SUPPORT); | 244 | printf(UT_SUPPORT); |
267 | } | 245 | } |
268 | 246 | ||
269 | 247 | void print_usage(void) { | |
270 | 248 | printf("%s\n", _("Usage:")); | |
271 | void | 249 | 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 | } | 250 | } |