summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/negate.c91
-rw-r--r--plugins/t/negate.t33
2 files changed, 88 insertions, 36 deletions
diff --git a/plugins/negate.c b/plugins/negate.c
index cbde6a12..00e46d6b 100644
--- a/plugins/negate.c
+++ b/plugins/negate.c
@@ -3,7 +3,7 @@
3* Nagios negate plugin 3* Nagios negate plugin
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2002-2006 nagios-plugins team 6* Copyright (c) 2002-2007 nagios-plugins team
7* 7*
8* Last Modified: $Date$ 8* Last Modified: $Date$
9* 9*
@@ -70,7 +70,7 @@
70 70
71const char *progname = "negate"; 71const char *progname = "negate";
72const char *revision = "$Revision$"; 72const char *revision = "$Revision$";
73const char *copyright = "2002-2006"; 73const char *copyright = "2002-2007";
74const char *email = "nagiosplug-devel@lists.sourceforge.net"; 74const char *email = "nagiosplug-devel@lists.sourceforge.net";
75 75
76#define DEFAULT_TIMEOUT 9 76#define DEFAULT_TIMEOUT 9
@@ -86,7 +86,12 @@ int validate_arguments (char **);
86void print_help (void); 86void print_help (void);
87void print_usage (void); 87void print_usage (void);
88 88
89 89static int state[4] = {
90 STATE_OK,
91 STATE_WARNING,
92 STATE_CRITICAL,
93 STATE_UNKNOWN,
94};
90 95
91int 96int
92main (int argc, char **argv) 97main (int argc, char **argv)
@@ -130,12 +135,11 @@ main (int argc, char **argv)
130 printf ("%s\n", chld_out.line[i]); 135 printf ("%s\n", chld_out.line[i]);
131 } 136 }
132 137
133 if (result == STATE_OK) 138 if (result >= 0 && result <= 4) {
134 exit (STATE_CRITICAL); 139 exit (state[result]);
135 else if (result == STATE_CRITICAL) 140 } else {
136 exit (EXIT_SUCCESS);
137 else
138 exit (result); 141 exit (result);
142 }
139} 143}
140 144
141/****************************************************************************** 145/******************************************************************************
@@ -163,17 +167,22 @@ static const char **
163process_arguments (int argc, char **argv) 167process_arguments (int argc, char **argv)
164{ 168{
165 int c; 169 int c;
170 int permute = TRUE;
166 171
167 int option = 0; 172 int option = 0;
168 static struct option longopts[] = { 173 static struct option longopts[] = {
169 {"help", no_argument, 0, 'h'}, 174 {"help", no_argument, 0, 'h'},
170 {"version", no_argument, 0, 'V'}, 175 {"version", no_argument, 0, 'V'},
171 {"timeout", required_argument, 0, 't'}, 176 {"timeout", required_argument, 0, 't'},
177 {"ok", required_argument, 0, 'o'},
178 {"warning", required_argument, 0, 'w'},
179 {"critical", required_argument, 0, 'c'},
180 {"unknown", required_argument, 0, 'u'},
172 {0, 0, 0, 0} 181 {0, 0, 0, 0}
173 }; 182 };
174 183
175 while (1) { 184 while (1) {
176 c = getopt_long (argc, argv, "+hVt:", longopts, &option); 185 c = getopt_long (argc, argv, "+hVt:o:w:c:u:", longopts, &option);
177 186
178 if (c == -1 || c == EOF) 187 if (c == -1 || c == EOF)
179 break; 188 break;
@@ -195,11 +204,37 @@ process_arguments (int argc, char **argv)
195 else 204 else
196 timeout_interval = atoi (optarg); 205 timeout_interval = atoi (optarg);
197 break; 206 break;
207 case 'o': /* replacement for OK */
208 if ((state[STATE_OK] = translate_state(optarg)) == ERROR)
209 usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-4)."));
210 permute = FALSE;
211 break;
212
213 case 'w': /* replacement for WARNING */
214 if ((state[STATE_WARNING] = translate_state(optarg)) == ERROR)
215 usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
216 permute = FALSE;
217 break;
218 case 'c': /* replacement for CRITICAL */
219 if ((state[STATE_CRITICAL] = translate_state(optarg)) == ERROR)
220 usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
221 permute = FALSE;
222 break;
223 case 'u': /* replacement for UNKNOWN */
224 if ((state[STATE_UNKNOWN] = translate_state(optarg)) == ERROR)
225 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
226 permute = FALSE;
227 break;
198 } 228 }
199 } 229 }
200 230
201 validate_arguments (&argv[optind]); 231 validate_arguments (&argv[optind]);
202 232
233 if (permute) { /* No [owcu] switch specified, default to this */
234 state[STATE_OK] = STATE_CRITICAL;
235 state[STATE_CRITICAL] = STATE_OK;
236 }
237
203 return (const char **) &argv[optind]; 238 return (const char **) &argv[optind];
204} 239}
205 240
@@ -235,7 +270,23 @@ validate_arguments (char **command_line)
235-@@ 270-@@
236******************************************************************************/ 271******************************************************************************/
237 272
238 273int
274translate_state (char *state_text)
275{
276 char *temp_ptr;
277 for (temp_ptr = state_text; *temp_ptr; temp_ptr++) {
278 *temp_ptr = toupper(*temp_ptr);
279 }
280 if (!strcmp(state_text,"OK") || !strcmp(state_text,"0"))
281 return STATE_OK;
282 if (!strcmp(state_text,"WARNING") || !strcmp(state_text,"1"))
283 return STATE_WARNING;
284 if (!strcmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
285 return STATE_CRITICAL;
286 if (!strcmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
287 return STATE_UNKNOWN;
288 return ERROR;
289}
239 290
240void 291void
241print_help (void) 292print_help (void)
@@ -244,7 +295,8 @@ print_help (void)
244 295
245 printf (COPYRIGHT, copyright, email); 296 printf (COPYRIGHT, copyright, email);
246 297
247 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).")); 298 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
299 printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
248 300
249 printf ("\n\n"); 301 printf ("\n\n");
250 302
@@ -253,15 +305,22 @@ print_help (void)
253 printf (_(UT_HELP_VRSN)); 305 printf (_(UT_HELP_VRSN));
254 306
255 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT); 307 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
308 printf (" %s\n", _("Keep timeout lower than the plugin timeout to retain CRITICAL status."));
309
310 printf(" -o,--ok=STATUS\n");
311 printf(" -w,--warning=STATUS\n");
312 printf(" -c,--critical=STATUS\n");
313 printf(" -u,--unknown=STATUS\n");
314 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
315 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
316 printf(_(" OK and CRITICAL.\n"));
256 317
257 printf (" %s\n", _("[keep timeout than the plugin timeout to retain CRITICAL status]"));
258 printf ("\n"); 318 printf ("\n");
259 printf ("%s\n", _("Examples:")); 319 printf ("%s\n", _("Examples:"));
260 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host"); 320 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
261 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin")); 321 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
262 printf (" %s\n", "negate /usr/local/nagios/libexec/check_procs -a 'vi negate.c'"); 322 printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
263 printf (" %s\n", _("Use single quotes if you need to retain spaces")); 323 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
264 printf (_(UT_VERBOSE));
265 printf ("\n"); 324 printf ("\n");
266 printf ("%s\n", _("Notes:")); 325 printf ("%s\n", _("Notes:"));
267 printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); 326 printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
@@ -279,5 +338,5 @@ void
279print_usage (void) 338print_usage (void)
280{ 339{
281 printf (_("Usage:")); 340 printf (_("Usage:"));
282 printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname); 341 printf ("%s [-t timeout] [-owcu STATE] <definition of wrapped plugin>\n", progname);
283} 342}
diff --git a/plugins/t/negate.t b/plugins/t/negate.t
index 0efa0ca8..3a894f55 100644
--- a/plugins/t/negate.t
+++ b/plugins/t/negate.t
@@ -10,9 +10,8 @@ use strict;
10use Test::More; 10use Test::More;
11use NPTest; 11use NPTest;
12 12
13# 47 tests if the "map changes to return codes" patch is applied 13# 15 tests in the first part and 32 in the last loop
14#plan tests => 47; 14plan tests => 47;
15plan tests => 15;
16 15
17my $res; 16my $res;
18 17
@@ -57,23 +56,17 @@ $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' );
57is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' ); 56is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
58 57
59 58
60# Remove __DATA__ to run tests with future patch 59my %state = (
61__DATA__ 60 ok => 0,
62 61 warning => 1,
63TODO: { 62 critical => 2,
64 local $TODO = "Codes can be switched"; 63 unknown => 3,
65 my %state = ( 64 );
66 ok => 0, 65foreach my $current_state (qw(ok warning critical unknown)) {
67 warning => 1, 66 foreach my $new_state (qw(ok warning critical unknown)) {
68 critical => 2, 67 $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
69 unknown => 3, 68 is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
70 ); 69 is( $res->output, uc($current_state).": Fake $new_state" );
71 foreach my $current_state (qw(ok warning critical unknown)) {
72 foreach my $new_state (qw(ok warning critical unknown)) {
73 $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
74 is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
75 is( $res->output, uc($current_state).": Fake $new_state" );
76 }
77 } 70 }
78} 71}
79 72