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