Changed default test values for check_dns (using nagios.com)
[monitoring-plugins.git] / plugins / check_by_ssh.c
blob7306e743da5cb162a9179713b6f6c6eff64eafc5
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 for (i = skip_stdout; i < chld_out.lines; i++)
114 puts (chld_out.line[i]);
115 else
116 printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
117 state_text(result), remotecmd, result);
118 return result; /* return error status from remote command */
123 * Passive mode
126 /* process output */
127 if (!(fp = fopen (outputfile, "a"))) {
128 printf (_("SSH WARNING: could not open %s\n"), outputfile);
129 exit (STATE_UNKNOWN);
132 local_time = time (NULL);
133 commands = 0;
134 for(i = skip_stdout; i < chld_out.lines; i++) {
135 status_text = strstr (chld_out.line[i], "STATUS CODE: ");
136 if (status_text == NULL) {
137 printf ("%s", chld_out.line[i]);
138 return result;
140 if (service[commands] && status_text
141 && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1)
143 fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
144 (int) local_time, host_shortname, service[commands++],
145 cresult, chld_out.line[i]);
149 /* force an OK state */
150 return result;
153 /* process command-line arguments */
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}
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;
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();
293 c = optind;
294 if (hostname == NULL) {
295 if (c <= argc) {
296 die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
298 host_or_die(argv[c]);
299 hostname = argv[c++];
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]);
310 if (commands > 1)
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 ();
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_HOST_PORT), 'p', "none");
357 printf (_(UT_IPv46));
359 printf (" %s\n", "-1, --proto1");
360 printf (" %s\n", _("tell ssh to use Protocol 1 [optional]"));
361 printf (" %s\n", "-2, --proto2");
362 printf (" %s\n", _("tell ssh to use Protocol 2 [optional]"));
363 printf (" %s\n", "-S, --skip-stdout[=n]");
364 printf (" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
365 printf (" %s\n", "-E, --skip-stderr[=n]");
366 printf (" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
367 printf (" %s\n", "-f");
368 printf (" %s\n", _("tells ssh to fork rather than create a tty [optional]"));
369 printf (" %s\n","-C, --command='COMMAND STRING'");
370 printf (" %s\n", _("command to execute on the remote machine"));
371 printf (" %s\n","-l, --logname=USERNAME");
372 printf (" %s\n", _("SSH user name on remote host [optional]"));
373 printf (" %s\n","-i, --identity=KEYFILE");
374 printf (" %s\n", _("identity of an authorized key [optional]"));
375 printf (" %s\n","-O, --output=FILE");
376 printf (" %s\n", _("external command file for nagios [optional]"));
377 printf (" %s\n","-s, --services=LIST");
378 printf (" %s\n", _("list of nagios service names, separated by ':' [optional]"));
379 printf (" %s\n","-n, --name=NAME");
380 printf (" %s\n", _("short name of host in nagios configuration [optional]"));
381 printf (" %s\n","-o, --ssh-option=OPTION");
382 printf (" %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
383 printf (" %s\n","-q, --quiet");
384 printf (" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
385 printf (_(UT_WARN_CRIT));
386 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
387 printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
388 printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
389 printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
390 printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
391 printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
392 printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
393 printf (" %s\n", _("execute additional commands as proxy"));
394 printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
395 printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
396 printf ("\n");
397 printf ("%s\n", _("Examples:"));
398 printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
399 printf (" %s\n", "$ cat /tmp/foo");
400 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
401 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
402 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
403 printf (_(UT_VERBOSE));
404 printf (_(UT_SUPPORT));
409 void
410 print_usage (void)
412 printf (_("Usage:"));
413 printf (" %s -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]\n"
414 " [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n"
415 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
416 " [-p port] [-o ssh-option]\n",
417 progname);