Set the pointers to the SSL and SSL_CTX objects back to NULL after
[monitoring-plugins.git] / plugins / check_by_ssh.c
blob6855ebc06eb35a8f29064d638a9d89ee2ecc1ac8
1 /******************************************************************************
3 * Nagios check_by_ssh plugin
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
8 * Last Modified: $Date$
10 * Description:
12 * This file contains the check_by_ssh plugin
14 * License Information:
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.
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.
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.
30 * $Id$
32 ******************************************************************************/
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 int skip_stdout = 0;
52 int skip_stderr = 0;
53 char *remotecmd = NULL;
54 char *comm = NULL;
55 char *hostname = NULL;
56 char *outputfile = NULL;
57 char *host_shortname = NULL;
58 char **service;
59 int passive = FALSE;
60 int verbose = FALSE;
62 int
63 main (int argc, char **argv)
66 char *status_text;
67 int cresult;
68 int result = STATE_UNKNOWN;
69 int i;
70 time_t local_time;
71 FILE *fp = NULL;
72 struct output chld_out, chld_err;
74 remotecmd = "";
75 comm = strdup (SSH_COMMAND);
77 setlocale (LC_ALL, "");
78 bindtextdomain (PACKAGE, LOCALEDIR);
79 textdomain (PACKAGE);
81 /* process arguments */
82 if (process_arguments (argc, argv) == ERROR)
83 usage_va(_("Could not parse arguments"));
85 /* Set signal handling and alarm timeout */
86 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
87 usage_va(_("Cannot catch SIGALRM"));
89 alarm (timeout_interval);
91 /* run the command */
92 if (verbose)
93 printf ("%s\n", comm);
95 result = np_runcmd(comm, &chld_out, &chld_err, 0);
97 if (skip_stdout == -1) /* --skip-stdout specified without argument */
98 skip_stdout = chld_out.lines;
99 if (skip_stderr == -1) /* --skip-stderr specified without argument */
100 skip_stderr = chld_err.lines;
102 /* UNKNOWN if (non-skipped) output found on stderr */
103 if(chld_err.lines > skip_stderr) {
104 printf (_("Remote command execution failed: %s\n"),
105 chld_err.line[skip_stderr]);
106 return STATE_UNKNOWN;
109 /* this is simple if we're not supposed to be passive.
110 * Wrap up quickly and keep the tricks below */
111 if(!passive) {
112 if (chld_out.lines > skip_stdout)
113 puts (chld_out.line[skip_stdout]);
114 else
115 printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
116 state_text(result), remotecmd, result);
117 return result; /* return error status from remote command */
122 * Passive mode
125 /* process output */
126 if (!(fp = fopen (outputfile, "a"))) {
127 printf (_("SSH WARNING: could not open %s\n"), outputfile);
128 exit (STATE_UNKNOWN);
131 local_time = time (NULL);
132 commands = 0;
133 for(i = skip_stdout; i < chld_out.lines; i++) {
134 status_text = strstr (chld_out.line[i], "STATUS CODE: ");
135 if (status_text == NULL) {
136 printf ("%s", chld_out.line[i]);
137 return result;
139 if (service[commands] && status_text
140 && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1)
142 fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
143 (int) local_time, host_shortname, service[commands++],
144 cresult, chld_out.line[i]);
148 /* force an OK state */
149 return result;
152 /* process command-line arguments */
154 process_arguments (int argc, char **argv)
156 int c;
157 char *p1, *p2;
159 int option = 0;
160 static struct option longopts[] = {
161 {"version", no_argument, 0, 'V'},
162 {"help", no_argument, 0, 'h'},
163 {"verbose", no_argument, 0, 'v'},
164 {"fork", no_argument, 0, 'f'},
165 {"timeout", required_argument, 0, 't'},
166 {"host", required_argument, 0, 'H'},
167 {"port", required_argument,0,'p'},
168 {"output", required_argument, 0, 'O'},
169 {"name", required_argument, 0, 'n'},
170 {"services", required_argument, 0, 's'},
171 {"identity", required_argument, 0, 'i'},
172 {"user", required_argument, 0, 'u'},
173 {"logname", required_argument, 0, 'l'},
174 {"command", required_argument, 0, 'C'},
175 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
176 {"skip-stdout", optional_argument, 0, 'S'},
177 {"skip-stderr", optional_argument, 0, 'E'},
178 {"proto1", no_argument, 0, '1'},
179 {"proto2", no_argument, 0, '2'},
180 {"use-ipv4", no_argument, 0, '4'},
181 {"use-ipv6", no_argument, 0, '6'},
182 {"ssh-option", required_argument, 0, 'o'},
183 {"quiet", no_argument, 0, 'q'},
184 {0, 0, 0, 0}
187 if (argc < 2)
188 return ERROR;
190 for (c = 1; c < argc; c++)
191 if (strcmp ("-to", argv[c]) == 0)
192 strcpy (argv[c], "-t");
194 while (1) {
195 c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts,
196 &option);
198 if (c == -1 || c == EOF)
199 break;
201 switch (c) {
202 case 'V': /* version */
203 print_revision (progname, revision);
204 exit (STATE_OK);
205 case 'h': /* help */
206 print_help ();
207 exit (STATE_OK);
208 case 'v': /* help */
209 verbose = TRUE;
210 break;
211 case 't': /* timeout period */
212 if (!is_integer (optarg))
213 usage_va(_("Timeout interval must be a positive integer"));
214 else
215 timeout_interval = atoi (optarg);
216 break;
217 case 'H': /* host */
218 host_or_die(optarg);
219 hostname = optarg;
220 break;
221 case 'p': /* port number */
222 if (!is_integer (optarg))
223 usage_va(_("Port must be a positive integer"));
224 asprintf (&comm,"%s -p %s", comm, optarg);
225 break;
226 case 'O': /* output file */
227 outputfile = optarg;
228 passive = TRUE;
229 break;
230 case 's': /* description of service to check */
231 p1 = optarg;
232 service = realloc (service, (++services) * sizeof(char *));
233 while ((p2 = index (p1, ':'))) {
234 *p2 = '\0';
235 service[services - 1] = p1;
236 service = realloc (service, (++services) * sizeof(char *));
237 p1 = p2 + 1;
239 service[services - 1] = p1;
240 break;
241 case 'n': /* short name of host in nagios configuration */
242 host_shortname = optarg;
243 break;
245 case 'u':
246 c = 'l';
247 case 'l': /* login name */
248 case 'i': /* identity */
249 asprintf (&comm, "%s -%c %s", comm, c, optarg);
250 break;
252 case '1': /* Pass these switches directly to ssh */
253 case '2': /* 1 to force version 1, 2 to force version 2 */
254 case '4': /* -4 for IPv4 */
255 case '6': /* -6 for IPv6 */
256 case 'f': /* fork to background */
257 asprintf (&comm, "%s -%c", comm, c);
258 break;
259 case 'C': /* Command for remote machine */
260 commands++;
261 if (commands > 1)
262 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
263 asprintf (&remotecmd, "%s%s", remotecmd, optarg);
264 break;
265 case 'S': /* skip n (or all) lines on stdout */
266 if (optarg == NULL)
267 skip_stdout = -1; /* skip all output on stdout */
268 else if (!is_integer (optarg))
269 usage_va(_("skip-stdout argument must be an integer"));
270 else
271 skip_stdout = atoi (optarg);
272 break;
273 case 'E': /* skip n (or all) lines on stderr */
274 if (optarg == NULL)
275 skip_stderr = -1; /* skip all output on stderr */
276 else if (!is_integer (optarg))
277 usage_va(_("skip-stderr argument must be an integer"));
278 else
279 skip_stderr = atoi (optarg);
280 break;
281 case 'o': /* Extra options for the ssh command */
282 asprintf (&comm, "%s -%c '%s'", comm, c, optarg);
283 break;
284 case 'q': /* Tell the ssh command to be quiet */
285 asprintf (&comm, "%s -%c", comm, c);
286 break;
287 default: /* help */
288 usage5();
292 c = optind;
293 if (hostname == NULL) {
294 if (c <= argc) {
295 die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
297 host_or_die(argv[c]);
298 hostname = argv[c++];
301 if (strlen(remotecmd) == 0) {
302 for (; c < argc; c++)
303 if (strlen(remotecmd) > 0)
304 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
305 else
306 asprintf (&remotecmd, "%s", argv[c]);
309 if (commands > 1)
310 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
312 if (remotecmd == NULL || strlen (remotecmd) <= 1)
313 usage_va(_("No remotecmd"));
315 asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
317 return validate_arguments ();
323 validate_arguments (void)
325 if (remotecmd == NULL || hostname == NULL)
326 return ERROR;
328 if (passive && commands != services)
329 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
331 if (passive && host_shortname == NULL)
332 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
334 return OK;
338 void
339 print_help (void)
341 print_revision (progname, revision);
343 printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
344 printf (COPYRIGHT, copyright, email);
346 printf (_("This plugin uses SSH to execute commands on a remote host"));
348 printf ("\n\n");
350 print_usage ();
352 printf (_(UT_HELP_VRSN));
354 printf (_(UT_HOST_PORT), 'p', "none");
356 printf (_(UT_IPv46));
358 printf (" %s\n", "-1, --proto1");
359 printf (" %s\n", _("tell ssh to use Protocol 1 [optional]"));
360 printf (" %s\n", "-2, --proto2");
361 printf (" %s\n", _("tell ssh to use Protocol 2 [optional]"));
362 printf (" %s\n", "-S, --skip-stdout[=n]");
363 printf (" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
364 printf (" %s\n", "-E, --skip-stderr[=n]");
365 printf (" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
366 printf (" %s\n", "-f");
367 printf (" %s\n", _("tells ssh to fork rather than create a tty [optional]"));
368 printf (" %s\n","-C, --command='COMMAND STRING'");
369 printf (" %s\n", _("command to execute on the remote machine"));
370 printf (" %s\n","-l, --logname=USERNAME");
371 printf (" %s\n", _("SSH user name on remote host [optional]"));
372 printf (" %s\n","-i, --identity=KEYFILE");
373 printf (" %s\n", _("identity of an authorized key [optional]"));
374 printf (" %s\n","-O, --output=FILE");
375 printf (" %s\n", _("external command file for nagios [optional]"));
376 printf (" %s\n","-s, --services=LIST");
377 printf (" %s\n", _("list of nagios service names, separated by ':' [optional]"));
378 printf (" %s\n","-n, --name=NAME");
379 printf (" %s\n", _("short name of host in nagios configuration [optional]"));
380 printf (" %s\n","-o, --ssh-option=OPTION");
381 printf (" %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
382 printf (" %s\n","-q, --quiet");
383 printf (" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
384 printf (_(UT_WARN_CRIT));
385 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
386 printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
387 printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
388 printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
389 printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
390 printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
391 printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
392 printf (" %s\n", _("execute additional commands as proxy"));
393 printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
394 printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
395 printf ("\n");
396 printf ("%s\n", _("Examples:"));
397 printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
398 printf (" %s\n", "$ cat /tmp/foo");
399 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
400 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
401 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
402 printf (_(UT_SUPPORT));
407 void
408 print_usage (void)
410 printf (_("Usage:"));
411 printf (" %s -H <host> -C <command> [-fq] [-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);