Code

Added CVE MITRE tracking number
[nagiosplug.git] / plugins / check_by_ssh.c
1 /*****************************************************************************
2
3 * Nagios check_by_ssh plugin
4
5 * License: GPL
6 * Copyright (c) 2000-2007 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 *****************************************************************************/
31  
32 const char *progname = "check_by_ssh";
33 const char *revision = "$Revision$";
34 const char *copyright = "2000-2007";
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         /* process arguments */
80         if (process_arguments (argc, argv) == ERROR)
81                 usage_va(_("Could not parse arguments"));
83         /* Set signal handling and alarm timeout */
84         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
85                 usage_va(_("Cannot catch SIGALRM"));
86         }
87         alarm (timeout_interval);
89         /* run the command */
90         if (verbose)
91                 printf ("%s\n", comm);
93         result = np_runcmd(comm, &chld_out, &chld_err, 0);
95         if (skip_stdout == -1) /* --skip-stdout specified without argument */
96                 skip_stdout = chld_out.lines;
97         if (skip_stderr == -1) /* --skip-stderr specified without argument */
98                 skip_stderr = chld_err.lines;
100         /* UNKNOWN if (non-skipped) output found on stderr */
101         if(chld_err.lines > skip_stderr) {
102                 printf (_("Remote command execution failed: %s\n"),
103                         chld_err.line[skip_stderr]);
104                 return STATE_UNKNOWN;
105         }
107         /* this is simple if we're not supposed to be passive.
108          * Wrap up quickly and keep the tricks below */
109         if(!passive) {
110                 if (chld_out.lines > skip_stdout)
111                         for (i = skip_stdout; i < chld_out.lines; i++)
112                                 puts (chld_out.line[i]);
113                 else
114                         printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
115                                 state_text(result), remotecmd, result);
116                 return result;  /* return error status from remote command */
117         }
120         /*
121          * Passive mode
122          */
124         /* process output */
125         if (!(fp = fopen (outputfile, "a"))) {
126                 printf (_("SSH WARNING: could not open %s\n"), outputfile);
127                 exit (STATE_UNKNOWN);
128         }
130         local_time = time (NULL);
131         commands = 0;
132         for(i = skip_stdout; i < chld_out.lines; i++) {
133                 status_text = strstr (chld_out.line[i], "STATUS CODE: ");
134                 if (status_text == NULL) {
135                         printf ("%s", chld_out.line[i]);
136                         return result;
137                 }
138                 if (service[commands] && status_text
139                         && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1)
140                 {
141                         fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
142                                  (int) local_time, host_shortname, service[commands++],
143                                  cresult, chld_out.line[i]);
144                 }
145         }
146         
147         /* force an OK state */
148         return result;
151 /* process command-line arguments */
152 int
153 process_arguments (int argc, char **argv)
155         int c;
156         char *p1, *p2;
158         int option = 0;
159         static struct option longopts[] = {
160                 {"version", no_argument, 0, 'V'},
161                 {"help", no_argument, 0, 'h'},
162                 {"verbose", no_argument, 0, 'v'},
163                 {"fork", no_argument, 0, 'f'},
164                 {"timeout", required_argument, 0, 't'},
165                 {"host", required_argument, 0, 'H'},
166                 {"port", required_argument,0,'p'},
167                 {"output", required_argument, 0, 'O'},
168                 {"name", required_argument, 0, 'n'},
169                 {"services", required_argument, 0, 's'},
170                 {"identity", required_argument, 0, 'i'},
171                 {"user", required_argument, 0, 'u'},
172                 {"logname", required_argument, 0, 'l'},
173                 {"command", required_argument, 0, 'C'},
174                 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
175                 {"skip-stdout", optional_argument, 0, 'S'},
176                 {"skip-stderr", optional_argument, 0, 'E'},
177                 {"proto1", no_argument, 0, '1'},
178                 {"proto2", no_argument, 0, '2'},
179                 {"use-ipv4", no_argument, 0, '4'},
180                 {"use-ipv6", no_argument, 0, '6'},
181                 {"ssh-option", required_argument, 0, 'o'},
182                 {"quiet", no_argument, 0, 'q'},
183                 {0, 0, 0, 0}
184         };
186         if (argc < 2)
187                 return ERROR;
189         for (c = 1; c < argc; c++)
190                 if (strcmp ("-to", argv[c]) == 0)
191                         strcpy (argv[c], "-t");
193         while (1) {
194                 c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts,
195                                  &option);
197                 if (c == -1 || c == EOF)
198                         break;
200                 switch (c) {
201                 case 'V':                                                                       /* version */
202                         print_revision (progname, revision);
203                         exit (STATE_OK);
204                 case 'h':                                                                       /* help */
205                         print_help ();
206                         exit (STATE_OK);
207                 case 'v':                                                                       /* help */
208                         verbose = TRUE;
209                         break;
210                 case 't':                                                                       /* timeout period */
211                         if (!is_integer (optarg))
212                                 usage_va(_("Timeout interval must be a positive integer"));
213                         else
214                                 timeout_interval = atoi (optarg);
215                         break;
216                 case 'H':                                                                       /* host */
217                         host_or_die(optarg);
218                         hostname = optarg;
219                         break;
220                 case 'p': /* port number */
221                         if (!is_integer (optarg))
222                                 usage_va(_("Port must be a positive integer"));
223                         asprintf (&comm,"%s -p %s", comm, optarg);
224                         break;
225                 case 'O':                                                                       /* output file */
226                         outputfile = optarg;
227                         passive = TRUE;
228                         break;
229                 case 's':                                                                       /* description of service to check */
230                         p1 = optarg;
231                         service = realloc (service, (++services) * sizeof(char *));
232                         while ((p2 = index (p1, ':'))) {
233                                 *p2 = '\0';
234                                 service[services - 1] = p1;
235                                 service = realloc (service, (++services) * sizeof(char *));
236                                 p1 = p2 + 1;
237                         }
238                         service[services - 1] = p1;
239                         break;
240                 case 'n':                                                                       /* short name of host in nagios configuration */
241                         host_shortname = optarg;
242                         break;
244                 case 'u':
245                         c = 'l';
246                 case 'l':                                                                       /* login name */
247                 case 'i':                                                                       /* identity */
248                         asprintf (&comm, "%s -%c %s", comm, c, optarg);
249                         break;
251                 case '1':                                                                       /* Pass these switches directly to ssh */
252                 case '2':                                                                       /* 1 to force version 1, 2 to force version 2 */
253                 case '4':                                                                       /* -4 for IPv4 */
254                 case '6':                                                               /* -6 for IPv6 */
255                 case 'f':                                                                       /* fork to background */
256                         asprintf (&comm, "%s -%c", comm, c);
257                         break;
258                 case 'C':                                                                       /* Command for remote machine */
259                         commands++;
260                         if (commands > 1)
261                                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
262                         asprintf (&remotecmd, "%s%s", remotecmd, optarg);
263                         break;
264                 case 'S':                                                                       /* skip n (or all) lines on stdout */
265                         if (optarg == NULL)
266                                 skip_stdout = -1; /* skip all output on stdout */
267                         else if (!is_integer (optarg))
268                                 usage_va(_("skip-stdout argument must be an integer"));
269                         else
270                                 skip_stdout = atoi (optarg);
271                         break;
272                 case 'E':                                                                       /* skip n (or all) lines on stderr */
273                         if (optarg == NULL)
274                                 skip_stderr = -1; /* skip all output on stderr */
275                         else if (!is_integer (optarg))
276                                 usage_va(_("skip-stderr argument must be an integer"));
277                         else
278                                 skip_stderr = atoi (optarg);
279                         break;
280                 case 'o':                                                                       /* Extra options for the ssh command */
281                         asprintf (&comm, "%s -%c '%s'", comm, c, optarg);
282                         break;
283                 case 'q':                                                                       /* Tell the ssh command to be quiet */
284                         asprintf (&comm, "%s -%c", comm, c);
285                         break;
286                 default:                                                                        /* help */
287                         usage5();
288                 }
289         }
291         c = optind;
292         if (hostname == NULL) {
293                 if (c <= argc) {
294                         die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
295                 }
296                 host_or_die(argv[c]);
297                 hostname = argv[c++];
298         }
300         if (strlen(remotecmd) == 0) {
301                 for (; c < argc; c++)
302                         if (strlen(remotecmd) > 0)
303                                 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
304                         else
305                                 asprintf (&remotecmd, "%s", argv[c]);
306         }
308         if (commands > 1)
309                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
311         if (remotecmd == NULL || strlen (remotecmd) <= 1)
312                 usage_va(_("No remotecmd"));
314         asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
316         return validate_arguments ();
321 int
322 validate_arguments (void)
324         if (remotecmd == NULL || hostname == NULL)
325                 return ERROR;
327         if (passive && commands != services)
328                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
330         if (passive && host_shortname == NULL)
331                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
333         return OK;
337 void
338 print_help (void)
340         print_revision (progname, revision);
342         printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
343         printf (COPYRIGHT, copyright, email);
345         printf (_("This plugin uses SSH to execute commands on a remote host"));
347   printf ("\n\n");
348   
349         print_usage ();
351         printf (_(UT_HELP_VRSN));
353         printf (_(UT_HOST_PORT), 'p', "none");
355         printf (_(UT_IPv46));
357   printf (" %s\n", "-1, --proto1");
358   printf ("    %s\n", _("tell ssh to use Protocol 1 [optional]"));
359   printf (" %s\n", "-2, --proto2");
360   printf ("    %s\n", _("tell ssh to use Protocol 2 [optional]"));
361   printf (" %s\n", "-S, --skip-stdout[=n]");
362   printf ("    %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
363   printf (" %s\n", "-E, --skip-stderr[=n]");
364   printf ("    %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
365   printf (" %s\n", "-f");
366   printf ("    %s\n", _("tells ssh to fork rather than create a tty [optional]"));
367   printf (" %s\n","-C, --command='COMMAND STRING'");
368   printf ("    %s\n", _("command to execute on the remote machine"));
369   printf (" %s\n","-l, --logname=USERNAME");
370   printf ("    %s\n", _("SSH user name on remote host [optional]"));
371   printf (" %s\n","-i, --identity=KEYFILE");
372   printf ("    %s\n", _("identity of an authorized key [optional]"));
373   printf (" %s\n","-O, --output=FILE");
374   printf ("    %s\n", _("external command file for nagios [optional]"));
375   printf (" %s\n","-s, --services=LIST");
376   printf ("    %s\n", _("list of nagios service names, separated by ':' [optional]"));
377   printf (" %s\n","-n, --name=NAME");
378   printf ("    %s\n", _("short name of host in nagios configuration [optional]"));
379   printf (" %s\n","-o, --ssh-option=OPTION");
380   printf ("    %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
381   printf (" %s\n","-q, --quiet");
382   printf ("    %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
383         printf (_(UT_WARN_CRIT));
384         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
385   printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
386   printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
387   printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
388   printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
389   printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
390   printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
391   printf (" %s\n", _("execute additional commands as proxy"));
392   printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
393   printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
394   printf ("\n");
395   printf ("%s\n", _("Examples:"));
396   printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
397   printf (" %s\n", "$ cat /tmp/foo");
398   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
399   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
400   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
401         printf (_(UT_VERBOSE));
402         printf (_(UT_SUPPORT));
407 void
408 print_usage (void)
410         printf (_("Usage:"));
411         printf (" %s -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]\n"
412                 "       [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n"
413                 "       [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
414                 "       [-p port] [-o ssh-option]\n",
415                 progname);