Code

Fix includes for gettext
[nagiosplug.git] / plugins / check_by_ssh.c
index 560ae0c61d73298f05bcda96980b11d0c22d5fb9..f31c9c61b6984536e7a872030cdd1f1593b6b7a4 100644 (file)
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
+ $Id$
  *****************************************************************************/
  
 const char *progname = "check_by_ssh";
 const char *revision = "$Revision$";
-const char *copyright = "2000-2003";
+const char *copyright = "2000-2004";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
@@ -33,6 +35,7 @@ void print_usage (void);
 
 int commands = 0;
 int services = 0;
+int skip_lines = 0;
 char *remotecmd = NULL;
 char *comm = NULL;
 char *hostname = NULL;
@@ -66,17 +69,14 @@ main (int argc, char **argv)
 
        /* process arguments */
        if (process_arguments (argc, argv) == ERROR)
-               usage (_("Could not parse arguments\n"));
-
+               usage4 (_("Could not parse arguments"));
 
        /* Set signal handling and alarm timeout */
        if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
-               printf (_("Cannot catch SIGALRM"));
-               return STATE_UNKNOWN;
+               usage4 (_("Cannot catch SIGALRM"));
        }
        alarm (timeout_interval);
 
-
        /* run the command */
 
        if (verbose)
@@ -85,7 +85,7 @@ main (int argc, char **argv)
        child_process = spopen (comm);
 
        if (child_process == NULL) {
-               printf (_("Unable to open pipe: %s"), comm);
+               printf (_("Could not open pipe: %s\n"), comm);
                return STATE_UNKNOWN;
        }
 
@@ -101,15 +101,20 @@ main (int argc, char **argv)
        while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
                asprintf (&result_text, "%s%s", result_text, input_buffer);
 
-
        /* WARNING if output found on stderr */
-       if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
-               printf ("%s\n", input_buffer);
-               while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
-                       printf ("%s\n", input_buffer);
-               return STATE_WARNING;
+       while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
+               if (skip_lines > 0) {
+                       if (input_buffer[strlen(input_buffer)-1] == '\n') {
+                               skip_lines--;
+                       }
+               } else {
+                       printf ("%s", input_buffer);
+                       result = STATE_WARNING;
+               }
        }
        (void) fclose (child_stderr);
+       if (result == STATE_WARNING)
+               return result;
 
 
        /* close the pipe */
@@ -139,7 +144,7 @@ main (int argc, char **argv)
                                eol[0] = 0;
                        if (service[commands] && status_text
                                        && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) {
-                               fprintf (fp, _("[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n"),
+                               fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
                                                                 (int) local_time, host_shortname, service[commands++], cresult,
                                                                 output);
                        }
@@ -161,11 +166,6 @@ main (int argc, char **argv)
        return result;
 }
 
-
-
-
-
-\f
 /* process command-line arguments */
 int
 process_arguments (int argc, char **argv)
@@ -189,6 +189,7 @@ process_arguments (int argc, char **argv)
                {"user", required_argument, 0, 'u'},
                {"logname", required_argument, 0, 'l'},
                {"command", required_argument, 0, 'C'},
+               {"skip", required_argument, 0, 'S'},
                {"proto1", no_argument, 0, '1'},
                {"proto2", no_argument, 0, '2'},
                {"use-ipv4", no_argument, 0, '4'},
@@ -204,7 +205,7 @@ process_arguments (int argc, char **argv)
                        strcpy (argv[c], "-t");
 
        while (1) {
-               c = getopt_long (argc, argv, "Vvh1246ft:H:O:p:i:u:l:C:n:s:", longopts,
+               c = getopt_long (argc, argv, "Vvh1246ft:H:O:p:i:u:l:C:S:n:s:", longopts,
                                                                         &option);
 
                if (c == -1 || c == EOF)
@@ -212,10 +213,11 @@ process_arguments (int argc, char **argv)
 
                switch (c) {
                case '?':                                                                       /* help */
+                       printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
                        print_usage ();
                        exit (STATE_UNKNOWN);
                case 'V':                                                                       /* version */
-                       print_revision (progname, "$Revision$");
+                       print_revision (progname, revision);
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
                        print_help ();
@@ -225,18 +227,18 @@ process_arguments (int argc, char **argv)
                        break;
                case 't':                                                                       /* timeout period */
                        if (!is_integer (optarg))
-                               usage2 (_("timeout interval must be an integer"), optarg);
+                               usage2 (_("Timeout interval must be a positive integer"), optarg);
                        else
                                timeout_interval = atoi (optarg);
                        break;
                case 'H':                                                                       /* host */
                        if (!is_host (optarg))
-                               usage2 (_("invalid host name"), optarg);
+                               usage2 (_("Invalid hostname/address"), optarg);
                        hostname = optarg;
                        break;
                case 'p': /* port number */
                        if (!is_integer (optarg))
-                               usage2 (_("port must be an integer"), optarg);
+                               usage2 (_("Port must be a positive integer"), optarg);
                        asprintf (&comm,"%s -p %s", comm, optarg);
                        break;
                case 'O':                                                                       /* output file */
@@ -275,6 +277,13 @@ process_arguments (int argc, char **argv)
                        if (commands > 1)
                                asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
                        asprintf (&remotecmd, "%s%s", remotecmd, optarg);
+                       break;
+               case 'S':                                                                       /* Skip n lines in the output to ignore system banner */
+                       if (!is_integer (optarg))
+                               usage2 (_("skip lines must be an integer"), optarg);
+                       else
+                               skip_lines = atoi (optarg);
+                       break;
                }
        }
 
@@ -283,7 +292,7 @@ process_arguments (int argc, char **argv)
                if (c <= argc) {
                        die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
                } else if (!is_host (argv[c]))
-                       die (STATE_UNKNOWN, _("%s: Invalid host name %s\n"), progname, argv[c]);
+                       die (STATE_UNKNOWN, "%s: %s %s\n", progname, _("Invalid hostname/address"), argv[c]);
                hostname = argv[c++];
        }
 
@@ -299,7 +308,7 @@ process_arguments (int argc, char **argv)
                asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
 
        if (remotecmd == NULL || strlen (remotecmd) <= 1)
-               usage (_("No remotecmd\n"));
+               usage4 (_("No remotecmd"));
 
        asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
 
@@ -308,8 +317,6 @@ process_arguments (int argc, char **argv)
 
 
 
-
-
 int
 validate_arguments (void)
 {
@@ -326,17 +333,13 @@ validate_arguments (void)
 }
 
 
-
-
-
-\f
 void
 print_help (void)
 {
        print_revision (progname, revision);
 
-       printf (_("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
-       printf (_(COPYRIGHT), copyright, email);
+       printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
+       printf (COPYRIGHT, copyright, email);
 
        printf (_("This plugin uses SSH to execute commands on a remote host\n\n"));
 
@@ -353,6 +356,8 @@ print_help (void)
     tell ssh to use Protocol 1\n\
  -2, --proto2\n\
     tell ssh to use Protocol 2\n\
+ -S, --skiplines=n\n\
+    Ignore first n lines on STDERR (to suppress a logon banner)\n\
  -f\n\
     tells ssh to fork rather than create a tty\n"));
 
@@ -388,19 +393,23 @@ To use passive mode, provide multiple '-C' options, and provide\n\
 all of -O, -s, and -n options (servicelist order must match '-C'\n\
 options)\n"));
 
+       printf ("\n\
+$ check_by_ssh -H localhost -n lh -s c1:c2:c3 \\\n\
+    -C uptime -C uptime -C uptime -O /tmp/foo\n\
+$ cat /tmp/foo\n\
+[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days...\n\
+[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days...\n\
+[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days...\n");
+
        printf (_(UT_SUPPORT));
 }
 
 
 
-
-
 void
 print_usage (void)
 {
-       printf (_("\n\
-Usage: %s [-f46] [-t timeout] [-i identity] [-l user] -H <host> \n\
-  -C <command> [-n name] [-s servicelist] [-O outputfile] [-p port]\n"),
-               progname);
-       printf (_(UT_HLP_VRS), progname, progname);
+       printf ("\n\
+Usage: %s [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>\n\
+                  [-n name] [-s servicelist] [-O outputfile] [-p port]\n", progname);
 }