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