check_http.t 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 => 42;
  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 'nagios'",
  27. "nagios.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", "Output OK");
  43. $res = NPTest->testCmd(
  44. "./check_http $hostname_invalid -wt 1 -ct 2"
  45. );
  46. cmp_ok( $res->return_code, '==', 3, "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, "/Invalid hostname\/address/", "Output OK");
  51. SKIP: {
  52. skip "No host serving nagios in index file", 7 unless $host_tcp_http2;
  53. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nagios'" );
  54. cmp_ok( $res->return_code, "==", 0, "Got a reference to 'nagios'");
  55. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nAGiOs'" );
  56. cmp_ok( $res->return_code, "==", 2, "Not got 'nAGiOs'");
  57. like ( $res->output, "/pattern not found/", "Error message says 'pattern not found'");
  58. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -R 'nAGiOs'" );
  59. cmp_ok( $res->return_code, "==", 0, "But case insensitive doesn't mind 'nAGiOs'");
  60. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nagios' --invert-regex" );
  61. cmp_ok( $res->return_code, "==", 2, "Invert results work when found");
  62. like ( $res->output, "/pattern found/", "Error message says 'pattern found'");
  63. $res = NPTest->testCmd( "./check_http -H $host_tcp_http2 -r 'nAGiOs' --invert-regex" );
  64. cmp_ok( $res->return_code, "==", 0, "And also when not found");
  65. }
  66. SKIP: {
  67. skip "No internet access", 16 if $internet_access eq "no";
  68. $res = NPTest->testCmd(
  69. "./check_http --ssl www.verisign.com"
  70. );
  71. cmp_ok( $res->return_code, '==', 0, "Can read https for www.verisign.com" );
  72. $res = NPTest->testCmd( "./check_http -C 1 --ssl www.verisign.com" );
  73. cmp_ok( $res->return_code, '==', 0, "Checking certificate for www.verisign.com");
  74. like ( $res->output, "/Certificate 'www.verisign.com' will expire in/", "Output OK" );
  75. my $saved_cert_output = $res->output;
  76. $res = NPTest->testCmd( "./check_http -C 8000,1 --ssl www.verisign.com" );
  77. cmp_ok( $res->return_code, '==', 1, "Checking certificate for www.verisign.com");
  78. like ( $res->output, qr/WARNING - Certificate 'www.verisign.com' expires in \d+ day/, "Output Warning" );
  79. $res = NPTest->testCmd( "./check_http www.verisign.com -C 1" );
  80. is( $res->return_code, 0, "Old syntax for cert checking okay" );
  81. is( $res->output, $saved_cert_output, "Same output as new syntax" );
  82. $res = NPTest->testCmd( "./check_http -H www.verisign.com -C 1" );
  83. is( $res->return_code, 0, "Updated syntax for cert checking okay" );
  84. is( $res->output, $saved_cert_output, "Same output as new syntax" );
  85. $res = NPTest->testCmd( "./check_http -C 1 www.verisign.com" );
  86. cmp_ok( $res->output, 'eq', $saved_cert_output, "--ssl option automatically added");
  87. $res = NPTest->testCmd( "./check_http www.verisign.com -C 1" );
  88. cmp_ok( $res->output, 'eq', $saved_cert_output, "Old syntax for cert checking still works");
  89. # run some certificate checks with faketime
  90. SKIP: {
  91. skip "No faketime binary found", 12 if !$faketime;
  92. $res = NPTest->testCmd("LC_TIME=C TZ=UTC ./check_http -C 1 www.verisign.com");
  93. like($res->output, qr/OK - Certificate 'www.verisign.com' will expire on/, "Catch cert output");
  94. is( $res->return_code, 0, "Catch cert output exit code" );
  95. my($mon,$day,$hour,$min,$sec,$year) = ($res->output =~ /(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)\./);
  96. if(!defined $year) {
  97. die("parsing date failed from: ".$res);
  98. }
  99. 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};
  100. my $ts = mktime($sec, $min, $hour, $day, $months->{$mon}, $year-1900);
  101. my $time = strftime("%Y-%m-%d %H:%M:%S", localtime($ts));
  102. $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");
  103. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' just expired/, "Output on expire date");
  104. is( $res->return_code, 2, "Output on expire date" );
  105. $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");
  106. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' expires in 0 minutes/, "cert expires in 1 second output");
  107. is( $res->return_code, 2, "cert expires in 1 second exit code" );
  108. $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");
  109. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' expires in 2 minutes/, "cert expires in 2 minutes output");
  110. is( $res->return_code, 2, "cert expires in 2 minutes exit code" );
  111. $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");
  112. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' expires in 2 hours/, "cert expires in 2 hours output");
  113. is( $res->return_code, 2, "cert expires in 2 hours exit code" );
  114. $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");
  115. like($res->output, qr/CRITICAL - Certificate 'www.verisign.com' expired on/, "Certificate expired output");
  116. is( $res->return_code, 2, "Certificate expired exit code" );
  117. };
  118. $res = NPTest->testCmd( "./check_http --ssl www.verisign.com -E" );
  119. like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' );
  120. like ( $res->output, '/time_ssl=[\d\.]+/', 'Extended Performance Data SSL Output OK' );
  121. $res = NPTest->testCmd(
  122. "./check_http --ssl -H www.e-paycobalt.com"
  123. );
  124. cmp_ok( $res->return_code, "==", 0, "Can read https for www.e-paycobalt.com (uses AES certificate)" );
  125. $res = NPTest->testCmd( "./check_http -H www.mozilla.com -u /firefox -f follow" );
  126. is( $res->return_code, 0, "Redirection based on location is okay");
  127. $res = NPTest->testCmd( "./check_http -H www.mozilla.com --extended-perfdata" );
  128. like ( $res->output, '/time_connect=[\d\.]+/', 'Extended Performance Data Output OK' );
  129. }