diff options
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/negate.c | 14 | ||||
| -rw-r--r-- | plugins/t/negate.t | 31 |
2 files changed, 37 insertions, 8 deletions
diff --git a/plugins/negate.c b/plugins/negate.c index e8be83d7..ebbb1523 100644 --- a/plugins/negate.c +++ b/plugins/negate.c | |||
| @@ -130,6 +130,7 @@ process_arguments (int argc, char **argv) | |||
| 130 | {"help", no_argument, 0, 'h'}, | 130 | {"help", no_argument, 0, 'h'}, |
| 131 | {"version", no_argument, 0, 'V'}, | 131 | {"version", no_argument, 0, 'V'}, |
| 132 | {"timeout", required_argument, 0, 't'}, | 132 | {"timeout", required_argument, 0, 't'}, |
| 133 | {"timeout-result", required_argument, 0, 'T'}, | ||
| 133 | {"ok", required_argument, 0, 'o'}, | 134 | {"ok", required_argument, 0, 'o'}, |
| 134 | {"warning", required_argument, 0, 'w'}, | 135 | {"warning", required_argument, 0, 'w'}, |
| 135 | {"critical", required_argument, 0, 'c'}, | 136 | {"critical", required_argument, 0, 'c'}, |
| @@ -139,7 +140,7 @@ process_arguments (int argc, char **argv) | |||
| 139 | }; | 140 | }; |
| 140 | 141 | ||
| 141 | while (1) { | 142 | while (1) { |
| 142 | c = getopt_long (argc, argv, "+hVt:o:w:c:u:s", longopts, &option); | 143 | c = getopt_long (argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option); |
| 143 | 144 | ||
| 144 | if (c == -1 || c == EOF) | 145 | if (c == -1 || c == EOF) |
| 145 | break; | 146 | break; |
| @@ -161,6 +162,10 @@ process_arguments (int argc, char **argv) | |||
| 161 | else | 162 | else |
| 162 | timeout_interval = atoi (optarg); | 163 | timeout_interval = atoi (optarg); |
| 163 | break; | 164 | break; |
| 165 | case 'T': /* Result to return on timeouts */ | ||
| 166 | if ((timeout_state = translate_state(optarg)) == ERROR) | ||
| 167 | usage4 (_("timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | ||
| 168 | break; | ||
| 164 | case 'o': /* replacement for OK */ | 169 | case 'o': /* replacement for OK */ |
| 165 | if ((state[STATE_OK] = translate_state(optarg)) == ERROR) | 170 | if ((state[STATE_OK] = translate_state(optarg)) == ERROR) |
| 166 | usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 171 | usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
| @@ -246,6 +251,8 @@ print_help (void) | |||
| 246 | 251 | ||
| 247 | printf (_(UT_TIMEOUT), timeout_interval); | 252 | printf (_(UT_TIMEOUT), timeout_interval); |
| 248 | printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status.")); | 253 | printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status.")); |
| 254 | printf (" -T, --timeout-result=STATUS\n"); | ||
| 255 | printf (" %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n")); | ||
| 249 | 256 | ||
| 250 | printf(" -o, --ok=STATUS\n"); | 257 | printf(" -o, --ok=STATUS\n"); |
| 251 | printf(" -w, --warning=STATUS\n"); | 258 | printf(" -w, --warning=STATUS\n"); |
| @@ -270,6 +277,9 @@ print_help (void) | |||
| 270 | printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL.")); | 277 | printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL.")); |
| 271 | printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK.")); | 278 | printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK.")); |
| 272 | printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); | 279 | printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); |
| 280 | printf ("\n"); | ||
| 281 | printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a")); | ||
| 282 | printf (" %s\n", _("plugin by setting the negate timeout a bit lower.")); | ||
| 273 | 283 | ||
| 274 | printf (_(UT_SUPPORT)); | 284 | printf (_(UT_SUPPORT)); |
| 275 | } | 285 | } |
| @@ -280,5 +290,5 @@ void | |||
| 280 | print_usage (void) | 290 | print_usage (void) |
| 281 | { | 291 | { |
| 282 | printf (_("Usage:")); | 292 | printf (_("Usage:")); |
| 283 | printf ("%s [-t timeout] [-owcu STATE] [-s] <definition of wrapped plugin>\n", progname); | 293 | printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname); |
| 284 | } | 294 | } |
diff --git a/plugins/t/negate.t b/plugins/t/negate.t index 989bf01f..0afe3ae6 100644 --- a/plugins/t/negate.t +++ b/plugins/t/negate.t | |||
| @@ -8,8 +8,8 @@ use strict; | |||
| 8 | use Test::More; | 8 | use Test::More; |
| 9 | use NPTest; | 9 | use NPTest; |
| 10 | 10 | ||
| 11 | # 15 tests in the first part and 32 in the last loop | 11 | # 15 tests in the first part, 9 in timeout tests and 2 * 32 in the last loops |
| 12 | plan tests => 47; | 12 | plan tests => 88; |
| 13 | 13 | ||
| 14 | my $res; | 14 | my $res; |
| 15 | 15 | ||
| @@ -53,18 +53,37 @@ is( $res->output, "No data returned from command", "Bad command, as expected (tr | |||
| 53 | $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' ); | 53 | $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' ); |
| 54 | is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' ); | 54 | is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' ); |
| 55 | 55 | ||
| 56 | |||
| 57 | my %state = ( | 56 | my %state = ( |
| 58 | ok => 0, | 57 | ok => 0, |
| 59 | warning => 1, | 58 | warning => 1, |
| 60 | critical => 2, | 59 | critical => 2, |
| 61 | unknown => 3, | 60 | unknown => 3, |
| 62 | ); | 61 | ); |
| 63 | foreach my $current_state (qw(ok warning critical unknown)) { | 62 | |
| 64 | foreach my $new_state (qw(ok warning critical unknown)) { | 63 | # Timeout tests |
| 64 | $res = NPTest->testCmd( "./negate -t 2 /bin/sh -c 'sleep 5'" ); | ||
| 65 | is( $res->output, 'CRITICAL - Plugin timed out after 2 seconds' ); | ||
| 66 | |||
| 67 | foreach my $state (keys(%state)) { | ||
| 68 | $res = NPTest->testCmd( "./negate -t 2 -T $state /bin/sh -c 'sleep 5'" ); | ||
| 69 | is( $res->return_code, $state{$state}, "Got timeout state $state" ); | ||
| 70 | is( $res->output, uc($state)." - Plugin timed out after 2 seconds", "Timeout state $state output"); | ||
| 71 | } | ||
| 72 | |||
| 73 | foreach my $current_state (keys(%state)) { | ||
| 74 | foreach my $new_state (keys(%state)) { | ||
| 65 | $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" ); | 75 | $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" ); |
| 66 | is( $res->return_code, $state{$new_state}, "Got fake $new_state" ); | 76 | is( $res->return_code, $state{$new_state}, "Got fake $new_state" ); |
| 67 | is( $res->output, uc($current_state).": Fake $new_state" ); | 77 | is( $res->output, uc($current_state).": Fake $new_state", "Fake $new_state output"); |
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | # Same as aboce with substitute | ||
| 82 | foreach my $current_state (keys(%state)) { | ||
| 83 | foreach my $new_state (keys(%state)) { | ||
| 84 | $res = NPTest->testCmd( "./negate -s --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" ); | ||
| 85 | is( $res->return_code, $state{$new_state}, "Got fake $new_state (with substitute)" ); | ||
| 86 | is( $res->output, uc($new_state).": Fake $new_state", "Substitued fake $new_state output"); | ||
| 68 | } | 87 | } |
| 69 | } | 88 | } |
| 70 | 89 | ||
