Code

Typo
[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 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 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 = strstr (chld_out.line[i], "STATUS CODE: ");
137                 if (status_text == NULL) {
138                         printf ("%s", chld_out.line[i]);
139                         return result;
140                 }
141                 if (service[commands] && status_text
142                         && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1)
143                 {
144                         fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
145                                  (int) local_time, host_shortname, service[commands++],
146                                  cresult, chld_out.line[i]);
147                 }
148         }
149         
150         /* force an OK state */
151         return result;
154 /* process command-line arguments */
155 int
156 process_arguments (int argc, char **argv)
158         int c;
159         char *p1, *p2;
161         int option = 0;
162         static struct option longopts[] = {
163                 {"version", no_argument, 0, 'V'},
164                 {"help", no_argument, 0, 'h'},
165                 {"verbose", no_argument, 0, 'v'},
166                 {"fork", no_argument, 0, 'f'},
167                 {"timeout", required_argument, 0, 't'},
168                 {"host", required_argument, 0, 'H'},
169                 {"port", required_argument,0,'p'},
170                 {"output", required_argument, 0, 'O'},
171                 {"name", required_argument, 0, 'n'},
172                 {"services", required_argument, 0, 's'},
173                 {"identity", required_argument, 0, 'i'},
174                 {"user", required_argument, 0, 'u'},
175                 {"logname", required_argument, 0, 'l'},
176                 {"command", required_argument, 0, 'C'},
177                 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
178                 {"skip-stdout", optional_argument, 0, 'S'},
179                 {"skip-stderr", optional_argument, 0, 'E'},
180                 {"proto1", no_argument, 0, '1'},
181                 {"proto2", no_argument, 0, '2'},
182                 {"use-ipv4", no_argument, 0, '4'},
183                 {"use-ipv6", no_argument, 0, '6'},
184                 {"ssh-option", required_argument, 0, 'o'},
185                 {"quiet", no_argument, 0, 'q'},
186                 {0, 0, 0, 0}
187         };
189         if (argc < 2)
190                 return ERROR;
192         for (c = 1; c < argc; c++)
193                 if (strcmp ("-to", argv[c]) == 0)
194                         strcpy (argv[c], "-t");
196         while (1) {
197                 c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts,
198                                  &option);
200                 if (c == -1 || c == EOF)
201                         break;
203                 switch (c) {
204                 case 'V':                                                                       /* version */
205                         print_revision (progname, revision);
206                         exit (STATE_OK);
207                 case 'h':                                                                       /* help */
208                         print_help ();
209                         exit (STATE_OK);
210                 case 'v':                                                                       /* help */
211                         verbose = TRUE;
212                         break;
213                 case 't':                                                                       /* timeout period */
214                         if (!is_integer (optarg))
215                                 usage_va(_("Timeout interval must be a positive integer"));
216                         else
217                                 timeout_interval = atoi (optarg);
218                         break;
219                 case 'H':                                                                       /* host */
220                         host_or_die(optarg);
221                         hostname = optarg;
222                         break;
223                 case 'p': /* port number */
224                         if (!is_integer (optarg))
225                                 usage_va(_("Port must be a positive integer"));
226                         asprintf (&comm,"%s -p %s", comm, optarg);
227                         break;
228                 case 'O':                                                                       /* output file */
229                         outputfile = optarg;
230                         passive = TRUE;
231                         break;
232                 case 's':                                                                       /* description of service to check */
233                         p1 = optarg;
234                         service = realloc (service, (++services) * sizeof(char *));
235                         while ((p2 = index (p1, ':'))) {
236                                 *p2 = '\0';
237                                 service[services - 1] = p1;
238                                 service = realloc (service, (++services) * sizeof(char *));
239                                 p1 = p2 + 1;
240                         }
241                         service[services - 1] = p1;
242                         break;
243                 case 'n':                                                                       /* short name of host in nagios configuration */
244                         host_shortname = optarg;
245                         break;
247                 case 'u':
248                         c = 'l';
249                 case 'l':                                                                       /* login name */
250                 case 'i':                                                                       /* identity */
251                         asprintf (&comm, "%s -%c %s", comm, c, optarg);
252                         break;
254                 case '1':                                                                       /* Pass these switches directly to ssh */
255                 case '2':                                                                       /* 1 to force version 1, 2 to force version 2 */
256                 case '4':                                                                       /* -4 for IPv4 */
257                 case '6':                                                               /* -6 for IPv6 */
258                 case 'f':                                                                       /* fork to background */
259                         asprintf (&comm, "%s -%c", comm, c);
260                         break;
261                 case 'C':                                                                       /* Command for remote machine */
262                         commands++;
263                         if (commands > 1)
264                                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
265                         asprintf (&remotecmd, "%s%s", remotecmd, optarg);
266                         break;
267                 case 'S':                                                                       /* skip n (or all) lines on stdout */
268                         if (optarg == NULL)
269                                 skip_stdout = -1; /* skip all output on stdout */
270                         else if (!is_integer (optarg))
271                                 usage_va(_("skip-stdout argument must be an integer"));
272                         else
273                                 skip_stdout = atoi (optarg);
274                         break;
275                 case 'E':                                                                       /* skip n (or all) lines on stderr */
276                         if (optarg == NULL)
277                                 skip_stderr = -1; /* skip all output on stderr */
278                         else if (!is_integer (optarg))
279                                 usage_va(_("skip-stderr argument must be an integer"));
280                         else
281                                 skip_stderr = atoi (optarg);
282                         break;
283                 case 'o':                                                                       /* Extra options for the ssh command */
284                         asprintf (&comm, "%s -%c '%s'", comm, c, optarg);
285                         break;
286                 case 'q':                                                                       /* Tell the ssh command to be quiet */
287                         asprintf (&comm, "%s -%c", comm, c);
288                         break;
289                 default:                                                                        /* help */
290                         usage5();
291                 }
292         }
294         c = optind;
295         if (hostname == NULL) {
296                 if (c <= argc) {
297                         die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
298                 }
299                 host_or_die(argv[c]);
300                 hostname = argv[c++];
301         }
303         if (strlen(remotecmd) == 0) {
304                 for (; c < argc; c++)
305                         if (strlen(remotecmd) > 0)
306                                 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
307                         else
308                                 asprintf (&remotecmd, "%s", argv[c]);
309         }
311         if (commands > 1)
312                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
314         if (remotecmd == NULL || strlen (remotecmd) <= 1)
315                 usage_va(_("No remotecmd"));
317         asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
319         return validate_arguments ();
324 int
325 validate_arguments (void)
327         if (remotecmd == NULL || hostname == NULL)
328                 return ERROR;
330         if (passive && commands != services)
331                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
333         if (passive && host_shortname == NULL)
334                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
336         return OK;
340 void
341 print_help (void)
343         print_revision (progname, revision);
345         printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
346         printf (COPYRIGHT, copyright, email);
348         printf (_("This plugin uses SSH to execute commands on a remote host"));
350   printf ("\n\n");
352         print_usage ();
354         printf (_(UT_HELP_VRSN));
356         printf (_(UT_EXTRA_OPTS));
358         printf (_(UT_HOST_PORT), 'p', "none");
360         printf (_(UT_IPv46));
362   printf (" %s\n", "-1, --proto1");
363   printf ("    %s\n", _("tell ssh to use Protocol 1 [optional]"));
364   printf (" %s\n", "-2, --proto2");
365   printf ("    %s\n", _("tell ssh to use Protocol 2 [optional]"));
366   printf (" %s\n", "-S, --skip-stdout[=n]");
367   printf ("    %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
368   printf (" %s\n", "-E, --skip-stderr[=n]");
369   printf ("    %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
370   printf (" %s\n", "-f");
371   printf ("    %s\n", _("tells ssh to fork rather than create a tty [optional]"));
372   printf (" %s\n","-C, --command='COMMAND STRING'");
373   printf ("    %s\n", _("command to execute on the remote machine"));
374   printf (" %s\n","-l, --logname=USERNAME");
375   printf ("    %s\n", _("SSH user name on remote host [optional]"));
376   printf (" %s\n","-i, --identity=KEYFILE");
377   printf ("    %s\n", _("identity of an authorized key [optional]"));
378   printf (" %s\n","-O, --output=FILE");
379   printf ("    %s\n", _("external command file for nagios [optional]"));
380   printf (" %s\n","-s, --services=LIST");
381   printf ("    %s\n", _("list of nagios service names, separated by ':' [optional]"));
382   printf (" %s\n","-n, --name=NAME");
383   printf ("    %s\n", _("short name of host in nagios configuration [optional]"));
384   printf (" %s\n","-o, --ssh-option=OPTION");
385   printf ("    %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
386   printf (" %s\n","-q, --quiet");
387   printf ("    %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
388         printf (_(UT_WARN_CRIT));
389         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
390         printf (_(UT_VERBOSE));
391         printf("\n");
392   printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
393   printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
394   printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
395   printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
396   printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
397   printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
398   printf (" %s\n", _("execute additional commands as proxy"));
399   printf("\n");
400   printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
401   printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
402   printf ("\n");
403   printf ("%s\n", _("Examples:"));
404   printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
405   printf (" %s\n", "$ cat /tmp/foo");
406   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
407   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
408   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
410 #ifdef NP_EXTRA_OPTS
411         printf("\n");
412         printf("%s\n", _("Notes:"));
413         printf(_(UT_EXTRA_OPTS_NOTES));
414 #endif
416         printf(_(UT_SUPPORT));
421 void
422 print_usage (void)
424         printf (_("Usage:"));
425         printf (" %s -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]\n"
426                 "       [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n"
427                 "       [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
428                 "       [-p port] [-o ssh-option]\n",
429                 progname);