]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/settings-tester.pl
typo; settings-tester update
[Evergreen.git] / Open-ILS / src / support-scripts / settings-tester.pl
1 #!/usr/bin/perl
2 # vim:noet:ts=4:
3 use strict;
4 use warnings;
5
6 BEGIN {
7         eval "use OpenSRF::Utils::Config;";
8         die "Please ensure that /openils/lib/perl5 is in your PERL5LIB environment variable.
9         You must run this script as the 'opensrf' user.\n" if ($@);
10         eval "use Error qw/:try/;";
11         die "Please install Error.pm.\n" if ($@);
12         eval "use UNIVERSAL::require;";
13         die "Please install the UNIVERSAL::require perl module.\n" if ($@);
14         eval "use Getopt::Long;";
15         die "Please install the Getopt::Long perl module.\n" if ($@);
16         eval "use Net::Domain;";
17         die "Please install the Net::Domain perl module.\n" if ($@);
18 }
19
20 my $output = '';
21 my $perloutput = '';
22 my $result;
23
24 my ($gather, $hostname, $core_config, $tmpdir) =
25         (0, Net::Domain::hostfqdn(), '/openils/conf/opensrf_core.xml', '/tmp/');
26
27 GetOptions(
28         'gather' => \$gather,
29         'hostname=s' => \$hostname,
30         'config_file=s' => \$core_config,
31         'tempdir=s' => \$tmpdir,
32 );
33
34 while (my $mod = <DATA>) {
35         chomp $mod;
36         my @list = split / /, $mod;
37
38         my $ok = 0;
39         for my $m (@list) {
40                 if ($m->use) {
41                         $ok++;
42                         my $version = $m->VERSION;
43                         print "$m version $version\n" if ($version);
44                 }
45         }
46
47         unless ($ok) {
48                 if (@list == 1) {
49                         warn "Please install $mod\n";
50                         $perloutput .= "Please install the $mod Perl module.\n";
51                 } else {
52                         warn "Please install one of the following modules: $mod\n";
53                         $perloutput .= "Please install one of the following modules: $mod\n";
54                 }
55         }
56                         
57 }
58
59 use OpenSRF::Transport::SlimJabber::Client;
60 use OpenSRF::Utils::SettingsParser;
61 use OpenSRF::Utils::SettingsClient;
62 use Data::Dumper;
63 use DBI;
64
65 (my $conf_dir = $core_config) =~ s#(.*)/.*#$1#;
66
67
68 OpenSRF::Utils::Config->load(config_file => $core_config);
69 my $conf = OpenSRF::Utils::Config->current;
70 my $j_username    = $conf->bootstrap->username;
71 my $j_password    = $conf->bootstrap->passwd;
72 my $j_port    = $conf->bootstrap->port;
73 my $j_domain    = $conf->bootstrap->domains->[0];
74 my $settings_config = $conf->bootstrap->settings_config;
75 my $logfile    = $conf->bootstrap->logfile;
76 (my $log_dir = $logfile) =~ s#(.*)/.*#$1#;
77
78
79 print "\nChecking Jabber connection\n";
80 # connect to jabber 
81 my $client = OpenSRF::Transport::SlimJabber::Client->new(
82     port => $j_port, 
83     username => $j_username, 
84     password => $j_password,
85     host => $j_domain,
86     resource => 'test123'
87 );
88
89
90 my $je = undef;
91 try {
92     unless($client->initialize()) {
93         $je = "* Unable to connect to jabber server $j_domain\n";
94         warn "* Unable to connect to jabber server $j_domain\n";
95     }
96 } catch Error with {
97     $je = "* Error connecting to jabber:\n" . shift() . "\n";
98     warn "* Error connecting to jabber:\n" . shift() . "\n";
99 };
100
101 print "* Jabber successfully connected\n" unless ($je);
102 $output .= ($je) ? $je : "* Jabber successfully connected\n";
103
104 my $xmlparser = XML::LibXML->new();
105 my $osrfxml = $xmlparser->parse_file($settings_config);
106
107 print "\nChecking database connections\n";
108 # Check database connections
109 my @databases = $osrfxml->findnodes('//database');
110 foreach my $database (@databases) {
111         my $db_name = $database->findvalue("./db");     
112         if (!$db_name) {
113                 $db_name = $database->findvalue("./name");      
114         }
115         my $db_host = $database->findvalue("./host");   
116         my $db_port = $database->findvalue("./port");   
117         my $db_user = $database->findvalue("./user");   
118         my $db_pw = $database->findvalue("./pw");       
119     if (!$db_pw && $database->parentNode->parentNode->nodeName eq 'reporter') {
120         $db_pw = $database->findvalue("./password");
121         warn "* WARNING: Deprecated <password> element used for the <reporter> entry.  ".
122             "Please use <pw> instead.\n" if ($db_pw);
123     }
124
125         my $osrf_xpath;
126         foreach my $node ($database->findnodes("ancestor::node()")) {
127                 next unless $node->nodeType == XML::LibXML::XML_ELEMENT_NODE;
128                 $osrf_xpath .= "/" . $node->nodeName;
129         }
130         $output .= test_db_connect($db_name, $db_host, $db_port, $db_user, $db_pw, $osrf_xpath);
131 }
132
133 print "\nChecking database drivers to ensure <driver> matches <language>\n";
134 # Check database drivers
135 # if language eq 'C', driver eq 'pgsql'
136 # if language eq 'perl', driver eq 'Pg'
137 my @drivers = $osrfxml->findnodes('//driver');
138 foreach my $driver_node (@drivers) {
139         my $language;
140         my $driver_xpath;
141         my @driver_xpath_nodes;
142         foreach my $node ($driver_node->findnodes("ancestor::node()")) {
143                 next unless $node->nodeType == XML::LibXML::XML_ELEMENT_NODE;
144                 $driver_xpath .= "/" . $node->nodeName;
145                 push @driver_xpath_nodes, $node->nodeName;
146         }
147         my $lang_xpath;
148         my $driver = $driver_node->findvalue("child::text()");
149         while (pop(@driver_xpath_nodes) && scalar(@driver_xpath_nodes) > 0 && !$language) {
150                 $lang_xpath = "/" . join('/', @driver_xpath_nodes) . "/language";
151                 my @lang_nodes = $osrfxml->findnodes($lang_xpath);
152                 next unless scalar(@lang_nodes > 0);
153                 $language = $lang_nodes[0]->findvalue("child::text()");
154         }
155         if ($driver eq "pgsql") {
156                 if ($driver_xpath =~ m#/reporter/#) {
157                         $result = "* ERROR: reporter application must use driver 'Pg', but '$driver' is defined\n";
158                         warn $result;
159                 } elsif ($language eq "C") {
160                         $result = "* OK: $driver language is $language in $lang_xpath\n";
161                 } else {
162                         $result = "* ERROR: $driver language is $language in $lang_xpath\n";
163                         warn $result;
164                 }
165         } elsif ($driver eq "Pg") {
166                 if ($driver_xpath =~ m#/reporter/#) {
167                         $result = "* OK: $driver language is undefined for reporter base configuration\n";
168                 } elsif ($language eq "perl") {
169                         $result = "* OK: $driver language is $language in $lang_xpath\n";
170                 } else {
171                         $result = "* ERROR: $driver language is $language in $lang_xpath\n";
172                         warn $result;
173                 }
174         } else {
175                 $result = "* ERROR: Unknown driver $driver in $driver_xpath\n";
176                 warn $result;
177         }
178         print $result;
179         $output .= $result;
180 }
181
182 print "\nChecking libdbi and libdbi-drivers\n";
183 $output .= check_libdbd();
184
185 print "\nChecking hostname\n";
186 my @hosts = $osrfxml->findnodes('/opensrf/hosts/*');
187 foreach my $host (@hosts) {
188         next unless $host->nodeType == XML::LibXML::XML_ELEMENT_NODE;
189         my $osrfhost = $host->nodeName;
190         my $he;
191         if ($osrfhost ne $hostname && $osrfhost ne "localhost") {
192                 $result = " * ERROR: expected hostname '$hostname', found '$osrfhost' in <hosts> section of opensrf.xml\n";
193                 warn $result;
194                 $he = 1;
195         } elsif ($osrfhost eq "localhost") {
196                 $result = " * OK: found hostname 'localhost' in <hosts> section of opensrf.xml\n";
197         } else {
198                 $result = " * OK: found hostname '$hostname' in <hosts> section of opensrf.xml\n";
199         }
200         print $result unless $he;
201         $output .= $result;
202 }
203
204
205 if ($gather) {
206         get_debug_info( $tmpdir, $log_dir, $conf_dir, $perloutput, $output );
207 }
208
209 sub test_db_connect {
210         my ($db_name, $db_host, $db_port, $db_user, $db_pw, $osrf_xpath) = @_;
211
212         my $dsn = "dbi:Pg:dbname=$db_name;host=$db_host;port=$db_port";
213         my $de = undef;
214         my ($dbh, $encoding);
215         try {
216                 $dbh = DBI->connect($dsn, $db_user, $db_pw);
217                 unless($dbh) {
218                         $de = "* $osrf_xpath :: Unable to connect to database $dsn, user=$db_user, password=$db_pw\n";
219                         warn "* $osrf_xpath :: Unable to connect to database $dsn, user=$db_user, password=$db_pw\n";
220                 }
221                 my $sth = $dbh->prepare("show server_encoding");
222                 $sth->execute;
223                 $sth->bind_col(1, \$encoding);
224                 $sth->fetch;
225                 $sth->finish;
226                 $dbh->disconnect;
227         } catch Error with {
228                 $de = "* $osrf_xpath :: Unable to connect to database $dsn, user=$db_user, password=$db_pw\n" . shift() . "\n";
229                 warn "* $osrf_xpath :: Unable to connect to database $dsn, user=$db_user, password=$db_pw\n" . shift() . "\n";
230         };
231         print "* $osrf_xpath :: Successfully connected to database $dsn\n" unless ($de);
232         if ($encoding !~ m/(utf-?8|unicode)/i) {
233                 $de .= "* ERROR: $osrf_xpath :: Database $dsn has encoding $encoding instead of UTF8 or UNICODE.\n";
234                 warn "* ERROR: $osrf_xpath :: Database $dsn has encoding $encoding instead of UTF8 or UNICODE.\n";
235         } else {
236                 print "  * Database has the expected server encoding $encoding.\n";
237         }
238         return ($de) ? $de : "* $osrf_xpath :: Successfully connected to database $dsn with encoding $encoding\n";
239
240 }
241
242 sub check_libdbd {
243         my $results = '';
244         my @location = `locate libdbdpgsql.so | grep -v home | grep -v .libs`; # simple(ton) attempt to filter out build versions
245         if (scalar(@location) > 1) {
246
247                 my $res = "Found more than one location for libdbdpgsql.so.
248   We have found that system packages don't link against libdbi.so;
249   therefore, we strongly recommend compiling libdbi and libdbi-drivers from source.\n";
250                 $results .= $res;
251                 print $res;
252         }
253         foreach my $loc (@location) {
254                 my @linkage = `ldd $loc`;
255                 if (!grep(/libdbi/, @linkage)) {
256                         my $res = "$loc was not linked against libdbi - you probably need to compile libdbi-drivers from source with the --enable-libdbi configure switch.\n";
257                         $results .= $res;
258                         print $res;
259                 }
260         }
261         return $results;
262 }
263
264 sub get_debug_info {
265   my $temp_dir = shift; # place we can write files
266   my $log = shift; # location of the log directory
267   my $config = shift; # location of the config files
268   my $perl_test = shift; # output from the Perl prereq testing
269   my $config_test = shift; # output from the config file testing
270
271   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
272   my $oils_time = sprintf("%04d-%02d-%02d_%02dh-%02d-%02d", $year+1900, $mon, $mday, $hour, $min, $sec);
273   
274   # evil approach that requires no other Perl dependencies
275   chdir($temp_dir);
276   my $oils_debug_dir = "$temp_dir/oils_$oils_time";
277
278   # Replace with something Perlish
279   mkdir($oils_debug_dir) or die $!;
280
281   # Replace with File::Copy
282   system("cp $log/*log $oils_debug_dir");
283
284   # Passwords will go through in the clear for now
285   system("cp $config/*xml $oils_debug_dir");
286
287   # Get Perl output
288   open(FH, ">", "$oils_debug_dir/perl_test.out") or die $!;
289   print FH $perl_test;
290   close(FH);
291
292   # Get XML output
293   open(FH, ">", "$oils_debug_dir/xml_test.out") or die $!;
294   print FH $config_test;
295   close(FH);
296   
297   # Tar this up - does any system not have tar?
298   system("tar czf oils_$oils_time.tar.gz oils_$oils_time");
299
300   # Clean up after ourselves, somewhat dangerously
301   system("rm -fr $oils_debug_dir");
302
303   print "Wrote your debug information to $temp_dir/oils_$oils_time.tar.gz.\n";
304 }
305
306 __DATA__
307 LWP::UserAgent
308 XML::LibXML
309 XML::LibXML::XPathContext
310 XML::LibXSLT
311 Net::Server::PreFork
312 Cache::Memcached
313 Class::DBI
314 Class::DBI::AbstractSearch
315 Template
316 DBD::Pg
317 Net::Z3950 Net::Z3950::ZOOM
318 MARC::Record
319 MARC::Charset
320 MARC::File::XML
321 Text::Aspell
322 CGI
323 DateTime::TimeZone
324 DateTime
325 DateTime::Format::ISO8601
326 Unix::Syslog
327 GD::Graph3d
328 JavaScript::SpiderMonkey
329 Log::Log4perl
330 Email::Send
331 Text::CSV
332 Text::CSV_XS
333 Spreadsheet::WriteExcel::Big
334 Tie::IxHash
335 Parse::RecDescent
336 SRU