summaryrefslogtreecommitdiffstats
path: root/plugins/check_by_ssh.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2024-10-31 13:57:43 (GMT)
committerGitHub <noreply@github.com>2024-10-31 13:57:43 (GMT)
commitb1d260a821b7d4916d6bf1a026fbc9b4f2b268ae (patch)
tree84a3fc6b26b181a1ed10d7ca55c32ac9170efbd4 /plugins/check_by_ssh.c
parentb4c5956591e9741ce9b190210e7b10940a6adbdd (diff)
parent8955f56de355403941bc8f02a4edb2bc72d1397a (diff)
downloadmonitoring-plugins-b1d260a821b7d4916d6bf1a026fbc9b4f2b268ae.tar.gz
Merge pull request #2035 from RincewindsHat/cleanup/rest-of-plugins
Cleanup for some more plugins
Diffstat (limited to 'plugins/check_by_ssh.c')
-rw-r--r--plugins/check_by_ssh.c528
1 files changed, 251 insertions, 277 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c
index 2a23b39..905b239 100644
--- a/plugins/check_by_ssh.c
+++ b/plugins/check_by_ssh.c
@@ -1,33 +1,33 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_by_ssh plugin 3 * Monitoring check_by_ssh plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2000-2008 Monitoring Plugins Development Team 6 * Copyright (c) 2000-2024 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the check_by_ssh plugin 10 * This file contains the check_by_ssh plugin
11* 11 *
12* 12 *
13* This program is free software: you can redistribute it and/or modify 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 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 15 * the Free Software Foundation, either version 3 of the License, or
16* (at your option) any later version. 16 * (at your option) any later version.
17* 17 *
18* This program is distributed in the hope that it will be useful, 18 * This program is distributed in the hope that it will be useful,
19* but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21* GNU General Public License for more details. 21 * GNU General Public License for more details.
22* 22 *
23* You should have received a copy of the GNU General Public License 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/>. 24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25* 25 *
26* 26 *
27*****************************************************************************/ 27 *****************************************************************************/
28 28
29const char *progname = "check_by_ssh"; 29const char *progname = "check_by_ssh";
30const char *copyright = "2000-2008"; 30const char *copyright = "2000-2024";
31const char *email = "devel@monitoring-plugins.org"; 31const char *email = "devel@monitoring-plugins.org";
32 32
33#include "common.h" 33#include "common.h"
@@ -36,82 +36,80 @@ const char *email = "devel@monitoring-plugins.org";
36#include "utils_cmd.h" 36#include "utils_cmd.h"
37 37
38#ifndef NP_MAXARGS 38#ifndef NP_MAXARGS
39#define NP_MAXARGS 1024 39# define NP_MAXARGS 1024
40#endif 40#endif
41 41
42int process_arguments (int, char **); 42static int process_arguments(int /*argc*/, char ** /*argv*/);
43int validate_arguments (void); 43static int validate_arguments(void);
44void comm_append (const char *); 44static void comm_append(const char * /*str*/);
45void print_help (void); 45static void print_help(void);
46void print_usage (void); 46void print_usage(void);
47 47
48unsigned int commands = 0; 48static unsigned int commands = 0;
49unsigned int services = 0; 49static unsigned int services = 0;
50int skip_stdout = 0; 50static int skip_stdout = 0;
51int skip_stderr = 0; 51static int skip_stderr = 0;
52int warn_on_stderr = 0; 52static int warn_on_stderr = 0;
53bool unknown_timeout = false; 53static bool unknown_timeout = false;
54char *remotecmd = NULL; 54static char *remotecmd = NULL;
55char **commargv = NULL; 55static char **commargv = NULL;
56int commargc = 0; 56static int commargc = 0;
57char *hostname = NULL; 57static char *hostname = NULL;
58char *outputfile = NULL; 58static char *outputfile = NULL;
59char *host_shortname = NULL; 59static char *host_shortname = NULL;
60char **service; 60static char **service;
61bool passive = false; 61static bool passive = false;
62bool verbose = false; 62static bool verbose = false;
63 63
64int 64int main(int argc, char **argv) {
65main (int argc, char **argv)
66{
67 65
68 char *status_text; 66 char *status_text;
69 int cresult; 67 int cresult;
70 int result = STATE_UNKNOWN; 68 int result = STATE_UNKNOWN;
71 time_t local_time; 69 time_t local_time;
72 FILE *fp = NULL; 70 FILE *file_pointer = NULL;
73 output chld_out, chld_err; 71 output chld_out;
72 output chld_err;
74 73
75 remotecmd = ""; 74 remotecmd = "";
76 comm_append(SSH_COMMAND); 75 comm_append(SSH_COMMAND);
77 76
78 setlocale (LC_ALL, ""); 77 setlocale(LC_ALL, "");
79 bindtextdomain (PACKAGE, LOCALEDIR); 78 bindtextdomain(PACKAGE, LOCALEDIR);
80 textdomain (PACKAGE); 79 textdomain(PACKAGE);
81 80
82 /* Parse extra opts if any */ 81 /* Parse extra opts if any */
83 argv=np_extra_opts (&argc, argv, progname); 82 argv = np_extra_opts(&argc, argv, progname);
84 83
85 /* process arguments */ 84 /* process arguments */
86 if (process_arguments (argc, argv) == ERROR) 85 if (process_arguments(argc, argv) == ERROR)
87 usage_va(_("Could not parse arguments")); 86 usage_va(_("Could not parse arguments"));
88 87
89 /* Set signal handling and alarm timeout */ 88 /* Set signal handling and alarm timeout */
90 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { 89 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
91 usage_va(_("Cannot catch SIGALRM")); 90 usage_va(_("Cannot catch SIGALRM"));
92 } 91 }
93 alarm (timeout_interval); 92 alarm(timeout_interval);
94 93
95 /* run the command */ 94 /* run the command */
96 if (verbose) { 95 if (verbose) {
97 printf ("Command: %s\n", commargv[0]); 96 printf("Command: %s\n", commargv[0]);
98 for (int i = 1; i < commargc; i++) 97 for (int i = 1; i < commargc; i++)
99 printf ("Argument %i: %s\n", i, commargv[i]); 98 printf("Argument %i: %s\n", i, commargv[i]);
100 } 99 }
101 100
102 result = cmd_run_array (commargv, &chld_out, &chld_err, 0); 101 result = cmd_run_array(commargv, &chld_out, &chld_err, 0);
103 102
104 /* SSH returns 255 if connection attempt fails; include the first line of error output */ 103 /* SSH returns 255 if connection attempt fails; include the first line of error output */
105 if (result == 255 && unknown_timeout) { 104 if (result == 255 && unknown_timeout) {
106 printf (_("SSH connection failed: %s\n"), 105 printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)");
107 chld_err.lines > 0 ? chld_err.line[0] : "(no error output)");
108 return STATE_UNKNOWN; 106 return STATE_UNKNOWN;
109 } 107 }
110 108
111 if (verbose) { 109 if (verbose) {
112 for(size_t i = 0; i < chld_out.lines; i++) 110 for (size_t i = 0; i < chld_out.lines; i++)
113 printf("stdout: %s\n", chld_out.line[i]); 111 printf("stdout: %s\n", chld_out.line[i]);
114 for(size_t i = 0; i < chld_err.lines; i++) 112 for (size_t i = 0; i < chld_err.lines; i++)
115 printf("stderr: %s\n", chld_err.line[i]); 113 printf("stderr: %s\n", chld_err.line[i]);
116 } 114 }
117 115
@@ -121,155 +119,144 @@ main (int argc, char **argv)
121 skip_stderr = chld_err.lines; 119 skip_stderr = chld_err.lines;
122 120
123 /* UNKNOWN or worse if (non-skipped) output found on stderr */ 121 /* UNKNOWN or worse if (non-skipped) output found on stderr */
124 if(chld_err.lines > (size_t)skip_stderr) { 122 if (chld_err.lines > (size_t)skip_stderr) {
125 printf (_("Remote command execution failed: %s\n"), 123 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]);
126 chld_err.line[skip_stderr]); 124 if (warn_on_stderr)
127 if ( warn_on_stderr )
128 return max_state_alt(result, STATE_WARNING); 125 return max_state_alt(result, STATE_WARNING);
129 else 126 return max_state_alt(result, STATE_UNKNOWN);
130 return max_state_alt(result, STATE_UNKNOWN);
131 } 127 }
132 128
133 /* this is simple if we're not supposed to be passive. 129 /* this is simple if we're not supposed to be passive.
134 * Wrap up quickly and keep the tricks below */ 130 * Wrap up quickly and keep the tricks below */
135 if(!passive) { 131 if (!passive) {
136 if (chld_out.lines > (size_t)skip_stdout) 132 if (chld_out.lines > (size_t)skip_stdout)
137 for (size_t i = skip_stdout; i < chld_out.lines; i++) 133 for (size_t i = skip_stdout; i < chld_out.lines; i++)
138 puts (chld_out.line[i]); 134 puts(chld_out.line[i]);
139 else 135 else
140 printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), 136 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result);
141 state_text(result), remotecmd, result); 137 return result; /* return error status from remote command */
142 return result; /* return error status from remote command */
143 } 138 }
144 139
145
146 /* 140 /*
147 * Passive mode 141 * Passive mode
148 */ 142 */
149 143
150 /* process output */ 144 /* process output */
151 if (!(fp = fopen (outputfile, "a"))) { 145 if (!(file_pointer = fopen(outputfile, "a"))) {
152 printf (_("SSH WARNING: could not open %s\n"), outputfile); 146 printf(_("SSH WARNING: could not open %s\n"), outputfile);
153 exit (STATE_UNKNOWN); 147 exit(STATE_UNKNOWN);
154 } 148 }
155 149
156 local_time = time (NULL); 150 local_time = time(NULL);
157 commands = 0; 151 commands = 0;
158 for(size_t i = skip_stdout; i < chld_out.lines; i++) { 152 for (size_t i = skip_stdout; i < chld_out.lines; i++) {
159 status_text = chld_out.line[i++]; 153 status_text = chld_out.line[i++];
160 if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL) 154 if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL)
161 die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); 155 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
162 156
163 if (service[commands] && status_text 157 if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) {
164 && sscanf (chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) 158 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++],
165 { 159 cresult, status_text);
166 fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
167 (int) local_time, host_shortname, service[commands++],
168 cresult, status_text);
169 } 160 }
170 } 161 }
171 162
172 /* Multiple commands and passive checking should always return OK */ 163 /* Multiple commands and passive checking should always return OK */
173 return result; 164 return result;
174} 165}
175 166
176/* process command-line arguments */ 167/* process command-line arguments */
177int 168int process_arguments(int argc, char **argv) {
178process_arguments (int argc, char **argv)
179{
180 int c; 169 int c;
181 char *p1, *p2; 170 char *p1;
171 char *p2;
182 172
183 int option = 0; 173 int option = 0;
184 static struct option longopts[] = { 174 static struct option longopts[] = {{"version", no_argument, 0, 'V'},
185 {"version", no_argument, 0, 'V'}, 175 {"help", no_argument, 0, 'h'},
186 {"help", no_argument, 0, 'h'}, 176 {"verbose", no_argument, 0, 'v'},
187 {"verbose", no_argument, 0, 'v'}, 177 {"fork", no_argument, 0, 'f'},
188 {"fork", no_argument, 0, 'f'}, 178 {"timeout", required_argument, 0, 't'},
189 {"timeout", required_argument, 0, 't'}, 179 {"unknown-timeout", no_argument, 0, 'U'},
190 {"unknown-timeout", no_argument, 0, 'U'}, 180 {"host", required_argument, 0, 'H'}, /* backward compatibility */
191 {"host", required_argument, 0, 'H'}, /* backward compatibility */ 181 {"hostname", required_argument, 0, 'H'},
192 {"hostname", required_argument, 0, 'H'}, 182 {"port", required_argument, 0, 'p'},
193 {"port", required_argument,0,'p'}, 183 {"output", required_argument, 0, 'O'},
194 {"output", required_argument, 0, 'O'}, 184 {"name", required_argument, 0, 'n'},
195 {"name", required_argument, 0, 'n'}, 185 {"services", required_argument, 0, 's'},
196 {"services", required_argument, 0, 's'}, 186 {"identity", required_argument, 0, 'i'},
197 {"identity", required_argument, 0, 'i'}, 187 {"user", required_argument, 0, 'u'},
198 {"user", required_argument, 0, 'u'}, 188 {"logname", required_argument, 0, 'l'},
199 {"logname", required_argument, 0, 'l'}, 189 {"command", required_argument, 0, 'C'},
200 {"command", required_argument, 0, 'C'}, 190 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
201 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */ 191 {"skip-stdout", optional_argument, 0, 'S'},
202 {"skip-stdout", optional_argument, 0, 'S'}, 192 {"skip-stderr", optional_argument, 0, 'E'},
203 {"skip-stderr", optional_argument, 0, 'E'}, 193 {"warn-on-stderr", no_argument, 0, 'W'},
204 {"warn-on-stderr", no_argument, 0, 'W'}, 194 {"proto1", no_argument, 0, '1'},
205 {"proto1", no_argument, 0, '1'}, 195 {"proto2", no_argument, 0, '2'},
206 {"proto2", no_argument, 0, '2'}, 196 {"use-ipv4", no_argument, 0, '4'},
207 {"use-ipv4", no_argument, 0, '4'}, 197 {"use-ipv6", no_argument, 0, '6'},
208 {"use-ipv6", no_argument, 0, '6'}, 198 {"ssh-option", required_argument, 0, 'o'},
209 {"ssh-option", required_argument, 0, 'o'}, 199 {"quiet", no_argument, 0, 'q'},
210 {"quiet", no_argument, 0, 'q'}, 200 {"configfile", optional_argument, 0, 'F'},
211 {"configfile", optional_argument, 0, 'F'}, 201 {0, 0, 0, 0}};
212 {0, 0, 0, 0}
213 };
214 202
215 if (argc < 2) 203 if (argc < 2)
216 return ERROR; 204 return ERROR;
217 205
218 for (c = 1; c < argc; c++) 206 for (c = 1; c < argc; c++)
219 if (strcmp ("-to", argv[c]) == 0) 207 if (strcmp("-to", argv[c]) == 0)
220 strcpy (argv[c], "-t"); 208 strcpy(argv[c], "-t");
221 209
222 while (1) { 210 while (1) {
223 c = getopt_long (argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, 211 c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option);
224 &option);
225 212
226 if (c == -1 || c == EOF) 213 if (c == -1 || c == EOF)
227 break; 214 break;
228 215
229 switch (c) { 216 switch (c) {
230 case 'V': /* version */ 217 case 'V': /* version */
231 print_revision (progname, NP_VERSION); 218 print_revision(progname, NP_VERSION);
232 exit (STATE_UNKNOWN); 219 exit(STATE_UNKNOWN);
233 case 'h': /* help */ 220 case 'h': /* help */
234 print_help (); 221 print_help();
235 exit (STATE_UNKNOWN); 222 exit(STATE_UNKNOWN);
236 case 'v': /* help */ 223 case 'v': /* help */
237 verbose = true; 224 verbose = true;
238 break; 225 break;
239 case 't': /* timeout period */ 226 case 't': /* timeout period */
240 if (!is_integer (optarg)) 227 if (!is_integer(optarg))
241 usage_va(_("Timeout interval must be a positive integer")); 228 usage_va(_("Timeout interval must be a positive integer"));
242 else 229 else
243 timeout_interval = atoi (optarg); 230 timeout_interval = atoi(optarg);
244 break; 231 break;
245 case 'U': 232 case 'U':
246 unknown_timeout = true; 233 unknown_timeout = true;
247 break; 234 break;
248 case 'H': /* host */ 235 case 'H': /* host */
249 hostname = optarg; 236 hostname = optarg;
250 break; 237 break;
251 case 'p': /* port number */ 238 case 'p': /* port number */
252 if (!is_integer (optarg)) 239 if (!is_integer(optarg))
253 usage_va(_("Port must be a positive integer")); 240 usage_va(_("Port must be a positive integer"));
254 comm_append("-p"); 241 comm_append("-p");
255 comm_append(optarg); 242 comm_append(optarg);
256 break; 243 break;
257 case 'O': /* output file */ 244 case 'O': /* output file */
258 outputfile = optarg; 245 outputfile = optarg;
259 passive = true; 246 passive = true;
260 break; 247 break;
261 case 's': /* description of service to check */ 248 case 's': /* description of service to check */
262 p1 = optarg; 249 p1 = optarg;
263 service = realloc (service, (++services) * sizeof(char *)); 250 service = realloc(service, (++services) * sizeof(char *));
264 while ((p2 = index (p1, ':'))) { 251 while ((p2 = index(p1, ':'))) {
265 *p2 = '\0'; 252 *p2 = '\0';
266 service[services - 1] = p1; 253 service[services - 1] = p1;
267 service = realloc (service, (++services) * sizeof(char *)); 254 service = realloc(service, (++services) * sizeof(char *));
268 p1 = p2 + 1; 255 p1 = p2 + 1;
269 } 256 }
270 service[services - 1] = p1; 257 service[services - 1] = p1;
271 break; 258 break;
272 case 'n': /* short name of host in the monitoring configuration */ 259 case 'n': /* short name of host in the monitoring configuration */
273 host_shortname = optarg; 260 host_shortname = optarg;
274 break; 261 break;
275 262
@@ -277,67 +264,67 @@ process_arguments (int argc, char **argv)
277 comm_append("-l"); 264 comm_append("-l");
278 comm_append(optarg); 265 comm_append(optarg);
279 break; 266 break;
280 case 'l': /* login name */ 267 case 'l': /* login name */
281 comm_append("-l"); 268 comm_append("-l");
282 comm_append(optarg); 269 comm_append(optarg);
283 break; 270 break;
284 case 'i': /* identity */ 271 case 'i': /* identity */
285 comm_append("-i"); 272 comm_append("-i");
286 comm_append(optarg); 273 comm_append(optarg);
287 break; 274 break;
288 275
289 case '1': /* Pass these switches directly to ssh */ 276 case '1': /* Pass these switches directly to ssh */
290 comm_append("-1"); 277 comm_append("-1");
291 break; 278 break;
292 case '2': /* 1 to force version 1, 2 to force version 2 */ 279 case '2': /* 1 to force version 1, 2 to force version 2 */
293 comm_append("-2"); 280 comm_append("-2");
294 break; 281 break;
295 case '4': /* -4 for IPv4 */ 282 case '4': /* -4 for IPv4 */
296 comm_append("-4"); 283 comm_append("-4");
297 break; 284 break;
298 case '6': /* -6 for IPv6 */ 285 case '6': /* -6 for IPv6 */
299 comm_append("-6"); 286 comm_append("-6");
300 break; 287 break;
301 case 'f': /* fork to background */ 288 case 'f': /* fork to background */
302 comm_append("-f"); 289 comm_append("-f");
303 break; 290 break;
304 case 'C': /* Command for remote machine */ 291 case 'C': /* Command for remote machine */
305 commands++; 292 commands++;
306 if (commands > 1) 293 if (commands > 1)
307 xasprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); 294 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
308 xasprintf (&remotecmd, "%s%s", remotecmd, optarg); 295 xasprintf(&remotecmd, "%s%s", remotecmd, optarg);
309 break; 296 break;
310 case 'S': /* skip n (or all) lines on stdout */ 297 case 'S': /* skip n (or all) lines on stdout */
311 if (optarg == NULL) 298 if (optarg == NULL)
312 skip_stdout = -1; /* skip all output on stdout */ 299 skip_stdout = -1; /* skip all output on stdout */
313 else if (!is_integer (optarg)) 300 else if (!is_integer(optarg))
314 usage_va(_("skip-stdout argument must be an integer")); 301 usage_va(_("skip-stdout argument must be an integer"));
315 else 302 else
316 skip_stdout = atoi (optarg); 303 skip_stdout = atoi(optarg);
317 break; 304 break;
318 case 'E': /* skip n (or all) lines on stderr */ 305 case 'E': /* skip n (or all) lines on stderr */
319 if (optarg == NULL) 306 if (optarg == NULL)
320 skip_stderr = -1; /* skip all output on stderr */ 307 skip_stderr = -1; /* skip all output on stderr */
321 else if (!is_integer (optarg)) 308 else if (!is_integer(optarg))
322 usage_va(_("skip-stderr argument must be an integer")); 309 usage_va(_("skip-stderr argument must be an integer"));
323 else 310 else
324 skip_stderr = atoi (optarg); 311 skip_stderr = atoi(optarg);
325 break; 312 break;
326 case 'W': /* exit with warning if there is an output on stderr */ 313 case 'W': /* exit with warning if there is an output on stderr */
327 warn_on_stderr = 1; 314 warn_on_stderr = 1;
328 break; 315 break;
329 case 'o': /* Extra options for the ssh command */ 316 case 'o': /* Extra options for the ssh command */
330 comm_append("-o"); 317 comm_append("-o");
331 comm_append(optarg); 318 comm_append(optarg);
332 break; 319 break;
333 case 'q': /* Tell the ssh command to be quiet */ 320 case 'q': /* Tell the ssh command to be quiet */
334 comm_append("-q"); 321 comm_append("-q");
335 break; 322 break;
336 case 'F': /* ssh configfile */ 323 case 'F': /* ssh configfile */
337 comm_append("-F"); 324 comm_append("-F");
338 comm_append(optarg); 325 comm_append(optarg);
339 break; 326 break;
340 default: /* help */ 327 default: /* help */
341 usage5(); 328 usage5();
342 } 329 }
343 } 330 }
@@ -345,7 +332,7 @@ process_arguments (int argc, char **argv)
345 c = optind; 332 c = optind;
346 if (hostname == NULL) { 333 if (hostname == NULL) {
347 if (c <= argc) { 334 if (c <= argc) {
348 die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); 335 die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
349 } 336 }
350 hostname = argv[c++]; 337 hostname = argv[c++];
351 } 338 }
@@ -353,143 +340,130 @@ process_arguments (int argc, char **argv)
353 if (strlen(remotecmd) == 0) { 340 if (strlen(remotecmd) == 0) {
354 for (; c < argc; c++) 341 for (; c < argc; c++)
355 if (strlen(remotecmd) > 0) 342 if (strlen(remotecmd) > 0)
356 xasprintf (&remotecmd, "%s %s", remotecmd, argv[c]); 343 xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]);
357 else 344 else
358 xasprintf (&remotecmd, "%s", argv[c]); 345 xasprintf(&remotecmd, "%s", argv[c]);
359 } 346 }
360 347
361 if (commands > 1 || passive) 348 if (commands > 1 || passive)
362 xasprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); 349 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
363 350
364 if (remotecmd == NULL || strlen (remotecmd) <= 1) 351 if (remotecmd == NULL || strlen(remotecmd) <= 1)
365 usage_va(_("No remotecmd")); 352 usage_va(_("No remotecmd"));
366 353
367 comm_append(hostname); 354 comm_append(hostname);
368 comm_append(remotecmd); 355 comm_append(remotecmd);
369 356
370 return validate_arguments (); 357 return validate_arguments();
371} 358}
372 359
373 360void comm_append(const char *str) {
374void
375comm_append (const char *str)
376{
377 361
378 if (++commargc > NP_MAXARGS) 362 if (++commargc > NP_MAXARGS)
379 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); 363 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS);
380 364
381 if ((commargv = (char **)realloc(commargv, (commargc+1) * sizeof(char *))) == NULL) 365 if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL)
382 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); 366 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n"));
383 367
384 commargv[commargc-1] = strdup(str); 368 commargv[commargc - 1] = strdup(str);
385 commargv[commargc] = NULL; 369 commargv[commargc] = NULL;
386
387} 370}
388 371
389int 372int validate_arguments(void) {
390validate_arguments (void)
391{
392 if (remotecmd == NULL || hostname == NULL) 373 if (remotecmd == NULL || hostname == NULL)
393 return ERROR; 374 return ERROR;
394 375
395 if (passive && commands != services) 376 if (passive && commands != services)
396 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); 377 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
397 378
398 if (passive && host_shortname == NULL) 379 if (passive && host_shortname == NULL)
399 die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); 380 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname);
400 381
401 return OK; 382 return OK;
402} 383}
403 384
404 385void print_help(void) {
405void 386 print_revision(progname, NP_VERSION);
406print_help (void) 387
407{ 388 printf("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
408 print_revision (progname, NP_VERSION); 389 printf(COPYRIGHT, copyright, email);
409 390
410 printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"); 391 printf(_("This plugin uses SSH to execute commands on a remote host"));
411 printf (COPYRIGHT, copyright, email); 392
412 393 printf("\n\n");
413 printf (_("This plugin uses SSH to execute commands on a remote host")); 394
414 395 print_usage();
415 printf ("\n\n"); 396
416 397 printf(UT_HELP_VRSN);
417 print_usage (); 398
418 399 printf(UT_EXTRA_OPTS);
419 printf (UT_HELP_VRSN); 400
420 401 printf(UT_HOST_PORT, 'p', "none");
421 printf (UT_EXTRA_OPTS); 402
422 403 printf(UT_IPv46);
423 printf (UT_HOST_PORT, 'p', "none"); 404
424 405 printf(" %s\n", "-1, --proto1");
425 printf (UT_IPv46); 406 printf(" %s\n", _("tell ssh to use Protocol 1 [optional]"));
426 407 printf(" %s\n", "-2, --proto2");
427 printf (" %s\n", "-1, --proto1"); 408 printf(" %s\n", _("tell ssh to use Protocol 2 [optional]"));
428 printf (" %s\n", _("tell ssh to use Protocol 1 [optional]")); 409 printf(" %s\n", "-S, --skip-stdout[=n]");
429 printf (" %s\n", "-2, --proto2"); 410 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
430 printf (" %s\n", _("tell ssh to use Protocol 2 [optional]")); 411 printf(" %s\n", "-E, --skip-stderr[=n]");
431 printf (" %s\n", "-S, --skip-stdout[=n]"); 412 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
432 printf (" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]")); 413 printf(" %s\n", "-W, --warn-on-stderr]");
433 printf (" %s\n", "-E, --skip-stderr[=n]"); 414 printf(" %s\n", _("Exit with an warning, if there is an output on STDERR"));
434 printf (" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]")); 415 printf(" %s\n", "-f");
435 printf (" %s\n", "-W, --warn-on-stderr]"); 416 printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed"));
436 printf (" %s\n", _("Exit with an warning, if there is an output on STDERR")); 417 printf(" %s\n", "-C, --command='COMMAND STRING'");
437 printf (" %s\n", "-f"); 418 printf(" %s\n", _("command to execute on the remote machine"));
438 printf (" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed")); 419 printf(" %s\n", "-l, --logname=USERNAME");
439 printf (" %s\n","-C, --command='COMMAND STRING'"); 420 printf(" %s\n", _("SSH user name on remote host [optional]"));
440 printf (" %s\n", _("command to execute on the remote machine")); 421 printf(" %s\n", "-i, --identity=KEYFILE");
441 printf (" %s\n","-l, --logname=USERNAME"); 422 printf(" %s\n", _("identity of an authorized key [optional]"));
442 printf (" %s\n", _("SSH user name on remote host [optional]")); 423 printf(" %s\n", "-O, --output=FILE");
443 printf (" %s\n","-i, --identity=KEYFILE"); 424 printf(" %s\n", _("external command file for monitoring [optional]"));
444 printf (" %s\n", _("identity of an authorized key [optional]")); 425 printf(" %s\n", "-s, --services=LIST");
445 printf (" %s\n","-O, --output=FILE"); 426 printf(" %s\n", _("list of monitoring service names, separated by ':' [optional]"));
446 printf (" %s\n", _("external command file for monitoring [optional]")); 427 printf(" %s\n", "-n, --name=NAME");
447 printf (" %s\n","-s, --services=LIST"); 428 printf(" %s\n", _("short name of host in the monitoring configuration [optional]"));
448 printf (" %s\n", _("list of monitoring service names, separated by ':' [optional]")); 429 printf(" %s\n", "-o, --ssh-option=OPTION");
449 printf (" %s\n","-n, --name=NAME"); 430 printf(" %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
450 printf (" %s\n", _("short name of host in the monitoring configuration [optional]")); 431 printf(" %s\n", "-F, --configfile");
451 printf (" %s\n","-o, --ssh-option=OPTION"); 432 printf(" %s\n", _("Tell ssh to use this configfile [optional]"));
452 printf (" %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]")); 433 printf(" %s\n", "-q, --quiet");
453 printf (" %s\n","-F, --configfile"); 434 printf(" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
454 printf (" %s\n", _("Tell ssh to use this configfile [optional]")); 435 printf(UT_WARN_CRIT);
455 printf (" %s\n","-q, --quiet"); 436 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
456 printf (" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]")); 437 printf(" %s\n", "-U, --unknown-timeout");
457 printf (UT_WARN_CRIT); 438 printf(" %s\n", _("Make connection problems return UNKNOWN instead of CRITICAL"));
458 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 439 printf(UT_VERBOSE);
459 printf (" %s\n","-U, --unknown-timeout"); 440 printf("\n");
460 printf (" %s\n", _("Make connection problems return UNKNOWN instead of CRITICAL")); 441 printf(" %s\n", _("The most common mode of use is to refer to a local identity file with"));
461 printf (UT_VERBOSE); 442 printf(" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
443 printf(" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
444 printf(" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
445 printf(" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
446 printf(" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
447 printf(" %s\n", _("execute additional commands as proxy"));
448 printf("\n");
449 printf(" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
450 printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
462 printf("\n"); 451 printf("\n");
463 printf (" %s\n", _("The most common mode of use is to refer to a local identity file with")); 452 printf("%s\n", _("Examples:"));
464 printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null")); 453 printf(" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
465 printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys")); 454 printf(" %s\n", "$ cat /tmp/foo");
466 printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running")); 455 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
467 printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks")); 456 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
468 printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can")); 457 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
469 printf (" %s\n", _("execute additional commands as proxy"));
470 printf("\n");
471 printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
472 printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
473 printf ("\n");
474 printf ("%s\n", _("Examples:"));
475 printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
476 printf (" %s\n", "$ cat /tmp/foo");
477 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
478 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
479 printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
480 458
481 printf(UT_SUPPORT); 459 printf(UT_SUPPORT);
482} 460}
483 461
484 462void print_usage(void) {
485 463 printf("%s\n", _("Usage:"));
486void 464 printf(" %s -H <host> -C <command> [-fqvU] [-1|-2] [-4|-6]\n"
487print_usage (void) 465 " [-S [lines]] [-E [lines]] [-W] [-t timeout] [-i identity]\n"
488{ 466 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
489 printf ("%s\n", _("Usage:")); 467 " [-p port] [-o ssh-option] [-F configfile]\n",
490 printf (" %s -H <host> -C <command> [-fqvU] [-1|-2] [-4|-6]\n" 468 progname);
491 " [-S [lines]] [-E [lines]] [-W] [-t timeout] [-i identity]\n"
492 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
493 " [-p port] [-o ssh-option] [-F configfile]\n",
494 progname);
495} 469}