https.monitor 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/perl
  2. #
  3. # $Id: https.monitor,v 1.15 2000/02/04 23:34:03 andrewr Exp $
  4. #
  5. # An extremely simple https monitor for mon.
  6. #
  7. # Code structure based on Jon Meek & Jim Trocki's http.monitor program.
  8. #
  9. # https code taken from the get_page.pl function from the
  10. # Net::SSLeay distribution by Sampo Kellomaki <sampo@iki.fi>
  11. #
  12. # It makes use of the Net::SSLeay library and the OpenSSL package
  13. # (www.openssl.org).
  14. #
  15. # To get around the problem that Net::SSLeay carps to STDERR
  16. # uncontrollably about a number of things (e.g. connection refused),
  17. # we get around this by running the actual ssl get as an escaped
  18. # perl program and dropping the stderr of that instance. Gross, but
  19. # strangely effective.
  20. #
  21. # Use the -v option if you actually want to see the full result and
  22. # all headers. You'd never use this from mon, since it provides
  23. # non-mon-compliant output, but it can be interesting from the command
  24. # line.
  25. #
  26. #
  27. #
  28. # Distribution and use of this program is under the same terms
  29. # as the OpenSSL package itself (i.e. free, but mandatory
  30. # attribution; NO WARRANTY). Please consult COPYRIGHT file in
  31. # the root of the SSLeay distribution.
  32. #
  33. use strict;
  34. use Socket;
  35. use Net::SSLeay qw(die_now die_if_ssl_error) ;
  36. use Getopt::Std;
  37. #
  38. use English;
  39. #Net::SSLeay::load_error_strings();
  40. #Net::SSLeay::SSLeay_add_ssl_algorithms();
  41. # Comment this out since on systems without a /dev/[u]random this
  42. # line causes an unneccesary carp which will confuse mon.
  43. # If you use Linux or BSD or other OS which supports a random device,
  44. # feel free to uncomment this line.
  45. #Net::SSLeay::randomize();
  46. use vars qw($opt_p $opt_t $opt_u $opt_v);
  47. getopts ("vp:t:u:");
  48. my $PORT = $opt_p || 443;
  49. my $TIMEOUT = $opt_t || 30;
  50. my $URL = $opt_u || "/";
  51. my $perl = "/usr/bin/perl"; # where you keep perl
  52. my $field_delim = "<>"; # html field delimiter
  53. my @failures = ();
  54. my @detail = ();
  55. my ($host, $OK, $default_header, $auth_header, $end_header, $request_header, $msg);
  56. my ($dest_ip, $dest_serv, $sockaddr_template, $dest_serv_params, $ctx, $ssl, $res, $reply, $got, $ServerOK);
  57. foreach $host (@ARGV) {
  58. $OK = &httpsGET($host, $PORT, $URL);
  59. if (!defined ($OK) || $OK == 0) {
  60. push (@failures, $host);
  61. }
  62. }
  63. if (@failures == 0) {
  64. exit 0;
  65. }
  66. print "@failures\n";
  67. print join(";",@detail);
  68. exit 1;
  69. # Main function begins here
  70. sub httpsGET {
  71. my ($site, $port, $path) = (@_);
  72. my $total_bytes = 0; #set total bytes transferred to 0
  73. my ($page, $result, %headers);
  74. # print "attempting to contact site $site on port $port with path $path\n";
  75. eval {
  76. local $SIG{ALRM} = sub { die "Timeout Alarm" };
  77. alarm $TIMEOUT;
  78. $result = `$perl -e'use Net::SSLeay ; Net::SSLeay::SSLeay_add_ssl_algorithms() ; print join("$field_delim",Net::SSLeay::get_https("$site", "$port", "$path"))' 2>/dev/null`;
  79. alarm 0; #cancel the alarm
  80. ($page, $result, %headers) = split ("<>",$result);
  81. print "Result was `$result'\n" if $opt_v;
  82. foreach my $h (sort keys %headers) {
  83. print "Header `$h'\tvalue `$headers{$h}'\n" if $opt_v;
  84. }
  85. if ($result =~ /^HTTP\/([\d\.]+)\s+(200|301|302)\b/) {
  86. $ServerOK = 1;
  87. } else {
  88. $ServerOK = 0;
  89. push(@detail,"$result");
  90. }
  91. };
  92. if ($EVAL_ERROR and ($EVAL_ERROR eq 'Timeout Alarm')) {
  93. print "**** Time Out\n";
  94. return 0;
  95. }
  96. return $ServerOK;
  97. }