#!/usr/bin/perl
#
# The LearningOnline Network with CAPA
#
# Tests the MySQL layer of the metadata database.
#
# YEAR=2001
# 9/25,9/30 Scott Harrison
# YEAR=2002
# 5/10,5/11 Scott Harrison

###############################################################################
##                                                                           ##
## ORGANIZATION OF THIS PERL CGI SCRIPT                                      ##
##                                                                           ##
## 1. Status of this code                                                    ##
## 2. Purpose and description of program                                     ##
## 3. Modules used by this script                                            ##
## 4. Print MIME Content-type and other initialization                       ##
## 5. Make sure database can be accessed and that this is a library server   ##
##                                                                           ##
###############################################################################

# --------------------------------------------------------- Status of this code
#
# 1=horrible 2=poor 3=fair 4=good 5=excellent
# Organization 5
# Functionality 4
# Has it been tested? 3
#

# ------------------------------------------ Purpose and description of program
#
# This program tests the connection to the MySQL database.

# ------------------------------------------------- Modules used by this script
use strict;
use DBI;

# ---------------------------- Print MIME Content-type and other initialization
$|=1;
print 'Probing for SQL metadata database'."\n\n";

# --- Make sure that database can be accessed and that this is a library server
# library server test
my %perlvar;
open (CONFIG,"/etc/httpd/conf/access.conf") || 
    (print "Can't read access.conf\n" && exit);
while (my $configline=<CONFIG>) {
    if ($configline =~ /^[^\#]*PerlSetVar/) {
	my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
        chomp($varvalue);
        $perlvar{$varname}=$varvalue;
    }
}
close(CONFIG);
open (CONFIG,"/etc/httpd/conf/loncapa.conf") || 
    (print "Can't read loncapa.conf\n" && exit);
while (my $configline=<CONFIG>) {
    if ($configline =~ /^[^\#]*PerlSetVar/) {
	my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
        chomp($varvalue);
        $perlvar{$varname}=$varvalue;
    }
}
close(CONFIG);
open (CONFIG,"/etc/httpd/conf/loncapa_apache.conf") || 
    (print "Can't read loncapa_apache.conf\n" && exit);
while (my $configline=<CONFIG>) {
    if ($configline =~ /^[^\#]*PerlSetVar/) {
	my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
        chomp($varvalue);
        $perlvar{$varname}=$varvalue;
    }
}
close(CONFIG);
unless ($perlvar{'lonRole'} eq 'library') {
    print "SQL testing can only be run on a library server. Skipping test..\n";
    exit 0;
}
# database test
my $dbh;
{
    unless (
	    $dbh = DBI->connect("DBI:mysql:loncapa","www",
				$perlvar{'lonSqlAccess'},
				{ RaiseError =>0,PrintError=>0})
	    ) { 
	print "Cannot connect to database!\n";
	my $checkDBImodule=`perl pmvers DBI 2>/dev/null`;
	my $checkMYSQLmodule=`perl pmvers Mysql 2>/dev/null`;
	my $checkprocess=`/etc/rc.d/init.d/mysqld status`;
	if (!$checkDBImodule) {
	    print "**** ERROR **** SYSTEM IS MISSING THE DBI PERL ".
		"MODULE (DBI.pm)\n";
	}
	elsif (!$checkMYSQLmodule) {
	    print "**** ERROR **** SYSTEM IS MISSING THE MYSQL PERL ".
		"MODULE (Mysql.pm)\n";
	}
	elsif (!-e '/etc/rc.d/init.d/mysqld') {
	    print "**** ERROR **** IT APPEARS THAT THE MYSQL SERVER HAS NOT ".
		"BEEN INSTALLED\n";
	}
	elsif ($checkprocess=~/is stopped/) {
	    print "**** ERROR **** IT APPEARS THAT THE MYSQL SERVER IS NOT ".
		"RUNNING\n";
	    print(<<END);
To fix temporarily, run the command:
   /etc/rc.d/init.d/mysqld start

You may also want to check and see that mysqld is started on boot time.

   /sbin/chkconfig --list mysqld

This is bad output:
mysqld         0:off  1:off  2:off  3:off  4:off  5:off  6:off

This is good output:
mysqld         0:off  1:off  2:off  3:on   4:on   5:on   6:off

To configure mysqld to launch correctly upon system startup, type the command:
   /sbin/chkconfig --level 345 mysqld on
END
	}
	else {
	    print "**** ERROR **** IT APPEARS THAT WWW\@LOCALHOST AND/OR ".
		"PASSWORD ARE NOT CORRECTLY ENABLED\n";
	    print(<<END);
To diagnose, try logging in from the command line with
       mysql -u www -p mysql
and use the lonSqlAccess password
listed in loncapa.conf (PerlSetVar lonSqlAccess ....).
If this does not work, you may need to REMOVE the www\@localhost MySQL user.
mysql -u root -p mysql
mysql> delete from user where user='www'
And then, you will need to repeat the MySQL configuration steps described at:
       http://install.lon-capa.org/.

**** NOTE **** ANOTHER possibility is that you are not running
a compatible set of DBI, Mysql perl modules and MySQL server software.
END
	}
	exit 1;
    }
}
%perlvar=(); # undefine it

print "SQL metadata database is found and is accessible\n";

# --------------------------------------------------- Close database connection
$dbh->disconnect();
