check_http.t 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # HyperText Transfer Protocol (HTTP) Test via check_http
  4. #
  5. #
  6. use strict;
  7. use Test::More;
  8. use POSIX qw/mktime strftime/;
  9. use NPTest;
  10. plan tests => 49;
  11. my $successOutput = '/OK.*HTTP.*second/';
  12. my $res;
  13. my $host_tcp_http = getTestParameter( "NP_HOST_TCP_HTTP",
  14. "A host providing the HTTP Service (a web server)",
  15. "localhost" );
  16. my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
  17. "The hostname of system not responsive to network requests",
  18. "10.0.0.1" );
  19. my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID",
  20. "An invalid (not known to DNS) hostname",
  21. "nosuchhost");
  22. my $internet_access = getTestParameter( "NP_INTERNET_ACCESS",
  23. "Is this system directly connected to the internet?",
  24. "yes");
  25. my $host_tcp_http2 = getTestParameter( "NP_HOST_TCP_HTTP2",
  26. "A host providing an index page containing the string 'monitoring'",
  27. "test.monitoring-plugins.org" );
  28. my $faketime = -x '/usr/bin/faketime' ? 1 : 0;
  29. $res = NPTest->testCmd(
  30. "./check_http $host_tcp_http -wt 300 -ct 600"
  31. );
  32. cmp_ok( $res->return_code, '==', 0, "Webserver $host_tcp_http responded" );
  33. like( $res->output, $successOutput, "Output OK" );
  34. $res = NPTest->testCmd(
  35. "./check_http $host_tcp_http -wt 300 -ct 600 -v -v -v -k 'bob:there' -k 'carl:frown'"
  36. );
  37. like( $res->output, '/bob:there\r\ncarl:frown\r\n/', "Got headers with multiple -k options" );
  38. $res = NPTest->testCmd(
  39. "./check_http $host_nonresponsive -wt 1 -ct 2 -t 3"
  40. );
  41. cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" );
  42. cmp_ok( $res->output, 'eq', "CRITICAL - Socket timeout after 3 seconds", "Output OK");
  43. $res = NPTest->testCmd(
  44. "./check_http $hostname_invalid -wt 1 -ct 2"
  45. );
  46. cmp_ok( $res->return_code, '==', 2, "Webserver $hostname_invalid not valid" );
  47. # The first part of the message comes from the OS catalogue, so cannot check this.
  48. # On Debian, it is Name or service not known, on Darwin, it is No address associated with nodename
  49. # Is also possible to get a socket timeout if DNS is not responding fast enough
  50. like( $res->output, "/Unable to open TCP socket|Socket timeout after/", "Output OK");
  51. # host header checks
  52. $res = NPTest->testCmd("./check_http -v -H $host_tcp_http");
  53. like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" );
  54. $res = NPTest->testCmd("./check_http -v -H $host_tcp_http -p 80");
  55. like( $res->output, '/^Host: '.$host_tcp_http.'\s*$/ms', "Host Header OK" );
  56. $res = NPTest->testCmd("./check_http -v -H $host_tcp_http:8080 -p 80");
  57. like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" );
  58. $res = NPTest->testCmd("./check_http -v -H $host_tcp_http:8080 -p 80");
  59. like( $res->output, '/^Host: '.$host_tcp_http.':8080\s*$/ms', "Host Header OK" );
  60. SKIP: {
  61. skip "No internet access", 3 if $internet_access eq "no";
  62. $res = NPTest->testCmd("./check_http -v -H www.verisign.com -S");
  63. like( $res->output, '/^Host: www.verisign.com\s*$/ms', "Host Header OK" );
  64. $res = NPTest->testCmd("./check_http -v -H www.verisign.com:8080 -S -p 443");
  65. like( $res->output, '/^Host: www.verisign.com:8080\s*$/ms', "Host Header OK" );
  66. $res = NPTest->testCmd("./check_http -v -H www.verisign.com:443 -S -p 443");
  67. like( $res->output, '/^Host: www.verisign.com\s*$/ms', "Host Header OK" );
  68. };
  69. SKIP: {
  70. skip "No host serving monitoring in index file", 7 unless $host_tcp_http2;
  71. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'monitoring'" );
  72. cmp_ok( $res->return_code, "==", 0, "Got a reference to 'monitoring'");
  73. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'mONiTORing'" );
  74. cmp_ok( $res->return_code, "==", 2, "Not got 'mONiTORing'");
  75. like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'");
  76. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -R 'mONiTORing'" );
  77. cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'mONiTORing'");
  78. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'monitoring' --invert-regex" );
  79. cmp_ok( $res->return_code, "==", 2, "Invert results work when found");
  80. like ( $res->output, "/pattern found/", "Error message says 'pattern found'");
  81. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'mONiTORing' --invert-regex" );
  82. cmp_ok( $res->return_code, "==", 0, "And also when not found");
  83. }
  84. SKIP: {
  85. skip "No internet access", 16 if $internet_access eq "no";
  86. $res = NPTest->testCmd(
  87. "./check_http --ssl www.verisign.com"
  88. );
  89. cmp_ok( $res->return_code, '==', 0, "Can read https for www.verisign.com" );
  90. $res = NPTest->testCmd( "./check_http -C 1 --ssl www.verisign.com" );
  91. cmp_ok( $res->return_code, '==', 0, "Checking certificate for www.verisign.com");
  92. like ( $res->output, "/Certificate 'www.verisign.com' will expire on/", "Output OK" );
  93. my $saved_cert_output = $res->output;
  94. $res = NPTest->testCmd( "./check_http -C 8000,1 --ssl www.verisign.com" );
  95. cmp_ok( $res->return_code, '==', 1, "Checking certificate for www.verisign.com");
  96. like ( $res->output, qr/WARNING - Certificate 'www.verisign.com' expires in \d+ day/, "Output Warning" );
  97. $res = NPTest->testCmd( "./check_http www.verisign.com -C 1" );
  98. is( $res->return_code, 0, "Old syntax for cert checking okay" );
  99. is( $res->output, $saved_cert_output, "Same output as new syntax" );
  100. $res = NPTest->testCmd( "./check_http -H www.verisign.com -C 1" );
  101. is( $res->return_code, 0, "Updated syntax for cert checking okay" );
  102. is( $res->output, $saved_cert_output, "Same output as new syntax" );
  103. $res = NPTest->testCmd( "./check_http -C 1 www.verisign.com" );
  104. cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added");
  105. $res = NPTest->testCmd( "./check_http www.verisign.com -C 1" );
  106. cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works");
  107. # run some certificate checks with faketime
  108. SKIP: {
  109. skip "No faketime binary found", 12 if !$faketime;
  110. $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./check_http -C 1 www.verisign.com");
  111. like($res->output, qr/OK - Certificate 'www.verisign.com' will expire on/, "Catch cert output");
  112. is( $res->return_code, 0, "Catch cert output exit code" );
  113. my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/);
  114. if(!defined $year) {
  115. die("parsing date failed from: ".$res->output);
  116. }
  117. 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};
  118. my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900);
  119. my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts));
  120. $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts))."' ./check_http -C 1 www.verisign.com");
  121. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' just expired/, "Output on expire date");
  122. is( $res->return_code, 2, "Output on expire date" );
  123. $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-1))."' ./check_http -C 1 www.verisign.com");
  124. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' expires in 0 minutes/, "cert expires in 1 second output");
  125. is( $res->return_code, 2, "cert expires in 1 second exit code" );
  126. $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-120))."' ./check_http -C 1 www.verisign.com");
  127. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' expires in 2 minutes/, "cert expires in 2 minutes output");
  128. is( $res->return_code, 2, "cert expires in 2 minutes exit code" );
  129. $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts-7200))."' ./check_http -C 1 www.verisign.com");
  130. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' expires in 2 hours/, "cert expires in 2 hours output");
  131. is( $res->return_code, 2, "cert expires in 2 hours exit code" );
  132. $res = NPTest->testCmd("LC_TIME=C TZ=UTC faketime -f '".strftime("%Y-%m-%d %H:%M:%S", localtime($ts+1))."' ./check_http -C 1 www.verisign.com");
  133. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' expired on/, "Certificate expired output");
  134. is( $res->return_code, 2, "Certificate expired exit code" );
  135. };
  136. $res = NPTest->testCmd( "./check_http --ssl www.verisign.com -E" );
  137. like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' );
  138. like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' );
  139. $res = NPTest->testCmd(
  140. "./check_http --ssl -H www.e-paycobalt.com"
  141. );
  142. cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" );
  143. $res = NPTest->testCmd( "./check_http -H www.mozilla.com -u /firefox -f follow" );
  144. is( $res->return_code, 0, "Redirection based on location is okay");
  145. $res = NPTest->testCmd( "./check_http -H www.mozilla.com --extended-perfdata" );
  146. like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' );
  147. }