diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-15 12:59:37 +0200 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-15 12:59:37 +0200 |
| commit | 802e46f8ea36c344f112d7e1dd8d64d17a4cc939 (patch) | |
| tree | cabf480269113686430053d66df3dadcd4d137a7 /plugins/check_by_ssh.c | |
| parent | a3cf9041af810770daf5d9b83f1906fa9bb0dd11 (diff) | |
| download | monitoring-plugins-802e46f8ea36c344f112d7e1dd8d64d17a4cc939.tar.gz | |
Run clang-format again
Diffstat (limited to 'plugins/check_by_ssh.c')
| -rw-r--r-- | plugins/check_by_ssh.c | 108 |
1 files changed, 64 insertions, 44 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 2bc38d49..74b7a46f 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c | |||
| @@ -45,7 +45,8 @@ typedef struct { | |||
| 45 | check_by_ssh_config config; | 45 | check_by_ssh_config config; |
| 46 | } check_by_ssh_config_wrapper; | 46 | } check_by_ssh_config_wrapper; |
| 47 | static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | 47 | static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
| 48 | static check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/); | 48 | static check_by_ssh_config_wrapper |
| 49 | validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/); | ||
| 49 | 50 | ||
| 50 | static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/); | 51 | static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/); |
| 51 | static void print_help(void); | 52 | static void print_help(void); |
| @@ -90,7 +91,8 @@ int main(int argc, char **argv) { | |||
| 90 | 91 | ||
| 91 | /* 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 */ |
| 92 | if (result == 255 && config.unknown_timeout) { | 93 | if (result == 255 && config.unknown_timeout) { |
| 93 | 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)"); | ||
| 94 | return STATE_UNKNOWN; | 96 | return STATE_UNKNOWN; |
| 95 | } | 97 | } |
| 96 | 98 | ||
| @@ -134,7 +136,8 @@ int main(int argc, char **argv) { | |||
| 134 | puts(chld_out.line[i]); | 136 | puts(chld_out.line[i]); |
| 135 | } | 137 | } |
| 136 | } else { | 138 | } else { |
| 137 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), config.remotecmd, result); | 139 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), |
| 140 | state_text(result), config.remotecmd, result); | ||
| 138 | } | 141 | } |
| 139 | return result; /* return error status from remote command */ | 142 | return result; /* return error status from remote command */ |
| 140 | } | 143 | } |
| @@ -160,9 +163,11 @@ int main(int argc, char **argv) { | |||
| 160 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); | 163 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); |
| 161 | } | 164 | } |
| 162 | 165 | ||
| 163 | if (config.service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { | 166 | if (config.service[commands] && status_text && |
| 164 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, config.host_shortname, | 167 | sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { |
| 165 | config.service[commands++], cresult, status_text); | 168 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", |
| 169 | (int)local_time, config.host_shortname, config.service[commands++], cresult, | ||
| 170 | status_text); | ||
| 166 | } | 171 | } |
| 167 | } | 172 | } |
| 168 | 173 | ||
| @@ -172,34 +177,35 @@ int main(int argc, char **argv) { | |||
| 172 | 177 | ||
| 173 | /* process command-line arguments */ | 178 | /* process command-line arguments */ |
| 174 | check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { | 179 | check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { |
| 175 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 180 | static struct option longopts[] = { |
| 176 | {"help", no_argument, 0, 'h'}, | 181 | {"version", no_argument, 0, 'V'}, |
| 177 | {"verbose", no_argument, 0, 'v'}, | 182 | {"help", no_argument, 0, 'h'}, |
| 178 | {"fork", no_argument, 0, 'f'}, | 183 | {"verbose", no_argument, 0, 'v'}, |
| 179 | {"timeout", required_argument, 0, 't'}, | 184 | {"fork", no_argument, 0, 'f'}, |
| 180 | {"unknown-timeout", no_argument, 0, 'U'}, | 185 | {"timeout", required_argument, 0, 't'}, |
| 181 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ | 186 | {"unknown-timeout", no_argument, 0, 'U'}, |
| 182 | {"hostname", required_argument, 0, 'H'}, | 187 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ |
| 183 | {"port", required_argument, 0, 'p'}, | 188 | {"hostname", required_argument, 0, 'H'}, |
| 184 | {"output", required_argument, 0, 'O'}, | 189 | {"port", required_argument, 0, 'p'}, |
| 185 | {"name", required_argument, 0, 'n'}, | 190 | {"output", required_argument, 0, 'O'}, |
| 186 | {"services", required_argument, 0, 's'}, | 191 | {"name", required_argument, 0, 'n'}, |
| 187 | {"identity", required_argument, 0, 'i'}, | 192 | {"services", required_argument, 0, 's'}, |
| 188 | {"user", required_argument, 0, 'u'}, | 193 | {"identity", required_argument, 0, 'i'}, |
| 189 | {"logname", required_argument, 0, 'l'}, | 194 | {"user", required_argument, 0, 'u'}, |
| 190 | {"command", required_argument, 0, 'C'}, | 195 | {"logname", required_argument, 0, 'l'}, |
| 191 | {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */ | 196 | {"command", required_argument, 0, 'C'}, |
| 192 | {"skip-stdout", optional_argument, 0, 'S'}, | 197 | {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */ |
| 193 | {"skip-stderr", optional_argument, 0, 'E'}, | 198 | {"skip-stdout", optional_argument, 0, 'S'}, |
| 194 | {"warn-on-stderr", no_argument, 0, 'W'}, | 199 | {"skip-stderr", optional_argument, 0, 'E'}, |
| 195 | {"proto1", no_argument, 0, '1'}, | 200 | {"warn-on-stderr", no_argument, 0, 'W'}, |
| 196 | {"proto2", no_argument, 0, '2'}, | 201 | {"proto1", no_argument, 0, '1'}, |
| 197 | {"use-ipv4", no_argument, 0, '4'}, | 202 | {"proto2", no_argument, 0, '2'}, |
| 198 | {"use-ipv6", no_argument, 0, '6'}, | 203 | {"use-ipv4", no_argument, 0, '4'}, |
| 199 | {"ssh-option", required_argument, 0, 'o'}, | 204 | {"use-ipv6", no_argument, 0, '6'}, |
| 200 | {"quiet", no_argument, 0, 'q'}, | 205 | {"ssh-option", required_argument, 0, 'o'}, |
| 201 | {"configfile", optional_argument, 0, 'F'}, | 206 | {"quiet", no_argument, 0, 'q'}, |
| 202 | {0, 0, 0, 0}}; | 207 | {"configfile", optional_argument, 0, 'F'}, |
| 208 | {0, 0, 0, 0}}; | ||
| 203 | 209 | ||
| 204 | check_by_ssh_config_wrapper result = { | 210 | check_by_ssh_config_wrapper result = { |
| 205 | .errorcode = OK, | 211 | .errorcode = OK, |
| @@ -221,7 +227,8 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { | |||
| 221 | 227 | ||
| 222 | int option = 0; | 228 | int option = 0; |
| 223 | while (true) { | 229 | while (true) { |
| 224 | int opt_index = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | 230 | int opt_index = |
| 231 | getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | ||
| 225 | 232 | ||
| 226 | if (opt_index == -1 || opt_index == EOF) { | 233 | if (opt_index == -1 || opt_index == EOF) { |
| 227 | break; | 234 | break; |
| @@ -266,11 +273,13 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { | |||
| 266 | char *p2; | 273 | char *p2; |
| 267 | 274 | ||
| 268 | p1 = optarg; | 275 | p1 = optarg; |
| 269 | result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); | 276 | result.config.service = realloc(result.config.service, |
| 277 | (++result.config.number_of_services) * sizeof(char *)); | ||
| 270 | while ((p2 = index(p1, ':'))) { | 278 | while ((p2 = index(p1, ':'))) { |
| 271 | *p2 = '\0'; | 279 | *p2 = '\0'; |
| 272 | result.config.service[result.config.number_of_services - 1] = p1; | 280 | result.config.service[result.config.number_of_services - 1] = p1; |
| 273 | result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); | 281 | result.config.service = realloc( |
| 282 | result.config.service, (++result.config.number_of_services) * sizeof(char *)); | ||
| 274 | p1 = p2 + 1; | 283 | p1 = p2 + 1; |
| 275 | } | 284 | } |
| 276 | result.config.service[result.config.number_of_services - 1] = p1; | 285 | result.config.service[result.config.number_of_services - 1] = p1; |
| @@ -309,7 +318,8 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { | |||
| 309 | case 'C': /* Command for remote machine */ | 318 | case 'C': /* Command for remote machine */ |
| 310 | result.config.commands++; | 319 | result.config.commands++; |
| 311 | if (result.config.commands > 1) { | 320 | if (result.config.commands > 1) { |
| 312 | xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd); | 321 | xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", |
| 322 | result.config.remotecmd); | ||
| 313 | } | 323 | } |
| 314 | xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg); | 324 | xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg); |
| 315 | break; | 325 | break; |
| @@ -396,7 +406,8 @@ command_construct comm_append(command_construct cmd, const char *str) { | |||
| 396 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); | 406 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); |
| 397 | } | 407 | } |
| 398 | 408 | ||
| 399 | if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) == NULL) { | 409 | if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) == |
| 410 | NULL) { | ||
| 400 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); | 411 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); |
| 401 | } | 412 | } |
| 402 | 413 | ||
| @@ -412,12 +423,18 @@ check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper confi | |||
| 412 | return config_wrapper; | 423 | return config_wrapper; |
| 413 | } | 424 | } |
| 414 | 425 | ||
| 415 | if (config_wrapper.config.passive && config_wrapper.config.commands != config_wrapper.config.number_of_services) { | 426 | if (config_wrapper.config.passive && |
| 416 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); | 427 | config_wrapper.config.commands != config_wrapper.config.number_of_services) { |
| 428 | die(STATE_UNKNOWN, | ||
| 429 | _("%s: In passive mode, you must provide a service name for each command.\n"), | ||
| 430 | progname); | ||
| 417 | } | 431 | } |
| 418 | 432 | ||
| 419 | if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) { | 433 | if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) { |
| 420 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); | 434 | die(STATE_UNKNOWN, |
| 435 | _("%s: In passive mode, you must provide the host short name from the monitoring " | ||
| 436 | "configs.\n"), | ||
| 437 | progname); | ||
| 421 | } | 438 | } |
| 422 | 439 | ||
| 423 | return config_wrapper; | 440 | return config_wrapper; |
| @@ -454,7 +471,8 @@ void print_help(void) { | |||
| 454 | printf(" %s\n", "-W, --warn-on-stderr]"); | 471 | printf(" %s\n", "-W, --warn-on-stderr]"); |
| 455 | printf(" %s\n", _("Exit with an warning, if there is an output on STDERR")); | 472 | printf(" %s\n", _("Exit with an warning, if there is an output on STDERR")); |
| 456 | printf(" %s\n", "-f"); | 473 | printf(" %s\n", "-f"); |
| 457 | printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed")); | 474 | printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always " |
| 475 | "return OK if ssh is executed")); | ||
| 458 | printf(" %s\n", "-C, --command='COMMAND STRING'"); | 476 | printf(" %s\n", "-C, --command='COMMAND STRING'"); |
| 459 | printf(" %s\n", _("command to execute on the remote machine")); | 477 | printf(" %s\n", _("command to execute on the remote machine")); |
| 460 | printf(" %s\n", "-l, --logname=USERNAME"); | 478 | printf(" %s\n", "-l, --logname=USERNAME"); |
| @@ -490,7 +508,9 @@ void print_help(void) { | |||
| 490 | printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)")); | 508 | printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)")); |
| 491 | printf("\n"); | 509 | printf("\n"); |
| 492 | printf("%s\n", _("Examples:")); | 510 | printf("%s\n", _("Examples:")); |
| 493 | printf(" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo"); | 511 | printf( |
| 512 | " %s\n", | ||
| 513 | "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo"); | ||
| 494 | printf(" %s\n", "$ cat /tmp/foo"); | 514 | printf(" %s\n", "$ cat /tmp/foo"); |
| 495 | printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days"); | 515 | printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days"); |
| 496 | printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days"); | 516 | printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days"); |
