From 520f297fa931d391f81af672b5b0e34db71b8c73 Mon Sep 17 00:00:00 2001
From: Thomas Guyot-Sionnest <dermoth@users.sourceforge.net>
Date: Sat, 22 Sep 2007 03:00:46 +0000
Subject: Rewrite the "map changes to return codes" patch nearly from scratch.

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1785 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/NEWS b/NEWS
index 6455122..7572b59 100644
--- a/NEWS
+++ b/NEWS
@@ -19,6 +19,7 @@ This file documents the major additions and syntax changes between releases.
 	WARNING: Fix for negate which may break existing commands:
 	  - stop evaluating command line options through shell twice 
 	  - enforce a full path for the command to run
+	The "negate" utility can now remap custom states
 
 1.4.9 4th June 2006
 	Inclusion of contrib/check_cluster2 as check_cluster with some improvements
diff --git a/plugins/negate.c b/plugins/negate.c
index cbde6a1..00e46d6 100644
--- a/plugins/negate.c
+++ b/plugins/negate.c
@@ -3,7 +3,7 @@
 * Nagios negate plugin
 *
 * License: GPL
-* Copyright (c) 2002-2006 nagios-plugins team
+* Copyright (c) 2002-2007 nagios-plugins team
 *
 * Last Modified: $Date$
 *
@@ -70,7 +70,7 @@
 
 const char *progname = "negate";
 const char *revision = "$Revision$";
-const char *copyright = "2002-2006";
+const char *copyright = "2002-2007";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #define DEFAULT_TIMEOUT 9
@@ -86,7 +86,12 @@ int validate_arguments (char **);
 void print_help (void);
 void print_usage (void);
 
-
+static int state[4] = {
+	STATE_OK,
+	STATE_WARNING,
+	STATE_CRITICAL,
+	STATE_UNKNOWN,
+};
 
 int
 main (int argc, char **argv)
@@ -130,12 +135,11 @@ main (int argc, char **argv)
 		printf ("%s\n", chld_out.line[i]);
 	}
 
-	if (result == STATE_OK)
-		exit (STATE_CRITICAL);
-	else if (result == STATE_CRITICAL)
-		exit (EXIT_SUCCESS);
-	else
+	if (result >= 0 && result <= 4) {
+		exit (state[result]);
+	} else {
 		exit (result);
+	}
 }
 
 /******************************************************************************
@@ -163,17 +167,22 @@ static const char **
 process_arguments (int argc, char **argv)
 {
 	int c;
+	int permute = TRUE;
 
 	int option = 0;
 	static struct option longopts[] = {
 		{"help", no_argument, 0, 'h'},
 		{"version", no_argument, 0, 'V'},
 		{"timeout", required_argument, 0, 't'},
+		{"ok", required_argument, 0, 'o'},
+		{"warning", required_argument, 0, 'w'},
+		{"critical", required_argument, 0, 'c'},
+		{"unknown", required_argument, 0, 'u'},
 		{0, 0, 0, 0}
 	};
 
 	while (1) {
-		c = getopt_long (argc, argv, "+hVt:", longopts, &option);
+		c = getopt_long (argc, argv, "+hVt:o:w:c:u:", longopts, &option);
 
 		if (c == -1 || c == EOF)
 			break;
@@ -195,11 +204,37 @@ process_arguments (int argc, char **argv)
 			else
 				timeout_interval = atoi (optarg);
 			break;
+		case 'o':     /* replacement for OK */
+			if ((state[STATE_OK] = translate_state(optarg)) == ERROR)
+				usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-4)."));
+			permute = FALSE;
+			break;
+
+		case 'w':     /* replacement for WARNING */
+			if ((state[STATE_WARNING] = translate_state(optarg)) == ERROR)
+				usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
+			permute = FALSE;
+			break;
+		case 'c':     /* replacement for CRITICAL */
+			if ((state[STATE_CRITICAL] = translate_state(optarg)) == ERROR)
+				usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
+			permute = FALSE;
+			break;
+		case 'u':     /* replacement for UNKNOWN */
+			if ((state[STATE_UNKNOWN] = translate_state(optarg)) == ERROR)
+				usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
+			permute = FALSE;
+			break;
 		}
 	}
 
 	validate_arguments (&argv[optind]);
 
+	if (permute) { /* No [owcu] switch specified, default to this */
+		state[STATE_OK] = STATE_CRITICAL;
+		state[STATE_CRITICAL] = STATE_OK;
+	}
+
 	return (const char **) &argv[optind];
 }
 
@@ -235,7 +270,23 @@ validate_arguments (char **command_line)
 -@@
 ******************************************************************************/
 
-
+int
+translate_state (char *state_text)
+{
+	char *temp_ptr;
+	for (temp_ptr = state_text; *temp_ptr; temp_ptr++) {
+		*temp_ptr = toupper(*temp_ptr);
+	}
+	if (!strcmp(state_text,"OK") || !strcmp(state_text,"0"))
+		return STATE_OK;
+	if (!strcmp(state_text,"WARNING") || !strcmp(state_text,"1"))
+		return STATE_WARNING;
+	if (!strcmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
+		return STATE_CRITICAL;
+	if (!strcmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
+		return STATE_UNKNOWN;
+	return ERROR;
+}
 
 void
 print_help (void)
@@ -244,7 +295,8 @@ print_help (void)
 
 	printf (COPYRIGHT, copyright, email);
 
-	printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa)."));
+	printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL and vice-versa)."));
+	printf ("%s\n", _("Additional switches can be used to control which state becomes what."));
 
 	printf ("\n\n");
 
@@ -253,15 +305,22 @@ print_help (void)
 	printf (_(UT_HELP_VRSN));
 
 	printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
+	printf ("    %s\n", _("Keep timeout lower than the plugin timeout to retain CRITICAL status."));
+
+	printf(" -o,--ok=STATUS\n");
+	printf(" -w,--warning=STATUS\n");
+	printf(" -c,--critical=STATUS\n");
+	printf(" -u,--unknown=STATUS\n");
+	printf(_("    STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
+	printf(_("    quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
+	printf(_("    OK and CRITICAL.\n"));
 
-	printf ("    %s\n", _("[keep timeout than the plugin timeout to retain CRITICAL status]"));
 	printf ("\n");
 	printf ("%s\n", _("Examples:"));
 	printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
 	printf ("    %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
-	printf (" %s\n", "negate /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
-	printf ("    %s\n", _("Use single quotes if you need to retain spaces"));
-	printf (_(UT_VERBOSE));
+	printf (" %s\n", "negate -w OK -c UNKNOWN /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
+	printf ("    %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
 	printf ("\n");
 	printf ("%s\n", _("Notes:"));
 	printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
@@ -279,5 +338,5 @@ void
 print_usage (void)
 {
 	printf (_("Usage:"));
-	printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname);
+	printf ("%s [-t timeout] [-owcu STATE] <definition of wrapped plugin>\n", progname);
 }
diff --git a/plugins/t/negate.t b/plugins/t/negate.t
index 0efa0ca..3a894f5 100644
--- a/plugins/t/negate.t
+++ b/plugins/t/negate.t
@@ -10,9 +10,8 @@ use strict;
 use Test::More;
 use NPTest;
 
-# 47 tests if the "map changes to return codes" patch is applied
-#plan tests => 47;
-plan tests => 15;
+# 15 tests in the first part and 32 in the last loop
+plan tests => 47;
 
 my $res;
 
@@ -57,23 +56,17 @@ $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' );
 
 
-# Remove __DATA__ to run tests with future patch
-__DATA__
-
-TODO: {
-	local $TODO = "Codes can be switched";
-	my %state = (
-		ok => 0,
-		warning => 1,
-		critical => 2,
-		unknown => 3,
-		);
-	foreach my $current_state (qw(ok warning critical unknown)) {
-		foreach my $new_state (qw(ok warning critical unknown)) {
-			$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->output, uc($current_state).": Fake $new_state" );
-		}
+my %state = (
+	ok => 0,
+	warning => 1,
+	critical => 2,
+	unknown => 3,
+	);
+foreach my $current_state (qw(ok warning critical unknown)) {
+	foreach my $new_state (qw(ok warning critical unknown)) {
+		$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->output, uc($current_state).": Fake $new_state" );
 	}
 }
 
-- 
cgit v0.10-9-g596f

