check_http and check_curl improve --timeout-state

return sc_result instead of subcheck sc_curl on check_curl on timeouts

change argument  --timeout-custom-result-state to --timeout-state

add support for specifying --timeout-state in integers 0,1,2,3

add cli help messages for --timeout-state

clang-format
This commit is contained in:
Ahmet Oeztuerk 2026-05-12 12:14:11 +02:00
parent 4f35a424b9
commit 5173d05246
2 changed files with 28 additions and 18 deletions

View file

@ -276,7 +276,7 @@ mp_subcheck check_http(const check_curl_config config, check_curl_working_state
xasprintf(&sc_curl.output, _("cURL got a timeout error"));
sc_curl = mp_set_subcheck_state(sc_curl, config.on_timeout_result_state);
mp_add_subcheck_to_subcheck(&sc_result, sc_curl);
return sc_curl;
return sc_result;
}
/* Curl errors, result in critical Nagios state */
@ -898,7 +898,7 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) {
STATE_REGEX,
OUTPUT_FORMAT,
NO_PROXY,
TIMEOUT_CUSTOM_RESULT_STATE,
TIMEOUT_RESULT,
};
static struct option longopts[] = {
@ -948,7 +948,7 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) {
{"cookie-jar", required_argument, 0, COOKIE_JAR},
{"haproxy-protocol", no_argument, 0, HAPROXY_PROTOCOL},
{"output-format", required_argument, 0, OUTPUT_FORMAT},
{"timeout-custom-result-state", required_argument, 0, TIMEOUT_CUSTOM_RESULT_STATE},
{"timeout-result", required_argument, 0, TIMEOUT_RESULT},
{0, 0, 0, 0}};
check_curl_config_wrapper result = {
@ -1014,17 +1014,17 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) {
result.config.curl_config.socket_timeout = (int)strtol(optarg, NULL, 10);
}
break;
case TIMEOUT_CUSTOM_RESULT_STATE:
if (!strcmp(optarg, "ok")) {
case TIMEOUT_RESULT:
if (!strcmp(optarg, "0") || !strcmp(optarg, "ok")) {
result.config.on_timeout_result_state = STATE_OK;
} else if (!strcmp(optarg, "warning")) {
} else if (!strcmp(optarg, "1") || !strcmp(optarg, "warning")) {
result.config.on_timeout_result_state = STATE_WARNING;
} else if (!strcmp(optarg, "critical")) {
} else if (!strcmp(optarg, "2") || !strcmp(optarg, "critical")) {
result.config.on_timeout_result_state = STATE_CRITICAL;
} else if (!strcmp(optarg, "unknown")) {
} else if (!strcmp(optarg, "3") || !strcmp(optarg, "unknown")) {
result.config.on_timeout_result_state = STATE_UNKNOWN;
} else {
usage2(_("Invalid custom timeout result state option"), optarg);
usage2(_("Invalid timeout-result state option, give either a return code or state name in lowercase"), optarg);
}
break;
case 'c': /* critical time threshold */
@ -1724,6 +1724,10 @@ void print_help(void) {
printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
printf(" %s\n", "--timeout-result=RESULT|INTEGER");
printf(" %s\n",_("Timeouts default to returning STATE_CRITICAL."));
printf(" %s\n",_("This argument changes the return state on timeouts."));
printf(UT_VERBOSE);
printf(UT_OUTPUT_FORMAT);

View file

@ -206,7 +206,7 @@ bool process_arguments(int argc, char **argv) {
MAX_REDIRS_OPTION,
CONTINUE_AFTER_CHECK_CERT,
STATE_REGEX,
TIMEOUT_CUSTOM_RESULT_STATE
TIMEOUT_RESULT
};
int option = 0;
@ -248,7 +248,7 @@ bool process_arguments(int argc, char **argv) {
{"extended-perfdata", no_argument, 0, 'E'},
{"show-body", no_argument, 0, 'B'},
{"max-redirs", required_argument, 0, MAX_REDIRS_OPTION},
{"timeout-custom-result-state", required_argument, 0, TIMEOUT_CUSTOM_RESULT_STATE},
{"timeout-result", required_argument, 0, TIMEOUT_RESULT},
{0, 0, 0, 0}};
if (argc < 2) {
@ -300,17 +300,19 @@ bool process_arguments(int argc, char **argv) {
socket_timeout = atoi(optarg);
}
break;
case TIMEOUT_CUSTOM_RESULT_STATE:
if (!strcmp(optarg, "ok")) {
case TIMEOUT_RESULT:
if (!strcmp(optarg, "0") || !strcmp(optarg, "ok")) {
socket_timeout_state = STATE_OK;
} else if (!strcmp(optarg, "warning")) {
} else if (!strcmp(optarg, "1") || !strcmp(optarg, "warning")) {
socket_timeout_state = STATE_WARNING;
} else if (!strcmp(optarg, "critical")) {
} else if (!strcmp(optarg, "2") || !strcmp(optarg, "critical")) {
socket_timeout_state = STATE_CRITICAL;
} else if (!strcmp(optarg, "unknown")) {
} else if (!strcmp(optarg, "3") || !strcmp(optarg, "unknown")) {
socket_timeout_state = STATE_UNKNOWN;
} else {
usage2(_("Invalid custom timeout result state option"), optarg);
usage2(_("Invalid timeout-result state option, give either a return code or state "
"name in lowercase"),
optarg);
}
break;
case 'c': /* critical time threshold */
@ -1047,7 +1049,7 @@ int check_http(void) {
printf("SSL initialized\n");
}
if (result != STATE_OK) {
die(STATE_CRITICAL, _("HTTP CRITICAL - SSL error\n"));
die(STATE_CRITICAL, _("HTTP CRITICAL - SSL error\n"));
}
microsec_ssl = deltime(tv_temp);
elapsed_time_ssl = (double)microsec_ssl / 1.0e6;
@ -1898,6 +1900,10 @@ void print_help(void) {
printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
printf(" %s\n", "--timeout-result=RESULT|INTEGER");
printf(" %s\n", _("Timeouts default to returning STATE_CRITICAL."));
printf(" %s\n", _("This argument changes the return state on timeouts."));
printf(UT_VERBOSE);
printf("\n");