Code

Stop double expansion of parameters for negate - works like
[nagiosplug.git] / plugins / negate.c
index 9e5cf46fd8a490e9bebec8b24b534ac7696cf340..cbde6a1279e716e79646e667bee1f3ba223cbaaf 100644 (file)
@@ -1,20 +1,35 @@
 /******************************************************************************
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id$
+*
+* Nagios negate plugin
+*
+* License: GPL
+* Copyright (c) 2002-2006 nagios-plugins team
+*
+* Last Modified: $Date$
+*
+* Description:
+*
+* This file contains the negate plugin
+*
+*  Negates the status of a plugin (returns OK for CRITICAL, and vice-versa)
+*
+* License Information:
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*
+* $Id$
 
 @@-<article>
 
 
 const char *progname = "negate";
 const char *revision = "$Revision$";
-const char *copyright = "2002-2003";
+const char *copyright = "2002-2006";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #define DEFAULT_TIMEOUT 9
 
 #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);
 
@@ -78,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)
-               usage (_("negate: could not parse arguments\n"));
+       command_line = (char **) process_arguments (argc, argv);
 
        /* Set signal handling and alarm */
        if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
@@ -92,35 +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);
@@ -151,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;
@@ -165,17 +173,14 @@ 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;
 
                switch (c) {
                case '?':     /* help */
-                       printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
-                       print_usage ();
-                       exit (STATE_UNKNOWN);
+                       usage5 ();
                        break;
                case 'h':     /* help */
                        print_help ();
@@ -193,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];
 }
 
 
@@ -216,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"));
 }
 
 /******************************************************************************
@@ -238,11 +242,11 @@ print_help (void)
 {
        print_revision (progname, revision);
 
-       printf (_(COPYRIGHT), copyright, email);
+       printf (COPYRIGHT, copyright, email);
 
-       printf (_("\
-Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n\
-\n"));
+       printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa)."));
+
+       printf ("\n\n");
 
        print_usage ();
 
@@ -250,20 +254,21 @@ Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n\
 
        printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
 
-       printf (_("\
-     [keep timeout than the plugin timeout to retain CRITICAL status]\n"));
-
-       printf (_("\
-  negate \"/usr/local/nagios/libexec/check_ping -H host\"\n\
-    Run check_ping and invert result. Must use full path to plugin\n\
-  negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"\n\
-    Use single quotes if you need to retain spaces\n"));
-
-       printf (_("\
-This plugin is a wrapper to take the output of another plugin and invert it.\n\
-If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
-If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
-Otherwise, the output state of the wrapped plugin is unchanged.\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 ("\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", _("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));
 }
@@ -273,7 +278,6 @@ Otherwise, the output state of the wrapped plugin is unchanged.\n"));
 void
 print_usage (void)
 {
-       printf (_("Usage: %s [-t timeout] <definition of wrapped plugin>\n"),
-               progname);
-       printf (_(UT_HLP_VRS), progname, progname);
+       printf (_("Usage:"));
+       printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname);
 }