#!/usr/bin/perl
$|=1;
# The LearningOnline Network with CAPA
# Cluster Status
# (Versions
# (Running loncron
# 09/06/01 Gerd Kortemeyer)
# 02/18/02 Gerd Kortemeyer)
#
use LWP::UserAgent();
use HTTP::Headers;
use IO::File;

sub connected {
    my ($local,$remote)=@_;
    $local=~s/\W//g;
    $remote=~s/\W//g;

    unless ($hostname{$local}) { return 'local_unknown'; }
    unless ($hostname{$remote}) { return 'remote_unknown'; }

    my $ua=new LWP::UserAgent;
    
    my $request=new HTTP::Request('GET',
      "http://".$hostname{$local}.'/cgi-bin/ping.pl?'.$remote);

    my $response=$ua->request($request);

    unless ($response->is_success) { return 'local_offline'; }

    my $reply=$response->content;
    $reply=(split("\n",$reply))[0];
    $reply=~s/\W//g;
    if ($reply ne $remote) { return $reply; }
    return 'ok';
}

 
print "Content-type: text/html\n\n".
      "<html><body bgcolor=#FFFFFF>\n";
# ------------------------------------------------------------ Read access.conf
{
    my $config=IO::File->new("/etc/httpd/conf/access.conf");

    while (my $configline=<$config>) {
        if ($configline =~ /PerlSetVar/) {
	   my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
           $perlvar{$varname}=$varvalue;
        }
    }
    delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
    delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
}

# ------------------------------------------------------------- Read hosts file
{
    my $config=IO::File->new("$perlvar{'lonTabDir'}/hosts.tab");

    while (my $configline=<$config>) {
       my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
       $hostname{$id}=$name;
       $hostdom{$id}=$domain;
       $hostrole{$id}=$role;
       $hostip{$id}=$ip;
       if (($role eq 'library') && ($id ne $perlvar{'lonHostID'})) {
	   $libserv{$id}=$name;
       }
    }
}

print "<h1>Cluster Status</h1>\n";
print &connected('gerdl1','msul1');

print "</body></html>";
