Code

The "-e" option now accepts a comma-delimited list of expected status
[nagiosplug.git] / plugins / check_by_ssh.c
1 /*****************************************************************************
2
3 * Nagios check_by_ssh plugin
4
5 * License: GPL
6 * Copyright (c) 2000-2008 Nagios Plugins Development Team
7
8 * Last Modified: $Date$
9
10 * Description:
11
12 * This file contains the check_by_ssh plugin
13
14
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24
25 * You should have received a copy of the GNU General Public License
26 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28 * $Id$
29
30 *****************************************************************************/
32 const char *progname = "check_by_ssh";
33 const char *revision = "$Revision$";
34 const char *copyright = "2000-2008";
35 const char *email = "nagiosplug-devel@lists.sourceforge.net";
37 #include "common.h"
38 #include "netutils.h"
39 #include "utils.h"
40 #include "runcmd.h"
42 int process_arguments (int, char **);
43 int validate_arguments (void);
44 void print_help (void);
45 void print_usage (void);
47 unsigned int commands = 0;
48 unsigned int services = 0;
49 int skip_stdout = 0;
50 int skip_stderr = 0;
51 char *remotecmd = NULL;
52 char *comm = NULL;
53 char *hostname = NULL;
54 char *outputfile = NULL;
55 char *host_shortname = NULL;
56 char **service;
57 int passive = FALSE;
58 int verbose = FALSE;
60 int
61 main (int argc, char **argv)
62 {
64         char *status_text;
65         int cresult;
66         int result = STATE_UNKNOWN;
67         int i;
68         time_t local_time;
69         FILE *fp = NULL;
70         struct output chld_out, chld_err;
72         remotecmd = "";
73         comm = strdup (SSH_COMMAND);
75         setlocale (LC_ALL, "");
76         bindtextdomain (PACKAGE, LOCALEDIR);
77         textdomain (PACKAGE);
79         /* Parse extra opts if any */
80         argv=np_extra_opts (&argc, argv, progname);
82         /* process arguments */
83         if (process_arguments (argc, argv) == ERROR)
84                 usage_va(_("Could not parse arguments"));
86         /* Set signal handling and alarm timeout */
87         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
88                 usage_va(_("Cannot catch SIGALRM"));
89         }
90         alarm (timeout_interval);
92         /* run the command */
93         if (verbose)
94                 printf ("%s\n", comm);
96         result = np_runcmd(comm, &chld_out, &chld_err, 0);
98         if (skip_stdout == -1) /* --skip-stdout specified without argument */
99                 skip_stdout = chld_out.lines;
100         if (skip_stderr == -1) /* --skip-stderr specified without argument */
101                 skip_stderr = chld_err.lines;
103         /* UNKNOWN or worse if (non-skipped) output found on stderr */
104         if(chld_err.lines > skip_stderr) {
105                 printf (_("Remote command execution failed: %s\n"),
106                         chld_err.line[skip_stderr]);
107                 return max_state_alt(result, STATE_UNKNOWN);
108         }
110         /* this is simple if we're not supposed to be passive.
111          * Wrap up quickly and keep the tricks below */
112         if(!passive) {
113                 if (chld_out.lines > skip_stdout)
114                         for (i = skip_stdout; i < chld_out.lines; i++)
115                                 puts (chld_out.line[i]);
116                 else
117                         printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
118                                 state_text(result), remotecmd, result);
119                 return result;  /* return error status from remote command */
120         }
123         /*
124          * Passive mode
125          */
127         /* process output */
128         if (!(fp = fopen (outputfile, "a"))) {
129                 printf (_("SSH WARNING: could not open %s\n"), outputfile);
130                 exit (STATE_UNKNOWN);
131         }
133         local_time = time (NULL);
134         commands = 0;
135         for(i = skip_stdout; i < chld_out.lines; i++) {
136                 status_text = chld_out.line[i++];
137                 if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL)
138                         die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
140                 if (service[commands] && status_text
141                         && sscanf (chld_out.line[i], "STATUS CODE: %d", &cresult) == 1)
142                 {
143                         fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
144                                  (int) local_time, host_shortname, service[commands++],
145                                  cresult, status_text);
146                 }
147         }
148         
149         /* Multiple commands and passive checking should always return OK */
150         return result;
153 /* process command-line arguments */
154 int
155 process_arguments (int argc, char **argv)
157         int c;
158         char *p1, *p2;
160         int option = 0;
161         static struct option longopts[] = {
162                 {"version", no_argument, 0, 'V'},
163                 {"help", no_argument, 0, 'h'},
164                 {"verbose", no_argument, 0, 'v'},
165                 {"fork", no_argument, 0, 'f'},
166                 {"timeout", required_argument, 0, 't'},
167                 {"host", required_argument, 0, 'H'},
168                 {"port", required_argument,0,'p'},
169                 {"output", required_argument, 0, 'O'},
170                 {"name", required_argument, 0, 'n'},
171                 {"services", required_argument, 0, 's'},
172                 {"identity", required_argument, 0, 'i'},
173                 {"user", required_argument, 0, 'u'},
174                 {"logname", required_argument, 0, 'l'},
175                 {"command", required_argument, 0, 'C'},
176                 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
177                 {"skip-stdout", optional_argument, 0, 'S'},
178                 {"skip-stderr", optional_argument, 0, 'E'},
179                 {"proto1", no_argument, 0, '1'},
180                 {"proto2", no_argument, 0, '2'},
181                 {"use-ipv4", no_argument, 0, '4'},
182                 {"use-ipv6", no_argument, 0, '6'},
183                 {"ssh-option", required_argument, 0, 'o'},
184                 {"quiet", no_argument, 0, 'q'},
185                 {0, 0, 0, 0}
186         };
188         if (argc < 2)
189                 return ERROR;
191         for (c = 1; c < argc; c++)
192                 if (strcmp ("-to", argv[c]) == 0)
193                         strcpy (argv[c], "-t");
195         while (1) {
196                 c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts,
197                                  &option);
199                 if (c == -1 || c == EOF)
200                         break;
202                 switch (c) {
203                 case 'V':                                                                       /* version */
204                         print_revision (progname, revision);
205                         exit (STATE_OK);
206                 case 'h':                                                                       /* help */
207                         print_help ();
208                         exit (STATE_OK);
209                 case 'v':                                                                       /* help */
210                         verbose = TRUE;
211                         break;
212                 case 't':                                                                       /* timeout period */
213                         if (!is_integer (optarg))
214                                 usage_va(_("Timeout interval must be a positive integer"));
215                         else
216                                 timeout_interval = atoi (optarg);
217                         break;
218                 case 'H':                                                                       /* host */
219                         host_or_die(optarg);
220                         hostname = optarg;
221                         break;
222                 case 'p': /* port number */
223                         if (!is_integer (optarg))
224                                 usage_va(_("Port must be a positive integer"));
225                         asprintf (&comm,"%s -p %s", comm, optarg);
226                         break;
227                 case 'O':                                                                       /* output file */
228                         outputfile = optarg;
229                         passive = TRUE;
230                         break;
231                 case 's':                                                                       /* description of service to check */
232                         p1 = optarg;
233                         service = realloc (service, (++services) * sizeof(char *));
234                         while ((p2 = index (p1, ':'))) {
235                                 *p2 = '\0';
236                                 service[services - 1] = p1;
237                                 service = realloc (service, (++services) * sizeof(char *));
238                                 p1 = p2 + 1;
239                         }
240                         service[services - 1] = p1;
241                         break;
242                 case 'n':                                                                       /* short name of host in nagios configuration */
243                         host_shortname = optarg;
244                         break;
246                 case 'u':
247                         c = 'l';
248                 case 'l':                                                                       /* login name */
249                 case 'i':                                                                       /* identity */
250                         asprintf (&comm, "%s -%c %s", comm, c, optarg);
251                         break;
253                 case '1':                                                                       /* Pass these switches directly to ssh */
254                 case '2':                                                                       /* 1 to force version 1, 2 to force version 2 */
255                 case '4':                                                                       /* -4 for IPv4 */
256                 case '6':                                                               /* -6 for IPv6 */
257                 case 'f':                                                                       /* fork to background */
258                         asprintf (&comm, "%s -%c", comm, c);
259                         break;
260                 case 'C':                                                                       /* Command for remote machine */
261                         commands++;
262                         if (commands > 1)
263                                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
264                         asprintf (&remotecmd, "%s%s", remotecmd, optarg);
265                         break;
266                 case 'S':                                                                       /* skip n (or all) lines on stdout */
267                         if (optarg == NULL)
268                                 skip_stdout = -1; /* skip all output on stdout */
269                         else if (!is_integer (optarg))
270                                 usage_va(_("skip-stdout argument must be an integer"));
271                         else
272                                 skip_stdout = atoi (optarg);
273                         break;
274                 case 'E':                                                                       /* skip n (or all) lines on stderr */
275                         if (optarg == NULL)
276                                 skip_stderr = -1; /* skip all output on stderr */
277                         else if (!is_integer (optarg))
278                                 usage_va(_("skip-stderr argument must be an integer"));
279                         else
280                                 skip_stderr = atoi (optarg);
281                         break;
282                 case 'o':                                                                       /* Extra options for the ssh command */
283                         asprintf (&comm, "%s -%c '%s'", comm, c, optarg);
284                         break;
285                 case 'q':                                                                       /* Tell the ssh command to be quiet */
286                         asprintf (&comm, "%s -%c", comm, c);
287                         break;
288                 default:                                                                        /* help */
289                         usage5();
290                 }
291         }
293         c = optind;
294         if (hostname == NULL) {
295                 if (c <= argc) {
296                         die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
297                 }
298                 host_or_die(argv[c]);
299                 hostname = argv[c++];
300         }
302         if (strlen(remotecmd) == 0) {
303                 for (; c < argc; c++)
304                         if (strlen(remotecmd) > 0)
305                                 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
306                         else
307                                 asprintf (&remotecmd, "%s", argv[c]);
308         }
310         if (commands > 1 || passive)
311                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
313         if (remotecmd == NULL || strlen (remotecmd) <= 1)
314                 usage_va(_("No remotecmd"));
316         asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
318         return validate_arguments ();
323 int
324 validate_arguments (void)
326         if (remotecmd == NULL || hostname == NULL)
327                 return ERROR;
329         if (passive && commands != services)
330                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
332         if (passive && host_shortname == NULL)
333                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
335         return OK;
339 void
340 print_help (void)
342         print_revision (progname, revision);
344         printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
345         printf (COPYRIGHT, copyright, email);
347         printf (_("This plugin uses SSH to execute commands on a remote host"));
349   printf ("\n\n");
351         print_usage ();
353         printf (_(UT_HELP_VRSN));
355         printf (_(UT_EXTRA_OPTS));
357         printf (_(UT_HOST_PORT), 'p', "none");
359         printf (_(UT_IPv46));
361   printf (" %s\n", "-1, --proto1");
362   printf ("    %s\n", _("tell ssh to use Protocol 1 [optional]"));
363   printf (" %s\n", "-2, --proto2");
364   printf ("    %s\n", _("tell ssh to use Protocol 2 [optional]"));
365   printf (" %s\n", "-S, --skip-stdout[=n]");
366   printf ("    %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
367   printf (" %s\n", "-E, --skip-stderr[=n]");
368   printf ("    %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
369   printf (" %s\n", "-f");
370   printf ("    %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed"));
371   printf (" %s\n","-C, --command='COMMAND STRING'");
372   printf ("    %s\n", _("command to execute on the remote machine"));
373   printf (" %s\n","-l, --logname=USERNAME");
374   printf ("    %s\n", _("SSH user name on remote host [optional]"));
375   printf (" %s\n","-i, --identity=KEYFILE");
376   printf ("    %s\n", _("identity of an authorized key [optional]"));
377   printf (" %s\n","-O, --output=FILE");
378   printf ("    %s\n", _("external command file for nagios [optional]"));
379   printf (" %s\n","-s, --services=LIST");
380   printf ("    %s\n", _("list of nagios service names, separated by ':' [optional]"));
381   printf (" %s\n","-n, --name=NAME");
382   printf ("    %s\n", _("short name of host in nagios configuration [optional]"));
383   printf (" %s\n","-o, --ssh-option=OPTION");
384   printf ("    %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
385   printf (" %s\n","-q, --quiet");
386   printf ("    %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
387         printf (_(UT_WARN_CRIT));
388         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
389         printf (_(UT_VERBOSE));
390         printf("\n");
391   printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
392   printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
393   printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
394   printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
395   printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
396   printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
397   printf (" %s\n", _("execute additional commands as proxy"));
398   printf("\n");
399   printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
400   printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
401   printf ("\n");
402   printf ("%s\n", _("Examples:"));
403   printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
404   printf (" %s\n", "$ cat /tmp/foo");
405   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
406   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
407   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
409 #ifdef NP_EXTRA_OPTS
410         printf("\n");
411         printf("%s\n", _("Notes:"));
412         printf(_(UT_EXTRA_OPTS_NOTES));
413 #endif
415         printf(_(UT_SUPPORT));
420 void
421 print_usage (void)
423         printf (_("Usage:"));
424         printf (" %s -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]\n"
425                 "       [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n"
426                 "       [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
427                 "       [-p port] [-o ssh-option]\n",
428                 progname);