Sfoglia il codice sorgente

negate: make timeouts configurable

Thomas Guyot-Sionnest 17 anni fa
parent
commit
d36c976129
3 ha cambiato i file con 38 aggiunte e 8 eliminazioni
  1. 1 0
      NEWS
  2. 12 2
      plugins/negate.c
  3. 25 6
      plugins/t/negate.t

+ 1 - 0
NEWS

@@ -26,6 +26,7 @@ This file documents the major additions and syntax changes between releases.
 	Fixed check_http sending HTTP/1.0 with v1.1 headers (#2638765)
 	Fixed check_http sending HTTP/1.0 with v1.1 headers (#2638765)
 	Fixed check_http not timing-out on redirects
 	Fixed check_http not timing-out on redirects
 	Fixed negate not printing the real timeout in ALRM handler when left to default
 	Fixed negate not printing the real timeout in ALRM handler when left to default
+	negate timeout result is now configurable
 
 
 1.4.13 25th Sept 2008
 1.4.13 25th Sept 2008
 	Fix Debian bug #460097: check_http --max-age broken (Hilko Bengen)
 	Fix Debian bug #460097: check_http --max-age broken (Hilko Bengen)

+ 12 - 2
plugins/negate.c

@@ -130,6 +130,7 @@ process_arguments (int argc, char **argv)
 		{"help", no_argument, 0, 'h'},
 		{"help", no_argument, 0, 'h'},
 		{"version", no_argument, 0, 'V'},
 		{"version", no_argument, 0, 'V'},
 		{"timeout", required_argument, 0, 't'},
 		{"timeout", required_argument, 0, 't'},
+		{"timeout-result", required_argument, 0, 'T'},
 		{"ok", required_argument, 0, 'o'},
 		{"ok", required_argument, 0, 'o'},
 		{"warning", required_argument, 0, 'w'},
 		{"warning", required_argument, 0, 'w'},
 		{"critical", required_argument, 0, 'c'},
 		{"critical", required_argument, 0, 'c'},
@@ -139,7 +140,7 @@ process_arguments (int argc, char **argv)
 	};
 	};
 
 
 	while (1) {
 	while (1) {
-		c = getopt_long (argc, argv, "+hVt:o:w:c:u:s", longopts, &option);
+		c = getopt_long (argc, argv, "+hVt:T:o:w:c:u:s", longopts, &option);
 
 
 		if (c == -1 || c == EOF)
 		if (c == -1 || c == EOF)
 			break;
 			break;
@@ -161,6 +162,10 @@ process_arguments (int argc, char **argv)
 			else
 			else
 				timeout_interval = atoi (optarg);
 				timeout_interval = atoi (optarg);
 			break;
 			break;
+		case 'T':     /* Result to return on timeouts */
+			if ((timeout_state = translate_state(optarg)) == ERROR)
+				usage4 (_("timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
+			break;
 		case 'o':     /* replacement for OK */
 		case 'o':     /* replacement for OK */
 			if ((state[STATE_OK] = translate_state(optarg)) == ERROR)
 			if ((state[STATE_OK] = translate_state(optarg)) == ERROR)
 				usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
 				usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
@@ -246,6 +251,8 @@ print_help (void)
 
 
 	printf (_(UT_TIMEOUT), timeout_interval);
 	printf (_(UT_TIMEOUT), timeout_interval);
 	printf ("    %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
 	printf ("    %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
+	printf (" -T, --timeout-result=STATUS\n");
+	printf ("    %s\n", _("Custom result on Negate timeouts; see below for STATUS definition\n"));
 
 
 	printf(" -o, --ok=STATUS\n");
 	printf(" -o, --ok=STATUS\n");
 	printf(" -w, --warning=STATUS\n");
 	printf(" -w, --warning=STATUS\n");
@@ -270,6 +277,9 @@ print_help (void)
 	printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
 	printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
 	printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
 	printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
 	printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
 	printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
+	printf ("\n");
+	printf (" %s\n", _("Using timeout-result, it is possible to override the timeout behaviour or a"));
+	printf (" %s\n", _("plugin by setting the negate timeout a bit lower."));
 
 
 	printf (_(UT_SUPPORT));
 	printf (_(UT_SUPPORT));
 }
 }
@@ -280,5 +290,5 @@ void
 print_usage (void)
 print_usage (void)
 {
 {
 	printf (_("Usage:"));
 	printf (_("Usage:"));
-	printf ("%s [-t timeout] [-owcu STATE] [-s] <definition of wrapped plugin>\n", progname);
+	printf ("%s [-t timeout] [-Towcu STATE] [-s] <definition of wrapped plugin>\n", progname);
 }
 }

+ 25 - 6
plugins/t/negate.t

@@ -8,8 +8,8 @@ use strict;
 use Test::More;
 use Test::More;
 use NPTest;
 use NPTest;
 
 
-# 15 tests in the first part and 32 in the last loop
-plan tests => 47;
+# 15 tests in the first part, 9 in timeout tests and 2 * 32 in the last loops
+plan tests => 88;
 
 
 my $res;
 my $res;
 
 
@@ -53,18 +53,37 @@ is( $res->output, "No data returned from command", "Bad command, as expected (tr
 $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' );
 $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' );
 is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
 is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
 
 
-
 my %state = (
 my %state = (
 	ok => 0,
 	ok => 0,
 	warning => 1,
 	warning => 1,
 	critical => 2,
 	critical => 2,
 	unknown => 3,
 	unknown => 3,
 	);
 	);
-foreach my $current_state (qw(ok warning critical unknown)) {
-	foreach my $new_state (qw(ok warning critical unknown)) {
+
+# Timeout tests
+$res = NPTest->testCmd( "./negate -t 2 /bin/sh -c 'sleep 5'" );
+is( $res->output, 'CRITICAL - Plugin timed out after 2 seconds' );
+
+foreach my $state (keys(%state)) {
+	$res = NPTest->testCmd( "./negate -t 2 -T $state /bin/sh -c 'sleep 5'" );
+	is( $res->return_code, $state{$state}, "Got timeout state $state" );
+	is( $res->output,  uc($state)." - Plugin timed out after 2 seconds", "Timeout state $state output");
+}
+
+foreach my $current_state (keys(%state)) {
+	foreach my $new_state (keys(%state)) {
 		$res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
 		$res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
 		is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
 		is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
-		is( $res->output, uc($current_state).": Fake $new_state" );
+		is( $res->output, uc($current_state).": Fake $new_state", "Fake $new_state output");
+	}
+}
+
+# Same as aboce with substitute
+foreach my $current_state (keys(%state)) {
+	foreach my $new_state (keys(%state)) {
+		$res = NPTest->testCmd( "./negate -s --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
+		is( $res->return_code, $state{$new_state}, "Got fake $new_state (with substitute)" );
+		is( $res->output, uc($new_state).": Fake $new_state", "Substitued fake $new_state output");
 	}
 	}
 }
 }