You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Geoffrey Young <gy...@laserlink.net> on 2000/04/27 22:29:53 UTC

[new module development] Apache::Ping

hi all...

	well, this isn't a new module quite yet...  I've been toying with
the idea of a module that does automatic server monitoring.  I know there
must be programs out there that do this, and the guide has some examples,
but I was interested in creating a module add-in that would take care of
things with minimal intervention (as in PerlModule Apache::Ping and that's
all)...

I'm not really a systems person, so I don't get all the intricicies of
forking, daemons, and the like, but here's a skeleton that seems to work...

Does anyone think this is a worthy endeavor?  Are there some inherent
dangers here that should mentioned?

--Geoff


package Apache::Ping;

use 5.004;
use mod_perl 1.23;
use LWP::UserAgent;
use strict;

$SIG{CHLD} = 'IGNORE';

my $warmup   = 10;
my $wait = 10;
my $restart  = "/usr/local/apache/apachectl restart";
my $url  = "http://localhost/perl-status";

defined (my $kid = fork) or die "Cannot fork: $!\n";
unless ($kid) {
  close STDIN;
  close STDOUT;
  close STDERR;
  open STDIN, '/dev/null'    || die "Can't read /dev/null: $!";
  open STDOUT, '>/dev/null'  || die "Can't write to /dev/null: $!";
  open STDERR, '>/dev/null'  || die "Can't write to /dev/null: $!";
  sleep($warmup);
  while(1) {
    my $ua = new LWP::UserAgent;
    my $req = new HTTP::Request (GET => $url);
    my $res = $ua->request($req);
    if ($res->is_success) {
      sleep($wait);
    } else {
      system($restart);
      CORE::exit(0);
    }
  }
  close FH;
  CORE::exit(0);
}

1;