Code

first pass at cleaning localization for new release
[nagiosplug.git] / plugins / check_by_ssh.c
1 /******************************************************************************
2 *
3 * Nagios check_by_ssh plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_by_ssh plugin
13 *
14 * License Information:
15 *
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.
20 *
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.
25 *
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.
29 *
30 * $Id$
31
32 ******************************************************************************/
33  
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 int commands = 0;
50 int services = 0;
51 int skip = 0;
52 char *remotecmd = NULL;
53 char *comm = NULL;
54 char *hostname = NULL;
55 char *outputfile = NULL;
56 char *host_shortname = NULL;
57 char **service;
58 int passive = FALSE;
59 int verbose = FALSE;
61 int
62 main (int argc, char **argv)
63 {
65         char *status_text;
66         int cresult;
67         int result = STATE_UNKNOWN;
68         int i;
69         time_t local_time;
70         FILE *fp = NULL;
71         struct output chld_out, chld_err;
73         remotecmd = "";
74         comm = strdup (SSH_COMMAND);
76         setlocale (LC_ALL, "");
77         bindtextdomain (PACKAGE, LOCALEDIR);
78         textdomain (PACKAGE);
80         /* process arguments */
81         if (process_arguments (argc, argv) == ERROR)
82                 usage_va(_("Could not parse arguments"));
84         /* Set signal handling and alarm timeout */
85         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
86                 usage_va(_("Cannot catch SIGALRM"));
87         }
88         alarm (timeout_interval);
90         /* run the command */
91         if (verbose)
92                 printf ("%s\n", comm);
94         result = np_runcmd(comm, &chld_out, &chld_err, 0);
95         /* UNKNOWN if output found on stderr */
96         if(chld_err.buflen) {
97                 printf(_("Remote command execution failed: %s\n"),
98                            chld_err.buflen ? chld_err.buf : _("Unknown error"));
99                 return STATE_UNKNOWN;
100         }
102         /* this is simple if we're not supposed to be passive.
103          * Wrap up quickly and keep the tricks below */
104         if(!passive) {
105                 printf ("%s\n", skip < chld_out.lines ? chld_out.line[skip] : chld_out.buf);
106                 return result;  /* return error status from remote command */
107         }
110         /*
111          * Passive mode
112          */
114         /* process output */
115         if (!(fp = fopen (outputfile, "a"))) {
116                 printf (_("SSH WARNING: could not open %s\n"), outputfile);
117                 exit (STATE_UNKNOWN);
118         }
120         local_time = time (NULL);
121         commands = 0;
122         for(i = skip; chld_out.line[i]; i++) {
123                 status_text = strstr (chld_out.line[i], "STATUS CODE: ");
124                 if (status_text == NULL) {
125                         printf ("%s", chld_out.line[i]);
126                         return result;
127                 }
128                 if (service[commands] && status_text
129                         && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1)
130                 {
131                         fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
132                                  (int) local_time, host_shortname, service[commands++],
133                                  cresult, chld_out.line[i]);
134                 }
135         }
136         
137         /* force an OK state */
138         return result;
141 /* process command-line arguments */
142 int
143 process_arguments (int argc, char **argv)
145         int c;
146         char *p1, *p2;
148         int option = 0;
149         static struct option longopts[] = {
150                 {"version", no_argument, 0, 'V'},
151                 {"help", no_argument, 0, 'h'},
152                 {"verbose", no_argument, 0, 'v'},
153                 {"fork", no_argument, 0, 'f'},
154                 {"timeout", required_argument, 0, 't'},
155                 {"host", required_argument, 0, 'H'},
156                 {"port", required_argument,0,'p'},
157                 {"output", required_argument, 0, 'O'},
158                 {"name", required_argument, 0, 'n'},
159                 {"services", required_argument, 0, 's'},
160                 {"identity", required_argument, 0, 'i'},
161                 {"user", required_argument, 0, 'u'},
162                 {"logname", required_argument, 0, 'l'},
163                 {"command", required_argument, 0, 'C'},
164                 {"skip", required_argument, 0, 'S'},
165                 {"proto1", no_argument, 0, '1'},
166                 {"proto2", no_argument, 0, '2'},
167                 {"use-ipv4", no_argument, 0, '4'},
168                 {"use-ipv6", no_argument, 0, '6'},
169                 {0, 0, 0, 0}
170         };
172         if (argc < 2)
173                 return ERROR;
175         for (c = 1; c < argc; c++)
176                 if (strcmp ("-to", argv[c]) == 0)
177                         strcpy (argv[c], "-t");
179         while (1) {
180                 c = getopt_long (argc, argv, "Vvh1246ft:H:O:p:i:u:l:C:S:n:s:", longopts,
181                                  &option);
183                 if (c == -1 || c == EOF)
184                         break;
186                 switch (c) {
187                 case 'V':                                                                       /* version */
188                         print_revision (progname, revision);
189                         exit (STATE_OK);
190                 case 'h':                                                                       /* help */
191                         print_help ();
192                         exit (STATE_OK);
193                 case 'v':                                                                       /* help */
194                         verbose = TRUE;
195                         break;
196                 case 't':                                                                       /* timeout period */
197                         if (!is_integer (optarg))
198                                 usage_va(_("Timeout interval must be a positive integer"));
199                         else
200                                 timeout_interval = atoi (optarg);
201                         break;
202                 case 'H':                                                                       /* host */
203                         host_or_die(optarg);
204                         hostname = optarg;
205                         break;
206                 case 'p': /* port number */
207                         if (!is_integer (optarg))
208                                 usage_va(_("Port must be a positive integer"));
209                         asprintf (&comm,"%s -p %s", comm, optarg);
210                         break;
211                 case 'O':                                                                       /* output file */
212                         outputfile = optarg;
213                         passive = TRUE;
214                         break;
215                 case 's':                                                                       /* description of service to check */
216                         p1 = optarg;
217                         service = realloc (service, (++services) * sizeof(char *));
218                         while ((p2 = index (p1, ':'))) {
219                                 *p2 = '\0';
220                                 service[services - 1] = p1;
221                                 service = realloc (service, (++services) * sizeof(char *));
222                                 p1 = p2 + 1;
223                         }
224                         service[services - 1] = p1;
225                         break;
226                 case 'n':                                                                       /* short name of host in nagios configuration */
227                         host_shortname = optarg;
228                         break;
230                 case 'u':
231                         c = 'l';
232                 case 'l':                                                                       /* login name */
233                 case 'i':                                                                       /* identity */
234                         asprintf (&comm, "%s -%c %s", comm, c, optarg);
235                         break;
237                 case '1':                                                                       /* Pass these switches directly to ssh */
238                 case '2':                                                                       /* 1 to force version 1, 2 to force version 2 */
239                 case '4':                                                                       /* -4 for IPv4 */
240                 case '6':                                                               /* -6 for IPv6 */
241                 case 'f':                                                                       /* fork to background */
242                         asprintf (&comm, "%s -%c", comm, c);
243                         break;
244                 case 'C':                                                                       /* Command for remote machine */
245                         commands++;
246                         if (commands > 1)
247                                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
248                         asprintf (&remotecmd, "%s%s", remotecmd, optarg);
249                         break;
250                 case 'S':                                                                       /* Skip n lines in the output to ignore system banner */
251                         if (!is_integer (optarg))
252                                 usage_va(_("skip lines must be an integer"));
253                         else
254                                 skip = atoi (optarg);
255                         break;
256                 default:                                                                        /* help */
257                         usage_va(_("Unknown argument - %s"), optarg);
258                 }
259         }
261         c = optind;
262         if (hostname == NULL) {
263                 if (c <= argc) {
264                         die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
265                 }
266                 host_or_die(argv[c]);
267                 hostname = argv[c++];
268         }
270         if (strlen(remotecmd) == 0) {
271                 for (; c < argc; c++)
272                         if (strlen(remotecmd) > 0)
273                                 asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
274                         else
275                                 asprintf (&remotecmd, "%s", argv[c]);
276         }
278         if (commands > 1)
279                 asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
281         if (remotecmd == NULL || strlen (remotecmd) <= 1)
282                 usage_va(_("No remotecmd"));
284         asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
286         return validate_arguments ();
291 int
292 validate_arguments (void)
294         if (remotecmd == NULL || hostname == NULL)
295                 return ERROR;
297         if (passive && commands != services)
298                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
300         if (passive && host_shortname == NULL)
301                 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
303         return OK;
307 void
308 print_help (void)
310         print_revision (progname, revision);
312         printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
313         printf (COPYRIGHT, copyright, email);
315         printf (_("This plugin uses SSH to execute commands on a remote host"));
317   printf ("\n\n");
318   
319         print_usage ();
321         printf (_(UT_HELP_VRSN));
323         printf (_(UT_HOST_PORT), 'p', "none");
325         printf (_(UT_IPv46));
327   printf (" %s\n", "-1, --proto1");
328   printf ("    %s\n", _("tell ssh to use Protocol 1"));
329   printf (" %s\n", "-2, --proto2");
330   printf ("    %s\n", _("tell ssh to use Protocol 2"));
331   printf (" %s\n", "-S, --skiplines=n");
332   printf ("    %s\n", _("Ignore first n lines on STDERR (to suppress a logon banner)"));
333   printf (" %s\n", "-f");
334   printf ("    %s\n", _("tells ssh to fork rather than create a tty"));
335   printf (" %s\n","-C, --command='COMMAND STRING'");
336   printf ("    %s\n", _("command to execute on the remote machine"));
337   printf (" %s\n","-l, --logname=USERNAME");
338   printf ("    %s\n", _("SSH user name on remote host [optional]"));
339   printf (" %s\n","-i, --identity=KEYFILE");
340   printf ("    %s\n", _("identity of an authorized key [optional]"));
341   printf (" %s\n","-O, --output=FILE");
342   printf ("    %s\n", _("external command file for nagios [optional]"));
343   printf (" %s\n","-s, --services=LIST");
344   printf ("    %s\n", _("list of nagios service names, separated by ':' [optional]"));
345   printf (" %s\n","-n, --name=NAME");
346   printf ("    %s\n", _("short name of host in nagios configuration [optional]"));
347         printf (_(UT_WARN_CRIT));
348         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
349   printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
350   printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
351   printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
352   printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
353   printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
354   printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
355   printf (" %s\n", _("execute additional commands as proxy"));
356   printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
357   printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
358   printf ("\n");
359   printf ("%s\n", _("Examples:"));
360   printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
361   printf (" %s\n", "$ cat /tmp/foo");
362   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
363   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
364   printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
365         printf (_(UT_SUPPORT));
370 void
371 print_usage (void)
373         printf (_("Usage:"));
374   printf(" %s [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>",progname);
375   printf(" [-n name] [-s servicelist] [-O outputfile] [-p port]\n");