summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8a39526)
raw | patch | inline | side by side (parent: 8a39526)
author | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | |
Sat, 22 Sep 2007 03:00:46 +0000 (03:00 +0000) | ||
committer | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | |
Sat, 22 Sep 2007 03:00:46 +0000 (03:00 +0000) |
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1785 f882894a-f735-0410-b71e-b25c423dba1c
NEWS | patch | blob | history | |
plugins/negate.c | patch | blob | history | |
plugins/t/negate.t | patch | blob | history |
index 6455122c6e28e8d97a460ee3e2223295e4d7ae11..7572b5903d403797f26752753df85b18c6c11035 100644 (file)
--- a/NEWS
+++ b/NEWS
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 cbde6a1279e716e79646e667bee1f3ba223cbaaf..00e46d6b0d5dbec869877ed94dbd0f61e83714f6 100644 (file)
--- a/plugins/negate.c
+++ b/plugins/negate.c
* Nagios negate plugin
*
* License: GPL
-* Copyright (c) 2002-2006 nagios-plugins team
+* Copyright (c) 2002-2007 nagios-plugins team
*
* Last Modified: $Date$
*
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
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)
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);
+ }
}
/******************************************************************************
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;
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];
}
-@@
******************************************************************************/
-
+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)
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");
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."));
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 0efa0ca891423950847e347a94546b5c3a9f0d6c..3a894f558568c29a017281129d59984bc1f8300d 100644 (file)
--- a/plugins/t/negate.t
+++ b/plugins/t/negate.t
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;
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" );
}
}