1 /*****************************************************************************
3 * Nagios check_by_ssh plugin
6 * Copyright (c) 2000-2008 Nagios Plugins Development Team
8 * Last Modified: $Date$
12 * This file contains the check_by_ssh plugin
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.
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.
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/>.
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";
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;
51 char *remotecmd
= NULL
;
53 char *hostname
= NULL
;
54 char *outputfile
= NULL
;
55 char *host_shortname
= NULL
;
61 main (int argc
, char **argv
)
66 int result
= STATE_UNKNOWN
;
70 struct output chld_out
, chld_err
;
73 comm
= strdup (SSH_COMMAND
);
75 setlocale (LC_ALL
, "");
76 bindtextdomain (PACKAGE
, LOCALEDIR
);
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"));
87 alarm (timeout_interval
);
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
;
107 /* this is simple if we're not supposed to be passive.
108 * Wrap up quickly and keep the tricks below */
110 if (chld_out
.lines
> skip_stdout
)
111 for (i
= skip_stdout
; i
< chld_out
.lines
; i
++)
112 puts (chld_out
.line
[i
]);
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 */
125 if (!(fp
= fopen (outputfile
, "a"))) {
126 printf (_("SSH WARNING: could not open %s\n"), outputfile
);
127 exit (STATE_UNKNOWN
);
130 local_time
= time (NULL
);
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
]);
138 if (service
[commands
] && status_text
139 && sscanf (status_text
, "STATUS CODE: %d", &cresult
) == 1)
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
]);
147 /* force an OK state */
151 /* process command-line arguments */
153 process_arguments (int argc
, char **argv
)
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'},
189 for (c
= 1; c
< argc
; c
++)
190 if (strcmp ("-to", argv
[c
]) == 0)
191 strcpy (argv
[c
], "-t");
194 c
= getopt_long (argc
, argv
, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts
,
197 if (c
== -1 || c
== EOF
)
201 case 'V': /* version */
202 print_revision (progname
, revision
);
210 case 't': /* timeout period */
211 if (!is_integer (optarg
))
212 usage_va(_("Timeout interval must be a positive integer"));
214 timeout_interval
= atoi (optarg
);
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
);
225 case 'O': /* output file */
229 case 's': /* description of service to check */
231 service
= realloc (service
, (++services
) * sizeof(char *));
232 while ((p2
= index (p1
, ':'))) {
234 service
[services
- 1] = p1
;
235 service
= realloc (service
, (++services
) * sizeof(char *));
238 service
[services
- 1] = p1
;
240 case 'n': /* short name of host in nagios configuration */
241 host_shortname
= optarg
;
246 case 'l': /* login name */
247 case 'i': /* identity */
248 asprintf (&comm
, "%s -%c %s", comm
, c
, optarg
);
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
);
258 case 'C': /* Command for remote machine */
261 asprintf (&remotecmd
, "%s;echo STATUS CODE: $?;", remotecmd
);
262 asprintf (&remotecmd
, "%s%s", remotecmd
, optarg
);
264 case 'S': /* skip n (or all) lines on stdout */
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"));
270 skip_stdout
= atoi (optarg
);
272 case 'E': /* skip n (or all) lines on stderr */
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"));
278 skip_stderr
= atoi (optarg
);
280 case 'o': /* Extra options for the ssh command */
281 asprintf (&comm
, "%s -%c '%s'", comm
, c
, optarg
);
283 case 'q': /* Tell the ssh command to be quiet */
284 asprintf (&comm
, "%s -%c", comm
, c
);
292 if (hostname
== NULL
) {
294 die (STATE_UNKNOWN
, _("%s: You must provide a host name\n"), progname
);
296 host_or_die(argv
[c
]);
297 hostname
= argv
[c
++];
300 if (strlen(remotecmd
) == 0) {
301 for (; c
< argc
; c
++)
302 if (strlen(remotecmd
) > 0)
303 asprintf (&remotecmd
, "%s %s", remotecmd
, argv
[c
]);
305 asprintf (&remotecmd
, "%s", argv
[c
]);
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 ();
322 validate_arguments (void)
324 if (remotecmd
== NULL
|| hostname
== NULL
)
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
);
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"));
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 (_(UT_VERBOSE
));
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"));
395 printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
396 printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
398 printf ("%s\n", _("Examples:"));
399 printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
400 printf (" %s\n", "$ cat /tmp/foo");
401 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
402 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
403 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
405 printf(_(UT_SUPPORT
));
413 printf (_("Usage:"));
414 printf (" %s -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]\n"
415 " [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n"
416 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
417 " [-p port] [-o ssh-option]\n",