Revert "Trying out a patch for IRIX 11"
[monitoring-plugins.git] / plugins / check_by_ssh.c
blobd0dbf34a140e289342465d80dcf6f955568790a7
1 /*****************************************************************************
2 *
3 * Nagios check_by_ssh plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2008 Nagios Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_by_ssh plugin
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *****************************************************************************/
29 const char *progname = "check_by_ssh";
30 const char *copyright = "2000-2008";
31 const char *email = "nagiosplug-devel@lists.sourceforge.net";
33 #include "common.h"
34 #include "netutils.h"
35 #include "utils.h"
36 #include "runcmd.h"
38 int process_arguments (int, char **);
39 int validate_arguments (void);
40 void print_help (void);
41 void print_usage (void);
43 unsigned int commands = 0;
44 unsigned int services = 0;
45 int skip_stdout = 0;
46 int skip_stderr = 0;
47 char *remotecmd = NULL;
48 char *comm = NULL;
49 char *hostname = NULL;
50 char *outputfile = NULL;
51 char *host_shortname = NULL;
52 char **service;
53 int passive = FALSE;
54 int verbose = FALSE;
56 int
57 main (int argc, char **argv)
60 char *status_text;
61 int cresult;
62 int result = STATE_UNKNOWN;
63 int i;
64 time_t local_time;
65 FILE *fp = NULL;
66 struct output chld_out, chld_err;
68 remotecmd = "";
69 comm = strdup (SSH_COMMAND);
71 setlocale (LC_ALL, "");
72 bindtextdomain (PACKAGE, LOCALEDIR);
73 textdomain (PACKAGE);
75 /* Parse extra opts if any */
76 argv=np_extra_opts (&argc, argv, progname);
78 /* process arguments */
79 if (process_arguments (argc, argv) == ERROR)
80 usage_va(_("Could not parse arguments"));
82 /* Set signal handling and alarm timeout */
83 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
84 usage_va(_("Cannot catch SIGALRM"));
86 alarm (timeout_interval);
88 /* run the command */
89 if (verbose)
90 printf ("%s\n", comm);
92 result = np_runcmd(comm, &chld_out, &chld_err, 0);
94 if (skip_stdout == -1) /* --skip-stdout specified without argument */
95 skip_stdout = chld_out.lines;
96 if (skip_stderr == -1) /* --skip-stderr specified without argument */
97 skip_stderr = chld_err.lines;
99 /* UNKNOWN or worse if (non-skipped) output found on stderr */
100 if(chld_err.lines > skip_stderr) {
101 printf (_("Remote command execution failed: %s\n"),
102 chld_err.line[skip_stderr]);
103 return max_state_alt(result, STATE_UNKNOWN);
106 /* this is simple if we're not supposed to be passive.
107 * Wrap up quickly and keep the tricks below */
108 if(!passive) {
109 if (chld_out.lines > skip_stdout)
110 for (i = skip_stdout; i < chld_out.lines; i++)
111 puts (chld_out.line[i]);
112 else
113 printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
114 state_text(result), remotecmd, result);
115 return result; /* return error status from remote command */
120 * Passive mode
123 /* process output */
124 if (!(fp = fopen (outputfile, "a"))) {
125 printf (_("SSH WARNING: could not open %s\n"), outputfile);
126 exit (STATE_UNKNOWN);
129 local_time = time (NULL);
130 commands = 0;
131 for(i = skip_stdout; i < chld_out.lines; i++) {
132 status_text = chld_out.line[i++];
133 if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL)
134 die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
136 if (service[commands] && status_text
137 && sscanf (chld_out.line[i], "STATUS CODE: %d", &cresult) == 1)
139 fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
140 (int) local_time, host_shortname, service[commands++],
141 cresult, status_text);
145 /* Multiple commands and passive checking should always return OK */
146 return result;
149 /* process command-line arguments */
151 process_arguments (int argc, char **argv)
153 int c;
154 char *p1, *p2;
156 int option = 0;
157 static struct option longopts[] = {
158 {"version", no_argument, 0, 'V'},
159 {"help", no_argument, 0, 'h'},
160 {"verbose", no_argument, 0, 'v'},
161 {"fork", no_argument, 0, 'f'},
162 {"timeout", required_argument, 0, 't'},
163 {"host", required_argument, 0, 'H'},
164 {"port", required_argument,0,'p'},
165 {"output", required_argument, 0, 'O'},
166 {"name", required_argument, 0, 'n'},
167 {"services", required_argument, 0, 's'},
168 {"identity", required_argument, 0, 'i'},
169 {"user", required_argument, 0, 'u'},
170 {"logname", required_argument, 0, 'l'},
171 {"command", required_argument, 0, 'C'},
172 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
173 {"skip-stdout", optional_argument, 0, 'S'},
174 {"skip-stderr", optional_argument, 0, 'E'},
175 {"proto1", no_argument, 0, '1'},
176 {"proto2", no_argument, 0, '2'},
177 {"use-ipv4", no_argument, 0, '4'},
178 {"use-ipv6", no_argument, 0, '6'},
179 {"ssh-option", required_argument, 0, 'o'},
180 {"quiet", no_argument, 0, 'q'},
181 {0, 0, 0, 0}
184 if (argc < 2)
185 return ERROR;
187 for (c = 1; c < argc; c++)
188 if (strcmp ("-to", argv[c]) == 0)
189 strcpy (argv[c], "-t");
191 while (1) {
192 c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts,
193 &option);
195 if (c == -1 || c == EOF)
196 break;
198 switch (c) {
199 case 'V': /* version */
200 print_revision (progname, NP_VERSION);
201 exit (STATE_OK);
202 case 'h': /* help */
203 print_help ();
204 exit (STATE_OK);
205 case 'v': /* help */
206 verbose = TRUE;
207 break;
208 case 't': /* timeout period */
209 if (!is_integer (optarg))
210 usage_va(_("Timeout interval must be a positive integer"));
211 else
212 timeout_interval = atoi (optarg);
213 break;
214 case 'H': /* host */
215 host_or_die(optarg);
216 hostname = optarg;
217 break;
218 case 'p': /* port number */
219 if (!is_integer (optarg))
220 usage_va(_("Port must be a positive integer"));
221 asprintf (&comm,"%s -p %s", comm, optarg);
222 break;
223 case 'O': /* output file */
224 outputfile = optarg;
225 passive = TRUE;
226 break;
227 case 's': /* description of service to check */
228 p1 = optarg;
229 service = realloc (service, (++services) * sizeof(char *));
230 while ((p2 = index (p1, ':'))) {
231 *p2 = '\0';
232 service[services - 1] = p1;
233 service = realloc (service, (++services) * sizeof(char *));
234 p1 = p2 + 1;
236 service[services - 1] = p1;
237 break;
238 case 'n': /* short name of host in nagios configuration */
239 host_shortname = optarg;
240 break;
242 case 'u':
243 c = 'l';
244 case 'l': /* login name */
245 case 'i': /* identity */
246 asprintf (&comm, "%s -%c %s", comm, c, optarg);
247 break;
249 case '1': /* Pass these switches directly to ssh */
250 case '2': /* 1 to force version 1, 2 to force version 2 */
251 case '4': /* -4 for IPv4 */
252 case '6': /* -6 for IPv6 */
253 case 'f': /* fork to background */
254 asprintf (&comm, "%s -%c", comm, c);
255 break;
256 case 'C': /* Command for remote machine */
257 commands++;
258 if (commands > 1)
259 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
260 asprintf (&remotecmd, "%s%s", remotecmd, optarg);
261 break;
262 case 'S': /* skip n (or all) lines on stdout */
263 if (optarg == NULL)
264 skip_stdout = -1; /* skip all output on stdout */
265 else if (!is_integer (optarg))
266 usage_va(_("skip-stdout argument must be an integer"));
267 else
268 skip_stdout = atoi (optarg);
269 break;
270 case 'E': /* skip n (or all) lines on stderr */
271 if (optarg == NULL)
272 skip_stderr = -1; /* skip all output on stderr */
273 else if (!is_integer (optarg))
274 usage_va(_("skip-stderr argument must be an integer"));
275 else
276 skip_stderr = atoi (optarg);
277 break;
278 case 'o': /* Extra options for the ssh command */
279 asprintf (&comm, "%s -%c '%s'", comm, c, optarg);
280 break;
281 case 'q': /* Tell the ssh command to be quiet */
282 asprintf (&comm, "%s -%c", comm, c);
283 break;
284 default: /* help */
285 usage5();
289 c = optind;
290 if (hostname == NULL) {
291 if (c <= argc) {
292 die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
294 host_or_die(argv[c]);
295 hostname = argv[c++];
298 if (strlen(remotecmd) == 0) {
299 for (; c < argc; c++)
300 if (strlen(remotecmd) > 0)
301 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
302 else
303 asprintf (&remotecmd, "%s", argv[c]);
306 if (commands > 1 || passive)
307 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
309 if (remotecmd == NULL || strlen (remotecmd) <= 1)
310 usage_va(_("No remotecmd"));
312 asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
314 return validate_arguments ();
320 validate_arguments (void)
322 if (remotecmd == NULL || hostname == NULL)
323 return ERROR;
325 if (passive && commands != services)
326 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
328 if (passive && host_shortname == NULL)
329 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
331 return OK;
335 void
336 print_help (void)
338 print_revision (progname, NP_VERSION);
340 printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
341 printf (COPYRIGHT, copyright, email);
343 printf (_("This plugin uses SSH to execute commands on a remote host"));
345 printf ("\n\n");
347 print_usage ();
349 printf (_(UT_HELP_VRSN));
351 printf (_(UT_EXTRA_OPTS));
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]. This will always return OK if ssh is executed"));
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));
386 printf("\n");
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("\n");
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)"));
397 printf ("\n");
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 #ifdef NP_EXTRA_OPTS
406 printf("\n");
407 printf("%s\n", _("Notes:"));
408 printf(_(UT_EXTRA_OPTS_NOTES));
409 #endif
411 printf(_(UT_SUPPORT));
416 void
417 print_usage (void)
419 printf (_("Usage:"));
420 printf (" %s -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]\n"
421 " [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n"
422 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
423 " [-p port] [-o ssh-option]\n",
424 progname);