| 1 | package Watchdog::HTTP; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 0 | ▶ | use Alias; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use base qw(Watchdog::Base); | ||
| 6 | use HTTP::Request; | ||
| 7 | use LWP::UserAgent; | ||
| 8 | use vars qw($VERSION $HOST $PORT $FILE); | ||
| 9 | |||
| 10 | $VERSION = '0.02'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Watchdog::HTTP - Test status of HTTP service | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use Watchdog::HTTP; | ||
| 19 | $h = new Watchdog::HTTP($name,$host,$port,$file); | ||
| 20 | print $h->id, $h->is_alive ? ' is alive' : ' is dead', "\n"; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | B<Watchdog::HTTP> is an extension for monitoring an HTTP server. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | my($name,$port,$file) = ('httpd',80,''); | ||
| 29 | |||
| 30 | =head1 CLASS METHODS | ||
| 31 | |||
| 32 | =head2 new($name,$host,$port,$file) | ||
| 33 | |||
| 34 | Returns a new B<Watchdog::HTTP> object. I<$name> is a string which | ||
| 35 | will identify the service to a human (default is 'httpd'). I<$host> | ||
| 36 | is the hostname which is running the service (default is 'localhost'). | ||
| 37 | I<$port> is the port on which the service listens (default is 80). | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub new($$$) { | ||
| 42 | my $proto = shift; | ||
| 43 | my $class = ref($proto) || $proto; | ||
| 44 | $_[0] = $name unless defined($_[0]); | ||
| 45 | $_[2] = $port unless defined($_[2]); | ||
| 46 | my $self = bless($class->SUPER::new(@_),$class); | ||
| 47 | return $self; | ||
| 48 | } | ||
| 49 | |||
| 50 | #------------------------------------------------------------------------------ | ||
| 51 | |||
| 52 | =head1 OBJECT METHODS | ||
| 53 | |||
| 54 | =head2 is_alive() | ||
| 55 | |||
| 56 | Returns true if an HTTP B<GET> method succeeds for the URL | ||
| 57 | B<http://$host:$port/$file> or false if it doesn't. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub is_alive() { | ||
| 62 | my $self = attr shift; | ||
| 63 | my $request = new HTTP::Request(GET => "http://$HOST:$PORT/$FILE"); | ||
| 64 | my $ua = new LWP::UserAgent; | ||
| 65 | my $response = $ua->request($request); | ||
| 66 | return $response->is_success ? 1 : 0; | ||
| 67 | } | ||
| 68 | |||
| 69 | #------------------------------------------------------------------------------ | ||
| 70 | |||
| 71 | =head1 SEE ALSO | ||
| 72 | |||
| 73 | L<Watchdog::Base> | ||
| 74 | |||
| 75 | =head1 AUTHOR | ||
| 76 | |||
| 77 | new Maintainer: Clemens Gesell E<lt>clemens.gesell@vegatron.orgE<gt> | ||
| 78 | |||
| 79 | Paul Sharpe E<lt>paul@miraclefish.comE<gt> | ||
| 80 | |||
| 81 | =head1 COPYRIGHT | ||
| 82 | |||
| 83 | Copyright (c) 1998 Paul Sharpe. England. All rights reserved. This | ||
| 84 | program is free software; you can redistribute it and/or modify it | ||
| 85 | under the same terms as Perl itself. | ||
| 86 | |||
| 87 | =cut | ||