summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2007-09-21 23:01:28 (GMT)
committerTon Voon <tonvoon@users.sourceforge.net>2007-09-21 23:01:28 (GMT)
commit8a39526e1b8754a8b8fbb50f7f6806af4def7baa (patch)
treec80f8ddafe75a920cdcec549e30e728bb57125f9 /plugins
parent7a7052512953e6626edf8efc87664dba3ee01d74 (diff)
downloadmonitoring-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.am4
-rw-r--r--plugins/negate.c107
-rw-r--r--plugins/t/negate.pl48
-rw-r--r--plugins/t/negate.t79
4 files changed, 130 insertions, 108 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 5e3cb0f..c0486bc 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -87,7 +87,7 @@ check_ups_LDADD = $(NETLIBS)
87check_users_LDADD = $(BASEOBJS) popen.o 87check_users_LDADD = $(BASEOBJS) popen.o
88check_by_ssh_LDADD = $(NETLIBS) runcmd.o 88check_by_ssh_LDADD = $(NETLIBS) runcmd.o
89check_ide_smart_LDADD = $(BASEOBJS) 89check_ide_smart_LDADD = $(BASEOBJS)
90negate_LDADD = $(BASEOBJS) popen.o 90negate_LDADD = $(BASEOBJS)
91urlize_LDADD = $(BASEOBJS) popen.o 91urlize_LDADD = $(BASEOBJS) popen.o
92 92
93check_apt_DEPENDENCIES = check_apt.c $(BASEOBJS) runcmd.o $(DEPLIBS) 93check_apt_DEPENDENCIES = check_apt.c $(BASEOBJS) runcmd.o $(DEPLIBS)
@@ -126,7 +126,7 @@ check_time_DEPENDENCIES = check_time.c $(NETOBJS) $(DEPLIBS)
126check_ups_DEPENDENCIES = check_ups.c $(NETOBJS) $(DEPLIBS) 126check_ups_DEPENDENCIES = check_ups.c $(NETOBJS) $(DEPLIBS)
127check_users_DEPENDENCIES = check_users.c $(BASEOBJS) popen.o $(DEPLIBS) 127check_users_DEPENDENCIES = check_users.c $(BASEOBJS) popen.o $(DEPLIBS)
128check_by_ssh_DEPENDENCIES = check_by_ssh.c $(NETOBJS) runcmd.o $(DEPLIBS) 128check_by_ssh_DEPENDENCIES = check_by_ssh.c $(NETOBJS) runcmd.o $(DEPLIBS)
129negate_DEPENDENCIES = negate.c $(BASEOBJS) popen.o $(DEPLIBS) 129negate_DEPENDENCIES = negate.c $(BASEOBJS) $(DEPLIBS)
130urlize_DEPENDENCIES = urlize.c $(BASEOBJS) popen.o $(DEPLIBS) 130urlize_DEPENDENCIES = urlize.c $(BASEOBJS) popen.o $(DEPLIBS)
131 131
132############################################################################## 132##############################################################################
diff --git a/plugins/negate.c b/plugins/negate.c
index 8b2dff0..cbde6a1 100644
--- a/plugins/negate.c
+++ b/plugins/negate.c
@@ -77,12 +77,12 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
77 77
78#include "common.h" 78#include "common.h"
79#include "utils.h" 79#include "utils.h"
80#include "popen.h" 80#include "utils_cmd.h"
81 81
82char *command_line; 82//char *command_line;
83 83
84int process_arguments (int, char **); 84static const char **process_arguments (int, char **);
85int validate_arguments (void); 85int validate_arguments (char **);
86void print_help (void); 86void print_help (void);
87void print_usage (void); 87void print_usage (void);
88 88
@@ -93,13 +93,15 @@ main (int argc, char **argv)
93{ 93{
94 int found = 0, result = STATE_UNKNOWN; 94 int found = 0, result = STATE_UNKNOWN;
95 char *buf; 95 char *buf;
96 char **command_line;
97 output chld_out, chld_err;
98 int i;
96 99
97 setlocale (LC_ALL, ""); 100 setlocale (LC_ALL, "");
98 bindtextdomain (PACKAGE, LOCALEDIR); 101 bindtextdomain (PACKAGE, LOCALEDIR);
99 textdomain (PACKAGE); 102 textdomain (PACKAGE);
100 103
101 if (process_arguments (argc, argv) == ERROR) 104 command_line = (char **) process_arguments (argc, argv);
102 usage4 (_("Could not parse arguments"));
103 105
104 /* Set signal handling and alarm */ 106 /* Set signal handling and alarm */
105 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) 107 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
@@ -107,36 +109,26 @@ main (int argc, char **argv)
107 109
108 (void) alarm ((unsigned) timeout_interval); 110 (void) alarm ((unsigned) timeout_interval);
109 111
110 child_process = spopen (command_line); 112 /* catch when the command is quoted */
111 if (child_process == NULL) 113 if(command_line[1] == NULL) {
112 die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), command_line); 114 result = cmd_run (command_line[0], &chld_out, &chld_err, 0);
113 115 } else {
114 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); 116 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
115
116 if (child_stderr == NULL) {
117 printf (_("Could not open stderr for %s\n"), command_line);
118 } 117 }
119 118 if (chld_err.lines > 0) {
120 buf = malloc(MAX_INPUT_BUFFER); 119 printf ("Error output from command:\n");
121 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { 120 for (i = 0; i < chld_err.lines; i++) {
122 found++; 121 printf ("%s\n", chld_err.line[i]);
123 printf ("%s", buf); 122 }
123 exit (STATE_WARNING);
124 } 124 }
125 125
126 if (!found) 126 if (chld_out.lines == 0)
127 die (STATE_UNKNOWN, 127 die (STATE_UNKNOWN, _("No data returned from command\n"));
128 _("%s problem - No data received from host\nCMD: %s\n"),\
129 argv[0], command_line);
130
131 /* close the pipe */
132 result = spclose (child_process);
133
134 /* WARNING if output found on stderr */
135 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
136 result = max_state (result, STATE_WARNING);
137 128
138 /* close stderr */ 129 for (i = 0; i < chld_out.lines; i++) {
139 (void) fclose (child_stderr); 130 printf ("%s\n", chld_out.line[i]);
131 }
140 132
141 if (result == STATE_OK) 133 if (result == STATE_OK)
142 exit (STATE_CRITICAL); 134 exit (STATE_CRITICAL);
@@ -167,7 +159,7 @@ is a only a 'timeout' option.</para>
167 159
168 160
169/* process command-line arguments */ 161/* process command-line arguments */
170int 162static const char **
171process_arguments (int argc, char **argv) 163process_arguments (int argc, char **argv)
172{ 164{
173 int c; 165 int c;
@@ -181,8 +173,7 @@ process_arguments (int argc, char **argv)
181 }; 173 };
182 174
183 while (1) { 175 while (1) {
184 c = getopt_long (argc, argv, "+hVt:", 176 c = getopt_long (argc, argv, "+hVt:", longopts, &option);
185 longopts, &option);
186 177
187 if (c == -1 || c == EOF) 178 if (c == -1 || c == EOF)
188 break; 179 break;
@@ -207,12 +198,9 @@ process_arguments (int argc, char **argv)
207 } 198 }
208 } 199 }
209 200
210 asprintf (&command_line, "%s", argv[optind]); 201 validate_arguments (&argv[optind]);
211 for (c = optind+1; c < argc; c++) {
212 asprintf (&command_line, "%s %s", command_line, argv[c]);
213 }
214 202
215 return validate_arguments (); 203 return (const char **) &argv[optind];
216} 204}
217 205
218 206
@@ -230,11 +218,13 @@ process_arguments (int argc, char **argv)
230 218
231 219
232int 220int
233validate_arguments () 221validate_arguments (char **command_line)
234{ 222{
235 if (command_line == NULL) 223 if (command_line[0] == NULL)
236 return ERROR; 224 usage4 (_("Could not parse arguments"));
237 return STATE_OK; 225
226 if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
227 usage4 (_("Require path to command"));
238} 228}
239 229
240/****************************************************************************** 230/******************************************************************************
@@ -256,7 +246,7 @@ print_help (void)
256 246
257 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).")); 247 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa)."));
258 248
259 printf ("\n\n"); 249 printf ("\n\n");
260 250
261 print_usage (); 251 print_usage ();
262 252
@@ -265,19 +255,20 @@ print_help (void)
265 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT); 255 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
266 256
267 printf (" %s\n", _("[keep timeout than the plugin timeout to retain CRITICAL status]")); 257 printf (" %s\n", _("[keep timeout than the plugin timeout to retain CRITICAL status]"));
268 printf ("\n"); 258 printf ("\n");
269 printf ("%s\n", _("Examples:")); 259 printf ("%s\n", _("Examples:"));
270 printf (" %s\n", "negate \"/usr/local/nagios/libexec/check_ping -H host\""); 260 printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
271 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin")); 261 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
272 printf (" %s\n", "negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\""); 262 printf (" %s\n", "negate /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
273 printf (" %s\n", _("Use single quotes if you need to retain spaces")); 263 printf (" %s\n", _("Use single quotes if you need to retain spaces"));
274 printf (_(UT_VERBOSE)); 264 printf (_(UT_VERBOSE));
275 printf ("\n"); 265 printf ("\n");
276 printf ("%s\n", _("Notes:")); 266 printf ("%s\n", _("Notes:"));
277 printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); 267 printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
278 printf ("%s\n", _("If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.")); 268 printf ("%s\n", _("The full path of the plugin must be provided."));
279 printf ("%s\n", _("If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.")); 269 printf ("%s\n", _("If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL."));
280 printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); 270 printf ("%s\n", _("If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK."));
271 printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
281 272
282 printf (_(UT_SUPPORT)); 273 printf (_(UT_SUPPORT));
283} 274}
@@ -287,6 +278,6 @@ print_help (void)
287void 278void
288print_usage (void) 279print_usage (void)
289{ 280{
290 printf (_("Usage:")); 281 printf (_("Usage:"));
291 printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname); 282 printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname);
292} 283}
diff --git a/plugins/t/negate.pl b/plugins/t/negate.pl
deleted file mode 100644
index 6c56d4f..0000000
--- a/plugins/t/negate.pl
+++ /dev/null
@@ -1,48 +0,0 @@
1#! /usr/bin/perl -w -I ..
2#
3# negate checks
4# Need check_dummy to work for testing
5#
6# $Id$
7#
8
9use strict;
10use Test::More;
11use NPTest;
12
13plan tests => 40;
14
15my $res;
16
17$res = NPTest->testCmd( "./negate" );
18is( $res->return_code, 3, "Not enough parameters");
19like( $res->output, "/Could not parse arguments/", "Could not parse arguments");
20
21$res = NPTest->testCmd( "./negate ./check_dummy 0 'a dummy okay'" );
22is( $res->return_code, 2, "OK changed to CRITICAL" );
23is( $res->output, "OK: a dummy okay" );
24
25$res = NPTest->testCmd( "./negate './check_dummy 0 redsweaterblog'");
26is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" );
27is( $res->output, "OK: redsweaterblog" );
28
29$res = NPTest->testCmd( "./negate ./check_dummy 1 'a warn a day keeps the managers at bay'" );
30is( $res->return_code, 2, "WARN stays same" );
31
32$res = NPTest->testCmd( "./negate ./check_dummy 3 mysterious");
33is( $res->return_code, 3, "UNKNOWN stays same" );
34
35my %state = (
36 ok => 0,
37 warning => 1,
38 critical => 2,
39 unknown => 3,
40 );
41foreach my $current_state (qw(ok warning critical unknown)) {
42 foreach my $new_state (qw(ok warning critical unknown)) {
43 $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
44 is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
45 is( $res->output, uc($current_state).": Fake $new_state" );
46 }
47}
48
diff --git a/plugins/t/negate.t b/plugins/t/negate.t
new file mode 100644
index 0000000..0efa0ca
--- /dev/null
+++ b/plugins/t/negate.t
@@ -0,0 +1,79 @@
1#! /usr/bin/perl -w -I ..
2#
3# negate checks
4# Need check_dummy to work for testing
5#
6# $Id: negate.pl 1717 2007-05-24 08:53:50Z tonvoon $
7#
8
9use strict;
10use Test::More;
11use NPTest;
12
13# 47 tests if the "map changes to return codes" patch is applied
14#plan tests => 47;
15plan tests => 15;
16
17my $res;
18
19my $PWD = $ENV{PWD};
20
21$res = NPTest->testCmd( "./negate" );
22is( $res->return_code, 3, "Not enough parameters");
23like( $res->output, "/Could not parse arguments/", "Could not parse arguments");
24
25$res = NPTest->testCmd( "./negate bobthebuilder" );
26is( $res->return_code, 3, "Require full path" );
27like( $res->output, "/Require path to command/", "Appropriate error message");
28
29$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
30is( $res->return_code, 2, "OK changed to CRITICAL" );
31is( $res->output, "OK: a dummy okay", "Output as expected" );
32
33$res = NPTest->testCmd( "./negate '$PWD/check_dummy 0 redsweaterblog'");
34is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" );
35is( $res->output, "OK: redsweaterblog", "Output as expected" );
36
37$res = NPTest->testCmd( "./negate $PWD/check_dummy 1 'a warn a day keeps the managers at bay'" );
38is( $res->return_code, 1, "WARN stays same" );
39
40$res = NPTest->testCmd( "./negate $PWD/check_dummy 3 mysterious");
41is( $res->return_code, 3, "UNKNOWN stays same" );
42
43$res = NPTest->testCmd( "./negate \"$PWD/check_dummy 0 'a dummy okay'\"" );
44is( $res->output, "OK: a dummy okay", "Checking slashed quotes - the single quotes are re-evaluated at shell" );
45
46# Output is "OK: a" because check_dummy only returns the first arg
47$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 a dummy okay" );
48is( $res->output, "OK: a", "Multiple args passed as arrays" );
49
50$res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
51is( $res->output, "OK: a dummy okay", "The quoted string is passed through to subcommand correctly" );
52
53$res = NPTest->testCmd( "./negate '$PWD/check_dummy 0' 'a dummy okay'" );
54is( $res->output, "No data returned from command", "Bad command, as expected (trying to execute './check_dummy 0')");
55
56$res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' );
57is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
58
59
60# Remove __DATA__ to run tests with future patch
61__DATA__
62
63TODO: {
64 local $TODO = "Codes can be switched";
65 my %state = (
66 ok => 0,
67 warning => 1,
68 critical => 2,
69 unknown => 3,
70 );
71 foreach my $current_state (qw(ok warning critical unknown)) {
72 foreach my $new_state (qw(ok warning critical unknown)) {
73 $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
74 is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
75 is( $res->output, uc($current_state).": Fake $new_state" );
76 }
77 }
78}
79