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