diff options
Diffstat (limited to 'web/attachments/226292-check_by_ssh.c')
-rw-r--r-- | web/attachments/226292-check_by_ssh.c | 388 |
1 files changed, 388 insertions, 0 deletions
diff --git a/web/attachments/226292-check_by_ssh.c b/web/attachments/226292-check_by_ssh.c new file mode 100644 index 0000000..237d5b6 --- /dev/null +++ b/web/attachments/226292-check_by_ssh.c | |||
@@ -0,0 +1,388 @@ | |||
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: 2007/01/28 21:46:40 $ | ||
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: check_by_ssh.c,v 1.41 2007/01/28 21:46:40 hweiss Exp $ | ||
31 | * | ||
32 | ******************************************************************************/ | ||
33 | |||
34 | const char *progname = "check_by_ssh"; | ||
35 | const char *revision = "$Revision: 1.41 $"; | ||
36 | const char *copyright = "2000-2006"; | ||
37 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
38 | |||
39 | #include "common.h" | ||
40 | #include "netutils.h" | ||
41 | #include "utils.h" | ||
42 | #include "runcmd.h" | ||
43 | |||
44 | int process_arguments (int, char **); | ||
45 | int validate_arguments (void); | ||
46 | void print_help (void); | ||
47 | void print_usage (void); | ||
48 | |||
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; | ||
60 | |||
61 | int | ||
62 | main (int argc, char **argv) | ||
63 | { | ||
64 | |||
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; | ||
72 | |||
73 | remotecmd = ""; | ||
74 | comm = strdup (SSH_COMMAND); | ||
75 | |||
76 | setlocale (LC_ALL, ""); | ||
77 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
78 | textdomain (PACKAGE); | ||
79 | |||
80 | /* process arguments */ | ||
81 | if (process_arguments (argc, argv) == ERROR) | ||
82 | usage_va(_("Could not parse arguments")); | ||
83 | |||
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); | ||
89 | |||
90 | /* run the command */ | ||
91 | if (verbose) | ||
92 | printf ("%s\n", comm); | ||
93 | |||
94 | result = np_runcmd(comm, &chld_out, &chld_err, 0); | ||
95 | /* UNKNOWN if output found on stderr */ | ||
96 | if(chld_err.buflen && !skip) { | ||
97 | printf(_("Remote command execution failed: %s\n"), | ||
98 | chld_err.buflen ? chld_err.buf : _("Unknown error")); | ||
99 | return STATE_UNKNOWN; | ||
100 | } | ||
101 | |||
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 | } | ||
108 | |||
109 | |||
110 | /* | ||
111 | * Passive mode | ||
112 | */ | ||
113 | |||
114 | /* process output */ | ||
115 | if (!(fp = fopen (outputfile, "a"))) { | ||
116 | printf (_("SSH WARNING: could not open %s\n"), outputfile); | ||
117 | exit (STATE_UNKNOWN); | ||
118 | } | ||
119 | |||
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; | ||
139 | } | ||
140 | |||
141 | /* process command-line arguments */ | ||
142 | int | ||
143 | process_arguments (int argc, char **argv) | ||
144 | { | ||
145 | int c; | ||
146 | char *p1, *p2; | ||
147 | |||
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 | {"ssh-option", required_argument, 0, 'o'}, | ||
170 | {"quiet", no_argument, 0, 'q'}, | ||
171 | {0, 0, 0, 0} | ||
172 | }; | ||
173 | |||
174 | if (argc < 2) | ||
175 | return ERROR; | ||
176 | |||
177 | for (c = 1; c < argc; c++) | ||
178 | if (strcmp ("-to", argv[c]) == 0) | ||
179 | strcpy (argv[c], "-t"); | ||
180 | |||
181 | while (1) { | ||
182 | c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S:n:s:o:", longopts, | ||
183 | &option); | ||
184 | |||
185 | if (c == -1 || c == EOF) | ||
186 | break; | ||
187 | |||
188 | switch (c) { | ||
189 | case 'V': /* version */ | ||
190 | print_revision (progname, revision); | ||
191 | exit (STATE_OK); | ||
192 | case 'h': /* help */ | ||
193 | print_help (); | ||
194 | exit (STATE_OK); | ||
195 | case 'v': /* help */ | ||
196 | verbose = TRUE; | ||
197 | break; | ||
198 | case 't': /* timeout period */ | ||
199 | if (!is_integer (optarg)) | ||
200 | usage_va(_("Timeout interval must be a positive integer")); | ||
201 | else | ||
202 | timeout_interval = atoi (optarg); | ||
203 | break; | ||
204 | case 'H': /* host */ | ||
205 | host_or_die(optarg); | ||
206 | hostname = optarg; | ||
207 | break; | ||
208 | case 'p': /* port number */ | ||
209 | if (!is_integer (optarg)) | ||
210 | usage_va(_("Port must be a positive integer")); | ||
211 | asprintf (&comm,"%s -p %s", comm, optarg); | ||
212 | break; | ||
213 | case 'O': /* output file */ | ||
214 | outputfile = optarg; | ||
215 | passive = TRUE; | ||
216 | break; | ||
217 | case 's': /* description of service to check */ | ||
218 | p1 = optarg; | ||
219 | service = realloc (service, (++services) * sizeof(char *)); | ||
220 | while ((p2 = index (p1, ':'))) { | ||
221 | *p2 = '\0'; | ||
222 | service[services - 1] = p1; | ||
223 | service = realloc (service, (++services) * sizeof(char *)); | ||
224 | p1 = p2 + 1; | ||
225 | } | ||
226 | service[services - 1] = p1; | ||
227 | break; | ||
228 | case 'n': /* short name of host in nagios configuration */ | ||
229 | host_shortname = optarg; | ||
230 | break; | ||
231 | |||
232 | case 'u': | ||
233 | c = 'l'; | ||
234 | case 'l': /* login name */ | ||
235 | case 'i': /* identity */ | ||
236 | asprintf (&comm, "%s -%c %s", comm, c, optarg); | ||
237 | break; | ||
238 | |||
239 | case '1': /* Pass these switches directly to ssh */ | ||
240 | case '2': /* 1 to force version 1, 2 to force version 2 */ | ||
241 | case '4': /* -4 for IPv4 */ | ||
242 | case '6': /* -6 for IPv6 */ | ||
243 | case 'f': /* fork to background */ | ||
244 | asprintf (&comm, "%s -%c", comm, c); | ||
245 | break; | ||
246 | case 'C': /* Command for remote machine */ | ||
247 | commands++; | ||
248 | if (commands > 1) | ||
249 | asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | ||
250 | asprintf (&remotecmd, "%s%s", remotecmd, optarg); | ||
251 | break; | ||
252 | case 'S': /* Skip n lines in the output to ignore system banner */ | ||
253 | if (!is_integer (optarg)) | ||
254 | usage_va(_("skip lines must be an integer")); | ||
255 | else | ||
256 | skip = atoi (optarg); | ||
257 | break; | ||
258 | case 'o': /* Extra options for the ssh command */ | ||
259 | asprintf (&comm, "%s -%c '%s'", comm, c, optarg); | ||
260 | break; | ||
261 | case 'q': /* Tell the ssh command to be quiet */ | ||
262 | asprintf (&comm, "%s -%c", comm, c); | ||
263 | break; | ||
264 | default: /* help */ | ||
265 | usage5(); | ||
266 | } | ||
267 | } | ||
268 | |||
269 | c = optind; | ||
270 | if (hostname == NULL) { | ||
271 | if (c <= argc) { | ||
272 | die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); | ||
273 | } | ||
274 | host_or_die(argv[c]); | ||
275 | hostname = argv[c++]; | ||
276 | } | ||
277 | |||
278 | if (strlen(remotecmd) == 0) { | ||
279 | for (; c < argc; c++) | ||
280 | if (strlen(remotecmd) > 0) | ||
281 | asprintf (&remotecmd, "%s %s", remotecmd, argv[c]); | ||
282 | else | ||
283 | asprintf (&remotecmd, "%s", argv[c]); | ||
284 | } | ||
285 | |||
286 | if (commands > 1) | ||
287 | asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | ||
288 | |||
289 | if (remotecmd == NULL || strlen (remotecmd) <= 1) | ||
290 | usage_va(_("No remotecmd")); | ||
291 | |||
292 | asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd); | ||
293 | |||
294 | return validate_arguments (); | ||
295 | } | ||
296 | |||
297 | |||
298 | |||
299 | int | ||
300 | validate_arguments (void) | ||
301 | { | ||
302 | if (remotecmd == NULL || hostname == NULL) | ||
303 | return ERROR; | ||
304 | |||
305 | if (passive && commands != services) | ||
306 | die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); | ||
307 | |||
308 | if (passive && host_shortname == NULL) | ||
309 | die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname); | ||
310 | |||
311 | return OK; | ||
312 | } | ||
313 | |||
314 | |||
315 | void | ||
316 | print_help (void) | ||
317 | { | ||
318 | print_revision (progname, revision); | ||
319 | |||
320 | printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"); | ||
321 | printf (COPYRIGHT, copyright, email); | ||
322 | |||
323 | printf (_("This plugin uses SSH to execute commands on a remote host")); | ||
324 | |||
325 | printf ("\n\n"); | ||
326 | |||
327 | print_usage (); | ||
328 | |||
329 | printf (_(UT_HELP_VRSN)); | ||
330 | |||
331 | printf (_(UT_HOST_PORT), 'p', "none"); | ||
332 | |||
333 | printf (_(UT_IPv46)); | ||
334 | |||
335 | printf (" %s\n", "-1, --proto1"); | ||
336 | printf (" %s\n", _("tell ssh to use Protocol 1")); | ||
337 | printf (" %s\n", "-2, --proto2"); | ||
338 | printf (" %s\n", _("tell ssh to use Protocol 2")); | ||
339 | printf (" %s\n", "-S, --skip=n"); | ||
340 | printf (" %s\n", _("Ignore first n lines on STDERR (to suppress a logon banner)")); | ||
341 | printf (" %s\n", "-f"); | ||
342 | printf (" %s\n", _("tells ssh to fork rather than create a tty")); | ||
343 | printf (" %s\n","-C, --command='COMMAND STRING'"); | ||
344 | printf (" %s\n", _("command to execute on the remote machine")); | ||
345 | printf (" %s\n","-l, --logname=USERNAME"); | ||
346 | printf (" %s\n", _("SSH user name on remote host [optional]")); | ||
347 | printf (" %s\n","-i, --identity=KEYFILE"); | ||
348 | printf (" %s\n", _("identity of an authorized key [optional]")); | ||
349 | printf (" %s\n","-O, --output=FILE"); | ||
350 | printf (" %s\n", _("external command file for nagios [optional]")); | ||
351 | printf (" %s\n","-s, --services=LIST"); | ||
352 | printf (" %s\n", _("list of nagios service names, separated by ':' [optional]")); | ||
353 | printf (" %s\n","-n, --name=NAME"); | ||
354 | printf (" %s\n", _("short name of host in nagios configuration [optional]")); | ||
355 | printf (" %s\n","-o, --ssh-option=OPTION"); | ||
356 | printf (" %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]")); | ||
357 | printf (" %s\n","-q, --quiet"); | ||
358 | printf (" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]")); | ||
359 | printf (_(UT_WARN_CRIT)); | ||
360 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); | ||
361 | printf (" %s\n", _("The most common mode of use is to refer to a local identity file with")); | ||
362 | printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null")); | ||
363 | printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys")); | ||
364 | printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running")); | ||
365 | printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks")); | ||
366 | printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can")); | ||
367 | printf (" %s\n", _("execute additional commands as proxy")); | ||
368 | printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide")); | ||
369 | printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)")); | ||
370 | printf ("\n"); | ||
371 | printf ("%s\n", _("Examples:")); | ||
372 | printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo"); | ||
373 | printf (" %s\n", "$ cat /tmp/foo"); | ||
374 | printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days"); | ||
375 | printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days"); | ||
376 | printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days"); | ||
377 | printf (_(UT_SUPPORT)); | ||
378 | } | ||
379 | |||
380 | |||
381 | |||
382 | void | ||
383 | print_usage (void) | ||
384 | { | ||
385 | printf (_("Usage:")); | ||
386 | printf(" %s [-fq46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>",progname); | ||
387 | printf(" [-n name] [-s servicelist] [-O outputfile] [-p port] [-o ssh-option]\n"); | ||
388 | } | ||