1 /******************************************************************************
3 * Nagios check_by_ssh plugin
6 * Copyright (c) 1999-2006 nagios-plugins team
8 * Last Modified: $Date$
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.
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";
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;
53 char *remotecmd
= NULL
;
55 char *hostname
= NULL
;
56 char *outputfile
= NULL
;
57 char *host_shortname
= NULL
;
63 main (int argc
, char **argv
)
68 int result
= STATE_UNKNOWN
;
72 struct output chld_out
, chld_err
;
75 comm
= strdup (SSH_COMMAND
);
77 setlocale (LC_ALL
, "");
78 bindtextdomain (PACKAGE
, LOCALEDIR
);
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
);
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 */
112 if (chld_out
.lines
> skip_stdout
)
113 puts (chld_out
.line
[skip_stdout
]);
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 */
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
);
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
]);
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 */
152 /* process command-line arguments */
154 process_arguments (int argc
, char **argv
)
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'},
190 for (c
= 1; c
< argc
; c
++)
191 if (strcmp ("-to", argv
[c
]) == 0)
192 strcpy (argv
[c
], "-t");
195 c
= getopt_long (argc
, argv
, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts
,
198 if (c
== -1 || c
== EOF
)
202 case 'V': /* version */
203 print_revision (progname
, revision
);
211 case 't': /* timeout period */
212 if (!is_integer (optarg
))
213 usage_va(_("Timeout interval must be a positive integer"));
215 timeout_interval
= atoi (optarg
);
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
);
226 case 'O': /* output file */
230 case 's': /* description of service to check */
232 service
= realloc (service
, (++services
) * sizeof(char *));
233 while ((p2
= index (p1
, ':'))) {
235 service
[services
- 1] = p1
;
236 service
= realloc (service
, (++services
) * sizeof(char *));
239 service
[services
- 1] = p1
;
241 case 'n': /* short name of host in nagios configuration */
242 host_shortname
= optarg
;
247 case 'l': /* login name */
248 case 'i': /* identity */
249 asprintf (&comm
, "%s -%c %s", comm
, c
, optarg
);
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
);
259 case 'C': /* Command for remote machine */
262 asprintf (&remotecmd
, "%s;echo STATUS CODE: $?;", remotecmd
);
263 asprintf (&remotecmd
, "%s%s", remotecmd
, optarg
);
265 case 'S': /* skip n (or all) lines on stdout */
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"));
271 skip_stdout
= atoi (optarg
);
273 case 'E': /* skip n (or all) lines on stderr */
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"));
279 skip_stderr
= atoi (optarg
);
281 case 'o': /* Extra options for the ssh command */
282 asprintf (&comm
, "%s -%c '%s'", comm
, c
, optarg
);
284 case 'q': /* Tell the ssh command to be quiet */
285 asprintf (&comm
, "%s -%c", comm
, c
);
293 if (hostname
== NULL
) {
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
]);
306 asprintf (&remotecmd
, "%s", argv
[c
]);
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
)
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
);
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"));
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)"));
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
));
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",