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