Code

Fix translations when extra-opts aren't enabled
[nagiosplug.git] / plugins / check_by_ssh.c
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
11
12
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.
17
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.
22
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/>.
25
26
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 "utils.h"
35 #include "netutils.h"
36 #include "utils_cmd.h"
38 #ifndef NP_MAXARGS
39 #define NP_MAXARGS 1024
40 #endif
42 int process_arguments (int, char **);
43 int validate_arguments (void);
44 void comm_append (const char *);
45 void print_help (void);
46 void print_usage (void);
48 unsigned int commands = 0;
49 unsigned int services = 0;
50 int skip_stdout = 0;
51 int skip_stderr = 0;
52 char *remotecmd = NULL;
53 char **commargv = NULL;
54 int commargc = 0;
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)
64 {
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         output chld_out, chld_err;
74         remotecmd = "";
75         comm_append(SSH_COMMAND);
77         setlocale (LC_ALL, "");
78         bindtextdomain (PACKAGE, LOCALEDIR);
79         textdomain (PACKAGE);
81         /* Parse extra opts if any */
82         argv=np_extra_opts (&argc, argv, progname);
84         /* process arguments */
85         if (process_arguments (argc, argv) == ERROR)
86                 usage_va(_("Could not parse arguments"));
88         /* Set signal handling and alarm timeout */
89         if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
90                 usage_va(_("Cannot catch SIGALRM"));
91         }
92         alarm (timeout_interval);
94         /* run the command */
95         if (verbose) {
96                 printf ("Command: %s\n", commargv[0]);
97                 for (i=1; i<commargc; i++)
98                         printf ("Argument %i: %s\n", i, commargv[i]);
99         }
101         result = cmd_run_array (commargv, &chld_out, &chld_err, 0);
103         if (skip_stdout == -1) /* --skip-stdout specified without argument */
104                 skip_stdout = chld_out.lines;
105         if (skip_stderr == -1) /* --skip-stderr specified without argument */
106                 skip_stderr = chld_err.lines;
108         /* UNKNOWN or worse if (non-skipped) output found on stderr */
109         if(chld_err.lines > skip_stderr) {
110                 printf (_("Remote command execution failed: %s\n"),
111                         chld_err.line[skip_stderr]);
112                 return max_state_alt(result, STATE_UNKNOWN);
113         }
115         /* this is simple if we're not supposed to be passive.
116          * Wrap up quickly and keep the tricks below */
117         if(!passive) {
118                 if (chld_out.lines > skip_stdout)
119                         for (i = skip_stdout; i < chld_out.lines; i++)
120                                 puts (chld_out.line[i]);
121                 else
122                         printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
123                                 state_text(result), remotecmd, result);
124                 return result;  /* return error status from remote command */
125         }
128         /*
129          * Passive mode
130          */
132         /* process output */
133         if (!(fp = fopen (outputfile, "a"))) {
134                 printf (_("SSH WARNING: could not open %s\n"), outputfile);
135                 exit (STATE_UNKNOWN);
136         }
138         local_time = time (NULL);
139         commands = 0;
140         for(i = skip_stdout; i < chld_out.lines; i++) {
141                 status_text = chld_out.line[i++];
142                 if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL)
143                         die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
145                 if (service[commands] && status_text
146                         && sscanf (chld_out.line[i], "STATUS CODE: %d", &cresult) == 1)
147                 {
148                         fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
149                                  (int) local_time, host_shortname, service[commands++],
150                                  cresult, status_text);
151                 }
152         }
153         
154         /* Multiple commands and passive checking should always return OK */
155         return result;
158 /* process command-line arguments */
159 int
160 process_arguments (int argc, char **argv)
162         int c;
163         char *p1, *p2;
165         int option = 0;
166         static struct option longopts[] = {
167                 {"version", no_argument, 0, 'V'},
168                 {"help", no_argument, 0, 'h'},
169                 {"verbose", no_argument, 0, 'v'},
170                 {"fork", no_argument, 0, 'f'},
171                 {"timeout", required_argument, 0, 't'},
172                 {"host", required_argument, 0, 'H'},
173                 {"port", required_argument,0,'p'},
174                 {"output", required_argument, 0, 'O'},
175                 {"name", required_argument, 0, 'n'},
176                 {"services", required_argument, 0, 's'},
177                 {"identity", required_argument, 0, 'i'},
178                 {"user", required_argument, 0, 'u'},
179                 {"logname", required_argument, 0, 'l'},
180                 {"command", required_argument, 0, 'C'},
181                 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
182                 {"skip-stdout", optional_argument, 0, 'S'},
183                 {"skip-stderr", optional_argument, 0, 'E'},
184                 {"proto1", no_argument, 0, '1'},
185                 {"proto2", no_argument, 0, '2'},
186                 {"use-ipv4", no_argument, 0, '4'},
187                 {"use-ipv6", no_argument, 0, '6'},
188                 {"ssh-option", required_argument, 0, 'o'},
189                 {"quiet", no_argument, 0, 'q'},
190                 {0, 0, 0, 0}
191         };
193         if (argc < 2)
194                 return ERROR;
196         for (c = 1; c < argc; c++)
197                 if (strcmp ("-to", argv[c]) == 0)
198                         strcpy (argv[c], "-t");
200         while (1) {
201                 c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts,
202                                  &option);
204                 if (c == -1 || c == EOF)
205                         break;
207                 switch (c) {
208                 case 'V':                                                                       /* version */
209                         print_revision (progname, NP_VERSION);
210                         exit (STATE_OK);
211                 case 'h':                                                                       /* help */
212                         print_help ();
213                         exit (STATE_OK);
214                 case 'v':                                                                       /* help */
215                         verbose = TRUE;
216                         break;
217                 case 't':                                                                       /* timeout period */
218                         if (!is_integer (optarg))
219                                 usage_va(_("Timeout interval must be a positive integer"));
220                         else
221                                 timeout_interval = atoi (optarg);
222                         break;
223                 case 'H':                                                                       /* host */
224                         host_or_die(optarg);
225                         hostname = optarg;
226                         break;
227                 case 'p': /* port number */
228                         if (!is_integer (optarg))
229                                 usage_va(_("Port must be a positive integer"));
230                         comm_append("-p");
231                         comm_append(optarg);
232                         break;
233                 case 'O':                                                                       /* output file */
234                         outputfile = optarg;
235                         passive = TRUE;
236                         break;
237                 case 's':                                                                       /* description of service to check */
238                         p1 = optarg;
239                         service = realloc (service, (++services) * sizeof(char *));
240                         while ((p2 = index (p1, ':'))) {
241                                 *p2 = '\0';
242                                 service[services - 1] = p1;
243                                 service = realloc (service, (++services) * sizeof(char *));
244                                 p1 = p2 + 1;
245                         }
246                         service[services - 1] = p1;
247                         break;
248                 case 'n':                                                                       /* short name of host in nagios configuration */
249                         host_shortname = optarg;
250                         break;
252                 case 'u':
253                         comm_append("-l");
254                         comm_append(optarg);
255                         break;
256                 case 'l':                                                                       /* login name */
257                         comm_append("-l");
258                         comm_append(optarg);
259                         break;
260                 case 'i':                                                                       /* identity */
261                         comm_append("-i");
262                         comm_append(optarg);
263                         break;
265                 case '1':                                                                       /* Pass these switches directly to ssh */
266                         comm_append("-1");
267                         break;
268                 case '2':                                                                       /* 1 to force version 1, 2 to force version 2 */
269                         comm_append("-2");
270                         break;
271                 case '4':                                                                       /* -4 for IPv4 */
272                         comm_append("-4");
273                         break;
274                 case '6':                                                               /* -6 for IPv6 */
275                         comm_append("-6");
276                         break;
277                 case 'f':                                                                       /* fork to background */
278                         comm_append("-f");
279                         break;
280                 case 'C':                                                                       /* Command for remote machine */
281                         commands++;
282                         if (commands > 1)
283                                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
284                         asprintf (&remotecmd, "%s%s", remotecmd, optarg);
285                         break;
286                 case 'S':                                                                       /* skip n (or all) lines on stdout */
287                         if (optarg == NULL)
288                                 skip_stdout = -1; /* skip all output on stdout */
289                         else if (!is_integer (optarg))
290                                 usage_va(_("skip-stdout argument must be an integer"));
291                         else
292                                 skip_stdout = atoi (optarg);
293                         break;
294                 case 'E':                                                                       /* skip n (or all) lines on stderr */
295                         if (optarg == NULL)
296                                 skip_stderr = -1; /* skip all output on stderr */
297                         else if (!is_integer (optarg))
298                                 usage_va(_("skip-stderr argument must be an integer"));
299                         else
300                                 skip_stderr = atoi (optarg);
301                         break;
302                 case 'o':                                                                       /* Extra options for the ssh command */
303                         comm_append("-o");
304                         comm_append(optarg);
305                         break;
306                 case 'q':                                                                       /* Tell the ssh command to be quiet */
307                         comm_append("-q");
308                         break;
309                 default:                                                                        /* help */
310                         usage5();
311                 }
312         }
314         c = optind;
315         if (hostname == NULL) {
316                 if (c <= argc) {
317                         die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
318                 }
319                 host_or_die(argv[c]);
320                 hostname = argv[c++];
321         }
323         if (strlen(remotecmd) == 0) {
324                 for (; c < argc; c++)
325                         if (strlen(remotecmd) > 0)
326                                 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
327                         else
328                                 asprintf (&remotecmd, "%s", argv[c]);
329         }
331         if (commands > 1 || passive)
332                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
334         if (remotecmd == NULL || strlen (remotecmd) <= 1)
335                 usage_va(_("No remotecmd"));
337         comm_append(hostname);
338         comm_append(remotecmd);
340         return validate_arguments ();
344 void
345 comm_append (const char *str)
348         if (++commargc > NP_MAXARGS)
349                 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS);
351         if ((commargv = (char **)realloc(commargv, (commargc+1) * sizeof(char *))) == NULL)
352                 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n"));
354         commargv[commargc-1] = strdup(str);
355         commargv[commargc] = NULL;
359 int
360 validate_arguments (void)
362         if (remotecmd == NULL || hostname == NULL)
363                 return ERROR;
365         if (passive && commands != services)
366                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
368         if (passive && host_shortname == NULL)
369                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
371         return OK;
375 void
376 print_help (void)
378         print_revision (progname, NP_VERSION);
380         printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
381         printf (COPYRIGHT, copyright, email);
383         printf (_("This plugin uses SSH to execute commands on a remote host"));
385   printf ("\n\n");
387         print_usage ();
389         printf (UT_HELP_VRSN);
391         printf (UT_EXTRA_OPTS);
393         printf (UT_HOST_PORT, 'p', "none");
395         printf (UT_IPv46);
397   printf (" %s\n", "-1, --proto1");
398   printf ("    %s\n", _("tell ssh to use Protocol 1 [optional]"));
399   printf (" %s\n", "-2, --proto2");
400   printf ("    %s\n", _("tell ssh to use Protocol 2 [optional]"));
401   printf (" %s\n", "-S, --skip-stdout[=n]");
402   printf ("    %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
403   printf (" %s\n", "-E, --skip-stderr[=n]");
404   printf ("    %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
405   printf (" %s\n", "-f");
406   printf ("    %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed"));
407   printf (" %s\n","-C, --command='COMMAND STRING'");
408   printf ("    %s\n", _("command to execute on the remote machine"));
409   printf (" %s\n","-l, --logname=USERNAME");
410   printf ("    %s\n", _("SSH user name on remote host [optional]"));
411   printf (" %s\n","-i, --identity=KEYFILE");
412   printf ("    %s\n", _("identity of an authorized key [optional]"));
413   printf (" %s\n","-O, --output=FILE");
414   printf ("    %s\n", _("external command file for nagios [optional]"));
415   printf (" %s\n","-s, --services=LIST");
416   printf ("    %s\n", _("list of nagios service names, separated by ':' [optional]"));
417   printf (" %s\n","-n, --name=NAME");
418   printf ("    %s\n", _("short name of host in nagios configuration [optional]"));
419   printf (" %s\n","-o, --ssh-option=OPTION");
420   printf ("    %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
421   printf (" %s\n","-q, --quiet");
422   printf ("    %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
423         printf (UT_WARN_CRIT);
424         printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
425         printf (UT_VERBOSE);
426         printf("\n");
427   printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
428   printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
429   printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
430   printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
431   printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
432   printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
433   printf (" %s\n", _("execute additional commands as proxy"));
434   printf("\n");
435   printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
436   printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
437   printf ("\n");
438   printf ("%s\n", _("Examples:"));
439   printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
440   printf (" %s\n", "$ cat /tmp/foo");
441   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
442   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
443   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
445 #ifdef NP_EXTRA_OPTS
446         printf("\n");
447         printf("%s\n", _("Notes:"));
448         printf(UT_EXTRA_OPTS_NOTES);
449 #endif
451         printf(UT_SUPPORT);
456 void
457 print_usage (void)
459         printf (_("Usage:"));
460         printf (" %s -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]\n"
461                 "       [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n"
462                 "       [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
463                 "       [-p port] [-o ssh-option]\n",
464                 progname);