check_ldap: implement output format selection

This commit is contained in:
Lorenz Kästle 2025-11-25 12:41:07 +01:00
parent 2e3dff775d
commit d6c4b799e3
2 changed files with 28 additions and 0 deletions

View file

@ -82,6 +82,10 @@ int main(int argc, char *argv[]) {
const check_ldap_config config = tmp_config.config;
if (config.output_format_is_set) {
mp_set_format(config.output_format);
}
/* initialize alarm signal handling */
signal(SIGALRM, socket_timeout_alarm_handler);
@ -306,6 +310,10 @@ int main(int argc, char *argv[]) {
/* process command-line arguments */
check_ldap_config_wrapper process_arguments(int argc, char **argv) {
enum {
output_format_index = CHAR_MAX + 1,
};
/* initialize the long option struct */
static struct option longopts[] = {{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
@ -329,6 +337,7 @@ check_ldap_config_wrapper process_arguments(int argc, char **argv) {
{"warn-entries", required_argument, 0, 'W'},
{"crit-entries", required_argument, 0, 'C'},
{"verbose", no_argument, 0, 'v'},
{"output-format", required_argument, 0, output_format_index},
{0, 0, 0, 0}};
check_ldap_config_wrapper result = {
@ -458,6 +467,18 @@ check_ldap_config_wrapper process_arguments(int argc, char **argv) {
usage(_("IPv6 support not available\n"));
#endif
break;
case output_format_index: {
parsed_output_format parser = mp_parse_output_format(optarg);
if (!parser.parsing_success) {
// TODO List all available formats here, maybe add anothoer usage function
printf("Invalid output format: %s\n", optarg);
exit(STATE_UNKNOWN);
}
result.config.output_format_is_set = true;
result.config.output_format = parser.output_format;
break;
}
default:
usage5();
}
@ -553,6 +574,7 @@ void print_help(void) {
printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
printf(UT_VERBOSE);
printf(UT_OUTPUT_FORMAT);
printf("\n");
printf("%s\n", _("Notes:"));

View file

@ -1,6 +1,7 @@
#pragma once
#include "../../config.h"
#include "output.h"
#include "thresholds.h"
#include <stddef.h>
@ -27,6 +28,9 @@ typedef struct {
mp_thresholds entries_thresholds;
mp_thresholds connection_time_threshold;
bool output_format_is_set;
mp_output_format output_format;
} check_ldap_config;
check_ldap_config check_ldap_config_init() {
@ -45,6 +49,8 @@ check_ldap_config check_ldap_config_init() {
.entries_thresholds = mp_thresholds_init(),
.connection_time_threshold = mp_thresholds_init(),
.output_format_is_set = false,
};
return tmp;
}