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.c439
1 files changed, 253 insertions, 186 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c
index 905b2393..a43c0d34 100644
--- a/plugins/check_by_ssh.c
+++ b/plugins/check_by_ssh.c
@@ -32,48 +32,29 @@ const char *email = "devel@monitoring-plugins.org";
32 32
33#include "common.h" 33#include "common.h"
34#include "utils.h" 34#include "utils.h"
35#include "netutils.h"
36#include "utils_cmd.h" 35#include "utils_cmd.h"
36#include "check_by_ssh.d/config.h"
37#include "states.h"
37 38
38#ifndef NP_MAXARGS 39#ifndef NP_MAXARGS
39# define NP_MAXARGS 1024 40# define NP_MAXARGS 1024
40#endif 41#endif
41 42
42static int process_arguments(int /*argc*/, char ** /*argv*/); 43typedef struct {
43static int validate_arguments(void); 44 int errorcode;
44static void comm_append(const char * /*str*/); 45 check_by_ssh_config config;
46} check_by_ssh_config_wrapper;
47static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
48static check_by_ssh_config_wrapper
49 validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/);
50
51static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/);
45static void print_help(void); 52static void print_help(void);
46void print_usage(void); 53void print_usage(void);
47 54
48static unsigned int commands = 0;
49static unsigned int services = 0;
50static int skip_stdout = 0;
51static int skip_stderr = 0;
52static int warn_on_stderr = 0;
53static bool unknown_timeout = false;
54static char *remotecmd = NULL;
55static char **commargv = NULL;
56static int commargc = 0;
57static char *hostname = NULL;
58static char *outputfile = NULL;
59static char *host_shortname = NULL;
60static char **service;
61static bool passive = false;
62static bool verbose = false; 55static bool verbose = false;
63 56
64int main(int argc, char **argv) { 57int main(int argc, char **argv) {
65
66 char *status_text;
67 int cresult;
68 int result = STATE_UNKNOWN;
69 time_t local_time;
70 FILE *file_pointer = NULL;
71 output chld_out;
72 output chld_err;
73
74 remotecmd = "";
75 comm_append(SSH_COMMAND);
76
77 setlocale(LC_ALL, ""); 58 setlocale(LC_ALL, "");
78 bindtextdomain(PACKAGE, LOCALEDIR); 59 bindtextdomain(PACKAGE, LOCALEDIR);
79 textdomain(PACKAGE); 60 textdomain(PACKAGE);
@@ -81,9 +62,14 @@ int main(int argc, char **argv) {
81 /* Parse extra opts if any */ 62 /* Parse extra opts if any */
82 argv = np_extra_opts(&argc, argv, progname); 63 argv = np_extra_opts(&argc, argv, progname);
83 64
65 check_by_ssh_config_wrapper tmp_config = process_arguments(argc, argv);
66
84 /* process arguments */ 67 /* process arguments */
85 if (process_arguments(argc, argv) == ERROR) 68 if (tmp_config.errorcode == ERROR) {
86 usage_va(_("Could not parse arguments")); 69 usage_va(_("Could not parse arguments"));
70 }
71
72 const check_by_ssh_config config = tmp_config.config;
87 73
88 /* Set signal handling and alarm timeout */ 74 /* Set signal handling and alarm timeout */
89 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { 75 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
@@ -93,47 +79,67 @@ int main(int argc, char **argv) {
93 79
94 /* run the command */ 80 /* run the command */
95 if (verbose) { 81 if (verbose) {
96 printf("Command: %s\n", commargv[0]); 82 printf("Command: %s\n", config.cmd.commargv[0]);
97 for (int i = 1; i < commargc; i++) 83 for (int i = 1; i < config.cmd.commargc; i++) {
98 printf("Argument %i: %s\n", i, commargv[i]); 84 printf("Argument %i: %s\n", i, config.cmd.commargv[i]);
85 }
99 } 86 }
100 87
101 result = cmd_run_array(commargv, &chld_out, &chld_err, 0); 88 output chld_out;
89 output chld_err;
90 mp_state_enum result = cmd_run_array(config.cmd.commargv, &chld_out, &chld_err, 0);
102 91
103 /* SSH returns 255 if connection attempt fails; include the first line of error output */ 92 /* SSH returns 255 if connection attempt fails; include the first line of error output */
104 if (result == 255 && unknown_timeout) { 93 if (result == 255 && config.unknown_timeout) {
105 printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); 94 printf(_("SSH connection failed: %s\n"),
95 chld_err.lines > 0 ? chld_err.line[0] : "(no error output)");
106 return STATE_UNKNOWN; 96 return STATE_UNKNOWN;
107 } 97 }
108 98
109 if (verbose) { 99 if (verbose) {
110 for (size_t i = 0; i < chld_out.lines; i++) 100 for (size_t i = 0; i < chld_out.lines; i++) {
111 printf("stdout: %s\n", chld_out.line[i]); 101 printf("stdout: %s\n", chld_out.line[i]);
112 for (size_t i = 0; i < chld_err.lines; i++) 102 }
103 for (size_t i = 0; i < chld_err.lines; i++) {
113 printf("stderr: %s\n", chld_err.line[i]); 104 printf("stderr: %s\n", chld_err.line[i]);
105 }
114 } 106 }
115 107
116 if (skip_stdout == -1) /* --skip-stdout specified without argument */ 108 size_t skip_stdout = 0;
109 if (config.skip_stdout == -1) { /* --skip-stdout specified without argument */
117 skip_stdout = chld_out.lines; 110 skip_stdout = chld_out.lines;
118 if (skip_stderr == -1) /* --skip-stderr specified without argument */ 111 } else {
112 skip_stdout = config.skip_stdout;
113 }
114
115 size_t skip_stderr = 0;
116 if (config.skip_stderr == -1) { /* --skip-stderr specified without argument */
119 skip_stderr = chld_err.lines; 117 skip_stderr = chld_err.lines;
118 } else {
119 skip_stderr = config.skip_stderr;
120 }
120 121
121 /* UNKNOWN or worse if (non-skipped) output found on stderr */ 122 /* Allow UNKNOWN or WARNING state for (non-skipped) output found on stderr */
122 if (chld_err.lines > (size_t)skip_stderr) { 123 if (chld_err.lines > (size_t)skip_stderr && (config.unknown_on_stderr || config.warn_on_stderr)) {
123 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); 124 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]);
124 if (warn_on_stderr) 125 if (config.unknown_on_stderr) {
126 return max_state_alt(result, STATE_UNKNOWN);
127 } else if (config.warn_on_stderr) {
125 return max_state_alt(result, STATE_WARNING); 128 return max_state_alt(result, STATE_WARNING);
126 return max_state_alt(result, STATE_UNKNOWN); 129 }
127 } 130 }
128 131
129 /* this is simple if we're not supposed to be passive. 132 /* this is simple if we're not supposed to be passive.
130 * Wrap up quickly and keep the tricks below */ 133 * Wrap up quickly and keep the tricks below */
131 if (!passive) { 134 if (!config.passive) {
132 if (chld_out.lines > (size_t)skip_stdout) 135 if (chld_out.lines > (size_t)skip_stdout) {
133 for (size_t i = skip_stdout; i < chld_out.lines; i++) 136 for (size_t i = skip_stdout; i < chld_out.lines; i++) {
134 puts(chld_out.line[i]); 137 puts(chld_out.line[i]);
135 else 138 }
136 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result); 139 } else {
140 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
141 state_text(result), config.remotecmd, result);
142 }
137 return result; /* return error status from remote command */ 143 return result; /* return error status from remote command */
138 } 144 }
139 145
@@ -142,78 +148,95 @@ int main(int argc, char **argv) {
142 */ 148 */
143 149
144 /* process output */ 150 /* process output */
145 if (!(file_pointer = fopen(outputfile, "a"))) { 151 FILE *file_pointer = NULL;
146 printf(_("SSH WARNING: could not open %s\n"), outputfile); 152 if (!(file_pointer = fopen(config.outputfile, "a"))) {
153 printf(_("SSH WARNING: could not open %s\n"), config.outputfile);
147 exit(STATE_UNKNOWN); 154 exit(STATE_UNKNOWN);
148 } 155 }
149 156
150 local_time = time(NULL); 157 time_t local_time = time(NULL);
151 commands = 0; 158 unsigned int commands = 0;
159 char *status_text;
160 int cresult;
152 for (size_t i = skip_stdout; i < chld_out.lines; i++) { 161 for (size_t i = skip_stdout; i < chld_out.lines; i++) {
153 status_text = chld_out.line[i++]; 162 status_text = chld_out.line[i++];
154 if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) 163 if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) {
155 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); 164 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
165 }
156 166
157 if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { 167 if (config.service[commands] && status_text &&
158 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++], 168 sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) {
159 cresult, status_text); 169 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
170 (int)local_time, config.host_shortname, config.service[commands++], cresult,
171 status_text);
160 } 172 }
161 } 173 }
162 174
163 /* Multiple commands and passive checking should always return OK */ 175 /* Multiple commands and passive checking should always return OK */
164 return result; 176 exit(result);
165} 177}
166 178
167/* process command-line arguments */ 179/* process command-line arguments */
168int process_arguments(int argc, char **argv) { 180check_by_ssh_config_wrapper process_arguments(int argc, char **argv) {
169 int c; 181 static struct option longopts[] = {
170 char *p1; 182 {"version", no_argument, 0, 'V'},
171 char *p2; 183 {"help", no_argument, 0, 'h'},
184 {"verbose", no_argument, 0, 'v'},
185 {"fork", no_argument, 0, 'f'},
186 {"timeout", required_argument, 0, 't'},
187 {"unknown-timeout", no_argument, 0, 'U'},
188 {"host", required_argument, 0, 'H'}, /* backward compatibility */
189 {"hostname", required_argument, 0, 'H'},
190 {"port", required_argument, 0, 'p'},
191 {"output", required_argument, 0, 'O'},
192 {"name", required_argument, 0, 'n'},
193 {"services", required_argument, 0, 's'},
194 {"identity", required_argument, 0, 'i'},
195 {"user", required_argument, 0, 'u'}, /* backwards compatibility */
196 {"logname", required_argument, 0, 'l'},
197 {"command", required_argument, 0, 'C'},
198 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
199 {"skip-stdout", optional_argument, 0, 'S'},
200 {"skip-stderr", optional_argument, 0, 'E'},
201 {"unknown-on-stderr", no_argument, 0, 'e'},
202 {"warn-on-stderr", no_argument, 0, 'W'},
203 {"proto1", no_argument, 0, '1'},
204 {"proto2", no_argument, 0, '2'},
205 {"use-ipv4", no_argument, 0, '4'},
206 {"use-ipv6", no_argument, 0, '6'},
207 {"ssh-option", required_argument, 0, 'o'},
208 {"quiet", no_argument, 0, 'q'},
209 {"configfile", optional_argument, 0, 'F'},
210 {0, 0, 0, 0}};
211
212 check_by_ssh_config_wrapper result = {
213 .errorcode = OK,
214 .config = check_by_ssh_config_init(),
215 };
216
217 if (argc < 2) {
218 result.errorcode = ERROR;
219 return result;
220 }
221
222 for (int index = 1; index < argc; index++) {
223 if (strcmp("-to", argv[index]) == 0) {
224 strcpy(argv[index], "-t");
225 }
226 }
227
228 result.config.cmd = comm_append(result.config.cmd, SSH_COMMAND);
172 229
173 int option = 0; 230 int option = 0;
174 static struct option longopts[] = {{"version", no_argument, 0, 'V'}, 231 while (true) {
175 {"help", no_argument, 0, 'h'}, 232 int opt_index =
176 {"verbose", no_argument, 0, 'v'}, 233 getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option);
177 {"fork", no_argument, 0, 'f'}, 234
178 {"timeout", required_argument, 0, 't'}, 235 if (opt_index == -1 || opt_index == EOF) {
179 {"unknown-timeout", no_argument, 0, 'U'},
180 {"host", required_argument, 0, 'H'}, /* backward compatibility */
181 {"hostname", required_argument, 0, 'H'},
182 {"port", required_argument, 0, 'p'},
183 {"output", required_argument, 0, 'O'},
184 {"name", required_argument, 0, 'n'},
185 {"services", required_argument, 0, 's'},
186 {"identity", required_argument, 0, 'i'},
187 {"user", required_argument, 0, 'u'},
188 {"logname", required_argument, 0, 'l'},
189 {"command", required_argument, 0, 'C'},
190 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
191 {"skip-stdout", optional_argument, 0, 'S'},
192 {"skip-stderr", optional_argument, 0, 'E'},
193 {"warn-on-stderr", no_argument, 0, 'W'},
194 {"proto1", no_argument, 0, '1'},
195 {"proto2", no_argument, 0, '2'},
196 {"use-ipv4", no_argument, 0, '4'},
197 {"use-ipv6", no_argument, 0, '6'},
198 {"ssh-option", required_argument, 0, 'o'},
199 {"quiet", no_argument, 0, 'q'},
200 {"configfile", optional_argument, 0, 'F'},
201 {0, 0, 0, 0}};
202
203 if (argc < 2)
204 return ERROR;
205
206 for (c = 1; c < argc; c++)
207 if (strcmp("-to", argv[c]) == 0)
208 strcpy(argv[c], "-t");
209
210 while (1) {
211 c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option);
212
213 if (c == -1 || c == EOF)
214 break; 236 break;
237 }
215 238
216 switch (c) { 239 switch (opt_index) {
217 case 'V': /* version */ 240 case 'V': /* version */
218 print_revision(progname, NP_VERSION); 241 print_revision(progname, NP_VERSION);
219 exit(STATE_UNKNOWN); 242 exit(STATE_UNKNOWN);
@@ -224,162 +247,202 @@ int process_arguments(int argc, char **argv) {
224 verbose = true; 247 verbose = true;
225 break; 248 break;
226 case 't': /* timeout period */ 249 case 't': /* timeout period */
227 if (!is_integer(optarg)) 250 if (!is_integer(optarg)) {
228 usage_va(_("Timeout interval must be a positive integer")); 251 usage_va(_("Timeout interval must be a positive integer"));
229 else 252 } else {
230 timeout_interval = atoi(optarg); 253 timeout_interval = atoi(optarg);
254 }
231 break; 255 break;
232 case 'U': 256 case 'U':
233 unknown_timeout = true; 257 result.config.unknown_timeout = true;
234 break; 258 break;
235 case 'H': /* host */ 259 case 'H': /* host */
236 hostname = optarg; 260 result.config.hostname = optarg;
237 break; 261 break;
238 case 'p': /* port number */ 262 case 'p': /* port number */
239 if (!is_integer(optarg)) 263 if (!is_integer(optarg)) {
240 usage_va(_("Port must be a positive integer")); 264 usage_va(_("Port must be a positive integer"));
241 comm_append("-p"); 265 }
242 comm_append(optarg); 266 result.config.cmd = comm_append(result.config.cmd, "-p");
267 result.config.cmd = comm_append(result.config.cmd, optarg);
243 break; 268 break;
244 case 'O': /* output file */ 269 case 'O': /* output file */
245 outputfile = optarg; 270 result.config.outputfile = optarg;
246 passive = true; 271 result.config.passive = true;
247 break; 272 break;
248 case 's': /* description of service to check */ 273 case 's': /* description of service to check */ {
274 char *p1;
275 char *p2;
276
249 p1 = optarg; 277 p1 = optarg;
250 service = realloc(service, (++services) * sizeof(char *)); 278 result.config.service = realloc(result.config.service,
279 (++result.config.number_of_services) * sizeof(char *));
251 while ((p2 = index(p1, ':'))) { 280 while ((p2 = index(p1, ':'))) {
252 *p2 = '\0'; 281 *p2 = '\0';
253 service[services - 1] = p1; 282 result.config.service[result.config.number_of_services - 1] = p1;
254 service = realloc(service, (++services) * sizeof(char *)); 283 result.config.service = realloc(
284 result.config.service, (++result.config.number_of_services) * sizeof(char *));
255 p1 = p2 + 1; 285 p1 = p2 + 1;
256 } 286 }
257 service[services - 1] = p1; 287 result.config.service[result.config.number_of_services - 1] = p1;
258 break; 288 break;
259 case 'n': /* short name of host in the monitoring configuration */ 289 case 'n': /* short name of host in the monitoring configuration */
260 host_shortname = optarg; 290 result.config.host_shortname = optarg;
261 break; 291 } break;
262
263 case 'u': 292 case 'u':
264 comm_append("-l"); 293 result.config.cmd = comm_append(result.config.cmd, "-l");
265 comm_append(optarg); 294 result.config.cmd = comm_append(result.config.cmd, optarg);
266 break; 295 break;
267 case 'l': /* login name */ 296 case 'l': /* login name */
268 comm_append("-l"); 297 result.config.cmd = comm_append(result.config.cmd, "-l");
269 comm_append(optarg); 298 result.config.cmd = comm_append(result.config.cmd, optarg);
270 break; 299 break;
271 case 'i': /* identity */ 300 case 'i': /* identity */
272 comm_append("-i"); 301 result.config.cmd = comm_append(result.config.cmd, "-i");
273 comm_append(optarg); 302 result.config.cmd = comm_append(result.config.cmd, optarg);
274 break; 303 break;
275 304
276 case '1': /* Pass these switches directly to ssh */ 305 case '1': /* Pass these switches directly to ssh */
277 comm_append("-1"); 306 result.config.cmd = comm_append(result.config.cmd, "-1");
278 break; 307 break;
279 case '2': /* 1 to force version 1, 2 to force version 2 */ 308 case '2': /* 1 to force version 1, 2 to force version 2 */
280 comm_append("-2"); 309 result.config.cmd = comm_append(result.config.cmd, "-2");
281 break; 310 break;
282 case '4': /* -4 for IPv4 */ 311 case '4': /* -4 for IPv4 */
283 comm_append("-4"); 312 result.config.cmd = comm_append(result.config.cmd, "-4");
284 break; 313 break;
285 case '6': /* -6 for IPv6 */ 314 case '6': /* -6 for IPv6 */
286 comm_append("-6"); 315 result.config.cmd = comm_append(result.config.cmd, "-6");
287 break; 316 break;
288 case 'f': /* fork to background */ 317 case 'f': /* fork to background */
289 comm_append("-f"); 318 result.config.cmd = comm_append(result.config.cmd, "-f");
290 break; 319 break;
291 case 'C': /* Command for remote machine */ 320 case 'C': /* Command for remote machine */
292 commands++; 321 result.config.commands++;
293 if (commands > 1) 322 if (result.config.commands > 1) {
294 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); 323 xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;",
295 xasprintf(&remotecmd, "%s%s", remotecmd, optarg); 324 result.config.remotecmd);
325 }
326 xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg);
296 break; 327 break;
297 case 'S': /* skip n (or all) lines on stdout */ 328 case 'S': /* skip n (or all) lines on stdout */
298 if (optarg == NULL) 329 if (optarg == NULL) {
299 skip_stdout = -1; /* skip all output on stdout */ 330 result.config.skip_stdout = -1; /* skip all output on stdout */
300 else if (!is_integer(optarg)) 331 } else if (!is_integer(optarg)) {
301 usage_va(_("skip-stdout argument must be an integer")); 332 usage_va(_("skip-stdout argument must be an integer"));
302 else 333 } else {
303 skip_stdout = atoi(optarg); 334 result.config.skip_stdout = atoi(optarg);
335 }
304 break; 336 break;
305 case 'E': /* skip n (or all) lines on stderr */ 337 case 'E': /* skip n (or all) lines on stderr */
306 if (optarg == NULL) 338 if (optarg == NULL) {
307 skip_stderr = -1; /* skip all output on stderr */ 339 result.config.skip_stderr = -1; /* skip all output on stderr */
308 else if (!is_integer(optarg)) 340 } else if (!is_integer(optarg)) {
309 usage_va(_("skip-stderr argument must be an integer")); 341 usage_va(_("skip-stderr argument must be an integer"));
310 else 342 } else {
311 skip_stderr = atoi(optarg); 343 result.config.skip_stderr = atoi(optarg);
344 }
345 break;
346 case 'e': /* exit with unknown if there is an output on stderr */
347 result.config.unknown_on_stderr = true;
312 break; 348 break;
313 case 'W': /* exit with warning if there is an output on stderr */ 349 case 'W': /* exit with warning if there is an output on stderr */
314 warn_on_stderr = 1; 350 result.config.warn_on_stderr = true;
315 break; 351 break;
316 case 'o': /* Extra options for the ssh command */ 352 case 'o': /* Extra options for the ssh command */
317 comm_append("-o"); 353 result.config.cmd = comm_append(result.config.cmd, "-o");
318 comm_append(optarg); 354 result.config.cmd = comm_append(result.config.cmd, optarg);
319 break; 355 break;
320 case 'q': /* Tell the ssh command to be quiet */ 356 case 'q': /* Tell the ssh command to be quiet */
321 comm_append("-q"); 357 result.config.cmd = comm_append(result.config.cmd, "-q");
322 break; 358 break;
323 case 'F': /* ssh configfile */ 359 case 'F': /* ssh configfile */
324 comm_append("-F"); 360 result.config.cmd = comm_append(result.config.cmd, "-F");
325 comm_append(optarg); 361 result.config.cmd = comm_append(result.config.cmd, optarg);
326 break; 362 break;
327 default: /* help */ 363 default: /* help */
328 usage5(); 364 usage5();
329 } 365 }
330 } 366 }
331 367
332 c = optind; 368 int c = optind;
333 if (hostname == NULL) { 369 if (result.config.hostname == NULL) {
334 if (c <= argc) { 370 if (c <= argc) {
335 die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); 371 die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
336 } 372 }
337 hostname = argv[c++]; 373 result.config.hostname = argv[c++];
338 } 374 }
339 375
340 if (strlen(remotecmd) == 0) { 376 if (strlen(result.config.remotecmd) == 0) {
341 for (; c < argc; c++) 377 for (; c < argc; c++) {
342 if (strlen(remotecmd) > 0) 378 if (strlen(result.config.remotecmd) > 0) {
343 xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]); 379 xasprintf(&result.config.remotecmd, "%s %s", result.config.remotecmd, argv[c]);
344 else 380 } else {
345 xasprintf(&remotecmd, "%s", argv[c]); 381 xasprintf(&result.config.remotecmd, "%s", argv[c]);
382 }
383 }
346 } 384 }
347 385
348 if (commands > 1 || passive) 386 if (result.config.commands > 1 || result.config.passive) {
349 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); 387 xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd);
388 }
350 389
351 if (remotecmd == NULL || strlen(remotecmd) <= 1) 390 if (result.config.remotecmd == NULL || strlen(result.config.remotecmd) <= 1) {
352 usage_va(_("No remotecmd")); 391 usage_va(_("No remotecmd"));
392 }
353 393
354 comm_append(hostname); 394 result.config.cmd = comm_append(result.config.cmd, result.config.hostname);
355 comm_append(remotecmd); 395 result.config.cmd = comm_append(result.config.cmd, result.config.remotecmd);
356 396
357 return validate_arguments(); 397 return validate_arguments(result);
358} 398}
359 399
360void comm_append(const char *str) { 400command_construct comm_append(command_construct cmd, const char *str) {
401
402 if (verbose) {
403 for (int i = 0; i < cmd.commargc; i++) {
404 printf("Current command: [%i] %s\n", i, cmd.commargv[i]);
405 }
406
407 printf("Appending: %s\n", str);
408 }
361 409
362 if (++commargc > NP_MAXARGS) 410 if (++cmd.commargc > NP_MAXARGS) {
363 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); 411 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS);
412 }
364 413
365 if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL) 414 if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) ==
415 NULL) {
366 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); 416 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n"));
417 }
418
419 cmd.commargv[cmd.commargc - 1] = strdup(str);
420 cmd.commargv[cmd.commargc] = NULL;
367 421
368 commargv[commargc - 1] = strdup(str); 422 return cmd;
369 commargv[commargc] = NULL;
370} 423}
371 424
372int validate_arguments(void) { 425check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper config_wrapper) {
373 if (remotecmd == NULL || hostname == NULL) 426 if (config_wrapper.config.remotecmd == NULL || config_wrapper.config.hostname == NULL) {
374 return ERROR; 427 config_wrapper.errorcode = ERROR;
428 return config_wrapper;
429 }
375 430
376 if (passive && commands != services) 431 if (config_wrapper.config.passive &&
377 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); 432 config_wrapper.config.commands != config_wrapper.config.number_of_services) {
433 die(STATE_UNKNOWN,
434 _("%s: In passive mode, you must provide a service name for each command.\n"),
435 progname);
436 }
378 437
379 if (passive && host_shortname == NULL) 438 if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) {
380 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); 439 die(STATE_UNKNOWN,
440 _("%s: In passive mode, you must provide the host short name from the monitoring "
441 "configs.\n"),
442 progname);
443 }
381 444
382 return OK; 445 return config_wrapper;
383} 446}
384 447
385void print_help(void) { 448void print_help(void) {
@@ -410,10 +473,13 @@ void print_help(void) {
410 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]")); 473 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
411 printf(" %s\n", "-E, --skip-stderr[=n]"); 474 printf(" %s\n", "-E, --skip-stderr[=n]");
412 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]")); 475 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
413 printf(" %s\n", "-W, --warn-on-stderr]"); 476 printf(" %s\n", "-e, --unknown-on-stderr");
414 printf(" %s\n", _("Exit with an warning, if there is an output on STDERR")); 477 printf(" %s\n", _("Exit with UNKNOWN, if there is output on STDERR"));
478 printf(" %s\n", "-W, --warn-on-stderr");
479 printf(" %s\n", _("Exit with WARNING, if there is output on STDERR"));
415 printf(" %s\n", "-f"); 480 printf(" %s\n", "-f");
416 printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed")); 481 printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always "
482 "return OK if ssh is executed"));
417 printf(" %s\n", "-C, --command='COMMAND STRING'"); 483 printf(" %s\n", "-C, --command='COMMAND STRING'");
418 printf(" %s\n", _("command to execute on the remote machine")); 484 printf(" %s\n", _("command to execute on the remote machine"));
419 printf(" %s\n", "-l, --logname=USERNAME"); 485 printf(" %s\n", "-l, --logname=USERNAME");
@@ -432,7 +498,6 @@ void print_help(void) {
432 printf(" %s\n", _("Tell ssh to use this configfile [optional]")); 498 printf(" %s\n", _("Tell ssh to use this configfile [optional]"));
433 printf(" %s\n", "-q, --quiet"); 499 printf(" %s\n", "-q, --quiet");
434 printf(" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]")); 500 printf(" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
435 printf(UT_WARN_CRIT);
436 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 501 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
437 printf(" %s\n", "-U, --unknown-timeout"); 502 printf(" %s\n", "-U, --unknown-timeout");
438 printf(" %s\n", _("Make connection problems return UNKNOWN instead of CRITICAL")); 503 printf(" %s\n", _("Make connection problems return UNKNOWN instead of CRITICAL"));
@@ -450,7 +515,9 @@ void print_help(void) {
450 printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)")); 515 printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
451 printf("\n"); 516 printf("\n");
452 printf("%s\n", _("Examples:")); 517 printf("%s\n", _("Examples:"));
453 printf(" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo"); 518 printf(
519 " %s\n",
520 "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
454 printf(" %s\n", "$ cat /tmp/foo"); 521 printf(" %s\n", "$ cat /tmp/foo");
455 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days"); 522 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
456 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days"); 523 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
@@ -462,7 +529,7 @@ void print_help(void) {
462void print_usage(void) { 529void print_usage(void) {
463 printf("%s\n", _("Usage:")); 530 printf("%s\n", _("Usage:"));
464 printf(" %s -H <host> -C <command> [-fqvU] [-1|-2] [-4|-6]\n" 531 printf(" %s -H <host> -C <command> [-fqvU] [-1|-2] [-4|-6]\n"
465 " [-S [lines]] [-E [lines]] [-W] [-t timeout] [-i identity]\n" 532 " [-S [lines]] [-E [lines]] [-e|-W] [-t timeout] [-i identity]\n"
466 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n" 533 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
467 " [-p port] [-o ssh-option] [-F configfile]\n", 534 " [-p port] [-o ssh-option] [-F configfile]\n",
468 progname); 535 progname);