diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2007-09-21 23:01:28 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2007-09-21 23:01:28 +0000 |
commit | 8a39526e1b8754a8b8fbb50f7f6806af4def7baa (patch) | |
tree | c80f8ddafe75a920cdcec549e30e728bb57125f9 /plugins | |
parent | 7a7052512953e6626edf8efc87664dba3ee01d74 (diff) | |
download | monitoring-plugins-8a39526e1b8754a8b8fbb50f7f6806af4def7baa.tar.gz |
Stop double expansion of parameters for negate - works like
time command now
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1784 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/Makefile.am | 4 | ||||
-rw-r--r-- | plugins/negate.c | 107 | ||||
-rw-r--r-- | plugins/t/negate.pl | 48 | ||||
-rw-r--r-- | plugins/t/negate.t | 79 |
4 files changed, 130 insertions, 108 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 5e3cb0f1..c0486bc1 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -87,7 +87,7 @@ check_ups_LDADD = $(NETLIBS) check_users_LDADD = $(BASEOBJS) popen.o check_by_ssh_LDADD = $(NETLIBS) runcmd.o check_ide_smart_LDADD = $(BASEOBJS) -negate_LDADD = $(BASEOBJS) popen.o +negate_LDADD = $(BASEOBJS) urlize_LDADD = $(BASEOBJS) popen.o check_apt_DEPENDENCIES = check_apt.c $(BASEOBJS) runcmd.o $(DEPLIBS) @@ -126,7 +126,7 @@ check_time_DEPENDENCIES = check_time.c $(NETOBJS) $(DEPLIBS) check_ups_DEPENDENCIES = check_ups.c $(NETOBJS) $(DEPLIBS) check_users_DEPENDENCIES = check_users.c $(BASEOBJS) popen.o $(DEPLIBS) check_by_ssh_DEPENDENCIES = check_by_ssh.c $(NETOBJS) runcmd.o $(DEPLIBS) -negate_DEPENDENCIES = negate.c $(BASEOBJS) popen.o $(DEPLIBS) +negate_DEPENDENCIES = negate.c $(BASEOBJS) $(DEPLIBS) urlize_DEPENDENCIES = urlize.c $(BASEOBJS) popen.o $(DEPLIBS) ############################################################################## diff --git a/plugins/negate.c b/plugins/negate.c index 8b2dff0e..cbde6a12 100644 --- a/plugins/negate.c +++ b/plugins/negate.c @@ -77,12 +77,12 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include "common.h" #include "utils.h" -#include "popen.h" +#include "utils_cmd.h" -char *command_line; +//char *command_line; -int process_arguments (int, char **); -int validate_arguments (void); +static const char **process_arguments (int, char **); +int validate_arguments (char **); void print_help (void); void print_usage (void); @@ -93,13 +93,15 @@ main (int argc, char **argv) { int found = 0, result = STATE_UNKNOWN; char *buf; + char **command_line; + output chld_out, chld_err; + int i; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); - if (process_arguments (argc, argv) == ERROR) - usage4 (_("Could not parse arguments")); + command_line = (char **) process_arguments (argc, argv); /* Set signal handling and alarm */ if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) @@ -107,36 +109,26 @@ main (int argc, char **argv) (void) alarm ((unsigned) timeout_interval); - child_process = spopen (command_line); - if (child_process == NULL) - die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), command_line); - - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); - - if (child_stderr == NULL) { - printf (_("Could not open stderr for %s\n"), command_line); + /* catch when the command is quoted */ + if(command_line[1] == NULL) { + result = cmd_run (command_line[0], &chld_out, &chld_err, 0); + } else { + result = cmd_run_array (command_line, &chld_out, &chld_err, 0); } - - buf = malloc(MAX_INPUT_BUFFER); - while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { - found++; - printf ("%s", buf); + if (chld_err.lines > 0) { + printf ("Error output from command:\n"); + for (i = 0; i < chld_err.lines; i++) { + printf ("%s\n", chld_err.line[i]); + } + exit (STATE_WARNING); } - if (!found) - die (STATE_UNKNOWN, - _("%s problem - No data received from host\nCMD: %s\n"),\ - argv[0], command_line); - - /* close the pipe */ - result = spclose (child_process); - - /* WARNING if output found on stderr */ - if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) - result = max_state (result, STATE_WARNING); + if (chld_out.lines == 0) + die (STATE_UNKNOWN, _("No data returned from command\n")); - /* close stderr */ - (void) fclose (child_stderr); + for (i = 0; i < chld_out.lines; i++) { + printf ("%s\n", chld_out.line[i]); + } if (result == STATE_OK) exit (STATE_CRITICAL); @@ -167,7 +159,7 @@ is a only a 'timeout' option.</para> /* process command-line arguments */ -int +static const char ** process_arguments (int argc, char **argv) { int c; @@ -181,8 +173,7 @@ process_arguments (int argc, char **argv) }; while (1) { - c = getopt_long (argc, argv, "+hVt:", - longopts, &option); + c = getopt_long (argc, argv, "+hVt:", longopts, &option); if (c == -1 || c == EOF) break; @@ -207,12 +198,9 @@ process_arguments (int argc, char **argv) } } - asprintf (&command_line, "%s", argv[optind]); - for (c = optind+1; c < argc; c++) { - asprintf (&command_line, "%s %s", command_line, argv[c]); - } + validate_arguments (&argv[optind]); - return validate_arguments (); + return (const char **) &argv[optind]; } @@ -230,11 +218,13 @@ process_arguments (int argc, char **argv) int -validate_arguments () +validate_arguments (char **command_line) { - if (command_line == NULL) - return ERROR; - return STATE_OK; + if (command_line[0] == NULL) + usage4 (_("Could not parse arguments")); + + if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0) + usage4 (_("Require path to command")); } /****************************************************************************** @@ -256,7 +246,7 @@ print_help (void) printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).")); - printf ("\n\n"); + printf ("\n\n"); print_usage (); @@ -265,19 +255,20 @@ print_help (void) printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT); 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 ("\n"); - printf ("%s\n", _("Notes:")); + 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 ("\n"); + printf ("%s\n", _("Notes:")); printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); - printf ("%s\n", _("If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.")); - printf ("%s\n", _("If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.")); - printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); + printf ("%s\n", _("The full path of the plugin must be provided.")); + printf ("%s\n", _("If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.")); + printf ("%s\n", _("If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.")); + printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); printf (_(UT_SUPPORT)); } @@ -287,6 +278,6 @@ print_help (void) void print_usage (void) { - printf (_("Usage:")); + printf (_("Usage:")); printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname); } diff --git a/plugins/t/negate.pl b/plugins/t/negate.pl deleted file mode 100644 index 6c56d4f4..00000000 --- a/plugins/t/negate.pl +++ /dev/null @@ -1,48 +0,0 @@ -#! /usr/bin/perl -w -I .. -# -# negate checks -# Need check_dummy to work for testing -# -# $Id$ -# - -use strict; -use Test::More; -use NPTest; - -plan tests => 40; - -my $res; - -$res = NPTest->testCmd( "./negate" ); -is( $res->return_code, 3, "Not enough parameters"); -like( $res->output, "/Could not parse arguments/", "Could not parse arguments"); - -$res = NPTest->testCmd( "./negate ./check_dummy 0 'a dummy okay'" ); -is( $res->return_code, 2, "OK changed to CRITICAL" ); -is( $res->output, "OK: a dummy okay" ); - -$res = NPTest->testCmd( "./negate './check_dummy 0 redsweaterblog'"); -is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" ); -is( $res->output, "OK: redsweaterblog" ); - -$res = NPTest->testCmd( "./negate ./check_dummy 1 'a warn a day keeps the managers at bay'" ); -is( $res->return_code, 2, "WARN stays same" ); - -$res = NPTest->testCmd( "./negate ./check_dummy 3 mysterious"); -is( $res->return_code, 3, "UNKNOWN stays same" ); - -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" ); - } -} - diff --git a/plugins/t/negate.t b/plugins/t/negate.t new file mode 100644 index 00000000..0efa0ca8 --- /dev/null +++ b/plugins/t/negate.t @@ -0,0 +1,79 @@ +#! /usr/bin/perl -w -I .. +# +# negate checks +# Need check_dummy to work for testing +# +# $Id: negate.pl 1717 2007-05-24 08:53:50Z tonvoon $ +# + +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; + +my $res; + +my $PWD = $ENV{PWD}; + +$res = NPTest->testCmd( "./negate" ); +is( $res->return_code, 3, "Not enough parameters"); +like( $res->output, "/Could not parse arguments/", "Could not parse arguments"); + +$res = NPTest->testCmd( "./negate bobthebuilder" ); +is( $res->return_code, 3, "Require full path" ); +like( $res->output, "/Require path to command/", "Appropriate error message"); + +$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" ); +is( $res->return_code, 2, "OK changed to CRITICAL" ); +is( $res->output, "OK: a dummy okay", "Output as expected" ); + +$res = NPTest->testCmd( "./negate '$PWD/check_dummy 0 redsweaterblog'"); +is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" ); +is( $res->output, "OK: redsweaterblog", "Output as expected" ); + +$res = NPTest->testCmd( "./negate $PWD/check_dummy 1 'a warn a day keeps the managers at bay'" ); +is( $res->return_code, 1, "WARN stays same" ); + +$res = NPTest->testCmd( "./negate $PWD/check_dummy 3 mysterious"); +is( $res->return_code, 3, "UNKNOWN stays same" ); + +$res = NPTest->testCmd( "./negate \"$PWD/check_dummy 0 'a dummy okay'\"" ); +is( $res->output, "OK: a dummy okay", "Checking slashed quotes - the single quotes are re-evaluated at shell" ); + +# Output is "OK: a" because check_dummy only returns the first arg +$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 a dummy okay" ); +is( $res->output, "OK: a", "Multiple args passed as arrays" ); + +$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" ); +is( $res->output, "OK: a dummy okay", "The quoted string is passed through to subcommand correctly" ); + +$res = NPTest->testCmd( "./negate '$PWD/check_dummy 0' 'a dummy okay'" ); +is( $res->output, "No data returned from command", "Bad command, as expected (trying to execute './check_dummy 0')"); + +$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" ); + } + } +} + |