File:  [LON-CAPA] / loncom / build / system_dependencies / sqltest.pl
Revision 1.8: download - view: text, annotated - select for diffs
Wed Aug 7 18:43:42 2002 UTC (21 years, 11 months ago) by harris41
Branches: MAIN
CVS tags: version_0_5_1, version_0_5, HEAD
VERSION information; also testing exit code of /etc/rc.d/init.d/mysqld status

    1: #!/usr/bin/perl
    2: 
    3: # sqltest.pl - script to test MySQL database connectivity for LON-CAPA
    4: #
    5: # $Id: sqltest.pl,v 1.8 2002/08/07 18:43:42 harris41 Exp $
    6: #
    7: ###
    8: 
    9: =pod
   10: 
   11: =head1 NAME
   12: 
   13: B<sqltest.pl> - Test interoperability of the MySQL server for use by LON-CAPA
   14: 
   15: =cut
   16: 
   17: # Written to help LON-CAPA (The LearningOnline Network with CAPA)
   18: #
   19: # YEAR=2001
   20: # 9/25,9/30 Scott Harrison
   21: # YEAR=2002
   22: # 5/10,5/11 Scott Harrison
   23: # 8/6/2002 and onwards, Scott Harrison, sharrison@users.sourceforge.net
   24: 
   25: =pod
   26: 
   27: =head1 SYNOPSIS
   28: 
   29: perl sqltest.pl
   30: 
   31: This script is ordinarily located inside the LON-CAPA source code tree.
   32: This script is normally invoked by test-related targets inside
   33: F<loncapa/loncom/build/Makefile>.
   34: 
   35: =head1 DESCRIPTION
   36: 
   37: This program tests the status of the MySQL database needed by the LON-CAPA
   38: system.  As with the other LON-CAPA test scripts, when reasonable, I try
   39: to avoid importing functionality from other LON-CAPA modules so as to
   40: avoid indirectly testing software dependencies.
   41: 
   42: =head2 ORGANIZATION OF THIS PERL SCRIPT
   43: 
   44: The script is organized into the following sections.
   45: 
   46: =over 4
   47: 
   48: =item 1.
   49: 
   50: Process version information of this file.
   51: 
   52: =item 2.
   53: 
   54: Modules used by this script,
   55: 
   56: =item 3.
   57: 
   58: Initializations.
   59: 
   60: =item 4.
   61: 
   62: Read in current configuration.
   63: 
   64: =item 5.
   65: 
   66: Is this a library or access server?
   67: 
   68: This step in the script is both a sanity check and also allows for other
   69: future LON-CAPA server types (e.g. "admin", "backup", "firewall") to not
   70: be required to have MySQL.
   71: 
   72: =item 6.
   73: 
   74: Make sure that the database can be accessed.
   75: 
   76: If not, a variety of possible problems should be tested for, and a status
   77: report should be issued to standard output.
   78: 
   79: =item 7.
   80: 
   81: Close database connection.
   82: 
   83: This part of the script is only reached if the database was successfully
   84: connected to.
   85: 
   86: =item 8.
   87: 
   88: Subroutines.
   89: 
   90: B<configuration_scan> - look for PerlSetVar and store inside hash variable.
   91: 
   92: =back
   93: 
   94: =head1 STATUS
   95: 
   96: Ratings: 1=horrible 2=poor 3=fair 4=good 5=excellent
   97: 
   98: =over 4
   99: 
  100: =item Organization
  101: 
  102: 5
  103: 
  104: =item Functionality
  105: 
  106: 4
  107: 
  108: =item Has it been tested?
  109: 
  110: 3
  111: 
  112: =back
  113: 
  114: =head1 AUTHOR
  115: 
  116: Scott Harrison, sharrison@users.sourceforge.net, 2001, 2002
  117: 
  118: This software is distributed under the General Public License,
  119: version 2, June 1991 (which is the same terms as LON-CAPA).
  120: 
  121: This is free software; you can redistribute it and/or modify
  122: it under the terms of the GNU General Public License as published by
  123: the Free Software Foundation; either version 2 of the License, or
  124: (at your option) any later version.
  125: 
  126: This software is distributed in the hope that it will be useful,
  127: but WITHOUT ANY WARRANTY; without even the implied warranty of
  128: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  129: GNU General Public License for more details.
  130: 
  131: You should have received a copy of the GNU General Public License
  132: along with this software; if not, write to the Free Software
  133: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  134: 
  135: =cut
  136: 
  137: # =================================== Process version information of this file.
  138: my $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/);
  139: 
  140: print('Running sqltest.pl, version '.$VERSION.'.'."\n");
  141: print('(Test interoperability of the MySQL server for use by LON-CAPA.)'."\n");
  142: 
  143: # ================================================ Modules used by this script.
  144: use strict;
  145: use DBI;    # Needed to interface with the database.
  146: 
  147: # ============================================================ Initializations.
  148: $|=1;
  149: print 'Probing for SQL metadata database'."\n\n";
  150: 
  151: # ============================================== Read in current configuration.
  152: my %perlvar;
  153: 
  154: my $webconfdir='/etc/httpd/conf/';
  155: 
  156: # NOTE: DEPRECATED scanning of access.conf
  157: &configuration_scan(\%perlvar,$webconfdir.'access.conf');
  158: 
  159: # Scanning the standard loncapa configuration files.
  160: &configuration_scan(\%perlvar,$webconfdir.'loncapa.conf');
  161: &configuration_scan(\%perlvar,$webconfdir.'loncapa_apache.conf');
  162: # Note that the authoritative value for 'lonSqlAccess' comes from
  163: # loncapa_apache.conf; this is why loncapa_apache.conf is scanned
  164: # after loncapa.conf (in case a system administrator mistakenly
  165: # specifies lonSqlAccess inside loncapa.conf).
  166: 
  167: # ========================================== Is lonSqlAccess defined correctly?
  168: unless ($perlvar{'lonSqlAccess'} and $perlvar{'lonSqlAccess'}!~/^\{\[\[\[\[/) {
  169:     print('**** ERROR **** The lonSqlAccess variable was not defined '.
  170: 	  'correctly inside '.$webconfdir.'loncapa_apache.conf'."\n");
  171:     exit(1);
  172: }
  173: unless ($perlvar{'lonSqlAccess'} eq 'localhostkey') {
  174:     print('**** WARNING **** The lonSqlAccess variable is not set to the '.
  175: 	  'standard expected value of "localhostkey"'."\n");
  176: }
  177: 
  178: # ========================================= Is this a library or access server?
  179: unless ($perlvar{'lonRole'} eq 'library' or $perlvar{'lonRole'} eq 'access') {
  180:     print('**** NOTE **** SQL testing can only be run on a library or access '.
  181: 	  'server.  Skipping test.'."\n");
  182:     exit(0);
  183: }
  184: 
  185: # ================================ Make sure that the database can be accessed.
  186: my $dbh;
  187: {
  188:     unless (
  189: 	    $dbh = DBI->connect("DBI:mysql:loncapa","www",
  190: 				$perlvar{'lonSqlAccess'},
  191: 				{RaiseError=>0,PrintError=>0})
  192: 	    ) {
  193: 	print('Cannot connect to database!'."\n");
  194: 	# ------------------------------------ Check through possible problems.
  195: 	my $problem_flag=0;
  196: 	my $checkDBImodule=`perl pmvers DBI 2>/dev/null`;
  197: 	my $checkMYSQLmodule=`perl pmvers Mysql 2>/dev/null`;
  198: 	my $checkprocess=`/etc/rc.d/init.d/mysqld status`;
  199: 	my $process_ecode=system('/etc/rc.d/init.d/mysqld status');
  200: 
  201: 	# ---------------------------------------------- Issue a status report.
  202: 	if (!$checkDBImodule) {
  203: 	    print('**** ERROR **** SYSTEM IS MISSING THE DBI PERL '.
  204: 		  'MODULE (DBI.pm)'."\n");
  205: 	    $problem_flag=1;
  206: 	}
  207: 	if (!$checkMYSQLmodule) {
  208: 	    print('**** ERROR **** SYSTEM IS MISSING THE MYSQL PERL '.
  209: 		  'MODULE (Mysql.pm)'."\n");
  210: 	    $problem_flag=1;
  211: 	}
  212: 	if (!-e '/etc/rc.d/init.d/mysqld') {
  213: 	    print('**** ERROR **** IT APPEARS THAT THE MYSQL SERVER HAS NOT '.
  214: 		  'BEEN INSTALLED'."\n");
  215: 	    $problem_flag=1;
  216: 	}
  217: 	if ($checkprocess=~/is stopped/) {
  218: 	    print('**** ERROR **** IT APPEARS THAT THE MYSQL SERVER IS NOT '.
  219: 		  'RUNNING'."\n");
  220: 	    print(<<END);
  221: To fix temporarily, run the command:
  222:    /etc/rc.d/init.d/mysqld start
  223: 
  224: You may also want to check and see that mysqld is started on boot time.
  225: 
  226:    /sbin/chkconfig --list mysqld
  227: 
  228: This is bad output:
  229: mysqld         0:off  1:off  2:off  3:off  4:off  5:off  6:off
  230: 
  231: This is good output:
  232: mysqld         0:off  1:off  2:off  3:on   4:on   5:on   6:off
  233: 
  234: To configure mysqld to launch correctly upon system startup, type the command:
  235:    /sbin/chkconfig --level 345 mysqld on
  236: END
  237: 	    $problem_flag=1;
  238: 	}
  239: 	if ($checkprocess=~/mysqld dead but subsys locked/) {
  240: 	    print('**** ERROR **** IT APPEARS THAT THE MYSQLD PROCESSES'.
  241: 		  'WERE SOMEHOW TERMINATED'."\n");
  242: 	    print(<<END);
  243: To fix temporarily, run the command:
  244:    /etc/rc.d/init.d/mysqld restart
  245: Double-check that your mysqld processes are running by using the command:
  246:    ps auxwww | grep mysqld
  247: 
  248: Note that something really bad probably happened on your system to abnormally
  249: shutdown the mysqld processes.
  250: END
  251: 	    $problem_flag=1;
  252: 	}
  253: 	if ($process_ecode) { # The exit code for mysqld status was abnormal.
  254: 	    print('**** ERROR **** MYSQLD IS NOT AVAILABLE'."\n");
  255: 	    print(<<END);
  256: To check (and fix), try running these commands:
  257:      /etc/rc.d/init.d/mysqld start
  258:      /etc/rc.d/init.d/mysqld status
  259: 
  260: You may also want to check and see that mysqld is started on boot time.
  261:    /sbin/chkconfig --list mysqld
  262: If everything is off, you should run "/sbin/chkconfig --level 345 mysqld on".
  263: END
  264:             $problem_flag=1;
  265: 	}
  266: 	unless ($problem_flag) {
  267: 	    print('**** ERROR **** IT APPEARS THAT WWW@LOCALHOST AND/OR '.
  268: 		  'PASSWORD ARE NOT CORRECTLY ENABLED'."\n");
  269: 	    print(<<END);
  270: This is because all other known problems have been checked for.
  271: By process of elimination, the assumption is that www\@localhost
  272: must not be connecting to the database (a system administrator
  273: at your institution may want to look at other possibilities however).
  274: 
  275: To diagnose, try logging in from the command line with
  276:        mysql -u www -p mysql
  277: and use the lonSqlAccess password
  278: listed in loncapa_apache.conf (it normally is set to 'localhostkey').
  279: If logging in fails to work, one possible approach is to REMOVE the
  280: www\@localhost MySQL user.
  281: [shell]\$ mysql -u root -p mysql
  282: mysql> delete from user where user='www'
  283: And then, you will need to repeat the MySQL configuration steps described at:
  284:        http://install.lon-capa.org/docs/install/index.html
  285: 
  286: **** NOTE **** ANOTHER possibility is that you are not running
  287: a compatible set of DBI, Mysql perl modules, and MySQL server software.
  288: END
  289: 	}
  290: 	exit(1);
  291:     }
  292: }
  293: %perlvar=(); # clear memory
  294: 
  295: print('SQL metadata database is found and is accessible'."\n");
  296: 
  297: # ================================================== Close database connection.
  298: $dbh->disconnect();
  299: 
  300: # ================================================================ Subroutines.
  301: 
  302: # --------- configuration_scan: look for PerlSetVar and store in hash variable.
  303: sub configuration_scan {
  304:     my ($storagehashref,$filename)=@_;
  305:     # deprecated support for access.conf
  306:     open(CONFIG,$filename) or
  307: 	($filename=~/access\.conf$/ and return) or
  308: 	(print("Can't read $filename\n") && exit(1));
  309:     while (my $configline=<CONFIG>) {
  310: 	if ($configline =~ /^[^\#]*PerlSetVar/) {
  311: 	    my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
  312: 	    chomp($varvalue);
  313: 	    $storagehashref->{$varname}=$varvalue;
  314: 	}
  315:     }
  316:     close(CONFIG);
  317: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>