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