diff options
| -rw-r--r-- | plugins/check_ldap.c | 333 | ||||
| -rw-r--r-- | plugins/check_ldap.d/config.h | 24 | ||||
| -rw-r--r-- | plugins/t/check_ldap.t | 14 |
3 files changed, 236 insertions, 135 deletions
diff --git a/plugins/check_ldap.c b/plugins/check_ldap.c index 77a33304..1b2e2826 100644 --- a/plugins/check_ldap.c +++ b/plugins/check_ldap.c | |||
| @@ -27,12 +27,11 @@ | |||
| 27 | *****************************************************************************/ | 27 | *****************************************************************************/ |
| 28 | 28 | ||
| 29 | /* progname may be check_ldaps */ | 29 | /* progname may be check_ldaps */ |
| 30 | char *progname = "check_ldap"; | 30 | #include "output.h" |
| 31 | const char *copyright = "2000-2024"; | ||
| 32 | const char *email = "devel@monitoring-plugins.org"; | ||
| 33 | |||
| 34 | #include "common.h" | 31 | #include "common.h" |
| 35 | #include "netutils.h" | 32 | #include "netutils.h" |
| 33 | #include "perfdata.h" | ||
| 34 | #include "thresholds.h" | ||
| 36 | #include "utils.h" | 35 | #include "utils.h" |
| 37 | #include "check_ldap.d/config.h" | 36 | #include "check_ldap.d/config.h" |
| 38 | 37 | ||
| @@ -41,6 +40,10 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 41 | #define LDAP_DEPRECATED 1 | 40 | #define LDAP_DEPRECATED 1 |
| 42 | #include <ldap.h> | 41 | #include <ldap.h> |
| 43 | 42 | ||
| 43 | char *progname = "check_ldap"; | ||
| 44 | const char *copyright = "2000-2024"; | ||
| 45 | const char *email = "devel@monitoring-plugins.org"; | ||
| 46 | |||
| 44 | enum { | 47 | enum { |
| 45 | DEFAULT_PORT = 389 | 48 | DEFAULT_PORT = 389 |
| 46 | }; | 49 | }; |
| @@ -79,6 +82,10 @@ int main(int argc, char *argv[]) { | |||
| 79 | 82 | ||
| 80 | const check_ldap_config config = tmp_config.config; | 83 | const check_ldap_config config = tmp_config.config; |
| 81 | 84 | ||
| 85 | if (config.output_format_is_set) { | ||
| 86 | mp_set_format(config.output_format); | ||
| 87 | } | ||
| 88 | |||
| 82 | /* initialize alarm signal handling */ | 89 | /* initialize alarm signal handling */ |
| 83 | signal(SIGALRM, socket_timeout_alarm_handler); | 90 | signal(SIGALRM, socket_timeout_alarm_handler); |
| 84 | 91 | ||
| @@ -89,101 +96,172 @@ int main(int argc, char *argv[]) { | |||
| 89 | struct timeval start_time; | 96 | struct timeval start_time; |
| 90 | gettimeofday(&start_time, NULL); | 97 | gettimeofday(&start_time, NULL); |
| 91 | 98 | ||
| 99 | mp_check overall = mp_check_init(); | ||
| 100 | |||
| 92 | LDAP *ldap_connection; | 101 | LDAP *ldap_connection; |
| 93 | /* initialize ldap */ | 102 | /* initialize ldap */ |
| 103 | { | ||
| 94 | #ifdef HAVE_LDAP_INIT | 104 | #ifdef HAVE_LDAP_INIT |
| 95 | if (!(ldap_connection = ldap_init(config.ld_host, config.ld_port))) { | 105 | mp_subcheck sc_ldap_init = mp_subcheck_init(); |
| 96 | printf("Could not connect to the server at port %i\n", config.ld_port); | 106 | if (!(ldap_connection = ldap_init(config.ld_host, config.ld_port))) { |
| 97 | return STATE_CRITICAL; | 107 | xasprintf(&sc_ldap_init.output, "could not connect to the server at port %i", |
| 98 | } | 108 | config.ld_port); |
| 109 | sc_ldap_init = mp_set_subcheck_state(sc_ldap_init, STATE_CRITICAL); | ||
| 110 | mp_add_subcheck_to_check(&overall, sc_ldap_init); | ||
| 111 | mp_exit(overall); | ||
| 112 | } else { | ||
| 113 | xasprintf(&sc_ldap_init.output, "connected to the server at port %i", config.ld_port); | ||
| 114 | sc_ldap_init = mp_set_subcheck_state(sc_ldap_init, STATE_OK); | ||
| 115 | mp_add_subcheck_to_check(&overall, sc_ldap_init); | ||
| 116 | } | ||
| 99 | #else | 117 | #else |
| 100 | if (!(ld = ldap_open(config.ld_host, config.ld_port))) { | 118 | mp_subcheck sc_ldap_init = mp_subcheck_init(); |
| 101 | if (verbose) { | 119 | if (!(ld = ldap_open(config.ld_host, config.ld_port))) { |
| 102 | ldap_perror(ldap_connection, "ldap_open"); | 120 | if (verbose) { |
| 121 | ldap_perror(ldap_connection, "ldap_open"); | ||
| 122 | } | ||
| 123 | xasprintf(&sc_ldap_init.output, "Could not connect to the server at port %i"), config.ld_port); | ||
| 124 | sc_ldap_init = mp_set_subcheck_state(sc_ldap_init, STATE_CRITICAL); | ||
| 125 | mp_add_subcheck_to_check(&overall, sc_ldap_init); | ||
| 126 | mp_exit(overall); | ||
| 127 | } else { | ||
| 128 | xasprintf(&sc_ldap_init.output, "connected to the server at port %i", config.ld_port); | ||
| 129 | sc_ldap_init = mp_set_subcheck_state(sc_ldap_init, STATE_OK); | ||
| 130 | mp_add_subcheck_to_check(&overall, sc_ldap_init); | ||
| 103 | } | 131 | } |
| 104 | printf(_("Could not connect to the server at port %i\n"), config.ld_port); | ||
| 105 | return STATE_CRITICAL; | ||
| 106 | } | ||
| 107 | #endif /* HAVE_LDAP_INIT */ | 132 | #endif /* HAVE_LDAP_INIT */ |
| 133 | } | ||
| 108 | 134 | ||
| 109 | #ifdef HAVE_LDAP_SET_OPTION | 135 | #ifdef HAVE_LDAP_SET_OPTION |
| 110 | /* set ldap options */ | 136 | /* set ldap options */ |
| 137 | mp_subcheck sc_ldap_set_opts = mp_subcheck_init(); | ||
| 111 | if (ldap_set_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &config.ld_protocol) != | 138 | if (ldap_set_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &config.ld_protocol) != |
| 112 | LDAP_OPT_SUCCESS) { | 139 | LDAP_OPT_SUCCESS) { |
| 113 | printf(_("Could not set protocol version %d\n"), config.ld_protocol); | 140 | xasprintf(&sc_ldap_set_opts.output, "Could not set protocol version %d", |
| 114 | return STATE_CRITICAL; | 141 | config.ld_protocol); |
| 142 | sc_ldap_set_opts = mp_set_subcheck_state(sc_ldap_set_opts, STATE_CRITICAL); | ||
| 143 | mp_add_subcheck_to_check(&overall, sc_ldap_set_opts); | ||
| 144 | mp_exit(overall); | ||
| 145 | } else { | ||
| 146 | xasprintf(&sc_ldap_set_opts.output, "set protocol version %d", config.ld_protocol); | ||
| 147 | sc_ldap_set_opts = mp_set_subcheck_state(sc_ldap_set_opts, STATE_OK); | ||
| 148 | mp_add_subcheck_to_check(&overall, sc_ldap_set_opts); | ||
| 115 | } | 149 | } |
| 116 | #endif | 150 | #endif |
| 117 | 151 | ||
| 118 | int version = 3; | 152 | int version = 3; |
| 119 | int tls; | 153 | int tls; |
| 120 | if (config.ld_port == LDAPS_PORT || config.ssl_on_connect) { | 154 | { |
| 155 | if (config.ld_port == LDAPS_PORT || config.ssl_on_connect) { | ||
| 121 | #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS) | 156 | #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS) |
| 122 | /* ldaps: set option tls */ | 157 | /* ldaps: set option tls */ |
| 123 | tls = LDAP_OPT_X_TLS_HARD; | 158 | tls = LDAP_OPT_X_TLS_HARD; |
| 124 | 159 | ||
| 125 | if (ldap_set_option(ldap_connection, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS) { | 160 | mp_subcheck sc_ldap_tls_init = mp_subcheck_init(); |
| 126 | if (verbose) { | 161 | if (ldap_set_option(ldap_connection, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS) { |
| 127 | ldap_perror(ldap_connection, "ldaps_option"); | 162 | if (verbose) { |
| 163 | ldap_perror(ldap_connection, "ldaps_option"); | ||
| 164 | } | ||
| 165 | xasprintf(&sc_ldap_tls_init.output, "could not init TLS at port %i!", | ||
| 166 | config.ld_port); | ||
| 167 | sc_ldap_tls_init = mp_set_subcheck_state(sc_ldap_tls_init, STATE_CRITICAL); | ||
| 168 | mp_add_subcheck_to_check(&overall, sc_ldap_tls_init); | ||
| 169 | mp_exit(overall); | ||
| 170 | } else { | ||
| 171 | xasprintf(&sc_ldap_tls_init.output, "initiated TLS at port %i!", config.ld_port); | ||
| 172 | sc_ldap_tls_init = mp_set_subcheck_state(sc_ldap_tls_init, STATE_OK); | ||
| 173 | mp_add_subcheck_to_check(&overall, sc_ldap_tls_init); | ||
| 128 | } | 174 | } |
| 129 | printf(_("Could not init TLS at port %i!\n"), config.ld_port); | ||
| 130 | return STATE_CRITICAL; | ||
| 131 | } | ||
| 132 | #else | 175 | #else |
| 133 | printf(_("TLS not supported by the libraries!\n")); | 176 | printf(_("TLS not supported by the libraries!\n")); |
| 134 | return STATE_CRITICAL; | 177 | exit(STATE_CRITICAL); |
| 135 | #endif /* LDAP_OPT_X_TLS */ | 178 | #endif /* LDAP_OPT_X_TLS */ |
| 136 | } else if (config.starttls) { | 179 | } else if (config.starttls) { |
| 137 | #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S) | 180 | #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S) |
| 138 | /* ldap with startTLS: set option version */ | 181 | /* ldap with startTLS: set option version */ |
| 139 | if (ldap_get_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &version) == | 182 | if (ldap_get_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &version) == |
| 140 | LDAP_OPT_SUCCESS) { | 183 | LDAP_OPT_SUCCESS) { |
| 141 | if (version < LDAP_VERSION3) { | 184 | if (version < LDAP_VERSION3) { |
| 142 | version = LDAP_VERSION3; | 185 | version = LDAP_VERSION3; |
| 143 | ldap_set_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &version); | 186 | ldap_set_option(ldap_connection, LDAP_OPT_PROTOCOL_VERSION, &version); |
| 187 | } | ||
| 144 | } | 188 | } |
| 145 | } | 189 | /* call start_tls */ |
| 146 | /* call start_tls */ | 190 | mp_subcheck sc_ldap_starttls = mp_subcheck_init(); |
| 147 | if (ldap_start_tls_s(ldap_connection, NULL, NULL) != LDAP_SUCCESS) { | 191 | if (ldap_start_tls_s(ldap_connection, NULL, NULL) != LDAP_SUCCESS) { |
| 148 | if (verbose) { | 192 | if (verbose) { |
| 149 | ldap_perror(ldap_connection, "ldap_start_tls"); | 193 | ldap_perror(ldap_connection, "ldap_start_tls"); |
| 194 | } | ||
| 195 | xasprintf(&sc_ldap_starttls.output, "could not init STARTTLS at port %i!", | ||
| 196 | config.ld_port); | ||
| 197 | sc_ldap_starttls = mp_set_subcheck_state(sc_ldap_starttls, STATE_CRITICAL); | ||
| 198 | mp_add_subcheck_to_check(&overall, sc_ldap_starttls); | ||
| 199 | mp_exit(overall); | ||
| 200 | } else { | ||
| 201 | xasprintf(&sc_ldap_starttls.output, "initiated STARTTLS at port %i!", | ||
| 202 | config.ld_port); | ||
| 203 | sc_ldap_starttls = mp_set_subcheck_state(sc_ldap_starttls, STATE_OK); | ||
| 204 | mp_add_subcheck_to_check(&overall, sc_ldap_starttls); | ||
| 150 | } | 205 | } |
| 151 | printf(_("Could not init startTLS at port %i!\n"), config.ld_port); | ||
| 152 | return STATE_CRITICAL; | ||
| 153 | } | ||
| 154 | #else | 206 | #else |
| 155 | printf(_("startTLS not supported by the library, needs LDAPv3!\n")); | 207 | printf(_("startTLS not supported by the library, needs LDAPv3!\n")); |
| 156 | return STATE_CRITICAL; | 208 | exit(STATE_CRITICAL); |
| 157 | #endif /* HAVE_LDAP_START_TLS_S */ | 209 | #endif /* HAVE_LDAP_START_TLS_S */ |
| 210 | } | ||
| 158 | } | 211 | } |
| 159 | 212 | ||
| 160 | /* bind to the ldap server */ | 213 | /* bind to the ldap server */ |
| 161 | if (ldap_bind_s(ldap_connection, config.ld_binddn, config.ld_passwd, LDAP_AUTH_SIMPLE) != | 214 | { |
| 162 | LDAP_SUCCESS) { | 215 | mp_subcheck sc_ldap_bind = mp_subcheck_init(); |
| 163 | if (verbose) { | 216 | int ldap_error = |
| 164 | ldap_perror(ldap_connection, "ldap_bind"); | 217 | ldap_bind_s(ldap_connection, config.ld_binddn, config.ld_passwd, LDAP_AUTH_SIMPLE); |
| 218 | if (ldap_error != LDAP_SUCCESS) { | ||
| 219 | if (verbose) { | ||
| 220 | ldap_perror(ldap_connection, "ldap_bind"); | ||
| 221 | } | ||
| 222 | |||
| 223 | xasprintf(&sc_ldap_bind.output, "could not bind to the LDAP server: %s", | ||
| 224 | ldap_err2string(ldap_error)); | ||
| 225 | sc_ldap_bind = mp_set_subcheck_state(sc_ldap_bind, STATE_CRITICAL); | ||
| 226 | mp_add_subcheck_to_check(&overall, sc_ldap_bind); | ||
| 227 | mp_exit(overall); | ||
| 228 | } else { | ||
| 229 | xasprintf(&sc_ldap_bind.output, "execute bind to the LDAP server"); | ||
| 230 | sc_ldap_bind = mp_set_subcheck_state(sc_ldap_bind, STATE_OK); | ||
| 231 | mp_add_subcheck_to_check(&overall, sc_ldap_bind); | ||
| 165 | } | 232 | } |
| 166 | printf(_("Could not bind to the LDAP server\n")); | ||
| 167 | return STATE_CRITICAL; | ||
| 168 | } | 233 | } |
| 169 | 234 | ||
| 170 | LDAPMessage *result; | 235 | LDAPMessage *result; |
| 171 | int num_entries = 0; | ||
| 172 | /* do a search of all objectclasses in the base dn */ | 236 | /* do a search of all objectclasses in the base dn */ |
| 173 | if (ldap_search_s(ldap_connection, config.ld_base, | 237 | { |
| 174 | (config.crit_entries != NULL || config.warn_entries != NULL) | 238 | mp_subcheck sc_ldap_search = mp_subcheck_init(); |
| 175 | ? LDAP_SCOPE_SUBTREE | 239 | int ldap_error = ldap_search_s( |
| 176 | : LDAP_SCOPE_BASE, | 240 | ldap_connection, config.ld_base, |
| 177 | config.ld_attr, NULL, 0, &result) != LDAP_SUCCESS) { | 241 | (config.entries_thresholds.warning_is_set || config.entries_thresholds.critical_is_set) |
| 178 | if (verbose) { | 242 | ? LDAP_SCOPE_SUBTREE |
| 179 | ldap_perror(ldap_connection, "ldap_search"); | 243 | : LDAP_SCOPE_BASE, |
| 244 | config.ld_attr, NULL, 0, &result); | ||
| 245 | |||
| 246 | if (ldap_error != LDAP_SUCCESS) { | ||
| 247 | if (verbose) { | ||
| 248 | ldap_perror(ldap_connection, "ldap_search"); | ||
| 249 | } | ||
| 250 | xasprintf(&sc_ldap_search.output, "could not search/find objectclasses in %s: %s", | ||
| 251 | config.ld_base, ldap_err2string(ldap_error)); | ||
| 252 | sc_ldap_search = mp_set_subcheck_state(sc_ldap_search, STATE_CRITICAL); | ||
| 253 | mp_add_subcheck_to_check(&overall, sc_ldap_search); | ||
| 254 | mp_exit(overall); | ||
| 255 | } else { | ||
| 256 | xasprintf(&sc_ldap_search.output, "search/find objectclasses in %s", config.ld_base); | ||
| 257 | sc_ldap_search = mp_set_subcheck_state(sc_ldap_search, STATE_OK); | ||
| 258 | mp_add_subcheck_to_check(&overall, sc_ldap_search); | ||
| 180 | } | 259 | } |
| 181 | printf(_("Could not search/find objectclasses in %s\n"), config.ld_base); | ||
| 182 | return STATE_CRITICAL; | ||
| 183 | } | 260 | } |
| 184 | 261 | ||
| 185 | if (config.crit_entries != NULL || config.warn_entries != NULL) { | 262 | int num_entries = ldap_count_entries(ldap_connection, result); |
| 186 | num_entries = ldap_count_entries(ldap_connection, result); | 263 | if (verbose) { |
| 264 | printf("entries found: %d\n", num_entries); | ||
| 187 | } | 265 | } |
| 188 | 266 | ||
| 189 | /* unbind from the ldap server */ | 267 | /* unbind from the ldap server */ |
| @@ -193,50 +271,50 @@ int main(int argc, char *argv[]) { | |||
| 193 | alarm(0); | 271 | alarm(0); |
| 194 | 272 | ||
| 195 | /* calculate the elapsed time and compare to thresholds */ | 273 | /* calculate the elapsed time and compare to thresholds */ |
| 196 | |||
| 197 | long microsec = deltime(start_time); | 274 | long microsec = deltime(start_time); |
| 198 | double elapsed_time = (double)microsec / 1.0e6; | 275 | double elapsed_time = (double)microsec / 1.0e6; |
| 199 | mp_state_enum status = STATE_UNKNOWN; | 276 | mp_perfdata pd_connection_time = perfdata_init(); |
| 200 | if (config.crit_time_set && elapsed_time > config.crit_time) { | 277 | pd_connection_time.label = "time"; |
| 201 | status = STATE_CRITICAL; | 278 | pd_connection_time.value = mp_create_pd_value(elapsed_time); |
| 202 | } else if (config.warn_time_set && elapsed_time > config.warn_time) { | 279 | pd_connection_time = mp_pd_set_thresholds(pd_connection_time, config.connection_time_threshold); |
| 203 | status = STATE_WARNING; | ||
| 204 | } else { | ||
| 205 | status = STATE_OK; | ||
| 206 | } | ||
| 207 | 280 | ||
| 208 | if (config.entries_thresholds != NULL) { | 281 | mp_subcheck sc_connection_time = mp_subcheck_init(); |
| 209 | if (verbose) { | 282 | mp_add_perfdata_to_subcheck(&sc_connection_time, pd_connection_time); |
| 210 | printf("entries found: %d\n", num_entries); | 283 | |
| 211 | print_thresholds("entry thresholds", config.entries_thresholds); | 284 | mp_state_enum connection_time_state = mp_get_pd_status(pd_connection_time); |
| 212 | } | 285 | sc_connection_time = mp_set_subcheck_state(sc_connection_time, connection_time_state); |
| 213 | mp_state_enum status_entries = get_status(num_entries, config.entries_thresholds); | ||
| 214 | if (status_entries == STATE_CRITICAL) { | ||
| 215 | status = STATE_CRITICAL; | ||
| 216 | } else if (status != STATE_CRITICAL) { | ||
| 217 | status = status_entries; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | 286 | ||
| 221 | /* print out the result */ | 287 | if (connection_time_state == STATE_OK) { |
| 222 | if (config.crit_entries != NULL || config.warn_entries != NULL) { | 288 | xasprintf(&sc_connection_time.output, "connection time %.3fs is within thresholds", |
| 223 | printf(_("LDAP %s - found %d entries in %.3f seconds|%s %s\n"), state_text(status), | 289 | elapsed_time); |
| 224 | num_entries, elapsed_time, | ||
| 225 | fperfdata("time", elapsed_time, "s", config.warn_time_set, config.warn_time, | ||
| 226 | config.crit_time_set, config.crit_time, true, 0, false, 0), | ||
| 227 | sperfdata("entries", (double)num_entries, "", config.warn_entries, | ||
| 228 | config.crit_entries, true, 0.0, false, 0.0)); | ||
| 229 | } else { | 290 | } else { |
| 230 | printf(_("LDAP %s - %.3f seconds response time|%s\n"), state_text(status), elapsed_time, | 291 | xasprintf(&sc_connection_time.output, "connection time %.3fs is violating thresholds", |
| 231 | fperfdata("time", elapsed_time, "s", config.warn_time_set, config.warn_time, | 292 | elapsed_time); |
| 232 | config.crit_time_set, config.crit_time, true, 0, false, 0)); | ||
| 233 | } | 293 | } |
| 234 | 294 | ||
| 235 | exit(status); | 295 | mp_add_subcheck_to_check(&overall, sc_connection_time); |
| 296 | |||
| 297 | mp_perfdata pd_num_entries = perfdata_init(); | ||
| 298 | pd_num_entries.label = "entries"; | ||
| 299 | pd_num_entries.value = mp_create_pd_value(num_entries); | ||
| 300 | pd_num_entries = mp_pd_set_thresholds(pd_num_entries, config.entries_thresholds); | ||
| 301 | |||
| 302 | mp_subcheck sc_num_entries = mp_subcheck_init(); | ||
| 303 | mp_add_perfdata_to_subcheck(&sc_num_entries, pd_num_entries); | ||
| 304 | xasprintf(&sc_num_entries.output, "found %d entries", num_entries); | ||
| 305 | sc_num_entries = mp_set_subcheck_state(sc_num_entries, mp_get_pd_status(pd_num_entries)); | ||
| 306 | |||
| 307 | mp_add_subcheck_to_check(&overall, sc_num_entries); | ||
| 308 | |||
| 309 | mp_exit(overall); | ||
| 236 | } | 310 | } |
| 237 | 311 | ||
| 238 | /* process command-line arguments */ | 312 | /* process command-line arguments */ |
| 239 | check_ldap_config_wrapper process_arguments(int argc, char **argv) { | 313 | check_ldap_config_wrapper process_arguments(int argc, char **argv) { |
| 314 | enum { | ||
| 315 | output_format_index = CHAR_MAX + 1, | ||
| 316 | }; | ||
| 317 | |||
| 240 | /* initialize the long option struct */ | 318 | /* initialize the long option struct */ |
| 241 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, | 319 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, |
| 242 | {"version", no_argument, 0, 'V'}, | 320 | {"version", no_argument, 0, 'V'}, |
| @@ -260,6 +338,7 @@ check_ldap_config_wrapper process_arguments(int argc, char **argv) { | |||
| 260 | {"warn-entries", required_argument, 0, 'W'}, | 338 | {"warn-entries", required_argument, 0, 'W'}, |
| 261 | {"crit-entries", required_argument, 0, 'C'}, | 339 | {"crit-entries", required_argument, 0, 'C'}, |
| 262 | {"verbose", no_argument, 0, 'v'}, | 340 | {"verbose", no_argument, 0, 'v'}, |
| 341 | {"output-format", required_argument, 0, output_format_index}, | ||
| 263 | {0, 0, 0, 0}}; | 342 | {0, 0, 0, 0}}; |
| 264 | 343 | ||
| 265 | check_ldap_config_wrapper result = { | 344 | check_ldap_config_wrapper result = { |
| @@ -319,20 +398,38 @@ check_ldap_config_wrapper process_arguments(int argc, char **argv) { | |||
| 319 | case 'P': | 398 | case 'P': |
| 320 | result.config.ld_passwd = optarg; | 399 | result.config.ld_passwd = optarg; |
| 321 | break; | 400 | break; |
| 322 | case 'w': | 401 | case 'w': { |
| 323 | result.config.warn_time_set = true; | 402 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 324 | result.config.warn_time = strtod(optarg, NULL); | 403 | if (tmp.error != MP_PARSING_SUCCES) { |
| 325 | break; | 404 | die(STATE_UNKNOWN, "failed to parse warning connection time threshold"); |
| 326 | case 'c': | 405 | } |
| 327 | result.config.crit_time_set = true; | 406 | result.config.connection_time_threshold = |
| 328 | result.config.crit_time = strtod(optarg, NULL); | 407 | mp_thresholds_set_warn(result.config.connection_time_threshold, tmp.range); |
| 329 | break; | 408 | } break; |
| 330 | case 'W': | 409 | case 'c': { |
| 331 | result.config.warn_entries = optarg; | 410 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 332 | break; | 411 | if (tmp.error != MP_PARSING_SUCCES) { |
| 333 | case 'C': | 412 | die(STATE_UNKNOWN, "failed to parse critical connection time threshold"); |
| 334 | result.config.crit_entries = optarg; | 413 | } |
| 335 | break; | 414 | result.config.connection_time_threshold = |
| 415 | mp_thresholds_set_crit(result.config.connection_time_threshold, tmp.range); | ||
| 416 | } break; | ||
| 417 | case 'W': { | ||
| 418 | mp_range_parsed tmp = mp_parse_range_string(optarg); | ||
| 419 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 420 | die(STATE_UNKNOWN, "failed to parse number of entries warning threshold"); | ||
| 421 | } | ||
| 422 | result.config.entries_thresholds = | ||
| 423 | mp_thresholds_set_warn(result.config.entries_thresholds, tmp.range); | ||
| 424 | } break; | ||
| 425 | case 'C': { | ||
| 426 | mp_range_parsed tmp = mp_parse_range_string(optarg); | ||
| 427 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 428 | die(STATE_UNKNOWN, "failed to parse number of entries critical threshold"); | ||
| 429 | } | ||
| 430 | result.config.entries_thresholds = | ||
| 431 | mp_thresholds_set_crit(result.config.entries_thresholds, tmp.range); | ||
| 432 | } break; | ||
| 336 | #ifdef HAVE_LDAP_SET_OPTION | 433 | #ifdef HAVE_LDAP_SET_OPTION |
| 337 | case '2': | 434 | case '2': |
| 338 | result.config.ld_protocol = 2; | 435 | result.config.ld_protocol = 2; |
| @@ -371,6 +468,18 @@ check_ldap_config_wrapper process_arguments(int argc, char **argv) { | |||
| 371 | usage(_("IPv6 support not available\n")); | 468 | usage(_("IPv6 support not available\n")); |
| 372 | #endif | 469 | #endif |
| 373 | break; | 470 | break; |
| 471 | case output_format_index: { | ||
| 472 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 473 | if (!parser.parsing_success) { | ||
| 474 | // TODO List all available formats here, maybe add anothoer usage function | ||
| 475 | printf("Invalid output format: %s\n", optarg); | ||
| 476 | exit(STATE_UNKNOWN); | ||
| 477 | } | ||
| 478 | |||
| 479 | result.config.output_format_is_set = true; | ||
| 480 | result.config.output_format = parser.output_format; | ||
| 481 | break; | ||
| 482 | } | ||
| 374 | default: | 483 | default: |
| 375 | usage5(); | 484 | usage5(); |
| 376 | } | 485 | } |
| @@ -406,11 +515,6 @@ check_ldap_config_wrapper validate_arguments(check_ldap_config_wrapper config_wr | |||
| 406 | usage4(_("Please specify the LDAP base\n")); | 515 | usage4(_("Please specify the LDAP base\n")); |
| 407 | } | 516 | } |
| 408 | 517 | ||
| 409 | if (config_wrapper.config.crit_entries != NULL || config_wrapper.config.warn_entries != NULL) { | ||
| 410 | set_thresholds(&config_wrapper.config.entries_thresholds, | ||
| 411 | config_wrapper.config.warn_entries, config_wrapper.config.crit_entries); | ||
| 412 | } | ||
| 413 | |||
| 414 | if (config_wrapper.config.ld_passwd == NULL) { | 518 | if (config_wrapper.config.ld_passwd == NULL) { |
| 415 | config_wrapper.config.ld_passwd = getenv("LDAP_PASSWORD"); | 519 | config_wrapper.config.ld_passwd = getenv("LDAP_PASSWORD"); |
| 416 | } | 520 | } |
| @@ -471,6 +575,7 @@ void print_help(void) { | |||
| 471 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 575 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 472 | 576 | ||
| 473 | printf(UT_VERBOSE); | 577 | printf(UT_VERBOSE); |
| 578 | printf(UT_OUTPUT_FORMAT); | ||
| 474 | 579 | ||
| 475 | printf("\n"); | 580 | printf("\n"); |
| 476 | printf("%s\n", _("Notes:")); | 581 | printf("%s\n", _("Notes:")); |
diff --git a/plugins/check_ldap.d/config.h b/plugins/check_ldap.d/config.h index c8a40610..50191725 100644 --- a/plugins/check_ldap.d/config.h +++ b/plugins/check_ldap.d/config.h | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include "../../config.h" | 3 | #include "../../config.h" |
| 4 | #include "output.h" | ||
| 4 | #include "thresholds.h" | 5 | #include "thresholds.h" |
| 5 | #include <stddef.h> | 6 | #include <stddef.h> |
| 6 | 7 | ||
| @@ -25,13 +26,11 @@ typedef struct { | |||
| 25 | int ld_protocol; | 26 | int ld_protocol; |
| 26 | #endif | 27 | #endif |
| 27 | 28 | ||
| 28 | char *warn_entries; | 29 | mp_thresholds entries_thresholds; |
| 29 | char *crit_entries; | 30 | mp_thresholds connection_time_threshold; |
| 30 | thresholds *entries_thresholds; | 31 | |
| 31 | bool warn_time_set; | 32 | bool output_format_is_set; |
| 32 | double warn_time; | 33 | mp_output_format output_format; |
| 33 | bool crit_time_set; | ||
| 34 | double crit_time; | ||
| 35 | } check_ldap_config; | 34 | } check_ldap_config; |
| 36 | 35 | ||
| 37 | check_ldap_config check_ldap_config_init() { | 36 | check_ldap_config check_ldap_config_init() { |
| @@ -48,13 +47,10 @@ check_ldap_config check_ldap_config_init() { | |||
| 48 | .ld_protocol = DEFAULT_PROTOCOL, | 47 | .ld_protocol = DEFAULT_PROTOCOL, |
| 49 | #endif | 48 | #endif |
| 50 | 49 | ||
| 51 | .warn_entries = NULL, | 50 | .entries_thresholds = mp_thresholds_init(), |
| 52 | .crit_entries = NULL, | 51 | .connection_time_threshold = mp_thresholds_init(), |
| 53 | .entries_thresholds = NULL, | 52 | |
| 54 | .warn_time_set = false, | 53 | .output_format_is_set = false, |
| 55 | .warn_time = 0, | ||
| 56 | .crit_time_set = false, | ||
| 57 | .crit_time = 0, | ||
| 58 | }; | 54 | }; |
| 59 | return tmp; | 55 | return tmp; |
| 60 | } | 56 | } |
diff --git a/plugins/t/check_ldap.t b/plugins/t/check_ldap.t index fcba0393..f3162ebb 100644 --- a/plugins/t/check_ldap.t +++ b/plugins/t/check_ldap.t | |||
| @@ -32,7 +32,7 @@ SKIP: { | |||
| 32 | 32 | ||
| 33 | $result = NPTest->testCmd("$command -H $hostname_invalid -b ou=blah -t 5"); | 33 | $result = NPTest->testCmd("$command -H $hostname_invalid -b ou=blah -t 5"); |
| 34 | is( $result->return_code, 2, "$command -H $hostname_invalid -b ou=blah -t 5" ); | 34 | is( $result->return_code, 2, "$command -H $hostname_invalid -b ou=blah -t 5" ); |
| 35 | is( $result->output, 'Could not bind to the LDAP server', "output ok" ); | 35 | like( $result->output, '/could not bind to the LDAP server/', "output ok" ); |
| 36 | }; | 36 | }; |
| 37 | 37 | ||
| 38 | SKIP: { | 38 | SKIP: { |
| @@ -42,30 +42,30 @@ SKIP: { | |||
| 42 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3"; | 42 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3"; |
| 43 | $result = NPTest->testCmd($cmd); | 43 | $result = NPTest->testCmd($cmd); |
| 44 | is( $result->return_code, 0, $cmd ); | 44 | is( $result->return_code, 0, $cmd ); |
| 45 | like( $result->output, '/^LDAP OK - \d+.\d+ seconds response time\|time=\d+\.\d+s;2\.0+;3\.0+;0\.0+$/', "output ok" ); | 45 | like( $result->output, '/connection time \d+.\d+s/', "output ok" ); |
| 46 | 46 | ||
| 47 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000 -C 10000001"; | 47 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000 -C 10000001"; |
| 48 | $result = NPTest->testCmd($cmd); | 48 | $result = NPTest->testCmd($cmd); |
| 49 | is( $result->return_code, 0, $cmd ); | 49 | is( $result->return_code, 0, $cmd ); |
| 50 | like( $result->output, '/^LDAP OK - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;10000000;10000001;0\.0+$/', "output ok" ); | 50 | like( $result->output, '/found \d+ entries/', "output ok" ); |
| 51 | 51 | ||
| 52 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000: -C 10000001:"; | 52 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000: -C 10000001:"; |
| 53 | $result = NPTest->testCmd($cmd); | 53 | $result = NPTest->testCmd($cmd); |
| 54 | is( $result->return_code, 2, $cmd ); | 54 | is( $result->return_code, 2, $cmd ); |
| 55 | like( $result->output, '/^LDAP CRITICAL - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;10000000:;10000001:;0\.0+$/', "output ok" ); | 55 | like( $result->output, '/found \d+ entries/', "output ok" ); |
| 56 | 56 | ||
| 57 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 0 -C 0"; | 57 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 0 -C 0"; |
| 58 | $result = NPTest->testCmd($cmd); | 58 | $result = NPTest->testCmd($cmd); |
| 59 | is( $result->return_code, 2, $cmd ); | 59 | is( $result->return_code, 2, $cmd ); |
| 60 | like( $result->output, '/^LDAP CRITICAL - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;0;0;0\.0+$/', "output ok" ); | 60 | like( $result->output, '/found \d+ entries/', "output ok" ); |
| 61 | 61 | ||
| 62 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000: -C 10000001"; | 62 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -W 10000000: -C 10000001"; |
| 63 | $result = NPTest->testCmd($cmd); | 63 | $result = NPTest->testCmd($cmd); |
| 64 | is( $result->return_code, 1, $cmd ); | 64 | is( $result->return_code, 1, $cmd ); |
| 65 | like( $result->output, '/^LDAP WARNING - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;10000000:;10000001;0\.0+$/', "output ok" ); | 65 | like( $result->output, '/found \d+ entries/', "output ok" ); |
| 66 | 66 | ||
| 67 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -C 10000001"; | 67 | $cmd = "$command -H $host_tcp_ldap -b $ldap_base_dn -t 5 -w 2 -c 3 -3 -C 10000001"; |
| 68 | $result = NPTest->testCmd($cmd); | 68 | $result = NPTest->testCmd($cmd); |
| 69 | is( $result->return_code, 0, $cmd ); | 69 | is( $result->return_code, 0, $cmd ); |
| 70 | like( $result->output, '/^LDAP OK - found \d+ entries in \d+\.\d+ seconds\|time=\d\.\d+s;2\.0+;3\.0+;0\.0+ entries=\d+\.0+;;10000001;0\.0+$/', "output ok" ); | 70 | like( $result->output, '/found \d+ entries/', "output ok" ); |
| 71 | }; | 71 | }; |
