use errbuf[0] or curl_easy_strerror(res) on timeouts, just like other curl errors

adjust the existing timeout test and add more tests utilizing --timeout-state
This commit is contained in:
Ahmet Oeztuerk 2026-05-12 13:42:41 +02:00
parent 5173d05246
commit 9d9bc68b6a
2 changed files with 34 additions and 6 deletions

View file

@ -273,7 +273,8 @@ mp_subcheck check_http(const check_curl_config config, check_curl_working_state
/* Custom handling for timeouts */
if (res == CURLE_OPERATION_TIMEDOUT) {
xasprintf(&sc_curl.output, _("cURL got a timeout error"));
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);
mp_add_subcheck_to_subcheck(&sc_result, sc_curl);
return sc_result;
@ -1024,7 +1025,9 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) {
} else if (!strcmp(optarg, "3") || !strcmp(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);
usage2(_("Invalid timeout-result state option, give either a return code or state "
"name in lowercase"),
optarg);
}
break;
case 'c': /* critical time threshold */
@ -1725,8 +1728,8 @@ 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(" %s\n", _("Timeouts default to returning STATE_CRITICAL."));
printf(" %s\n", _("This argument changes the return state on timeouts."));
printf(UT_VERBOSE);

View file

@ -13,7 +13,7 @@ use vars qw($tests $has_ipv6);
BEGIN {
use NPTest;
$has_ipv6 = NPTest::has_ipv6();
$tests = $has_ipv6 ? 57 : 92;
$tests = $has_ipv6 ? 65 : 100;
plan tests => $tests;
}
@ -69,7 +69,32 @@ $res = NPTest->testCmd(
);
cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" );
# was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!)
like( $res->output, "/cURL returned 28 - Connection timed out after/", "Output OK");
like( $res->output, "/cURL returned 28 - Operation timed out after/", "Output OK");
# timeout return results can be changed using --timeout-result option
$res = NPTest->testCmd(
"./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3 --timeout-result ok"
);
like( $res->output, "/cURL returned 28 - Operation timed out after/", "Output OK");
cmp_ok( $res->return_code, "==", 0, "Return code is correct due argument: --timeout-result ok");
$res = NPTest->testCmd(
"./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3 --timeout-result warning"
);
like( $res->output, "/cURL returned 28 - Operation timed out after/", "Output OK");
cmp_ok( $res->return_code, "==", 1, "Return code is correct due argument: --timeout-result warning");
$res = NPTest->testCmd(
"./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3 --timeout-result 2"
);
like( $res->output, "/cURL returned 28 - Operation timed out after/", "Output OK");
cmp_ok( $res->return_code, "==", 2, "Return code is correct due argument: --timeout-result 2");
$res = NPTest->testCmd(
"./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3 --timeout-result 3"
);
like( $res->output, "/cURL returned 28 - Operation timed out after/", "Output OK");
cmp_ok( $res->return_code, "==", 3, "Return code is correct due argument: --timeout-result 3");
$res = NPTest->testCmd(
"./$plugin $hostname_invalid -wt 1 -ct 2"