diff options
Diffstat (limited to 'plugins/t/check_curl.t')
-rw-r--r-- | plugins/t/check_curl.t | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/plugins/t/check_curl.t b/plugins/t/check_curl.t new file mode 100644 index 00000000..eae98cc1 --- /dev/null +++ b/plugins/t/check_curl.t | |||
@@ -0,0 +1,213 @@ | |||
1 | #! /usr/bin/perl -w -I .. | ||
2 | # | ||
3 | # HyperText Transfer Protocol (HTTP) Test via check_curl | ||
4 | # | ||
5 | # | ||
6 | |||
7 | use strict; | ||
8 | use Test::More; | ||
9 | use POSIX qw/mktime strftime/; | ||
10 | |||
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 | |||
20 | |||
21 | my $successOutput = '/OK.*HTTP.*second/'; | ||
22 | |||
23 | my $res; | ||
24 | my $plugin = 'check_http'; | ||
25 | $plugin = 'check_curl' if $0 =~ m/check_curl/mx; | ||
26 | |||
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"); | ||
29 | my $host_tls_http = getTestParameter("NP_HOST_TLS_HTTP", "A host providing the HTTPS Service (a tls web server)", "localhost"); | ||
30 | my $host_tls_cert = getTestParameter("NP_HOST_TLS_CERT", "the common name of the certificate.", "localhost"); | ||
31 | my $host_nonresponsive = getTestParameter("NP_HOST_NONRESPONSIVE", "The hostname of system not responsive to network requests", "10.0.0.1"); | ||
32 | my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost"); | ||
33 | my $internet_access = getTestParameter("NP_INTERNET_ACCESS", "Is this system directly connected to the internet?", "yes"); | ||
34 | my $host_tcp_http2 = getTestParameter("NP_HOST_TCP_HTTP2", "A host providing an index page containing the string 'monitoring'", "test.monitoring-plugins.org"); | ||
35 | my $host_tcp_proxy = getTestParameter("NP_HOST_TCP_PROXY", "A host providing a HTTP proxy with CONNECT support", "localhost"); | ||
36 | my $port_tcp_proxy = getTestParameter("NP_PORT_TCP_PROXY", "Port of the proxy with HTTP and CONNECT support", "3128"); | ||
37 | |||
38 | my $faketime = -x '/usr/bin/faketime' ? 1 : 0; | ||
39 | |||
40 | |||
41 | $res = NPTest->testCmd( | ||
42 | "./$plugin $host_tcp_http -wt 300 -ct 600" | ||
43 | ); | ||
44 | cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" ); | ||
45 | like( $res->output, $successOutput, "Output OK" ); | ||
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 | |||
56 | $res = NPTest->testCmd( | ||
57 | "./$plugin $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'" | ||
58 | ); | ||
59 | like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" ); | ||
60 | |||
61 | $res = NPTest->testCmd( | ||
62 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" | ||
63 | ); | ||
64 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); | ||
65 | # was CRITICAL only, but both check_curl and check_http print HTTP CRITICAL (puzzle?!) | ||
66 | like( $res->output, "/HTTP CRITICAL - Invalid HTTP response received from host on port 80: cURL returned 28 - Connection timed out after/", "Output OK"); | ||
67 | |||
68 | $res = NPTest->testCmd( | ||
69 | "./$plugin $hostname_invalid -wt 1 -ct 2" | ||
70 | ); | ||
71 | cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" ); | ||
72 | # The first part of the message comes from the OS catalogue, so cannot check this. | ||
73 | # On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename | ||
74 | # Is also possible to get a socket timeout if DNS is not responding fast enough | ||
75 | # cURL gives us consistent strings from it's own 'lib/strerror.c' | ||
76 | like( $res->output, "/cURL returned 6 - Could not resolve host:/", "Output OK"); | ||
77 | |||
78 | # host header checks | ||
79 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http"); | ||
80 | like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" ); | ||
81 | like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); | ||
82 | |||
83 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http -p 80"); | ||
84 | like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" ); | ||
85 | like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); | ||
86 | |||
87 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80"); | ||
88 | like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" ); | ||
89 | like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); | ||
90 | |||
91 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80"); | ||
92 | like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" ); | ||
93 | like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); | ||
94 | |||
95 | $res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80 -k 'Host: testhost:8001'"); | ||
96 | like( $res->output, '/^Host: testhost:8001\s*$/ms', "Host Header OK" ); | ||
97 | like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); | ||
98 | |||
99 | $res = NPTest->testCmd("./$plugin -v -I $host_tcp_http -p 80 -k 'Host: testhost:8001'"); | ||
100 | like( $res->output, '/^Host: testhost:8001\s*$/ms', "Host Header OK" ); | ||
101 | like( $res->output, '/CURLOPT_URL: http:\/\/'.$host_tcp_http.':80\//ms', "Url OK" ); | ||
102 | |||
103 | SKIP: { | ||
104 | skip "No internet access", 4 if $internet_access eq "no"; | ||
105 | |||
106 | $res = NPTest->testCmd("./$plugin -v -H $host_tls_http -S"); | ||
107 | like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" ); | ||
108 | |||
109 | $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:8080 -S -p 443"); | ||
110 | like( $res->output, '/^Host: '.$host_tls_http.':8080\s*$/ms', "Host Header OK" ); | ||
111 | |||
112 | $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:443 -S -p 443"); | ||
113 | like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" ); | ||
114 | |||
115 | $res = NPTest->testCmd("./$plugin -v -H $host_tls_http -D -S -p 443"); | ||
116 | like( $res->output, '/(^Host: '.$host_tls_http.'\s*$)|(cURL returned 60)/ms', "Host Header OK" ); | ||
117 | }; | ||
118 | |||
119 | SKIP: { | ||
120 | skip "No host serving monitoring in index file", 7 unless $host_tcp_http2; | ||
121 | |||
122 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring'" ); | ||
123 | cmp_ok( $res->return_code, "==", 0, "Got a reference to 'monitoring'"); | ||
124 | |||
125 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing'" ); | ||
126 | cmp_ok( $res->return_code, "==", 2, "Not got 'mONiTORing'"); | ||
127 | like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'"); | ||
128 | |||
129 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -R 'mONiTORing'" ); | ||
130 | cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'mONiTORing'"); | ||
131 | |||
132 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring' --invert-regex" ); | ||
133 | cmp_ok( $res->return_code, "==", 2, "Invert results work when found"); | ||
134 | like ( $res->output, "/pattern found/", "Error message says 'pattern found'"); | ||
135 | |||
136 | $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing' --invert-regex" ); | ||
137 | cmp_ok( $res->return_code, "==", 0, "And also when not found"); | ||
138 | } | ||
139 | SKIP: { | ||
140 | skip "No internet access", 28 if $internet_access eq "no"; | ||
141 | |||
142 | $res = NPTest->testCmd( | ||
143 | "./$plugin --ssl $host_tls_http" | ||
144 | ); | ||
145 | cmp_ok( $res->return_code, '==', 0, "Can read https for $host_tls_http" ); | ||
146 | |||
147 | $res = NPTest->testCmd( "./$plugin -C 1 --ssl $host_tls_http" ); | ||
148 | cmp_ok( $res->return_code, '==', 0, "Checking certificate for $host_tls_http"); | ||
149 | like ( $res->output, "/Certificate '$host_tls_cert' will expire on/", "Output OK" ); | ||
150 | my $saved_cert_output = $res->output; | ||
151 | |||
152 | $res = NPTest->testCmd( "./$plugin -C 8000,1 --ssl $host_tls_http" ); | ||
153 | cmp_ok( $res->return_code, '==', 1, "Checking certificate for $host_tls_http"); | ||
154 | like ( $res->output, qr/WARNING - Certificate '$host_tls_cert' expires in \d+ day/, "Output Warning" ); | ||
155 | |||
156 | $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); | ||
157 | is( $res->return_code, 0, "Old syntax for cert checking okay" ); | ||
158 | is( $res->output, $saved_cert_output, "Same output as new syntax" ); | ||
159 | |||
160 | $res = NPTest->testCmd( "./$plugin -H $host_tls_http -C 1" ); | ||
161 | is( $res->return_code, 0, "Updated syntax for cert checking okay" ); | ||
162 | is( $res->output, $saved_cert_output, "Same output as new syntax" ); | ||
163 | |||
164 | $res = NPTest->testCmd( "./$plugin -C 1 $host_tls_http" ); | ||
165 | cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added"); | ||
166 | |||
167 | $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" ); | ||
168 | cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works"); | ||
169 | |||
170 | # run some certificate checks with faketime | ||
171 | SKIP: { | ||
172 | skip "No faketime binary found", 12 if !$faketime; | ||
173 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./$plugin -C 1 $host_tls_http"); | ||
174 | like($res->output, qr/OK - Certificate '$host_tls_cert' will expire on/, "Catch cert output"); | ||
175 | is( $res->return_code, 0, "Catch cert output exit code" ); | ||
176 | my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/); | ||
177 | if(!defined $year) { | ||
178 | die("parsing date failed from: ".$res->output); | ||
179 | } | ||
180 | my $months = {'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11}; | ||
181 | my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900); | ||
182 | my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts)); | ||
183 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./$plugin -C 1 $host_tls_http"); | ||
184 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' just expired/, "Output on expire date"); | ||
185 | is( $res->return_code, 2, "Output on expire date" ); | ||
186 | |||
187 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-1))."' ./$plugin -C 1 $host_tls_http"); | ||
188 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 0 minutes/, "cert expires in 1 second output"); | ||
189 | is( $res->return_code, 2, "cert expires in 1 second exit code" ); | ||
190 | |||
191 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-120))."' ./$plugin -C 1 $host_tls_http"); | ||
192 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 minutes/, "cert expires in 2 minutes output"); | ||
193 | is( $res->return_code, 2, "cert expires in 2 minutes exit code" ); | ||
194 | |||
195 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-7200))."' ./$plugin -C 1 $host_tls_http"); | ||
196 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 hours/, "cert expires in 2 hours output"); | ||
197 | is( $res->return_code, 2, "cert expires in 2 hours exit code" ); | ||
198 | |||
199 | $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./$plugin -C 1 $host_tls_http"); | ||
200 | like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expired on/, "Certificate expired output"); | ||
201 | is( $res->return_code, 2, "Certificate expired exit code" ); | ||
202 | }; | ||
203 | |||
204 | $res = NPTest->testCmd( "./$plugin --ssl $host_tls_http -E" ); | ||
205 | like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); | ||
206 | like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' ); | ||
207 | |||
208 | $res = NPTest->testCmd( "./$plugin -H www.mozilla.com -u /firefox -f curl" ); | ||
209 | is( $res->return_code, 0, "Redirection based on location is okay"); | ||
210 | |||
211 | $res = NPTest->testCmd( "./$plugin -H www.mozilla.com --extended-perfdata" ); | ||
212 | like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' ); | ||
213 | } | ||