summaryrefslogtreecommitdiffstats
path: root/plugins/negate.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/negate.c')
-rw-r--r--plugins/negate.c107
1 files changed, 49 insertions, 58 deletions
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";
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}