diff options
| author | Lorenz <12514511+RincewindsHat@users.noreply.github.com> | 2022-11-07 17:48:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-07 17:48:28 +0100 |
| commit | 4a5ddd201119260028db6a4f27027d72aa9a160a (patch) | |
| tree | ec8d01d90064f59cc82dd6c60ec7d9b6c3abc236 /plugins | |
| parent | 8708fd21a66656e297f1a7e6b2b679d932845ef1 (diff) | |
| download | monitoring-plugins-4a5ddd201119260028db6a4f27027d72aa9a160a.tar.gz | |
Check curl detect ipv6 (#1809)
* If server_address is an IPv6 address surround it with brackets
* If the message is too short, we should not have an underflow
* Add simple conditional test case available if IPv6 is
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/check_curl.c | 22 | ||||
| -rw-r--r-- | plugins/t/check_curl.t | 39 |
2 files changed, 46 insertions, 15 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c index a69854a8..2ad373c0 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c | |||
| @@ -476,6 +476,18 @@ check_http (void) | |||
| 476 | printf ("* curl CURLOPT_RESOLVE: %s\n", dnscache); | 476 | printf ("* curl CURLOPT_RESOLVE: %s\n", dnscache); |
| 477 | } | 477 | } |
| 478 | 478 | ||
| 479 | // If server_address is an IPv6 address it must be surround by square brackets | ||
| 480 | struct in6_addr tmp_in_addr; | ||
| 481 | if (inet_pton(AF_INET6, server_address, &tmp_in_addr) == 1) { | ||
| 482 | char *new_server_address = malloc(strlen(server_address) + 3); | ||
| 483 | if (new_server_address == NULL) { | ||
| 484 | die(STATE_UNKNOWN, "HTTP UNKNOWN - Unable to allocate memory\n"); | ||
| 485 | } | ||
| 486 | snprintf(new_server_address, strlen(server_address)+3, "[%s]", server_address); | ||
| 487 | free(server_address); | ||
| 488 | server_address = new_server_address; | ||
| 489 | } | ||
| 490 | |||
| 479 | /* compose URL: use the address we want to connect to, set Host: header later */ | 491 | /* compose URL: use the address we want to connect to, set Host: header later */ |
| 480 | snprintf (url, DEFAULT_BUFFER_SIZE, "%s://%s:%d%s", | 492 | snprintf (url, DEFAULT_BUFFER_SIZE, "%s://%s:%d%s", |
| 481 | use_ssl ? "https" : "http", | 493 | use_ssl ? "https" : "http", |
| @@ -999,10 +1011,12 @@ GOT_FIRST_CERT: | |||
| 999 | result = max_state_alt(get_status(total_time, thlds), result); | 1011 | result = max_state_alt(get_status(total_time, thlds), result); |
| 1000 | 1012 | ||
| 1001 | /* Cut-off trailing characters */ | 1013 | /* Cut-off trailing characters */ |
| 1002 | if(msg[strlen(msg)-2] == ',') | 1014 | if (strlen(msg) >= 2) { |
| 1003 | msg[strlen(msg)-2] = '\0'; | 1015 | if(msg[strlen(msg)-2] == ',') |
| 1004 | else | 1016 | msg[strlen(msg)-2] = '\0'; |
| 1005 | msg[strlen(msg)-3] = '\0'; | 1017 | else |
| 1018 | msg[strlen(msg)-3] = '\0'; | ||
| 1019 | } | ||
| 1006 | 1020 | ||
| 1007 | /* TODO: separate _() msg and status code: die (result, "HTTP %s: %s\n", state_text(result), msg); */ | 1021 | /* TODO: separate _() msg and status code: die (result, "HTTP %s: %s\n", state_text(result), msg); */ |
| 1008 | die (result, "HTTP %s: %s %d %s%s%s - %d bytes in %.3f second response time %s|%s\n%s%s", | 1022 | die (result, "HTTP %s: %s %d %s%s%s - %d bytes in %.3f second response time %s|%s\n%s%s", |
diff --git a/plugins/t/check_curl.t b/plugins/t/check_curl.t index 693f4b25..eae98cc1 100644 --- a/plugins/t/check_curl.t +++ b/plugins/t/check_curl.t | |||
| @@ -1,15 +1,22 @@ | |||
| 1 | #! /usr/bin/perl -w -I .. | 1 | #! /usr/bin/perl -w -I .. |
| 2 | # | 2 | # |
| 3 | # HyperText Transfer Protocol (HTTP) Test via check_http | 3 | # HyperText Transfer Protocol (HTTP) Test via check_curl |
| 4 | # | 4 | # |
| 5 | # | 5 | # |
| 6 | 6 | ||
| 7 | use strict; | 7 | use strict; |
| 8 | use Test::More; | 8 | use Test::More; |
| 9 | use POSIX qw/mktime strftime/; | 9 | use POSIX qw/mktime strftime/; |
| 10 | use NPTest; | ||
| 11 | 10 | ||
| 12 | plan tests => 57; | 11 | use vars qw($tests $has_ipv6); |
| 12 | |||
| 13 | BEGIN { | ||
| 14 | use NPTest; | ||
| 15 | $has_ipv6 = NPTest::has_ipv6(); | ||
| 16 | $tests = $has_ipv6 ? 59 : 57; | ||
| 17 | plan tests => $tests; | ||
| 18 | } | ||
| 19 | |||
| 13 | 20 | ||
| 14 | my $successOutput = '/OK.*HTTP.*second/'; | 21 | my $successOutput = '/OK.*HTTP.*second/'; |
| 15 | 22 | ||
| @@ -18,6 +25,7 @@ my $plugin = 'check_http'; | |||
| 18 | $plugin = 'check_curl' if $0 =~ m/check_curl/mx; | 25 | $plugin = 'check_curl' if $0 =~ m/check_curl/mx; |
| 19 | 26 | ||
| 20 | my $host_tcp_http = getTestParameter("NP_HOST_TCP_HTTP", "A host providing the HTTP Service (a web server)", "localhost"); | 27 | my $host_tcp_http = getTestParameter("NP_HOST_TCP_HTTP", "A host providing the HTTP Service (a web server)", "localhost"); |
| 28 | my $host_tcp_http_ipv6 = getTestParameter("NP_HOST_TCP_HTTP_IPV6", "An IPv6 address providing a HTTP Service (a web server)", "::1"); | ||
| 21 | my $host_tls_http = getTestParameter("NP_HOST_TLS_HTTP", "A host providing the HTTPS Service (a tls web server)", "localhost"); | 29 | my $host_tls_http = getTestParameter("NP_HOST_TLS_HTTP", "A host providing the HTTPS Service (a tls web server)", "localhost"); |
| 22 | my $host_tls_cert = getTestParameter("NP_HOST_TLS_CERT", "the common name of the certificate.", "localhost"); | 30 | my $host_tls_cert = getTestParameter("NP_HOST_TLS_CERT", "the common name of the certificate.", "localhost"); |
| 23 | my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); | 31 | my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); |
| @@ -31,26 +39,35 @@ my $faketime = -x '/usr/bin/faketime' ? 1 : 0; | |||
| 31 | 39 | ||
| 32 | 40 | ||
| 33 | $res = NPTest->testCmd( | 41 | $res = NPTest->testCmd( |
| 34 | "./$plugin $host_tcp_http -wt 300 -ct 600" | 42 | "./$plugin $host_tcp_http -wt 300 -ct 600" |
| 35 | ); | 43 | ); |
| 36 | cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" ); | 44 | cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" ); |
| 37 | like( $res->output, $successOutput, "Output OK" ); | 45 | like( $res->output, $successOutput, "Output OK" ); |
| 38 | 46 | ||
| 47 | if ($has_ipv6) { | ||
| 48 | # Test for IPv6 formatting | ||
| 49 | $res = NPTest->testCmd( | ||
| 50 | "./$plugin -I $host_tcp_http_ipv6 -wt 300 -ct 600" | ||
| 51 | ); | ||
| 52 | cmp_ok( $res->return_code, '==', 0, "IPv6 URL formatting is working" ); | ||
| 53 | like( $res->output, $successOutput, "Output OK" ); | ||
| 54 | } | ||
| 55 | |||
| 39 | $res = NPTest->testCmd( | 56 | $res = NPTest->testCmd( |
| 40 | "./$plugin $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'" | 57 | "./$plugin $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'" |
| 41 | ); | 58 | ); |
| 42 | like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" ); | 59 | like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" ); |
| 43 | 60 | ||
| 44 | $res = NPTest->testCmd( | 61 | $res = NPTest->testCmd( |
| 45 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" | 62 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" |
| 46 | ); | 63 | ); |
| 47 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); | 64 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); |
| 48 | # was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) | 65 | # was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) |
| 49 | like( $res->output, "/HTTP CRITICAL - Invalid HTTP response received from host on port 80: cURL returned 28 - Connection timed out after/", "Output OK"); | 66 | like( $res->output, "/HTTP CRITICAL - Invalid HTTP response received from host on port 80: cURL returned 28 - Connection timed out after/", "Output OK"); |
| 50 | 67 | ||
| 51 | $res = NPTest->testCmd( | 68 | $res = NPTest->testCmd( |
| 52 | "./$plugin $hostname_invalid -wt 1 -ct 2" | 69 | "./$plugin $hostname_invalid -wt 1 -ct 2" |
| 53 | ); | 70 | ); |
| 54 | cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" ); | 71 | cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" ); |
| 55 | # The first part of the message comes from the OS catalogue, so cannot check this. | 72 | # The first part of the message comes from the OS catalogue, so cannot check this. |
| 56 | # On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename | 73 | # On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename |
