From 178e9a02e367ab4527129ddbc623401e245d00b2 Mon Sep 17 00:00:00 2001 From: inqrphl <32687873+inqrphl@users.noreply.github.com> Date: Tue, 19 May 2026 16:01:43 +0200 Subject: check_http and check_curl: custom timeout return state (#2266) check_curl + check_http: hdd cli argument to return custom states on timeout --------- Co-authored-by: Ahmet Oeztuerk --- plugins/check_curl.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'plugins/check_curl.c') diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 67d89129..3f44c86b 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c @@ -273,9 +273,17 @@ mp_subcheck check_http(const check_curl_config config, check_curl_working_state /* Curl errors, result in critical Nagios state */ if (res != CURLE_OK) { - xasprintf(&sc_curl.output, _("Error while performing connection: cURL returned %d - %s"), - res, errbuf[0] ? errbuf : curl_easy_strerror(res)); - sc_curl = mp_set_subcheck_state(sc_curl, STATE_CRITICAL); + /* Custom handling for timeouts, state might be set to non CRITICAL */ + if (res == CURLE_OPERATION_TIMEDOUT) { + xasprintf(&sc_curl.output, _("cURL returned %d - %s"), res, + errbuf[0] ? errbuf : curl_easy_strerror(res)); + sc_curl = mp_set_subcheck_state(sc_curl, config.on_timeout_result_state); + } else { + xasprintf(&sc_curl.output, + _("Error while performing connection: cURL returned %d - %s"), res, + errbuf[0] ? errbuf : curl_easy_strerror(res)); + sc_curl = mp_set_subcheck_state(sc_curl, STATE_CRITICAL); + } mp_add_subcheck_to_subcheck(&sc_result, sc_curl); return sc_result; } @@ -890,6 +898,7 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) { STATE_REGEX, OUTPUT_FORMAT, NO_PROXY, + TIMEOUT_RESULT, }; static struct option longopts[] = { @@ -939,6 +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-result", required_argument, 0, TIMEOUT_RESULT}, {0, 0, 0, 0}}; check_curl_config_wrapper result = { @@ -1004,6 +1014,21 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) { result.config.curl_config.socket_timeout = (int)strtol(optarg, NULL, 10); } break; + case TIMEOUT_RESULT: + if (!strcmp(optarg, "0") || !strcasecmp(optarg, "ok")) { + result.config.on_timeout_result_state = STATE_OK; + } else if (!strcmp(optarg, "1") || !strcasecmp(optarg, "warning")) { + result.config.on_timeout_result_state = STATE_WARNING; + } else if (!strcmp(optarg, "2") || !strcasecmp(optarg, "critical")) { + result.config.on_timeout_result_state = STATE_CRITICAL; + } else if (!strcmp(optarg, "3") || !strcasecmp(optarg, "unknown")) { + result.config.on_timeout_result_state = STATE_UNKNOWN; + } else { + usage2(_("Invalid timeout-result state option, give either a return code or state " + "name in lowercase"), + optarg); + } + break; case 'c': /* critical time threshold */ { mp_range_parsed critical_range = mp_parse_range_string(optarg); @@ -1701,6 +1726,10 @@ void print_help(void) { printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); + printf(" %s\n", "--timeout-result=ok|warning|critical|unknown|0|1|2|3"); + 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); -- cgit v1.2.3-74-g34f1