summaryrefslogtreecommitdiffstats
path: root/plugins/t/check_curl.t
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/t/check_curl.t')
-rw-r--r--[l---------]plugins/t/check_curl.t205
1 files changed, 204 insertions, 1 deletions
diff --git a/plugins/t/check_curl.t b/plugins/t/check_curl.t
index a54db96..e67fafb 120000..100644
--- a/plugins/t/check_curl.t
+++ b/plugins/t/check_curl.t
@@ -1 +1,204 @@
1check_http.t \ No newline at end of file 1#! /usr/bin/perl -w -I ..
2#
3# HyperText Transfer Protocol (HTTP) Test via check_http
4#
5#
6
7use strict;
8use Test::More;
9use POSIX qw/mktime strftime/;
10use NPTest;
11
12plan tests => 49;
13
14my $successOutput = '/OK.*HTTP.*second/';
15
16my $res;
17my $plugin = 'check_http';
18$plugin = 'check_curl' if $0 =~ m/check_curl/mx;
19
20my $host_tcp_http = getTestParameter( "NP_HOST_TCP_HTTP",
21 "A host providing the HTTP Service (a web server)",
22 "localhost" );
23
24my $host_tls_http = getTestParameter( "host_tls_http", "NP_HOST_TLS_HTTP", "localhost",
25 "A host providing the HTTPS Service (a tls web server)" );
26
27my $host_tls_cert = getTestParameter( "host_tls_cert", "NP_HOST_TLS_CERT", "localhost",
28 "the common name of the certificate." );
29
30
31my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
32 "The hostname of system not responsive to network requests",
33 "10.0.0.1" );
34
35my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID",
36 "An invalid (not known to DNS) hostname",
37 "nosuchhost");
38
39my $internet_access = getTestParameter( "NP_INTERNET_ACCESS",
40 "Is this system directly connected to the internet?",
41 "yes");
42
43my $host_tcp_http2 = getTestParameter( "NP_HOST_TCP_HTTP2",
44 "A host providing an index page containing the string 'monitoring'",
45 "test.monitoring-plugins.org" );
46
47my $faketime = -x '/usr/bin/faketime' ? 1 : 0;
48
49
50$res = NPTest->testCmd(
51 "./$plugin $host_tcp_http -wt 300 -ct 600"
52 );
53cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" );
54like( $res->output, $successOutput, "Output OK" );
55
56$res = NPTest->testCmd(
57 "./$plugin $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'"
58 );
59like( $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 );
64cmp_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?!)
66cmp_ok( $res->output, 'eq', "HTTP CRITICAL - Invalid HTTP response received from host on port 80: cURL returned 28 - Timeout was reached", "Output OK");
67
68$res = NPTest->testCmd(
69 "./$plugin $hostname_invalid -wt 1 -ct 2"
70 );
71cmp_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'
76like( $res->output, "/cURL returned 6 - Couldn't resolve host name/", "Output OK");
77
78# host header checks
79$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http");
80like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" );
81
82$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http -p 80");
83like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" );
84
85$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80");
86like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" );
87
88$res = NPTest->testCmd("./$plugin -v -H $host_tcp_http:8080 -p 80");
89like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" );
90
91SKIP: {
92 skip "No internet access", 3 if $internet_access eq "no";
93
94 $res = NPTest->testCmd("./$plugin -v -H $host_tls_http -S");
95 like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" );
96
97 $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:8080 -S -p 443");
98 like( $res->output, '/^Host: '.$host_tls_http.':8080\s*$/ms', "Host Header OK" );
99
100 $res = NPTest->testCmd("./$plugin -v -H $host_tls_http:443 -S -p 443");
101 like( $res->output, '/^Host: '.$host_tls_http.'\s*$/ms', "Host Header OK" );
102};
103
104SKIP: {
105 skip "No host serving monitoring in index file", 7 unless $host_tcp_http2;
106
107 $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring'" );
108 cmp_ok( $res->return_code, "==", 0, "Got a reference to 'monitoring'");
109
110 $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing'" );
111 cmp_ok( $res->return_code, "==", 2, "Not got 'mONiTORing'");
112 like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'");
113
114 $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -R 'mONiTORing'" );
115 cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'mONiTORing'");
116
117 $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'monitoring' --invert-regex" );
118 cmp_ok( $res->return_code, "==", 2, "Invert results work when found");
119 like ( $res->output, "/pattern found/", "Error message says 'pattern found'");
120
121 $res = NPTest->testCmd( "./$plugin -H $host_tcp_http2 -r 'mONiTORing' --invert-regex" );
122 cmp_ok( $res->return_code, "==", 0, "And also when not found");
123}
124SKIP: {
125 skip "No internet access", 16 if $internet_access eq "no";
126
127 $res = NPTest->testCmd(
128 "./$plugin --ssl $host_tls_http"
129 );
130 cmp_ok( $res->return_code, '==', 0, "Can read https for $host_tls_http" );
131
132 $res = NPTest->testCmd( "./$plugin -C 1 --ssl $host_tls_http" );
133 cmp_ok( $res->return_code, '==', 0, "Checking certificate for $host_tls_http");
134 like ( $res->output, "/Certificate '$host_tls_cert' will expire on/", "Output OK" );
135 my $saved_cert_output = $res->output;
136
137 $res = NPTest->testCmd( "./$plugin -C 8000,1 --ssl $host_tls_http" );
138 cmp_ok( $res->return_code, '==', 1, "Checking certificate for $host_tls_http");
139 like ( $res->output, qr/WARNING - Certificate '$host_tls_cert' expires in \d+ day/, "Output Warning" );
140
141 $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" );
142 is( $res->return_code, 0, "Old syntax for cert checking okay" );
143 is( $res->output, $saved_cert_output, "Same output as new syntax" );
144
145 $res = NPTest->testCmd( "./$plugin -H $host_tls_http -C 1" );
146 is( $res->return_code, 0, "Updated syntax for cert checking okay" );
147 is( $res->output, $saved_cert_output, "Same output as new syntax" );
148
149 $res = NPTest->testCmd( "./$plugin -C 1 $host_tls_http" );
150 cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added");
151
152 $res = NPTest->testCmd( "./$plugin $host_tls_http -C 1" );
153 cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works");
154
155 # run some certificate checks with faketime
156 SKIP: {
157 skip "No faketime binary found", 12 if !$faketime;
158 $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./$plugin -C 1 $host_tls_http");
159 like($res->output, qr/OK - Certificate '$host_tls_cert' will expire on/, "Catch cert output");
160 is( $res->return_code, 0, "Catch cert output exit code" );
161 my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/);
162 if(!defined $year) {
163 die("parsing date failed from: ".$res->output);
164 }
165 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};
166 my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900);
167 my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts));
168 $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");
169 like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' just expired/, "Output on expire date");
170 is( $res->return_code, 2, "Output on expire date" );
171
172 $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");
173 like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 0 minutes/, "cert expires in 1 second output");
174 is( $res->return_code, 2, "cert expires in 1 second exit code" );
175
176 $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");
177 like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 minutes/, "cert expires in 2 minutes output");
178 is( $res->return_code, 2, "cert expires in 2 minutes exit code" );
179
180 $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");
181 like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expires in 2 hours/, "cert expires in 2 hours output");
182 is( $res->return_code, 2, "cert expires in 2 hours exit code" );
183
184 $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");
185 like($res->output, qr/CRITICAL - Certificate '$host_tls_cert' expired on/, "Certificate expired output");
186 is( $res->return_code, 2, "Certificate expired exit code" );
187 };
188
189 $res = NPTest->testCmd( "./$plugin --ssl $host_tls_http -E" );
190 like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' );
191 like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' );
192
193 $res = NPTest->testCmd(
194 "./$plugin --ssl -H www.e-paycobalt.com"
195 );
196 cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" );
197
198
199 $res = NPTest->testCmd( "./$plugin -H www.mozilla.com -u /firefox -f follow" );
200 is( $res->return_code, 0, "Redirection based on location is okay");
201
202 $res = NPTest->testCmd( "./$plugin -H www.mozilla.com --extended-perfdata" );
203 like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' );
204}