summaryrefslogtreecommitdiffstats
path: root/plugins/check_by_ssh.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_by_ssh.c')
-rw-r--r--plugins/check_by_ssh.c197
1 files changed, 95 insertions, 102 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c
index 7328f5a..de106f2 100644
--- a/plugins/check_by_ssh.c
+++ b/plugins/check_by_ssh.c
@@ -23,22 +23,91 @@
23 *****************************************************************************/ 23 *****************************************************************************/
24 24
25const char *progname = "check_by_ssh"; 25const char *progname = "check_by_ssh";
26#define DESCRIPTION "Run checks on a remote system using ssh, wrapping the proper timeout around the ssh invocation." 26const char *revision = "$Revision$";
27#define AUTHOR "Karl DeBisschop" 27const char *copyright = "2000-2003";
28#define EMAIL "karl@debisschop.net" 28const char *email = "nagiosplug-devel@lists.sourceforge.net";
29#define COPYRIGHTDATE "1999, 2000, 2001"
30 29
31#include "config.h" 30#include "config.h"
32#include "common.h" 31#include "common.h"
33#include "popen.h" 32#include "netutils.h"
34#include "utils.h" 33#include "utils.h"
35#include <time.h> 34#include "popen.h"
36 35
37int process_arguments (int, char **); 36int process_arguments (int, char **);
38int validate_arguments (void); 37int validate_arguments (void);
39void print_help (const char *command_name); 38void print_help (void);
40void print_usage (void); 39void print_usage (void);
41 40
41void
42print_help (void)
43{
44 print_revision (progname, revision);
45
46 printf (_(COPYRIGHT), copyright, email);
47
48 printf (_("This plugin uses SSH to execute commands on a remote host\n\n"));
49
50 print_usage ();
51
52 printf (_(HELP_VRSN));
53
54 printf (_(HOST_PORT), 'p', "none");
55
56 printf (_(IPv46));
57
58 printf (_("\
59 -1, --proto1\n\
60 tell ssh to use Protocol 1\n\
61 -2, --proto2\n\
62 tell ssh to use Protocol 2\n\
63 -f\n\
64 tells ssh to fork rather than create a tty\n"));
65
66 printf (_("\
67 -C, --command='COMMAND STRING'\n\
68 command to execute on the remote machine\n\
69 -l, --logname=USERNAME\n\
70 SSH user name on remote host [optional]\n\
71 -i, --identity=KEYFILE\n\
72 identity of an authorized key [optional]\n\
73 -O, --output=FILE\n\
74 external command file for nagios [optional]\n\
75 -s, --services=LIST\n\
76 list of nagios service names, separated by ':' [optional]\n\
77 -n, --name=NAME\n\
78 short name of host in nagios configuration [optional]\n"));
79
80 printf (_(WARN_CRIT_TO), DEFAULT_SOCKET_TIMEOUT);
81
82 printf (_("\n\
83The most common mode of use is to refer to a local identity file with\n\
84the '-i' option. In this mode, the identity pair should have a null\n\
85passphrase and the public key should be listed in the authorized_keys\n\
86file of the remote host. Usually the key will be restricted to running\n\
87only one command on the remote server. If the remote SSH server tracks\n\
88invocation agruments, the one remote program may be an agent that can\n\
89execute additional commands as proxy\n"));
90
91 printf (_("\n\
92To use passive mode, provide multiple '-C' options, and provide\n\
93all of -O, -s, and -n options (servicelist order must match '-C'\n\
94options)\n"));
95}
96
97
98
99
100
101void
102print_usage (void)
103{
104 printf (_("Usage:\n\
105check_by_ssh [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>\n\
106 [-n name] [-s servicelist] [-O outputfile] [-p port]\n\
107check_by_ssh -V prints version info\n\
108check_by_ssh -h prints more detailed help\n"));
109}
110
42 111
43int commands = 0; 112int commands = 0;
44int services = 0; 113int services = 0;
@@ -56,13 +125,11 @@ int
56main (int argc, char **argv) 125main (int argc, char **argv)
57{ 126{
58 127
59 char input_buffer[MAX_INPUT_BUFFER] = ""; 128 char input_buffer[MAX_INPUT_BUFFER];
60 char *result_text = ""; 129 char *result_text = "";
61 char *status_text; 130 char *status_text;
62 char *output = ""; 131 char *output = "";
63 char *summary = "";
64 char *eol = NULL; 132 char *eol = NULL;
65 char *srvc_desc = NULL;
66 int cresult; 133 int cresult;
67 int result = STATE_UNKNOWN; 134 int result = STATE_UNKNOWN;
68 time_t local_time; 135 time_t local_time;
@@ -71,7 +138,7 @@ main (int argc, char **argv)
71 138
72 /* process arguments */ 139 /* process arguments */
73 if (process_arguments (argc, argv) == ERROR) 140 if (process_arguments (argc, argv) == ERROR)
74 usage ("Could not parse arguments\n"); 141 usage (_("Could not parse arguments\n"));
75 142
76 143
77 /* Set signal handling and alarm timeout */ 144 /* Set signal handling and alarm timeout */
@@ -90,7 +157,7 @@ main (int argc, char **argv)
90 child_process = spopen (comm); 157 child_process = spopen (comm);
91 158
92 if (child_process == NULL) { 159 if (child_process == NULL) {
93 printf ("Unable to open pipe: %s", comm); 160 printf (_("Unable to open pipe: %s"), comm);
94 return STATE_UNKNOWN; 161 return STATE_UNKNOWN;
95 } 162 }
96 163
@@ -98,7 +165,7 @@ main (int argc, char **argv)
98 /* open STDERR for spopen */ 165 /* open STDERR for spopen */
99 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); 166 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
100 if (child_stderr == NULL) { 167 if (child_stderr == NULL) {
101 printf ("Could not open stderr for %s\n", SSH_COMMAND); 168 printf (_("Could not open stderr for %s\n"), SSH_COMMAND);
102 } 169 }
103 170
104 171
@@ -123,14 +190,14 @@ main (int argc, char **argv)
123 if (passive) { 190 if (passive) {
124 191
125 if (!(fp = fopen (outputfile, "a"))) { 192 if (!(fp = fopen (outputfile, "a"))) {
126 printf ("SSH WARNING: could not open %s\n", outputfile); 193 printf (_("SSH WARNING: could not open %s\n"), outputfile);
127 exit (STATE_UNKNOWN); 194 exit (STATE_UNKNOWN);
128 } 195 }
129 196
130 time (&local_time); 197 time (&local_time);
131 commands = 0; 198 commands = 0;
132 while (result_text && strlen(result_text) > 0) { 199 while (result_text && strlen(result_text) > 0) {
133 status_text = (strstr (result_text, "STATUS CODE: ")); 200 status_text = strstr (result_text, _("STATUS CODE: "));
134 if (status_text == NULL) { 201 if (status_text == NULL) {
135 printf ("%s", result_text); 202 printf ("%s", result_text);
136 return result; 203 return result;
@@ -141,8 +208,8 @@ main (int argc, char **argv)
141 if (eol != NULL) 208 if (eol != NULL)
142 eol[0] = 0; 209 eol[0] = 0;
143 if (service[commands] && status_text 210 if (service[commands] && status_text
144 && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) { 211 && sscanf (status_text, _("STATUS CODE: %d"), &cresult) == 1) {
145 fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", 212 fprintf (fp, _("[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n"),
146 (int) local_time, host_shortname, service[commands++], cresult, 213 (int) local_time, host_shortname, service[commands++], cresult,
147 output); 214 output);
148 } 215 }
@@ -170,9 +237,8 @@ main (int argc, char **argv)
170int 237int
171process_arguments (int argc, char **argv) 238process_arguments (int argc, char **argv)
172{ 239{
173 int c, i; 240 int c;
174 char *p1, *p2; 241 char *p1, *p2;
175 size_t len;
176 242
177 int option_index = 0; 243 int option_index = 0;
178 static struct option long_options[] = { 244 static struct option long_options[] = {
@@ -219,24 +285,24 @@ process_arguments (int argc, char **argv)
219 print_revision (progname, "$Revision$"); 285 print_revision (progname, "$Revision$");
220 exit (STATE_OK); 286 exit (STATE_OK);
221 case 'h': /* help */ 287 case 'h': /* help */
222 print_help (progname); 288 print_help ();
223 exit (STATE_OK); 289 exit (STATE_OK);
224 case 'v': /* help */ 290 case 'v': /* help */
225 verbose = TRUE; 291 verbose = TRUE;
226 break; 292 break;
227 case 't': /* timeout period */ 293 case 't': /* timeout period */
228 if (!is_integer (optarg)) 294 if (!is_integer (optarg))
229 usage2 ("timeout interval must be an integer", optarg); 295 usage2 (_("timeout interval must be an integer"), optarg);
230 timeout_interval = atoi (optarg); 296 timeout_interval = atoi (optarg);
231 break; 297 break;
232 case 'H': /* host */ 298 case 'H': /* host */
233 if (!is_host (optarg)) 299 if (!is_host (optarg))
234 usage2 ("invalid host name", optarg); 300 usage2 (_("invalid host name"), optarg);
235 hostname = optarg; 301 hostname = optarg;
236 break; 302 break;
237 case 'p': /* port number */ 303 case 'p': /* port number */
238 if (!is_integer (optarg)) 304 if (!is_integer (optarg))
239 usage2 ("port must be an integer", optarg); 305 usage2 (_("port must be an integer"), optarg);
240 asprintf (&comm,"%s -p %s", comm, optarg); 306 asprintf (&comm,"%s -p %s", comm, optarg);
241 break; 307 break;
242 case 'O': /* output file */ 308 case 'O': /* output file */
@@ -246,7 +312,7 @@ process_arguments (int argc, char **argv)
246 case 's': /* description of service to check */ 312 case 's': /* description of service to check */
247 service = realloc (service, (++services) * sizeof(char *)); 313 service = realloc (service, (++services) * sizeof(char *));
248 p1 = optarg; 314 p1 = optarg;
249 while (p2 = index (p1, ':')) { 315 while ((p2 = index (p1, ':'))) {
250 *p2 = '\0'; 316 *p2 = '\0';
251 asprintf (&service[services-1], "%s", p1); 317 asprintf (&service[services-1], "%s", p1);
252 service = realloc (service, (++services) * sizeof(char *)); 318 service = realloc (service, (++services) * sizeof(char *));
@@ -281,9 +347,9 @@ process_arguments (int argc, char **argv)
281 c = optind; 347 c = optind;
282 if (hostname == NULL) { 348 if (hostname == NULL) {
283 if (c <= argc) { 349 if (c <= argc) {
284 terminate (STATE_UNKNOWN, "%s: You must provide a host name\n", progname); 350 terminate (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
285 } else if (!is_host (argv[c])) 351 } else if (!is_host (argv[c]))
286 terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", progname, argv[c]); 352 terminate (STATE_UNKNOWN, _("%s: Invalid host name %s\n"), progname, argv[c]);
287 hostname = argv[c++]; 353 hostname = argv[c++];
288 } 354 }
289 355
@@ -299,7 +365,7 @@ process_arguments (int argc, char **argv)
299 remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;"); 365 remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;");
300 366
301 if (remotecmd == NULL || strlen (remotecmd) <= 1) 367 if (remotecmd == NULL || strlen (remotecmd) <= 1)
302 usage ("No remotecmd\n"); 368 usage (_("No remotecmd\n"));
303 369
304 asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd); 370 asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
305 371
@@ -317,83 +383,10 @@ validate_arguments (void)
317 return ERROR; 383 return ERROR;
318 384
319 if (passive && commands != services) 385 if (passive && commands != services)
320 terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide a service name for each command.\n", progname); 386 terminate (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
321 387
322 if (passive && host_shortname == NULL) 388 if (passive && host_shortname == NULL)
323 terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide the host short name from the nagios configs.\n", progname); 389 terminate (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
324 390
325 return OK; 391 return OK;
326} 392}
327
328
329
330
331
332void
333print_help (const char *cmd)
334{
335 print_revision (cmd, "$Revision$");
336
337 printf
338 ("Copyright (c) 1999 Karl DeBisschop (kdebisschop@alum.mit.edu)\n\n"
339 "This plugin will execute a command on a remote host using SSH\n\n");
340
341 print_usage ();
342
343 printf
344 ("\nOptions:\n"
345 "-H, --hostname=HOST\n"
346 " name or IP address of remote host\n"
347 "-C, --command='COMMAND STRING'\n"
348 " command to execute on the remote machine\n"
349 "-f tells ssh to fork rather than create a tty\n"
350 "-t, --timeout=INTEGER\n"
351 " specify timeout (default: %d seconds) [optional]\n"
352 "-p, --port=PORT\n"
353 " port to connect to on remote system [optional]\n"
354 "-l, --logname=USERNAME\n"
355 " SSH user name on remote host [optional]\n"
356 "-i, --identity=KEYFILE\n"
357 " identity of an authorized key [optional]\n"
358 "-O, --output=FILE\n"
359 " external command file for nagios [optional]\n"
360 "-s, --services=LIST\n"
361 " list of nagios service names, separated by ':' [optional]\n"
362 "-n, --name=NAME\n"
363 " short name of host in nagios configuration [optional]\n"
364 "-1, --proto1\n"
365 " tell ssh to use Protocol 1\n"
366 "-2, --proto2\n"
367 " tell ssh to use Protocol 2\n"
368 "-4, --use-ipv4\n"
369 " tell ssh to use IPv4\n"
370 "-6, --use-ipv6\n"
371 " tell ssh to use IPv6\n"
372 "\n"
373 "The most common mode of use is to refer to a local identity file with\n"
374 "the '-i' option. In this mode, the identity pair should have a null\n"
375 "passphrase and the public key should be listed in the authorized_keys\n"
376 "file of the remote host. Usually the key will be restricted to running\n"
377 "only one command on the remote server. If the remote SSH server tracks\n"
378 "invocation agruments, the one remote program may be an agent that can\n"
379 "execute additional commands as proxy\n"
380 "\n"
381 "To use passive mode, provide multiple '-C' options, and provide\n"
382 "all of -O, -s, and -n options (servicelist order must match '-C'\n"
383 "options)\n", DEFAULT_SOCKET_TIMEOUT);
384}
385
386
387
388
389
390void
391print_usage (void)
392{
393 printf
394 ("Usage:\n"
395 "check_by_ssh [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>\n"
396 " [-n name] [-s servicelist] [-O outputfile] [-p port]\n"
397 "check_by_ssh -V prints version info\n"
398 "check_by_ssh -h prints more detailed help\n");
399}