Code

Remove getopt_long checks
[nagiosplug.git] / plugins / check_by_ssh.c
1 /******************************************************************************
2  *
3  * This file is part of the Nagios Plugins.
4  *
5  * Copyright (c) 1999, 2000, 2001 Karl DeBisschop <karl@debisschop.net>
6  *
7  * The Nagios Plugins are free software; you can redistribute them
8  * and/or modify them under the terms of the GNU General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * $Id$
22  *
23  *****************************************************************************/
24  
25 const char *progname = "check_by_ssh";
26 #define DESCRIPTION "Run checks on a remote system using ssh, wrapping the proper timeout around the ssh invocation."
27 #define AUTHOR "Karl DeBisschop"
28 #define EMAIL "karl@debisschop.net"
29 #define COPYRIGHTDATE "1999, 2000, 2001"
31 #include "config.h"
32 #include "common.h"
33 #include "popen.h"
34 #include "utils.h"
35 #include <time.h>
37 int process_arguments (int, char **);
38 int validate_arguments (void);
39 void print_help (const char *command_name);
40 void print_usage (void);
43 int commands = 0;
44 int services = 0;
45 char *remotecmd = "";
46 char *comm = SSH_COMMAND;
47 char *hostname = NULL;
48 char *outputfile = NULL;
49 char *host_shortname = NULL;
50 char **service;
51 int passive = FALSE;
52 int verbose = FALSE;
55 int
56 main (int argc, char **argv)
57 {
59         char input_buffer[MAX_INPUT_BUFFER] = "";
60         char *result_text = "";
61         char *status_text;
62         char *output = "";
63         char *summary = "";
64         char *eol = NULL;
65         char *srvc_desc = NULL;
66         int cresult;
67         int result = STATE_UNKNOWN;
68         time_t local_time;
69         FILE *fp = NULL;
72         /* process arguments */
73         if (process_arguments (argc, argv) == ERROR)
74                 usage ("Could not parse arguments\n");
77         /* Set signal handling and alarm timeout */
78         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
79                 printf ("Cannot catch SIGALRM");
80                 return STATE_UNKNOWN;
81         }
82         alarm (timeout_interval);
85         /* run the command */
87         if (verbose)
88                 printf ("%s\n", comm);
90         child_process = spopen (comm);
92         if (child_process == NULL) {
93                 printf ("Unable to open pipe: %s", comm);
94                 return STATE_UNKNOWN;
95         }
98         /* open STDERR  for spopen */
99         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
100         if (child_stderr == NULL) {
101                 printf ("Could not open stderr for %s\n", SSH_COMMAND);
102         }
105         /* get results from remote command */
106         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
107                 asprintf (&result_text, "%s%s", result_text, input_buffer);
110         /* WARNING if output found on stderr */
111         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
112                 printf ("%s\n", input_buffer);
113                 return STATE_WARNING;
114         }
115         (void) fclose (child_stderr);
118         /* close the pipe */
119         result = spclose (child_process);
122         /* process output */
123         if (passive) {
125                 if (!(fp = fopen (outputfile, "a"))) {
126                         printf ("SSH WARNING: could not open %s\n", outputfile);
127                         exit (STATE_UNKNOWN);
128                 }
130                 time (&local_time);
131                 commands = 0;
132                 while (result_text && strlen(result_text) > 0) {
133                         status_text = (strstr (result_text, "STATUS CODE: "));
134                         if (status_text == NULL) {
135                                 printf ("%s", result_text);
136                                 return result;
137                         }
138                         asprintf (&output, "%s", result_text);
139                         result_text = strnl (status_text);
140                         eol = strpbrk (output, "\r\n");
141                         if (eol != NULL)
142                                 eol[0] = 0;
143                         if (service[commands] && status_text
144                                         && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) {
145                                 fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
146                                                                  (int) local_time, host_shortname, service[commands++], cresult,
147                                                                  output);
148                         }
149                 }
151         }
153         /* print the first line from the remote command */
154         else {
155                 eol = strpbrk (result_text, "\r\n");
156                 if (eol)
157                         eol[0] = 0;
158                 printf ("%s\n", result_text);
159         }
161         /* return error status from remote command */
162         return result;
169 /* process command-line arguments */
170 int
171 process_arguments (int argc, char **argv)
173         int c, i;
174         char *p1, *p2;
175         size_t len;
177         int option_index = 0;
178         static struct option long_options[] = {
179                 {"version", no_argument, 0, 'V'},
180                 {"help", no_argument, 0, 'h'},
181                 {"verbose", no_argument, 0, 'v'},
182                 {"fork", no_argument, 0, 'f'},
183                 {"timeout", required_argument, 0, 't'},
184                 {"host", required_argument, 0, 'H'},
185                 {"port", required_argument,0,'p'},
186                 {"output", required_argument, 0, 'O'},
187                 {"name", required_argument, 0, 'n'},
188                 {"services", required_argument, 0, 's'},
189                 {"identity", required_argument, 0, 'i'},
190                 {"user", required_argument, 0, 'u'},
191                 {"logname", required_argument, 0, 'l'},
192                 {"command", required_argument, 0, 'C'},
193                 {"use-ipv4", no_argument, 0, '4'},
194                 {"use-ipv6", no_argument, 0, '6'},
195                 {0, 0, 0, 0}
196         };
198         if (argc < 2)
199                 return ERROR;
201         for (c = 1; c < argc; c++)
202                 if (strcmp ("-to", argv[c]) == 0)
203                         strcpy (argv[c], "-t");
205         while (1) {
206                 c = getopt_long (argc, argv, "Vvh46ft:H:O:p:i:u:l:C:n:s:", long_options,
207                                                                          &option_index);
209                 if (c == -1 || c == EOF)
210                         break;
212                 switch (c) {
213                 case '?':                                                                       /* help */
214                         print_usage ();
215                         exit (STATE_UNKNOWN);
216                 case 'V':                                                                       /* version */
217                         print_revision (progname, "$Revision$");
218                         exit (STATE_OK);
219                 case 'h':                                                                       /* help */
220                         print_help (progname);
221                         exit (STATE_OK);
222                 case 'v':                                                                       /* help */
223                         verbose = TRUE;
224                         break;
225                 case 't':                                                                       /* timeout period */
226                         if (!is_integer (optarg))
227                                 usage2 ("timeout interval must be an integer", optarg);
228                         timeout_interval = atoi (optarg);
229                         break;
230                 case 'H':                                                                       /* host */
231                         if (!is_host (optarg))
232                                 usage2 ("invalid host name", optarg);
233                         hostname = optarg;
234                         break;
235                 case 'p': /* port number */
236                         if (!is_integer (optarg))
237                                 usage2 ("port must be an integer", optarg);
238                         asprintf (&comm,"%s -p %s", comm, optarg);
239                         break;
240                 case 'O':                                                                       /* output file */
241                         outputfile = optarg;
242                         passive = TRUE;
243                         break;
244                 case 's':                                                                       /* description of service to check */
245                         service = realloc (service, (++services) * sizeof(char *));
246                         p1 = optarg;
247                         while (p2 = index (p1, ':')) {
248                                 *p2 = '\0';
249                                 asprintf (&service[services-1], "%s", p1);
250                                 service = realloc (service, (++services) * sizeof(char *));
251                                 p1 = p2 + 1;
252                         }
253                         asprintf (&service[services-1], "%s", p1);
254                         break;
255                 case 'n':                                                                       /* short name of host in nagios configuration */
256                         host_shortname = optarg;
257                         break;
258                 case 'u':
259                         c = 'l';
260                 case 'l':                                                                       /* login name */
261                 case 'i':                                                                       /* identity */
262                         asprintf (&comm, "%s -%c %s", comm, c, optarg);
263                         break;
264                 case '4':                                                                       /* Pass these switches directly to ssh */
265                 case '6':                                                               /* -4 for IPv4, -6 for IPv6 */
266                 case 'f':                                                                       /* fork to background */
267                         asprintf (&comm, "%s -%c", comm, c);
268                         break;
269                 case 'C':                                                                       /* Command for remote machine */
270                         commands++;
271                         if (commands > 1)
272                                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
273                         asprintf (&remotecmd, "%s%s", remotecmd, optarg);
274                 }
275         }
277         c = optind;
278         if (hostname == NULL) {
279                 if (c <= argc) {
280                         terminate (STATE_UNKNOWN, "%s: You must provide a host name\n", progname);
281                 } else if (!is_host (argv[c]))
282                         terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", progname, argv[c]);
283                 hostname = argv[c++];
284         }
286         if (strlen(remotecmd) == 0) {
287                 for (; c < argc; c++)
288                         if (strlen(remotecmd) > 0)
289                                 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
290                         else
291                                 asprintf (&remotecmd, "%s", argv[c]);
292         }
294         if (commands > 1)
295                 remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;");
297         if (remotecmd == NULL || strlen (remotecmd) <= 1)
298                 usage ("No remotecmd\n");
300         asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
302         return validate_arguments ();
309 int
310 validate_arguments (void)
312         if (remotecmd == NULL || hostname == NULL)
313                 return ERROR;
315         if (passive && commands != services)
316                 terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide a service name for each command.\n", progname);
318         if (passive && host_shortname == NULL)
319                 terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide the host short name from the nagios configs.\n", progname);
321         return OK;
328 void
329 print_help (const char *cmd)
331         print_revision (cmd, "$Revision$");
333         printf
334                 ("Copyright (c) 1999    Karl DeBisschop (kdebisschop@alum.mit.edu)\n\n"
335                  "This plugin will execute a command on a remote host using SSH\n\n");
337         print_usage ();
339         printf
340                 ("\nOptions:\n"
341                  "-H, --hostname=HOST\n"
342                  "   name or IP address of remote host\n"
343                  "-C, --command='COMMAND STRING'\n"
344                  "   command to execute on the remote machine\n"
345                  "-f tells ssh to fork rather than create a tty\n"
346                  "-t, --timeout=INTEGER\n"
347                  "   specify timeout (default: %d seconds) [optional]\n"
348                  "-p, --port=PORT\n"
349                  "   port to connect to on remote system [optional]\n"
350          "-l, --logname=USERNAME\n"
351                  "   SSH user name on remote host [optional]\n"
352                  "-i, --identity=KEYFILE\n"
353                  "   identity of an authorized key [optional]\n"
354                  "-O, --output=FILE\n"
355                  "   external command file for nagios [optional]\n"
356                  "-s, --services=LIST\n"
357                  "   list of nagios service names, separated by ':' [optional]\n"
358                  "-n, --name=NAME\n"
359                  "   short name of host in nagios configuration [optional]\n"
360                  "-4, --use-ipv4\n"
361                  "   tell ssh to use IPv4\n"
362                  "-6, --use-ipv6\n"
363                  "   tell ssh to use IPv6\n"
364                  "\n"
365                  "The most common mode of use is to refer to a local identity file with\n"
366                  "the '-i' option. In this mode, the identity pair should have a null\n"
367                  "passphrase and the public key should be listed in the authorized_keys\n"
368                  "file of the remote host. Usually the key will be restricted to running\n"
369                  "only one command on the remote server. If the remote SSH server tracks\n"
370                  "invocation agruments, the one remote program may be an agent that can\n"
371                  "execute additional commands as proxy\n"
372                  "\n"
373                  "To use passive mode, provide multiple '-C' options, and provide\n"
374                  "all of -O, -s, and -n options (servicelist order must match '-C'\n"
375                  "options)\n", DEFAULT_SOCKET_TIMEOUT);
382 void
383 print_usage (void)
385         printf
386                 ("Usage:\n"
387                  "check_by_ssh [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>\n"
388                  "             [-n name] [-s servicelist] [-O outputfile] [-p port]\n"
389                  "check_by_ssh  -V prints version info\n"
390                  "check_by_ssh  -h prints more detailed help\n");