summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/Makefile.am3
-rw-r--r--plugins/check_apt.d/config.h1
-rw-r--r--plugins/check_by_ssh.c265
-rw-r--r--plugins/check_by_ssh.d/config.h56
4 files changed, 197 insertions, 128 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 2ffb5fd0..3d5ad1a9 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -54,7 +54,8 @@ EXTRA_DIST = t \
54 check_dbi.d \ 54 check_dbi.d \
55 check_ssh.d \ 55 check_ssh.d \
56 check_dns.d \ 56 check_dns.d \
57 check_apt.d 57 check_apt.d \
58 check_by_ssh.d
58 59
59PLUGINHDRS = common.h 60PLUGINHDRS = common.h
60 61
diff --git a/plugins/check_apt.d/config.h b/plugins/check_apt.d/config.h
index 2c962e5a..981f4f42 100644
--- a/plugins/check_apt.d/config.h
+++ b/plugins/check_apt.d/config.h
@@ -33,7 +33,6 @@ check_apt_config check_apt_config_init() {
33 .list = false, 33 .list = false,
34 .packages_warning = 1, 34 .packages_warning = 1,
35 .update_opts = NULL, 35 .update_opts = NULL,
36 .update_opts = NULL,
37 .do_include = NULL, 36 .do_include = NULL,
38 .do_exclude = NULL, 37 .do_exclude = NULL,
39 .do_critical = NULL, 38 .do_critical = NULL,
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c
index 2ac7805d..2bc38d49 100644
--- a/plugins/check_by_ssh.c
+++ b/plugins/check_by_ssh.c
@@ -32,48 +32,28 @@ 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 validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/);
49
50static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/);
45static void print_help(void); 51static void print_help(void);
46void print_usage(void); 52void print_usage(void);
47 53
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; 54static bool verbose = false;
63 55
64int main(int argc, char **argv) { 56int 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, ""); 57 setlocale(LC_ALL, "");
78 bindtextdomain(PACKAGE, LOCALEDIR); 58 bindtextdomain(PACKAGE, LOCALEDIR);
79 textdomain(PACKAGE); 59 textdomain(PACKAGE);
@@ -81,11 +61,15 @@ int main(int argc, char **argv) {
81 /* Parse extra opts if any */ 61 /* Parse extra opts if any */
82 argv = np_extra_opts(&argc, argv, progname); 62 argv = np_extra_opts(&argc, argv, progname);
83 63
64 check_by_ssh_config_wrapper tmp_config = process_arguments(argc, argv);
65
84 /* process arguments */ 66 /* process arguments */
85 if (process_arguments(argc, argv) == ERROR) { 67 if (tmp_config.errorcode == ERROR) {
86 usage_va(_("Could not parse arguments")); 68 usage_va(_("Could not parse arguments"));
87 } 69 }
88 70
71 const check_by_ssh_config config = tmp_config.config;
72
89 /* Set signal handling and alarm timeout */ 73 /* Set signal handling and alarm timeout */
90 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { 74 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
91 usage_va(_("Cannot catch SIGALRM")); 75 usage_va(_("Cannot catch SIGALRM"));
@@ -94,16 +78,18 @@ int main(int argc, char **argv) {
94 78
95 /* run the command */ 79 /* run the command */
96 if (verbose) { 80 if (verbose) {
97 printf("Command: %s\n", commargv[0]); 81 printf("Command: %s\n", config.cmd.commargv[0]);
98 for (int i = 1; i < commargc; i++) { 82 for (int i = 1; i < config.cmd.commargc; i++) {
99 printf("Argument %i: %s\n", i, commargv[i]); 83 printf("Argument %i: %s\n", i, config.cmd.commargv[i]);
100 } 84 }
101 } 85 }
102 86
103 result = cmd_run_array(commargv, &chld_out, &chld_err, 0); 87 output chld_out;
88 output chld_err;
89 mp_state_enum result = cmd_run_array(config.cmd.commargv, &chld_out, &chld_err, 0);
104 90
105 /* SSH returns 255 if connection attempt fails; include the first line of error output */ 91 /* SSH returns 255 if connection attempt fails; include the first line of error output */
106 if (result == 255 && unknown_timeout) { 92 if (result == 255 && config.unknown_timeout) {
107 printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); 93 printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)");
108 return STATE_UNKNOWN; 94 return STATE_UNKNOWN;
109 } 95 }
@@ -117,17 +103,24 @@ int main(int argc, char **argv) {
117 } 103 }
118 } 104 }
119 105
120 if (skip_stdout == -1) { /* --skip-stdout specified without argument */ 106 size_t skip_stdout = 0;
107 if (config.skip_stdout == -1) { /* --skip-stdout specified without argument */
121 skip_stdout = chld_out.lines; 108 skip_stdout = chld_out.lines;
109 } else {
110 skip_stdout = config.skip_stdout;
122 } 111 }
123 if (skip_stderr == -1) { /* --skip-stderr specified without argument */ 112
113 size_t skip_stderr = 0;
114 if (config.skip_stderr == -1) { /* --skip-stderr specified without argument */
124 skip_stderr = chld_err.lines; 115 skip_stderr = chld_err.lines;
116 } else {
117 skip_stderr = config.skip_stderr;
125 } 118 }
126 119
127 /* UNKNOWN or worse if (non-skipped) output found on stderr */ 120 /* UNKNOWN or worse if (non-skipped) output found on stderr */
128 if (chld_err.lines > (size_t)skip_stderr) { 121 if (chld_err.lines > (size_t)skip_stderr) {
129 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); 122 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]);
130 if (warn_on_stderr) { 123 if (config.warn_on_stderr) {
131 return max_state_alt(result, STATE_WARNING); 124 return max_state_alt(result, STATE_WARNING);
132 } 125 }
133 return max_state_alt(result, STATE_UNKNOWN); 126 return max_state_alt(result, STATE_UNKNOWN);
@@ -135,13 +128,13 @@ int main(int argc, char **argv) {
135 128
136 /* this is simple if we're not supposed to be passive. 129 /* this is simple if we're not supposed to be passive.
137 * Wrap up quickly and keep the tricks below */ 130 * Wrap up quickly and keep the tricks below */
138 if (!passive) { 131 if (!config.passive) {
139 if (chld_out.lines > (size_t)skip_stdout) { 132 if (chld_out.lines > (size_t)skip_stdout) {
140 for (size_t i = skip_stdout; i < chld_out.lines; i++) { 133 for (size_t i = skip_stdout; i < chld_out.lines; i++) {
141 puts(chld_out.line[i]); 134 puts(chld_out.line[i]);
142 } 135 }
143 } else { 136 } else {
144 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result); 137 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), config.remotecmd, result);
145 } 138 }
146 return result; /* return error status from remote command */ 139 return result; /* return error status from remote command */
147 } 140 }
@@ -151,36 +144,34 @@ int main(int argc, char **argv) {
151 */ 144 */
152 145
153 /* process output */ 146 /* process output */
154 if (!(file_pointer = fopen(outputfile, "a"))) { 147 FILE *file_pointer = NULL;
155 printf(_("SSH WARNING: could not open %s\n"), outputfile); 148 if (!(file_pointer = fopen(config.outputfile, "a"))) {
149 printf(_("SSH WARNING: could not open %s\n"), config.outputfile);
156 exit(STATE_UNKNOWN); 150 exit(STATE_UNKNOWN);
157 } 151 }
158 152
159 local_time = time(NULL); 153 time_t local_time = time(NULL);
160 commands = 0; 154 unsigned int commands = 0;
155 char *status_text;
156 int cresult;
161 for (size_t i = skip_stdout; i < chld_out.lines; i++) { 157 for (size_t i = skip_stdout; i < chld_out.lines; i++) {
162 status_text = chld_out.line[i++]; 158 status_text = chld_out.line[i++];
163 if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) { 159 if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) {
164 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); 160 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
165 } 161 }
166 162
167 if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { 163 if (config.service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) {
168 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++], 164 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, config.host_shortname,
169 cresult, status_text); 165 config.service[commands++], cresult, status_text);
170 } 166 }
171 } 167 }
172 168
173 /* Multiple commands and passive checking should always return OK */ 169 /* Multiple commands and passive checking should always return OK */
174 return result; 170 exit(result);
175} 171}
176 172
177/* process command-line arguments */ 173/* process command-line arguments */
178int process_arguments(int argc, char **argv) { 174check_by_ssh_config_wrapper process_arguments(int argc, char **argv) {
179 int c;
180 char *p1;
181 char *p2;
182
183 int option = 0;
184 static struct option longopts[] = {{"version", no_argument, 0, 'V'}, 175 static struct option longopts[] = {{"version", no_argument, 0, 'V'},
185 {"help", no_argument, 0, 'h'}, 176 {"help", no_argument, 0, 'h'},
186 {"verbose", no_argument, 0, 'v'}, 177 {"verbose", no_argument, 0, 'v'},
@@ -210,24 +201,33 @@ int process_arguments(int argc, char **argv) {
210 {"configfile", optional_argument, 0, 'F'}, 201 {"configfile", optional_argument, 0, 'F'},
211 {0, 0, 0, 0}}; 202 {0, 0, 0, 0}};
212 203
204 check_by_ssh_config_wrapper result = {
205 .errorcode = OK,
206 .config = check_by_ssh_config_init(),
207 };
208
213 if (argc < 2) { 209 if (argc < 2) {
214 return ERROR; 210 result.errorcode = ERROR;
211 return result;
215 } 212 }
216 213
217 for (c = 1; c < argc; c++) { 214 for (int index = 1; index < argc; index++) {
218 if (strcmp("-to", argv[c]) == 0) { 215 if (strcmp("-to", argv[index]) == 0) {
219 strcpy(argv[c], "-t"); 216 strcpy(argv[index], "-t");
220 } 217 }
221 } 218 }
222 219
223 while (1) { 220 result.config.cmd = comm_append(result.config.cmd, SSH_COMMAND);
224 c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option);
225 221
226 if (c == -1 || c == EOF) { 222 int option = 0;
223 while (true) {
224 int opt_index = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option);
225
226 if (opt_index == -1 || opt_index == EOF) {
227 break; 227 break;
228 } 228 }
229 229
230 switch (c) { 230 switch (opt_index) {
231 case 'V': /* version */ 231 case 'V': /* version */
232 print_revision(progname, NP_VERSION); 232 print_revision(progname, NP_VERSION);
233 exit(STATE_UNKNOWN); 233 exit(STATE_UNKNOWN);
@@ -245,169 +245,182 @@ int process_arguments(int argc, char **argv) {
245 } 245 }
246 break; 246 break;
247 case 'U': 247 case 'U':
248 unknown_timeout = true; 248 result.config.unknown_timeout = true;
249 break; 249 break;
250 case 'H': /* host */ 250 case 'H': /* host */
251 hostname = optarg; 251 result.config.hostname = optarg;
252 break; 252 break;
253 case 'p': /* port number */ 253 case 'p': /* port number */
254 if (!is_integer(optarg)) { 254 if (!is_integer(optarg)) {
255 usage_va(_("Port must be a positive integer")); 255 usage_va(_("Port must be a positive integer"));
256 } 256 }
257 comm_append("-p"); 257 result.config.cmd = comm_append(result.config.cmd, "-p");
258 comm_append(optarg); 258 result.config.cmd = comm_append(result.config.cmd, optarg);
259 break; 259 break;
260 case 'O': /* output file */ 260 case 'O': /* output file */
261 outputfile = optarg; 261 result.config.outputfile = optarg;
262 passive = true; 262 result.config.passive = true;
263 break; 263 break;
264 case 's': /* description of service to check */ 264 case 's': /* description of service to check */ {
265 char *p1;
266 char *p2;
267
265 p1 = optarg; 268 p1 = optarg;
266 service = realloc(service, (++services) * sizeof(char *)); 269 result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *));
267 while ((p2 = index(p1, ':'))) { 270 while ((p2 = index(p1, ':'))) {
268 *p2 = '\0'; 271 *p2 = '\0';
269 service[services - 1] = p1; 272 result.config.service[result.config.number_of_services - 1] = p1;
270 service = realloc(service, (++services) * sizeof(char *)); 273 result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *));
271 p1 = p2 + 1; 274 p1 = p2 + 1;
272 } 275 }
273 service[services - 1] = p1; 276 result.config.service[result.config.number_of_services - 1] = p1;
274 break; 277 break;
275 case 'n': /* short name of host in the monitoring configuration */ 278 case 'n': /* short name of host in the monitoring configuration */
276 host_shortname = optarg; 279 result.config.host_shortname = optarg;
277 break; 280 } break;
278
279 case 'u': 281 case 'u':
280 comm_append("-l"); 282 result.config.cmd = comm_append(result.config.cmd, "-l");
281 comm_append(optarg); 283 result.config.cmd = comm_append(result.config.cmd, optarg);
282 break; 284 break;
283 case 'l': /* login name */ 285 case 'l': /* login name */
284 comm_append("-l"); 286 result.config.cmd = comm_append(result.config.cmd, "-l");
285 comm_append(optarg); 287 result.config.cmd = comm_append(result.config.cmd, optarg);
286 break; 288 break;
287 case 'i': /* identity */ 289 case 'i': /* identity */
288 comm_append("-i"); 290 result.config.cmd = comm_append(result.config.cmd, "-i");
289 comm_append(optarg); 291 result.config.cmd = comm_append(result.config.cmd, optarg);
290 break; 292 break;
291 293
292 case '1': /* Pass these switches directly to ssh */ 294 case '1': /* Pass these switches directly to ssh */
293 comm_append("-1"); 295 result.config.cmd = comm_append(result.config.cmd, "-1");
294 break; 296 break;
295 case '2': /* 1 to force version 1, 2 to force version 2 */ 297 case '2': /* 1 to force version 1, 2 to force version 2 */
296 comm_append("-2"); 298 result.config.cmd = comm_append(result.config.cmd, "-2");
297 break; 299 break;
298 case '4': /* -4 for IPv4 */ 300 case '4': /* -4 for IPv4 */
299 comm_append("-4"); 301 result.config.cmd = comm_append(result.config.cmd, "-4");
300 break; 302 break;
301 case '6': /* -6 for IPv6 */ 303 case '6': /* -6 for IPv6 */
302 comm_append("-6"); 304 result.config.cmd = comm_append(result.config.cmd, "-6");
303 break; 305 break;
304 case 'f': /* fork to background */ 306 case 'f': /* fork to background */
305 comm_append("-f"); 307 result.config.cmd = comm_append(result.config.cmd, "-f");
306 break; 308 break;
307 case 'C': /* Command for remote machine */ 309 case 'C': /* Command for remote machine */
308 commands++; 310 result.config.commands++;
309 if (commands > 1) { 311 if (result.config.commands > 1) {
310 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); 312 xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd);
311 } 313 }
312 xasprintf(&remotecmd, "%s%s", remotecmd, optarg); 314 xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg);
313 break; 315 break;
314 case 'S': /* skip n (or all) lines on stdout */ 316 case 'S': /* skip n (or all) lines on stdout */
315 if (optarg == NULL) { 317 if (optarg == NULL) {
316 skip_stdout = -1; /* skip all output on stdout */ 318 result.config.skip_stdout = -1; /* skip all output on stdout */
317 } else if (!is_integer(optarg)) { 319 } else if (!is_integer(optarg)) {
318 usage_va(_("skip-stdout argument must be an integer")); 320 usage_va(_("skip-stdout argument must be an integer"));
319 } else { 321 } else {
320 skip_stdout = atoi(optarg); 322 result.config.skip_stdout = atoi(optarg);
321 } 323 }
322 break; 324 break;
323 case 'E': /* skip n (or all) lines on stderr */ 325 case 'E': /* skip n (or all) lines on stderr */
324 if (optarg == NULL) { 326 if (optarg == NULL) {
325 skip_stderr = -1; /* skip all output on stderr */ 327 result.config.skip_stderr = -1; /* skip all output on stderr */
326 } else if (!is_integer(optarg)) { 328 } else if (!is_integer(optarg)) {
327 usage_va(_("skip-stderr argument must be an integer")); 329 usage_va(_("skip-stderr argument must be an integer"));
328 } else { 330 } else {
329 skip_stderr = atoi(optarg); 331 result.config.skip_stderr = atoi(optarg);
330 } 332 }
331 break; 333 break;
332 case 'W': /* exit with warning if there is an output on stderr */ 334 case 'W': /* exit with warning if there is an output on stderr */
333 warn_on_stderr = 1; 335 result.config.warn_on_stderr = true;
334 break; 336 break;
335 case 'o': /* Extra options for the ssh command */ 337 case 'o': /* Extra options for the ssh command */
336 comm_append("-o"); 338 result.config.cmd = comm_append(result.config.cmd, "-o");
337 comm_append(optarg); 339 result.config.cmd = comm_append(result.config.cmd, optarg);
338 break; 340 break;
339 case 'q': /* Tell the ssh command to be quiet */ 341 case 'q': /* Tell the ssh command to be quiet */
340 comm_append("-q"); 342 result.config.cmd = comm_append(result.config.cmd, "-q");
341 break; 343 break;
342 case 'F': /* ssh configfile */ 344 case 'F': /* ssh configfile */
343 comm_append("-F"); 345 result.config.cmd = comm_append(result.config.cmd, "-F");
344 comm_append(optarg); 346 result.config.cmd = comm_append(result.config.cmd, optarg);
345 break; 347 break;
346 default: /* help */ 348 default: /* help */
347 usage5(); 349 usage5();
348 } 350 }
349 } 351 }
350 352
351 c = optind; 353 int c = optind;
352 if (hostname == NULL) { 354 if (result.config.hostname == NULL) {
353 if (c <= argc) { 355 if (c <= argc) {
354 die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); 356 die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
355 } 357 }
356 hostname = argv[c++]; 358 result.config.hostname = argv[c++];
357 } 359 }
358 360
359 if (strlen(remotecmd) == 0) { 361 if (strlen(result.config.remotecmd) == 0) {
360 for (; c < argc; c++) { 362 for (; c < argc; c++) {
361 if (strlen(remotecmd) > 0) { 363 if (strlen(result.config.remotecmd) > 0) {
362 xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]); 364 xasprintf(&result.config.remotecmd, "%s %s", result.config.remotecmd, argv[c]);
363 } else { 365 } else {
364 xasprintf(&remotecmd, "%s", argv[c]); 366 xasprintf(&result.config.remotecmd, "%s", argv[c]);
365 } 367 }
366 } 368 }
367 } 369 }
368 370
369 if (commands > 1 || passive) { 371 if (result.config.commands > 1 || result.config.passive) {
370 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); 372 xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd);
371 } 373 }
372 374
373 if (remotecmd == NULL || strlen(remotecmd) <= 1) { 375 if (result.config.remotecmd == NULL || strlen(result.config.remotecmd) <= 1) {
374 usage_va(_("No remotecmd")); 376 usage_va(_("No remotecmd"));
375 } 377 }
376 378
377 comm_append(hostname); 379 result.config.cmd = comm_append(result.config.cmd, result.config.hostname);
378 comm_append(remotecmd); 380 result.config.cmd = comm_append(result.config.cmd, result.config.remotecmd);
379 381
380 return validate_arguments(); 382 return validate_arguments(result);
381} 383}
382 384
383void comm_append(const char *str) { 385command_construct comm_append(command_construct cmd, const char *str) {
386
387 if (verbose) {
388 for (int i = 0; i < cmd.commargc; i++) {
389 printf("Current command: [%i] %s\n", i, cmd.commargv[i]);
390 }
384 391
385 if (++commargc > NP_MAXARGS) { 392 printf("Appending: %s\n", str);
393 }
394
395 if (++cmd.commargc > NP_MAXARGS) {
386 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); 396 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS);
387 } 397 }
388 398
389 if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL) { 399 if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) == NULL) {
390 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); 400 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n"));
391 } 401 }
392 402
393 commargv[commargc - 1] = strdup(str); 403 cmd.commargv[cmd.commargc - 1] = strdup(str);
394 commargv[commargc] = NULL; 404 cmd.commargv[cmd.commargc] = NULL;
405
406 return cmd;
395} 407}
396 408
397int validate_arguments(void) { 409check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper config_wrapper) {
398 if (remotecmd == NULL || hostname == NULL) { 410 if (config_wrapper.config.remotecmd == NULL || config_wrapper.config.hostname == NULL) {
399 return ERROR; 411 config_wrapper.errorcode = ERROR;
412 return config_wrapper;
400 } 413 }
401 414
402 if (passive && commands != services) { 415 if (config_wrapper.config.passive && config_wrapper.config.commands != config_wrapper.config.number_of_services) {
403 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); 416 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
404 } 417 }
405 418
406 if (passive && host_shortname == NULL) { 419 if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) {
407 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); 420 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname);
408 } 421 }
409 422
410 return OK; 423 return config_wrapper;
411} 424}
412 425
413void print_help(void) { 426void print_help(void) {
diff --git a/plugins/check_by_ssh.d/config.h b/plugins/check_by_ssh.d/config.h
new file mode 100644
index 00000000..05435def
--- /dev/null
+++ b/plugins/check_by_ssh.d/config.h
@@ -0,0 +1,56 @@
1#pragma once
2
3#include "../../config.h"
4#include <stddef.h>
5
6typedef struct {
7 int commargc;
8 char **commargv;
9} command_construct;
10
11typedef struct {
12 char *hostname;
13 char *host_shortname;
14
15 char **service;
16 unsigned int number_of_services;
17
18 unsigned int commands; // Not needed during actual test run
19 char *remotecmd;
20
21 command_construct cmd;
22
23 bool unknown_timeout;
24 bool warn_on_stderr;
25 int skip_stdout;
26 int skip_stderr;
27 bool passive;
28 char *outputfile;
29} check_by_ssh_config;
30
31check_by_ssh_config check_by_ssh_config_init() {
32 check_by_ssh_config tmp = {
33 .hostname = NULL,
34 .host_shortname = NULL,
35
36 .service = NULL,
37 .number_of_services = 0,
38
39 .commands = 0,
40 .remotecmd = "",
41
42 .cmd =
43 {
44 .commargc = 0,
45 .commargv = NULL,
46 },
47
48 .unknown_timeout = false,
49 .warn_on_stderr = false,
50 .skip_stderr = 0,
51 .skip_stdout = 0,
52 .passive = false,
53 .outputfile = NULL,
54 };
55 return tmp;
56}