Code

65eee3c56a448d0a37f84b60b9bf8714265e3bcd
[nagiosplug.git] / plugins / check_by_ssh.c
1 /******************************************************************************
2 *
3 * Nagios check_by_ssh plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_by_ssh plugin
13 *
14 * License Information:
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 * $Id$
31
32 ******************************************************************************/
33  
34 const char *progname = "check_by_ssh";
35 const char *revision = "$Revision$";
36 const char *copyright = "2000-2006";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
42 #include "runcmd.h"
44 int process_arguments (int, char **);
45 int validate_arguments (void);
46 void print_help (void);
47 void print_usage (void);
49 unsigned int commands = 0;
50 unsigned int services = 0;
51 unsigned int skip = 0;
52 char *remotecmd = NULL;
53 char *comm = NULL;
54 char *hostname = NULL;
55 char *outputfile = NULL;
56 char *host_shortname = NULL;
57 char **service;
58 int passive = FALSE;
59 int verbose = FALSE;
61 int
62 main (int argc, char **argv)
63 {
65         char *status_text;
66         int cresult;
67         int result = STATE_UNKNOWN;
68         int i;
69         time_t local_time;
70         FILE *fp = NULL;
71         struct output chld_out, chld_err;
73         remotecmd = "";
74         comm = strdup (SSH_COMMAND);
76         setlocale (LC_ALL, "");
77         bindtextdomain (PACKAGE, LOCALEDIR);
78         textdomain (PACKAGE);
80         /* process arguments */
81         if (process_arguments (argc, argv) == ERROR)
82                 usage_va(_("Could not parse arguments"));
84         /* Set signal handling and alarm timeout */
85         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
86                 usage_va(_("Cannot catch SIGALRM"));
87         }
88         alarm (timeout_interval);
90         /* run the command */
91         if (verbose)
92                 printf ("%s\n", comm);
94         result = np_runcmd(comm, &chld_out, &chld_err, 0);
95         /* UNKNOWN if (non-skipped) output found on stderr */
96         if((signed)chld_err.lines - (signed)skip > 0) {
97                 printf(_("Remote command execution failed: %s\n"),
98                        skip < chld_err.lines ? chld_err.line[skip] : chld_err.buf);
99                 return STATE_UNKNOWN;
100         }
101         skip -= chld_err.lines;
103         /* this is simple if we're not supposed to be passive.
104          * Wrap up quickly and keep the tricks below */
105         if(!passive) {
106                 printf ("%s\n", skip < chld_out.lines ? chld_out.line[skip] : chld_out.buf);
107                 return result;  /* return error status from remote command */
108         }
111         /*
112          * Passive mode
113          */
115         /* process output */
116         if (!(fp = fopen (outputfile, "a"))) {
117                 printf (_("SSH WARNING: could not open %s\n"), outputfile);
118                 exit (STATE_UNKNOWN);
119         }
121         local_time = time (NULL);
122         commands = 0;
123         for(i = skip; chld_out.line[i]; i++) {
124                 status_text = strstr (chld_out.line[i], "STATUS CODE: ");
125                 if (status_text == NULL) {
126                         printf ("%s", chld_out.line[i]);
127                         return result;
128                 }
129                 if (service[commands] && status_text
130                         && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1)
131                 {
132                         fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
133                                  (int) local_time, host_shortname, service[commands++],
134                                  cresult, chld_out.line[i]);
135                 }
136         }
137         
138         /* force an OK state */
139         return result;
142 /* process command-line arguments */
143 int
144 process_arguments (int argc, char **argv)
146         int c;
147         char *p1, *p2;
149         int option = 0;
150         static struct option longopts[] = {
151                 {"version", no_argument, 0, 'V'},
152                 {"help", no_argument, 0, 'h'},
153                 {"verbose", no_argument, 0, 'v'},
154                 {"fork", no_argument, 0, 'f'},
155                 {"timeout", required_argument, 0, 't'},
156                 {"host", required_argument, 0, 'H'},
157                 {"port", required_argument,0,'p'},
158                 {"output", required_argument, 0, 'O'},
159                 {"name", required_argument, 0, 'n'},
160                 {"services", required_argument, 0, 's'},
161                 {"identity", required_argument, 0, 'i'},
162                 {"user", required_argument, 0, 'u'},
163                 {"logname", required_argument, 0, 'l'},
164                 {"command", required_argument, 0, 'C'},
165                 {"skip", required_argument, 0, 'S'},
166                 {"proto1", no_argument, 0, '1'},
167                 {"proto2", no_argument, 0, '2'},
168                 {"use-ipv4", no_argument, 0, '4'},
169                 {"use-ipv6", no_argument, 0, '6'},
170                 {"ssh-option", required_argument, 0, 'o'},
171                 {"quiet", no_argument, 0, 'q'},
172                 {0, 0, 0, 0}
173         };
175         if (argc < 2)
176                 return ERROR;
178         for (c = 1; c < argc; c++)
179                 if (strcmp ("-to", argv[c]) == 0)
180                         strcpy (argv[c], "-t");
182         while (1) {
183                 c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S:n:s:o:", longopts,
184                                  &option);
186                 if (c == -1 || c == EOF)
187                         break;
189                 switch (c) {
190                 case 'V':                                                                       /* version */
191                         print_revision (progname, revision);
192                         exit (STATE_OK);
193                 case 'h':                                                                       /* help */
194                         print_help ();
195                         exit (STATE_OK);
196                 case 'v':                                                                       /* help */
197                         verbose = TRUE;
198                         break;
199                 case 't':                                                                       /* timeout period */
200                         if (!is_integer (optarg))
201                                 usage_va(_("Timeout interval must be a positive integer"));
202                         else
203                                 timeout_interval = atoi (optarg);
204                         break;
205                 case 'H':                                                                       /* host */
206                         host_or_die(optarg);
207                         hostname = optarg;
208                         break;
209                 case 'p': /* port number */
210                         if (!is_integer (optarg))
211                                 usage_va(_("Port must be a positive integer"));
212                         asprintf (&comm,"%s -p %s", comm, optarg);
213                         break;
214                 case 'O':                                                                       /* output file */
215                         outputfile = optarg;
216                         passive = TRUE;
217                         break;
218                 case 's':                                                                       /* description of service to check */
219                         p1 = optarg;
220                         service = realloc (service, (++services) * sizeof(char *));
221                         while ((p2 = index (p1, ':'))) {
222                                 *p2 = '\0';
223                                 service[services - 1] = p1;
224                                 service = realloc (service, (++services) * sizeof(char *));
225                                 p1 = p2 + 1;
226                         }
227                         service[services - 1] = p1;
228                         break;
229                 case 'n':                                                                       /* short name of host in nagios configuration */
230                         host_shortname = optarg;
231                         break;
233                 case 'u':
234                         c = 'l';
235                 case 'l':                                                                       /* login name */
236                 case 'i':                                                                       /* identity */
237                         asprintf (&comm, "%s -%c %s", comm, c, optarg);
238                         break;
240                 case '1':                                                                       /* Pass these switches directly to ssh */
241                 case '2':                                                                       /* 1 to force version 1, 2 to force version 2 */
242                 case '4':                                                                       /* -4 for IPv4 */
243                 case '6':                                                               /* -6 for IPv6 */
244                 case 'f':                                                                       /* fork to background */
245                         asprintf (&comm, "%s -%c", comm, c);
246                         break;
247                 case 'C':                                                                       /* Command for remote machine */
248                         commands++;
249                         if (commands > 1)
250                                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
251                         asprintf (&remotecmd, "%s%s", remotecmd, optarg);
252                         break;
253                 case 'S':                                                                       /* Skip n lines in the output to ignore system banner */
254                         if (!is_integer (optarg))
255                                 usage_va(_("skip lines must be an integer"));
256                         else
257                                 skip = atoi (optarg);
258                         break;
259                 case 'o':                                                                       /* Extra options for the ssh command */
260                         asprintf (&comm, "%s -%c '%s'", comm, c, optarg);
261                         break;
262                 case 'q':                                                                       /* Tell the ssh command to be quiet */
263                         asprintf (&comm, "%s -%c", comm, c);
264                         break;
265                 default:                                                                        /* help */
266                         usage5();
267                 }
268         }
270         c = optind;
271         if (hostname == NULL) {
272                 if (c <= argc) {
273                         die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
274                 }
275                 host_or_die(argv[c]);
276                 hostname = argv[c++];
277         }
279         if (strlen(remotecmd) == 0) {
280                 for (; c < argc; c++)
281                         if (strlen(remotecmd) > 0)
282                                 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
283                         else
284                                 asprintf (&remotecmd, "%s", argv[c]);
285         }
287         if (commands > 1)
288                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
290         if (remotecmd == NULL || strlen (remotecmd) <= 1)
291                 usage_va(_("No remotecmd"));
293         asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
295         return validate_arguments ();
300 int
301 validate_arguments (void)
303         if (remotecmd == NULL || hostname == NULL)
304                 return ERROR;
306         if (passive && commands != services)
307                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
309         if (passive && host_shortname == NULL)
310                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
312         return OK;
316 void
317 print_help (void)
319         print_revision (progname, revision);
321         printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
322         printf (COPYRIGHT, copyright, email);
324         printf (_("This plugin uses SSH to execute commands on a remote host"));
326   printf ("\n\n");
327   
328         print_usage ();
330         printf (_(UT_HELP_VRSN));
332         printf (_(UT_HOST_PORT), 'p', "none");
334         printf (_(UT_IPv46));
336   printf (" %s\n", "-1, --proto1");
337   printf ("    %s\n", _("tell ssh to use Protocol 1"));
338   printf (" %s\n", "-2, --proto2");
339   printf ("    %s\n", _("tell ssh to use Protocol 2"));
340   printf (" %s\n", "-S, --skip=n");
341   printf ("    %s\n", _("Ignore first n lines on STDERR (to suppress a logon banner)"));
342   printf (" %s\n", "-f");
343   printf ("    %s\n", _("tells ssh to fork rather than create a tty"));
344   printf (" %s\n","-C, --command='COMMAND STRING'");
345   printf ("    %s\n", _("command to execute on the remote machine"));
346   printf (" %s\n","-l, --logname=USERNAME");
347   printf ("    %s\n", _("SSH user name on remote host [optional]"));
348   printf (" %s\n","-i, --identity=KEYFILE");
349   printf ("    %s\n", _("identity of an authorized key [optional]"));
350   printf (" %s\n","-O, --output=FILE");
351   printf ("    %s\n", _("external command file for nagios [optional]"));
352   printf (" %s\n","-s, --services=LIST");
353   printf ("    %s\n", _("list of nagios service names, separated by ':' [optional]"));
354   printf (" %s\n","-n, --name=NAME");
355   printf ("    %s\n", _("short name of host in nagios configuration [optional]"));
356   printf (" %s\n","-o, --ssh-option=OPTION");
357   printf ("    %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
358   printf (" %s\n","-q, --quiet");
359   printf ("    %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
360         printf (_(UT_WARN_CRIT));
361         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
362   printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
363   printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
364   printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
365   printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
366   printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
367   printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
368   printf (" %s\n", _("execute additional commands as proxy"));
369   printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
370   printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
371   printf ("\n");
372   printf ("%s\n", _("Examples:"));
373   printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
374   printf (" %s\n", "$ cat /tmp/foo");
375   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
376   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
377   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
378         printf (_(UT_SUPPORT));
383 void
384 print_usage (void)
386         printf (_("Usage:"));
387         printf (" %s -H <host> -C <command> [-fq] [-1|-2] [-4|-6]\n"
388                 "       [-S lines] [-t timeout] [-i identity] [-l user] [-n name]\n"
389                 "       [-s servicelist] [-O outputfile] [-p port] [-o ssh-option]\n",
390                 progname);