Re: Nagios and WebObjects

Zak Burke <[email protected]> Thu, 17 Aug 2006 09:22:57 -0400
Newsgroups gmane.comp.web.webobjects.admin
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------010006010004000208010109
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Jan Grathwohl wrote on 8/17/06 4:41 AM:
> we just put some simple Direct Actions in our apps that are called by
> Nagios and return a certain string like "OK" if everything is fine.
> 
> Am 16.08.2006 um 00:22 schrieb Dave E:
>> I am trying to set up a monitoring system to monitor my WebObjects
>> applications, I have read that someone is using nagios to do this. Do
>> you know if there is a WebObjects plugin for Nagios?.

I do the following to monitor WO:

In the "Load Balancing and Adaptor Settings" section, I set the
"Redirection URL" to a CGI script (attached: redirection_script.cgi)
that displays a friendly message to the user and sends me mail.

I used to run a cron-job (attached: restart_script.pl) every 15 minutes
that checks whether requests are being correctly handled or are being
routed to the "Redirection URL" page. If the latter, it bounces all
WO-related processes. My first WO apps were unwieldy memory hogs and I
would occasionally get out-of-memory errors that would (1) cause wotaskd
to stop responding on port 1085 and thus (2) prevent WOMonitor from
letting me manage any instances.

zak.

--------------010006010004000208010109
Content-Type: text/plain; x-mac-type="54455854"; x-mac-creator="0";
	name="redirection_script.cgi"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="redirection_script.cgi"

#!/usr/bin/perl -w

# --
# -- KB error page, displayed by WO when our application is unavailable, 
# -- sends email to the webmaster if $ENV{DOCUMENT_ROOT}/i3pkb_error is
# -- more than 10 minutes old. 
# -- 
# -- 2005-10-25, zak burke
# -- 

use strict; 



# recipient of email sent by this script
use constant RECIPIENT => "receiver\@yourdomain.org"; 

# sender of email sent by this script
use constant SENDER    => "sender\@yourdomain.org"; 

# gateway to send mail through
use constant MAILHOST  => "mailserver.yourdomain.org"; 

# full path to error file
use constant ERROR_FILE => "$ENV{'DOCUMENT_ROOT'}/i3pkb_error";

# min age of the error file, in seconds. if we sent mail every time, 
# we'd be innundated when the server crashes, so this tempers things a bit.
use constant ERROR_FILE_AGE => 600;

# full path to page to show the user
use constant ERROR_MESSAGE_FILE => "$ENV{'DOCUMENT_ROOT'}/i3pkb_error";



# --
# -- nothing to configure below
# --





BEGIN
{
    use strict;
    use CGI;
    use Email::Send;
    use File::stat;
}


main();

sub main
{

    my ($cgi, $message, $content, $submitter, @list) = undef;

    # send mail if it's been 10 minutes since the last time
    # this error was recorded
    my $stat = lstat(ERROR_FILE); 
    my $age = time() - $stat->mtime;

    if ($age > ERROR_FILE_AGE) {
    
        $message .= "The application i3pkb on $ENV{'SERVER_NAME'} is not responding and must be restarted." ;

        # message to registrar
        send_mail($message, $age, RECIPIENT, SENDER);
    }

    # reset the error timestamp
    my $error_file = ERROR_FILE; 
    `touch $error_file`;

    # display error page in browser    
    $cgi = CGI->new();
    print $cgi->header();
    print template(); 

}


    
#
# send_mail
# send a message
sub send_mail
{
        my ($message, $age, $recipient, $sender) = @_; 

    my $error_file = ERROR_FILE; 

    send SMTP => <<"EOT", MAILHOST;
To: $recipient
From: $sender
Subject: ERROR: i3pkb on $ENV{'SERVER_NAME'} MUST BE RESTARTED

$message

$error_file was $age seconds old.


EOT

}



# --

# --
# -- template
# -- return the WO error page to display in place of the 
# -- requested page. 
sub template 
{
    open FILE, ERROR_MESSAGE_FILE;
    my @lines = <FILE>;
    close FILE;

    return join "\n", @lines; 
}

--------------010006010004000208010109
Content-Type: text/plain; x-mac-type="54455854"; x-mac-creator="21526368";
	name="restart_script.pl"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="restart_script.pl"

#!/usr/bin/perl 

# -- 
# -- 2006-08-01, zak burke
# -- monitor WO and if the KB locks up the whole server
# -- shutdown all WO processes and restart 'em 
# -- 


use constant HOST   => "http://www.thei3p.org"; 
use constant SCRIPT => "/System/Library/StartupItems/WebObjects/WebObjects start";

use LWP::UserAgent; 


main(); 

sub main
{

    my $ua = LWP::UserAgent->new(); 
    $ua->agent("MyApp/0.1 ");

    my $res = $ua->get(HOST);

    # when WO dies, it uses a Location: header to bounce
    # requests to the error page. check for that header
    # and restart WO if it is found. 
    server_restart() if ($res->header("Location"));
}



# --

#
# server_restart
# kill off any WO applications, and the WO manager (wotaskd) and 
# then restart 'em. 
sub server_restart
{
    print STDERR "WO is redirecting to the error page; restarting the server."; 


    my @ps = split "\n", `ps auxww | egrep "com.webobjects._bootstrap.WOBootstrap|wotaskd.woa" | grep -v grep`;

    # kill off existing java processes
    for (@ps) {
        chomp;
        my @words = split;
        print STDERR "killing $words[1]\n"; 
        system("kill -9 $words[1]"); 
    }

    print STDERR "java processes killed...\n"; 


    # restart WebObjects
    print STDERR "restarting WebObjects\n"; 
    system(SCRIPT); 

}

--------------010006010004000208010109
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
WebObjects-admin mailing list
[email protected]
http://www.omnigroup.com/mailman/listinfo/webobjects-admin

--------------010006010004000208010109--