diff options
| -rw-r--r-- | plugins/check_curl.c | 35 | ||||
| -rw-r--r-- | plugins/check_curl.d/check_curl_helpers.c | 1 | ||||
| -rw-r--r-- | plugins/check_curl.d/config.h | 3 | ||||
| -rw-r--r-- | plugins/check_http.c | 25 | ||||
| -rw-r--r-- | plugins/t/check_curl.t | 29 |
5 files changed, 86 insertions, 7 deletions
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 | |||
| 273 | 273 | ||
| 274 | /* Curl errors, result in critical Nagios state */ | 274 | /* Curl errors, result in critical Nagios state */ |
| 275 | if (res != CURLE_OK) { | 275 | if (res != CURLE_OK) { |
| 276 | xasprintf(&sc_curl.output, _("Error while performing connection: cURL returned %d - %s"), | 276 | /* Custom handling for timeouts, state might be set to non CRITICAL */ |
| 277 | res, errbuf[0] ? errbuf : curl_easy_strerror(res)); | 277 | if (res == CURLE_OPERATION_TIMEDOUT) { |
| 278 | sc_curl = mp_set_subcheck_state(sc_curl, STATE_CRITICAL); | 278 | xasprintf(&sc_curl.output, _("cURL returned %d - %s"), res, |
| 279 | errbuf[0] ? errbuf : curl_easy_strerror(res)); | ||
| 280 | sc_curl = mp_set_subcheck_state(sc_curl, config.on_timeout_result_state); | ||
| 281 | } else { | ||
| 282 | xasprintf(&sc_curl.output, | ||
| 283 | _("Error while performing connection: cURL returned %d - %s"), res, | ||
| 284 | errbuf[0] ? errbuf : curl_easy_strerror(res)); | ||
| 285 | sc_curl = mp_set_subcheck_state(sc_curl, STATE_CRITICAL); | ||
| 286 | } | ||
| 279 | mp_add_subcheck_to_subcheck(&sc_result, sc_curl); | 287 | mp_add_subcheck_to_subcheck(&sc_result, sc_curl); |
| 280 | return sc_result; | 288 | return sc_result; |
| 281 | } | 289 | } |
| @@ -890,6 +898,7 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) { | |||
| 890 | STATE_REGEX, | 898 | STATE_REGEX, |
| 891 | OUTPUT_FORMAT, | 899 | OUTPUT_FORMAT, |
| 892 | NO_PROXY, | 900 | NO_PROXY, |
| 901 | TIMEOUT_RESULT, | ||
| 893 | }; | 902 | }; |
| 894 | 903 | ||
| 895 | static struct option longopts[] = { | 904 | static struct option longopts[] = { |
| @@ -939,6 +948,7 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) { | |||
| 939 | {"cookie-jar", required_argument, 0, COOKIE_JAR}, | 948 | {"cookie-jar", required_argument, 0, COOKIE_JAR}, |
| 940 | {"haproxy-protocol", no_argument, 0, HAPROXY_PROTOCOL}, | 949 | {"haproxy-protocol", no_argument, 0, HAPROXY_PROTOCOL}, |
| 941 | {"output-format", required_argument, 0, OUTPUT_FORMAT}, | 950 | {"output-format", required_argument, 0, OUTPUT_FORMAT}, |
| 951 | {"timeout-result", required_argument, 0, TIMEOUT_RESULT}, | ||
| 942 | {0, 0, 0, 0}}; | 952 | {0, 0, 0, 0}}; |
| 943 | 953 | ||
| 944 | check_curl_config_wrapper result = { | 954 | check_curl_config_wrapper result = { |
| @@ -1004,6 +1014,21 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) { | |||
| 1004 | result.config.curl_config.socket_timeout = (int)strtol(optarg, NULL, 10); | 1014 | result.config.curl_config.socket_timeout = (int)strtol(optarg, NULL, 10); |
| 1005 | } | 1015 | } |
| 1006 | break; | 1016 | break; |
| 1017 | case TIMEOUT_RESULT: | ||
| 1018 | if (!strcmp(optarg, "0") || !strcasecmp(optarg, "ok")) { | ||
| 1019 | result.config.on_timeout_result_state = STATE_OK; | ||
| 1020 | } else if (!strcmp(optarg, "1") || !strcasecmp(optarg, "warning")) { | ||
| 1021 | result.config.on_timeout_result_state = STATE_WARNING; | ||
| 1022 | } else if (!strcmp(optarg, "2") || !strcasecmp(optarg, "critical")) { | ||
| 1023 | result.config.on_timeout_result_state = STATE_CRITICAL; | ||
| 1024 | } else if (!strcmp(optarg, "3") || !strcasecmp(optarg, "unknown")) { | ||
| 1025 | result.config.on_timeout_result_state = STATE_UNKNOWN; | ||
| 1026 | } else { | ||
| 1027 | usage2(_("Invalid timeout-result state option, give either a return code or state " | ||
| 1028 | "name in lowercase"), | ||
| 1029 | optarg); | ||
| 1030 | } | ||
| 1031 | break; | ||
| 1007 | case 'c': /* critical time threshold */ | 1032 | case 'c': /* critical time threshold */ |
| 1008 | { | 1033 | { |
| 1009 | mp_range_parsed critical_range = mp_parse_range_string(optarg); | 1034 | mp_range_parsed critical_range = mp_parse_range_string(optarg); |
| @@ -1701,6 +1726,10 @@ void print_help(void) { | |||
| 1701 | 1726 | ||
| 1702 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 1727 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 1703 | 1728 | ||
| 1729 | printf(" %s\n", "--timeout-result=ok|warning|critical|unknown|0|1|2|3"); | ||
| 1730 | printf(" %s\n", _("Timeouts default to returning STATE_CRITICAL.")); | ||
| 1731 | printf(" %s\n", _("This argument changes the return state on timeouts.")); | ||
| 1732 | |||
| 1704 | printf(UT_VERBOSE); | 1733 | printf(UT_VERBOSE); |
| 1705 | 1734 | ||
| 1706 | printf(UT_OUTPUT_FORMAT); | 1735 | printf(UT_OUTPUT_FORMAT); |
diff --git a/plugins/check_curl.d/check_curl_helpers.c b/plugins/check_curl.d/check_curl_helpers.c index 80d6f4f6..5b13a138 100644 --- a/plugins/check_curl.d/check_curl_helpers.c +++ b/plugins/check_curl.d/check_curl_helpers.c | |||
| @@ -755,6 +755,7 @@ check_curl_config check_curl_config_init(void) { | |||
| 755 | .header_expect = "", | 755 | .header_expect = "", |
| 756 | .on_redirect_result_state = STATE_OK, | 756 | .on_redirect_result_state = STATE_OK, |
| 757 | .on_redirect_dependent = false, | 757 | .on_redirect_dependent = false, |
| 758 | .on_timeout_result_state = STATE_CRITICAL, | ||
| 758 | 759 | ||
| 759 | .show_extended_perfdata = false, | 760 | .show_extended_perfdata = false, |
| 760 | .show_body = false, | 761 | .show_body = false, |
diff --git a/plugins/check_curl.d/config.h b/plugins/check_curl.d/config.h index bcdf3010..0a7fa01d 100644 --- a/plugins/check_curl.d/config.h +++ b/plugins/check_curl.d/config.h | |||
| @@ -113,9 +113,12 @@ typedef struct { | |||
| 113 | mp_state_enum on_redirect_result_state; | 113 | mp_state_enum on_redirect_result_state; |
| 114 | bool on_redirect_dependent; | 114 | bool on_redirect_dependent; |
| 115 | 115 | ||
| 116 | mp_state_enum on_timeout_result_state; | ||
| 117 | |||
| 116 | bool show_extended_perfdata; | 118 | bool show_extended_perfdata; |
| 117 | bool show_body; | 119 | bool show_body; |
| 118 | 120 | ||
| 121 | |||
| 119 | bool output_format_is_set; | 122 | bool output_format_is_set; |
| 120 | mp_output_format output_format; | 123 | mp_output_format output_format; |
| 121 | } check_curl_config; | 124 | } check_curl_config; |
diff --git a/plugins/check_http.c b/plugins/check_http.c index 71f94b91..73e4f3b6 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c | |||
| @@ -205,7 +205,8 @@ bool process_arguments(int argc, char **argv) { | |||
| 205 | SNI_OPTION, | 205 | SNI_OPTION, |
| 206 | MAX_REDIRS_OPTION, | 206 | MAX_REDIRS_OPTION, |
| 207 | CONTINUE_AFTER_CHECK_CERT, | 207 | CONTINUE_AFTER_CHECK_CERT, |
| 208 | STATE_REGEX | 208 | STATE_REGEX, |
| 209 | TIMEOUT_RESULT | ||
| 209 | }; | 210 | }; |
| 210 | 211 | ||
| 211 | int option = 0; | 212 | int option = 0; |
| @@ -247,6 +248,7 @@ bool process_arguments(int argc, char **argv) { | |||
| 247 | {"extended-perfdata", no_argument, 0, 'E'}, | 248 | {"extended-perfdata", no_argument, 0, 'E'}, |
| 248 | {"show-body", no_argument, 0, 'B'}, | 249 | {"show-body", no_argument, 0, 'B'}, |
| 249 | {"max-redirs", required_argument, 0, MAX_REDIRS_OPTION}, | 250 | {"max-redirs", required_argument, 0, MAX_REDIRS_OPTION}, |
| 251 | {"timeout-result", required_argument, 0, TIMEOUT_RESULT}, | ||
| 250 | {0, 0, 0, 0}}; | 252 | {0, 0, 0, 0}}; |
| 251 | 253 | ||
| 252 | if (argc < 2) { | 254 | if (argc < 2) { |
| @@ -298,6 +300,21 @@ bool process_arguments(int argc, char **argv) { | |||
| 298 | socket_timeout = atoi(optarg); | 300 | socket_timeout = atoi(optarg); |
| 299 | } | 301 | } |
| 300 | break; | 302 | break; |
| 303 | case TIMEOUT_RESULT: | ||
| 304 | if (!strcmp(optarg, "0") || !strcasecmp(optarg, "ok")) { | ||
| 305 | socket_timeout_state = STATE_OK; | ||
| 306 | } else if (!strcmp(optarg, "1") || !strcasecmp(optarg, "warning")) { | ||
| 307 | socket_timeout_state = STATE_WARNING; | ||
| 308 | } else if (!strcmp(optarg, "2") || !strcasecmp(optarg, "critical")) { | ||
| 309 | socket_timeout_state = STATE_CRITICAL; | ||
| 310 | } else if (!strcmp(optarg, "3") || !strcasecmp(optarg, "unknown")) { | ||
| 311 | socket_timeout_state = STATE_UNKNOWN; | ||
| 312 | } else { | ||
| 313 | usage2(_("Invalid timeout-result state option, give either a return code or state " | ||
| 314 | "name in lowercase"), | ||
| 315 | optarg); | ||
| 316 | } | ||
| 317 | break; | ||
| 301 | case 'c': /* critical time threshold */ | 318 | case 'c': /* critical time threshold */ |
| 302 | critical_thresholds = optarg; | 319 | critical_thresholds = optarg; |
| 303 | break; | 320 | break; |
| @@ -1032,7 +1049,7 @@ int check_http(void) { | |||
| 1032 | printf("SSL initialized\n"); | 1049 | printf("SSL initialized\n"); |
| 1033 | } | 1050 | } |
| 1034 | if (result != STATE_OK) { | 1051 | if (result != STATE_OK) { |
| 1035 | die(STATE_CRITICAL, _("HTTP CRITICAL - SSL error\n")); | 1052 | die(STATE_CRITICAL, _("HTTP CRITICAL - SSL error\n")); |
| 1036 | } | 1053 | } |
| 1037 | microsec_ssl = deltime(tv_temp); | 1054 | microsec_ssl = deltime(tv_temp); |
| 1038 | elapsed_time_ssl = (double)microsec_ssl / 1.0e6; | 1055 | elapsed_time_ssl = (double)microsec_ssl / 1.0e6; |
| @@ -1883,6 +1900,10 @@ void print_help(void) { | |||
| 1883 | 1900 | ||
| 1884 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 1901 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 1885 | 1902 | ||
| 1903 | printf(" %s\n", "--timeout-result=ok|warning|critical|unknown|0|1|2|3"); | ||
| 1904 | printf(" %s\n", _("Timeouts default to returning STATE_CRITICAL.")); | ||
| 1905 | printf(" %s\n", _("This argument changes the return state on timeouts.")); | ||
| 1906 | |||
| 1886 | printf(UT_VERBOSE); | 1907 | printf(UT_VERBOSE); |
| 1887 | 1908 | ||
| 1888 | printf("\n"); | 1909 | printf("\n"); |
diff --git a/plugins/t/check_curl.t b/plugins/t/check_curl.t index 0f4d0de7..7ec8ca06 100644 --- a/plugins/t/check_curl.t +++ b/plugins/t/check_curl.t | |||
| @@ -13,7 +13,7 @@ use vars qw($tests $has_ipv6); | |||
| 13 | BEGIN { | 13 | BEGIN { |
| 14 | use NPTest; | 14 | use NPTest; |
| 15 | $has_ipv6 = NPTest::has_ipv6(); | 15 | $has_ipv6 = NPTest::has_ipv6(); |
| 16 | $tests = $has_ipv6 ? 57 : 92; | 16 | $tests = $has_ipv6 ? 65 : 100; |
| 17 | plan tests => $tests; | 17 | plan tests => $tests; |
| 18 | } | 18 | } |
| 19 | 19 | ||
| @@ -69,7 +69,32 @@ $res = NPTest->testCmd( | |||
| 69 | ); | 69 | ); |
| 70 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); | 70 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); |
| 71 | # was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) | 71 | # was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) |
| 72 | like( $res->output, "/cURL returned 28 - Connection timed out after/", "Output OK"); | 72 | like( $res->output, "/cURL returned 28 - (Connection|Operation) timed out after/", "Output OK"); |
| 73 | |||
| 74 | # timeout return results can be changed using --timeout-result option | ||
| 75 | $res = NPTest->testCmd( | ||
| 76 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3 --timeout-result ok" | ||
| 77 | ); | ||
| 78 | like( $res->output, "/cURL returned 28 - (Connection|Operation) timed out after/", "Output OK"); | ||
| 79 | cmp_ok( $res->return_code, "==", 0, "Return code is correct due argument: --timeout-result ok"); | ||
| 80 | |||
| 81 | $res = NPTest->testCmd( | ||
| 82 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3 --timeout-result warning" | ||
| 83 | ); | ||
| 84 | like( $res->output, "/cURL returned 28 - (Connection|Operation) timed out after/", "Output OK"); | ||
| 85 | cmp_ok( $res->return_code, "==", 1, "Return code is correct due argument: --timeout-result warning"); | ||
| 86 | |||
| 87 | $res = NPTest->testCmd( | ||
| 88 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3 --timeout-result 2" | ||
| 89 | ); | ||
| 90 | like( $res->output, "/cURL returned 28 - (Connection|Operation) timed out after/", "Output OK"); | ||
| 91 | cmp_ok( $res->return_code, "==", 2, "Return code is correct due argument: --timeout-result 2"); | ||
| 92 | |||
| 93 | $res = NPTest->testCmd( | ||
| 94 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3 --timeout-result 3" | ||
| 95 | ); | ||
| 96 | like( $res->output, "/cURL returned 28 - (Connection|Operation) timed out after/", "Output OK"); | ||
| 97 | cmp_ok( $res->return_code, "==", 3, "Return code is correct due argument: --timeout-result 3"); | ||
| 73 | 98 | ||
| 74 | $res = NPTest->testCmd( | 99 | $res = NPTest->testCmd( |
| 75 | "./$plugin $hostname_invalid -wt 1 -ct 2" | 100 | "./$plugin $hostname_invalid -wt 1 -ct 2" |
